<?php require_once('../auth/auth.php'); ?>
     <div class="row">
      <div class="col-md-12">
      <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
            <div class="card-header" style="padding: 3px 8px;">
              <h3 class="card-title">Bulk Item Add</h3>
              <div class="card-tools m-0">
                <button type="button" class="btn btn-tool" data-card-widget="remove">
                  <i class="fas fa-times"></i>
                </button>
              </div>
            </div>
            <form id="file_form" class="mt-1" autocomplete="off" enctype="multipart/form-data">
              <div class="card-body">
                <div class="row">
                <div class="col-md-4"></div>
                 <div class="col-md-3">
                    <div class="form-group">
                       <label>Select your CSV File</label>
                        <input type="file" name="csv_file" accept=".csv" class="form-control form-control-sm" required="true" style="height:29px;line-height:13px;">
                    </div>
                 </div> 
                 <div class="col-md-2 pl-0">
                    <button type="submit" id="save" class="btn btn-success btn-sm mt-3"><i class="fa fa-upload"></i> Upload File</button>
                 </div>  
                 <div class="col-md-3"></div> 
                 
                </div>            
              </div>
            </form>
            <div class="col-md-12"><hr style="margin-top:-5px;margin-bottom: 7px;"> <div id="csv_file_data"></div></div> 
          </div>
        </div>
      </div>
<script>
$(document).ready(function() {
  $('#file_form').on('submit', function(e){
  e.preventDefault(); 
  $('#save').prop('disabled', true);
  $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
  $.ajax({
   url:"../itemstock/csv_fetch.php",
   method:"POST",
   data:new FormData(this),
   dataType:'json',
   contentType:false,
   cache:false,
   processData:false,
   success:function(data){
    $('#file_form')[0].reset();
    $('#save').prop('disabled', false);
    $('#save').html('Upload File');   
    var html = '<table class="ttable table-responsive" cellpadding="0" cellspacing="0">';
    if(data.column){
        html += '<tr><th class="text-center">S/L</th>';
       for(var i = 0; i < data.column.length; i++){
        html += '<th class="text-center">'+data.column[i]+'</th>';
       }
        html += '</tr>';
    }
    if(data.row_data){
        var ii =0;
       for(var x = 0; x < data.row_data.length; x++){
        html += '<tr><td class="text-center">'+ ++ii +'</td>';
        html += '<td class="group" contenteditable>'+data.row_data[x].group+'</td>';
        html += '<td class="category" contenteditable>'+data.row_data[x].category+'</td>';
        html += '<td class="brand" contenteditable>'+data.row_data[x].brand+'</td>';
        html += '<td class="name" contenteditable>'+data.row_data[x].name+'</td>';
        html += '<td class="code text-center" contenteditable>'+data.row_data[x].code+'</td>';
        html += '<td class="qty text-center" contenteditable>'+Number(data.row_data[x].qty)+'</td>';
        html += '<td class="pmrp text-right" contenteditable>'+Number(data.row_data[x].pmrp)+'</td>';
        html += '<td class="mnrp text-right" contenteditable>'+Number(data.row_data[x].mnrp)+'</td>';
        html += '<td class="mxrp text-right" contenteditable>'+Number(data.row_data[x].mxrp)+'</td>';
        html += '<td class="details" contenteditable>'+data.row_data[x].details+'</td>';
        html += '<td class="location" contenteditable>'+data.row_data[x].locat+'</td>';
        html += '<td class="sku text-center" contenteditable>'+data.row_data[x].sku+'</td>';
        html += '<td class="utype text-center" contenteditable>'+data.row_data[x].utype+'</td>';
        html += '<td class="ptax text-center" contenteditable>'+data.row_data[x].ptax+'</td>';
        html += '<td class="stax text-center" contenteditable>'+data.row_data[x].stax+'</td>';
        html += '<td class="batch text-center" contenteditable>'+data.row_data[x].batch+'</td>';
        html += '<td class="notice text-center" contenteditable>'+Number(data.row_data[x].notice)+'</td>';
        html += '<td class="weight text-center" contenteditable>'+data.row_data[x].weight+'</td>';
        html += '<td class="disc text-center" contenteditable>'+Number(data.row_data[x].disc)+'</td></tr>';
       }
    }

    html += '<table>';
    html += '<hr style="margin-bottom: 7px;"><div class="card-footer" style="padding: 8px 5px;" align="center"><button type="button" onclick="importt(this)" class="btn btn-success">Import Your Data</button></div>';
    $('#csv_file_data').html(html);
   }
  })
 }); 
});  
</script>


<script>
function importt(abc){
  $(abc).prop('disabled', true);
  $(abc).html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
  var group    = [];
  var category = [];
  var brand    = [];
  var name     = [];
  var code     = [];
  var qty      = [];
  var pmrp     = [];
  var mnrp     = [];
  var mxrp     = [];
  var details  = [];
  var location = [];
  var sku      = [];
  var utype    = [];
  var ptax     = [];
  var stax     = [];
  var batch    = [];
  var notice   = [];
  var weight   = [];
  var disc     = [];

  $('.group').each(function(){
   group.push($(this).text());
  });
  $('.category').each(function(){
   category.push($(this).text());
  });
  $('.brand').each(function(){
   brand.push($(this).text());
  });
  $('.name').each(function(){
   name.push($(this).text());
  });
  $('.code').each(function(){
   code.push($(this).text());
  });
  $('.qty').each(function(){
   qty.push($(this).text());
  });
  $('.pmrp').each(function(){
   pmrp.push($(this).text());
  });
  $('.mnrp').each(function(){
   mnrp.push($(this).text());
  });
  $('.mxrp').each(function(){
   mxrp.push($(this).text());
  });
  $('.details').each(function(){
   details.push($(this).text());
  });
  $('.location').each(function(){
   location.push($(this).text());
  });
  $('.sku').each(function(){
   sku.push($(this).text());
  });
  $('.utype').each(function(){
   utype.push($(this).text());
  });
  $('.ptax').each(function(){
   ptax.push($(this).text());
  });
  $('.stax').each(function(){
   stax.push($(this).text());
  });
  $('.batch').each(function(){
   batch.push($(this).text());
  });
  $('.notice').each(function(){
   notice.push($(this).text());
  });
  $('.weight').each(function(){
   weight.push($(this).text());
  });
  $('.disc').each(function(){
   disc.push($(this).text());
  });
  $.post("../itemstock/csv_import.php",
    {
       group    : group,
       category : category,
       brand    : brand,
       name     : name,
       code     : code,
       qty      : qty,
       pmrp     : pmrp,
       mnrp     : mnrp,
       mxrp     : mxrp,
       details  : details,
       location : location,
       sku      : sku,
       utype    : utype,
       ptax     : ptax,
       stax     : stax,
       batch    : batch,
       notice   : notice,
       weight   : weight,
       disc     : disc
   },function(data){
    $('#csv_file_data').html('<div class="alert alert-success">Data Imported Successfully</div>');
    pagecall('../itemstock/item_view.php','#item_v');
   });
  };
</script>
