<?php require_once('../auth/auth.php');?>
<?php
   $search     = $_GET['search'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $itype      = $_GET['itype'];
   $nrow       = $_GET['nrow'];
   $fdate      = $_GET['fdate'];
   $edate      = $_GET['edate'];

    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND order_list_edit.bid=$bbid";
    }

    if ($bbrid=='0') {
       $bbridcond = '';
    }elseif ($bbrid >0){
        $bbridcond= " AND order_list_edit.brid=$bbrid";
    }

    if ($search =='0') {
         $blacksearch='';
      } else {
         $blacksearch = " AND (accounts.ac_name LIKE '%$search%' || accounts.ac_alise LIKE '%$search%' || accounts.ac_number LIKE '%$search%' || order_list_edit.invoice LIKE '%$search%')";
    }


     $inv_query= mysqli_query($conn,"SELECT DATE_FORMAT(order_list_edit.datetime, '%d/%m/%Y') tdate,order_list_edit.invoice,SUM(order_list_edit.t_amount) amount,SUM(order_list_edit.dis_amount) discount,SUM(order_list_edit.net_amount) net_bill,IF(LEFT(order_list_edit.invoice,4)='ERSL',ac_name,c_name) name,IF(LEFT(order_list_edit.invoice,4)='ERSL',ac_number,c_gsm) gsm,em_acname
      FROM order_list_edit 
        LEFT JOIN accounts ON (accounts.acid=order_list_edit.acid)
        LEFT JOIN c_customer ON (c_customer.c_id=order_list_edit.acid)
        LEFT JOIN user ON (user.uid=order_list_edit.userid)
        LEFT JOIN em_employee ON (em_employee.em_id=user.us_em_id)
        WHERE order_list_edit.coid='$coid' $bbidcond $bbridcond $blacksearch AND order_list_edit.order_type='$itype' AND date(order_list_edit.datetime) BETWEEN '$fdate' AND '$edate' GROUP BY order_list_edit.invoice ORDER BY order_list_edit.datetime ASC LIMIT $nrow;") or die (mysqli_error($conn));
     $dataa = [];
     while ($invrow= mysqli_fetch_assoc($inv_query)){ $dataa[]=$invrow;}
?>


<table id="inv_table" class="ttable" cellpadding="0" cellspacing="0">
<thead>
    <tr class="sticky-top">
        <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(131);?></th>
        <th style="width:17%;"><?php echo language(215);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(111);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(151);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(253);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(88);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(145);?></th>
        <th class="text-center" style="width:13%;"><?php echo language(256);?></th>
        <th class="text-center" style="width:2%;">#</th>
    </tr>
</thead>
 <tbody id="inv_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
            <th class="text-right" colspan="5"><?php echo language(233);?></th>
            <th class="text-right"><dt id="t_bill"></dt></th>
            <th class="text-right"><dt id="t_discount"></dt></th>
            <th class="text-right"><dt id="t_net_bill"></dt></th>
            <th class="text-right"></th>
            <th class="text-right"></th>
        </tr>                                 
     </tfoot>   
</table>
<script id="inv_script" type="text/javascript">
   $(document).ready(function(){
       let dataa      = '';
       let i          = 0;
       let t_bill     = 0;
       let t_discount = 0;
       let net_bill   = 0;
       let t_net_bill = 0;
       
       $.each(<?php echo json_encode($dataa);?>, function(key,value){
       t_bill      += Number(value.amount);
       t_discount  += Number(value.discount);
       t_net_bill  += Number(value.net_bill);


       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td class="text-center">' + value.tdate + '</td>';
       dataa += '<td>' + value.name +  '</td>';
       dataa += '<td class="text-center">' + value.gsm + '</td>';
       dataa += '<td class="text-center btn-outline-dark" onclick="vprint(this)" invoice="'+value.invoice+'">' + (value.invoice).split("-")[5] + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.amount)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.discount)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.net_bill)) + '</td>';
       dataa += '<td class="text-center">' + value.em_acname + '</td>';
       dataa += '<td class="text-center" data-toggle="dropdown" title="More Tools" aria-expanded="false"><i class="fa-solid fa-ellipsis-vertical"></i><ul class="dropdown-menu" role="menu"><li><a class="dropdown-item" title="Complete" complete="'+value.invoice+'"><i class="fa fa-check" aria-hidden="true"></i> Complete Invoice</a></li><div class="dropdown-divider m-0"></div><li><a class="dropdown-item" title="Delete" delet="'+value.invoice+'"><i class="fas fa-trash"></i> Delete Invoice</a></li></ul></td>';
       dataa += '</tr>';
   });
      $("#inv_show").html(dataa);
      $("#t_bill").text(bden.format(t_bill));
      $("#t_discount").text(bden.format(t_discount));
      $("#t_net_bill").text(bden.format(t_net_bill));
      $("#inv_script").remove();
   });
</script>
<script>
  function vprint(tdd){
      // $("#modal-lg").modal({backdrop: "static"});
      // pagecall('../purchase/purchase_invoice.php?invoice='+ $(tdd).attr('invoice'),'#mdview');
  }  
</script>

<script>
  $(document).ready(function() {

    $('[complete]').on('click', function(e){
      e.preventDefault();
       pagecall('../sales/draft_index.php?invoice='+ $(this).attr("complete"),'#salmview');
    });

    $('[delet]').on('click', function(e){
    let pdraftinvoice = $(this).attr('delet');
    e.preventDefault(); 
      Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this Invoice!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Yes, delete it!'
      }).then((result) => {
        if (result.isConfirmed) {
          Swal.fire({icon: 'success',title: 'Your Invoice is Successfully Delete',showConfirmButton: false,timer: 1500})
          $.post('../sales/order_list_delete.php',{pdraftinvoice:pdraftinvoice});   
          $(this).closest('tr').fadeOut('slow');         
        }
      })
    })

  })
</script>
