<?php
session_start();
require_once("./connection_sql.php");
?>
<style type="text/css">
		tfoot input {
			width: 100%;
			padding: 3px;
			box-sizing: border-box;
		}
	</style>
   
    <!--<link rel="stylesheet" href="css/bootstrap.min.css"> -->
    <!--<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">-->
 
    <link  href="vendor/new/multi/jquery.dataTables.min.css" rel="stylesheet" type="text/css"> 
	<script src="vendor/new/multi/jquery-3.5.1.js" ></script>
	<script src="vendor/new/multi/jquery.dataTables.min.js" ></script>


<style>

    
    .table-hover tbody tr:hover td {
        background: aqua;
    }
</style>

<section class="content" onload='load_list();' >

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">INVOICE EXCEL DOWNLOAD</h3>
            <b style="margin-top: -10px;"><p style="float: right; color: black" id="time"></p></b> 
        
             
            <div class="box-body " style='background-color: white;  '> 
            
            
                <div class="panel-body" style="overflow-x: auto">
                     <div id='div_table' class='hidden'></div>
                     <div class='col-md-4'></div>
            <div class='col-md-4'><button type='button' class='btn btn-success btn-sm' data-toggle='modal' onclick='down_excel();'   ><i class='fa fa-check '></i> Download Excel</button></div>
            <div class='col-md-4'></div>
                     
                     <!--hidden-->
                <table width="100%" class="table  table-bordered table-hover" id="example" style="width:auto">
                    <thead> 
        
                     <tr class="danger"> 
                              <td align="center">Invoice No</td>
                              <td align="center">Customer</td>
                              <td align="center">Date</td>
                              <td align="center">Delivery Date</td>
                              <td align="center">Invoice Value</td> 
                              <td align="center">Download</td> 
                    </tr>
                </thead>


                <tbody>
                    
             <?php 
						 
							 //and (deli_date='' or deli_date='0000-00-00' or deli_date IS NULL)
							$sql="SELECT * FROM s_salma where  CANCELL='0'   and company='".$_SESSION['company']."'  order by id desc";
							$t = 1;
						foreach ($conn->query($sql) as $row) {
						  //  $invcheck = "invcheck" . $t;
						    $invcheck=$row['id'];
						    $stname="";
							echo "<tr>      
							  <td>".$row['REF_NO']."</td>
                              <td>".$row["CUS_NAME"]."</td>
                              <td  align='center'>".$row['SDATE']."</td>
                              <td  align='center'>".$row['deli_date']."</td>
							  <td align='right'>".$row['GRAND_TOT']."</td>
			                    <td align='center'>   <input type='checkbox'  onclick='select_list(".$row['id'].");'   name='".$invcheck."' id='".$invcheck."'>    </td>
                              
                            </tr>";
                            $t = $t + 1;
							}
				// 			<td align='center'><button type='button' class='btn btn-success btn-sm' data-toggle='modal' onclick='down_excel(".$row['id'].");'  ><i class='fa fa-check '></i> Download</button> </td>
							  ?>
       </tbody>
       <tfoot>
           	<tr>
    	<th >Invoice No</th>
                              <th >Customer</th>
                              <th >Date</th>
                              <th >Delivery Date</th>
                              <th >Invoice Value</th> 
                              <th >Download</th>
                              	</tr>
		</tfoot>
        </table>

    </div>

    <!-- =============================== -->

 

<!-- =============================== -->



</div>
 
</div>
</div>
</section>
<script type="text/javascript">


	$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#example tfoot th').each( function () {
    	var title = $(this).text();
    	$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

    // DataTable
    var table = $('#example').DataTable({
    	
    	lengthChange: true,
    	fixedHeader: true,
    	responsive: true,
    	"deferRender": true,
    	"order": [[ 0, 'desc' ]],
    	lengthMenu: [[ 15, 25, 50,100, -1 ],[ '15 rows', '25 rows', '50 rows', '100 rows', 'Show all' ]],
    	initComplete: function () {
            // Apply the search
            this.api().columns().every( function () {
            	var that = this;

            	$( 'input', this.footer() ).on( 'keyup change clear', function () {
            		if ( that.search() !== this.value ) {
            			that
            			.search( this.value )
            			.draw();
            		}
            	} );
            } );
        }
    });

} );
</script>
<script>
   load_list(1);
</script>

 <script type="text/javascript">
  
 
   function load_list(refno) {
       
  

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "invoice_excel_data.php";                                
    var params ="Command="+"load_list1";    
    params = params + "&refno=" + refno;  
    
   
    xmlHttp.open("POST", url, true);
    
   

    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");

    // xmlHttp.onreadystatechange=re_select_list;

    xmlHttp.send(params);  

}


 function select_list(refno) {
       
    //   document.getElementById('div_table').innerHTML="";

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "invoice_excel_data.php";     
    var params = "Command=" + "select_list"; 
    params = params + "&refno=" + refno;  
    if (document.getElementById(refno).checked == true) {
        params = params + "&invcheck=Y";
    } else {
        params = params + "&invcheck=N";
    }
    
    
     

    xmlHttp.open("POST", url, true);

    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");

    // xmlHttp.onreadystatechange=re_select_list;

    xmlHttp.send(params);  

}
 
   function down_excel(refno) {
       
    //   document.getElementById('div_table').innerHTML="";

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "invoice_excel_data.php";                                 
    var params ="Command="+"down_excel";    
    params = params + "&refno=" + refno;  
     
    xmlHttp.open("POST", url, true);

    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");

    xmlHttp.onreadystatechange=re_down_excel;

    xmlHttp.send(params);  

}
function re_down_excel() {

    var XMLAddress1;
   
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
 
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("div_table");
        document.getElementById('div_table').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;  
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("invno");
       
       tableToExcel('div_table', 'Report','INVOICE LIST')

}

 
function tableToExcel(table, name, filename) {
        let uri = 'data:application/vnd.ms-excel;base64,', 
        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><title></title><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>', 
        base64 = function(s) { return window.btoa(decodeURIComponent(encodeURIComponent(s))) },         format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; })}
        
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}

        var link = document.createElement('a');
        link.download = filename;
        link.href = uri + base64(format(template, ctx));
        link.click();
}

 
</script>