<?php require_once('../auth/auth.php');
if(isset($_GET['ac_type'])){
    $ac_type=$_GET['ac_type'];
    $gcatid=$_GET['gcatid'];

}
?>
<section class="content">
   <div class="container-fluid">
      <div class="row">
        <div class="col-md-12">
          <div class="card card-success 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: 5px 8px;">
              <h3 class="card-title">
<?php if ($ac_type=='su-cre') {
 echo language(41);
} else if($ac_type=='su-deb'){
echo language(37);
} ?>

 </h3>
              <div class="card-tools">
                <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-1"></div>

                 <div class="col-md-3 pl-1 mt-1">
                    <button type="button"  class="btn btn-danger  btn-sm mt-3">

                    <a href="../customer/account_upload.csv" class="text-white" download="Customer_sample_file"> <i class="fa fa-download"></i> <?php echo language(288); ?> </a> </button>
                 </div> 


                 <div class="col-md-3">
                    <div class="form-group">
                       <label><?php echo language(287);?></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-1 mt-1">
                    <button type="submit" id="save" class="btn btn-success btn-sm mt-3"><i class="fa fa-upload"></i> <?php echo language(95); ?></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>
   </div>
</section>
<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:"../customer/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');   
    data.column.push('Invoice'); 
    var html = '<table class="ttable" cellpadding="0" cellspacing="0">';
    if(data.column){
        html += '<tr>';
       for(var i = 0; i < data.column.length; i++){
        html += '<th class="text-center">'+data.column[i]+'</th>';
       }
        html += '</tr>';
    }

    if(data.row_data){
       for(var x = 0; x < data.row_data.length; x++){
        html += '<tr><td class="outlet_id" contenteditable>'+data.row_data[x].outlet_id+'</td>';
        html += '<td class="ac_name" contenteditable>'+data.row_data[x].ac_name+'</td>';
        html += '<td class="ac_display" contenteditable>'+data.row_data[x].ac_display+'</td>';
        html += '<td class="ac_number" contenteditable>'+data.row_data[x].ac_number+'</td>';
        html += '<td class="ac_address" contenteditable>'+data.row_data[x].ac_address+'</td>';
        html += '<td class="ac_route" contenteditable>'+data.row_data[x].ac_route+'</td>';
        html += '<td class="ac_balance" contenteditable>'+data.row_data[x].ac_balance+'</td>';
        html += '<td class="ac_nid" contenteditable>'+data.row_data[x].ac_nid+'</td>';
        html += '<td class="ac_genarel" contenteditable>'+data.row_data[x].ac_genarel+'</td>';
        html += '<td class="invoice">'+data.row_data[x].invoice+'</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 outlet_id  = [];
  var ac_name    = [];
  var ac_display = [];
  var ac_number  = [];
  var ac_address = [];
  var ac_route   = [];
  var ac_balance = [];
  var ac_nid     = [];
  var ac_genarel = [];
  var invoice    = [];

  $('.outlet_id').each(function(){
   outlet_id.push($(this).text());
  });
  $('.ac_name').each(function(){
   ac_name.push($(this).text());
  });
  $('.ac_display').each(function(){
   ac_display.push($(this).text());
  });
  $('.ac_number').each(function(){
   ac_number.push($(this).text());
  });
  $('.ac_address').each(function(){
   ac_address.push($(this).text());
  });
  $('.ac_route').each(function(){
   ac_route.push($(this).text());
  });
  $('.ac_balance').each(function(){
   ac_balance.push($(this).text());
  });
  $('.ac_nid').each(function(){
   ac_nid.push($(this).text());
  });
  $('.ac_genarel').each(function(){
   ac_genarel.push($(this).text());
  });
  $('.invoice').each(function(){
   invoice.push($(this).text());
  });
  $.post("../customer/csv_import.php",
    {
    outlet_id  : outlet_id,
    ac_name    : ac_name,
    ac_display : ac_display,
    ac_number  : ac_number,
    ac_address : ac_address,
    ac_route   : ac_route,
    ac_balance : ac_balance,
    ac_nid     : ac_nid,
    ac_genarel : ac_genarel,
    invoice    : invoice,
    ac_type    :  'su-deb',
    gcatid     : '<?php echo $gcatid ?>'
   },function(data){
        <?php if($ac_type=='supplier'){ ?>
        <?php echo "Not Support"; } else{  ?>
            $('#csv_file_data').html('<div class="alert alert-success">Data Imported Successfully</div>');
        <?php  } ?>
   });
  }
</script>
