<?php
error_reporting(E_ERROR | E_PARSE);
session_start();
include '../../class/ClassForm.php';
$query = "select distinct a.*,b.biller_GSTIN,b.biller_PAN from domestic_invS a 
inner join biller_details b on a.billed_to = b.id where Client_id =".$_SESSION['user_id'];
$result = $ClassForm->Fetch_Result_Set($query);



?>

<table class="table  tesla" id="inv_table">
    <thead>
        <tr>
            <th> Billed to GSTIN
            </th>
            <th> Invoice No
            </th>
            <th> Invoice Date
            </th>
            <th> Irn
            </th>
            <th> Download QR
            </th>
            <th> Download Excel
            </th>

        </tr>
    </thead>
    <tbody>
        <?php
    while($row = mysqli_fetch_assoc($result)){
        ?>
        <tr>
            <td> <?php echo $row['biller_GSTIN']; ?> </td>
            <td> <?php echo $row['inv_no']; ?> </td>
            <td> <?php echo $row['inv_date']; ?> </td>
            <td> <input type="file" name="myfile[]" class="file" />
                <input type="hidden" name="inv_id" class="nextTofile " title="<?php echo $row['id']; ?>" value="<?php echo $row['inv_no']; ?>" />
            </td>
            <td> </td>
            <td> <a href="#" onClick='dispInv("+<?php echo $row['id']; ?>+")'>Download File</a></td>
        </tr>
        <?php
    }
    ?>
    </tbody>
</table>

<script>
function dispInv(id) {    
    $.ajax({
        url: 'pages/domestic/display_domestic.php?id=' + id,
        method: 'GET',
        // data: {id : id},
        contentType: false,
        processData: false,
        //dataType: 'json',
        success: function(response) {
            $('#displayPanel').html(response);
        }
    })
}
$('.file').change(function(e) {
    
    var ext = $(this).val().split('.').pop().toLowerCase();   
    if ($.inArray(ext, ['json', 'JSON']) == -1) {
        swal('Please Select Json File ...');
    } else {
        var InvoiceNo = $(this).next('.nextTofile').val();
        var InvoiceId = $(this).next('.nextTofile').attr('title');
        var file = e.target.files[0];        
        var data = '';
        var path = (window.URL || window.webkitURL).createObjectURL(file);
        readTextFile(path, function(text) {
            data = JSON.parse(text);
            console.log(data);  
            var formdata = new FormData();
            formdata.append('inv_id',InvoiceId);
            formdata.append('inv_no',InvoiceNo);
            formdata.append('inv_Irn',data.Irn);
            formdata.append('SignedInvoice',data.SignedInvoice);
            formdata.append('SignedQRCode',data.SignedQRCode);
            formdata.append('file',file);
        });        
        setTimeout(() => {
            callValidation(InvoiceNo,data,file.name);
        }, 5000);
    }

});
function readTextFile(file, callback) {
    var rawFile = new XMLHttpRequest();
    rawFile.overrideMimeType("application/json");
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4 && rawFile.status == "200") {
            callback(rawFile.responseText);
        }
    }
    rawFile.send(null);
}
function callValidation(inv_no,data,file){
    var message = 'The file "'+file+'" data not match your invoice No '+inv_no+'.. uploaded data='+data.Irn;
    //swal('File',message);
    //var res = JSON.stringify(data);
    swal('Data MissMatch please upload relevant json file..',message);
}
</script>