<?php	
include('db-connect.php');


$idtrfdetails='160';

$idcertificate='5';
$row1 = mysqli_query($con,"SELECT * FROM auditdpi WHERE auditdpi.idtrfdetails='$idtrfdetails'");
$idstandards=mysqli_query($con,"SELECT idstandards ,standardname FROM standards");

//query list protectioncategory
$proteccategorylist=mysqli_query($con,"SELECT idprotectioncategory ,name_protectioncategory FROM protectioncategory");

$idproteccategorylistRecord=array();       
while($idprotectioncategoryrow = mysqli_fetch_assoc($proteccategorylist)) {
    $idproteccategorylistRecord[$idprotectioncategoryrow['idprotectioncategory']]=$idprotectioncategoryrow['name_protectioncategory'];
}
$idprotectioncategoryJson=json_encode($idproteccategorylistRecord);
?>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!--  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> -->
  <script src="https://kit.fontawesome.com/f13e61f9bf.js" crossorigin="anonymous"></script>


<script src="jquery.tabledit.js"></script>

</head>
<script type="text/javascript">

$(document).ready(function(){
	$("#add").click(function(e){
    var table = $(this).attr('for-table');  //get the target table selector
    var $tr = $(table + ">tbody>tr:last-child").clone(true, true);  //clone the last row
    var nextID = parseInt($tr.find("input.tabledit-identifier").val()) + 1; //get the ID and add one.
    $tr.find("input.tabledit-identifier").val(nextID);  //set the row identifier
    $tr.find("span.tabledit-identifier").text(nextID);  //set the row identifier
    $(table + ">tbody").append($tr);    //add the row to the table
    $tr.find(".tabledit-edit-button").click();  //pretend to click the edit button
    $tr.find("input:not([type=hidden]):not([type=checkbox]), select").val(""); 
    $tr.find("input[type='checkbox']").attr('checked', false); 
    $tr.find("input[name=idtrfdetails]").val('<?php echo $idtrfdetails; ?>');
      //wipe out the inputs.
    $(this).prop('disabled', true);
});
$('#example1').Tabledit({
    url: 'editinline/logic-edit-delete.php',
    columns: {
        identifier: [0, 'id'],
        editable: [[2, 'certificatenumber'], [3, 'issuebycimac','checkbox'], [4, 'moduleselection'], [5, 'sampleinstore','checkbox'], [6, 'filenameaudit','file'],[7, 'idtrfdetails']]
    },
    onDraw: function() {
        console.log('onDraw()');
    },
    onSuccess: function(data, textStatus, jqXHR) {
        console.log('onSuccess(data, textStatus, jqXHR)');
        console.log(data);
        console.log(textStatus);
        console.log(jqXHR);
        $("#add").prop('disabled', false);
    },
    onFail: function(jqXHR, textStatus, errorThrown) {
        console.log('onFail(jqXHR, textStatus, errorThrown)');
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    },
    onAlways: function() {
        console.log('onAlways()');
    },
    onAjax: function(action, serialize) {
        console.log('onAjax(action, serialize)');
        console.log(action);
        console.log(serialize);
    }
	});


});

</script>
<style>

.table.user-select-none {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
.file-upload {
    display:none !important;
}
.uploadfile {
    cursor: pointer;
    color: #fff;
    background-color: #0d6efd;
    border-color: #0d6efd;
    padding: 0.375rem 0.75rem;
}
</style>
<script>
$(function(){
    //file input field trigger when the drop box is clicked
    //call a function to handle file upload on select file
    $(".uploadfile").click(function(e){
        $('#fileInput').click();
        
    });   
    $('input[type=file]').on('change', fileUpload);
});

function fileUpload(event){
    //get selected file
    files = event.target.files;
    
    //form data check the above bullet for what it is  
    var data = new FormData();                                   

    //file data is presented as an array
    for (var i = 0; i < files.length; i++) {
        var file = files[i];
        /*if(!file.type.match('image.*')) {              
            //check file type
            $("#dropBox").html("Please choose an images file.");
        }else if(file.size > 1048576){
            //check file size (in bytes)
            $("#dropBox").html("Sorry, your file is too large (>1 MB");
        }else{ */
            //append the uploadable file to FormData object
            data.append('file', file, file.name);
            
            //create a new XMLHttpRequest
            var xhr = new XMLHttpRequest();     
            
            //post file data for upload
            xhr.open('POST', 'upload.php', true);  
            xhr.send(data);
            xhr.onload = function () {
                //get response and show the uploading status
                var response = JSON.parse(xhr.responseText);
                if(xhr.status === 200 && response.status == 'ok'){
                    $('.file-upload').each(function(){
                        
                       
if ( $(this).closest( ".tabledit-edit-mode" ).find('.uploadfile').css('display') == 'inline')
{
    $(this).val(response.filename);
}
});
                   
                    $("#dropBox").html("File has been uploaded successfully.");
                }else if(response.status == 'type_err'){
                    $("#dropBox").html("Please choose an images file.");
                }else{
                    $("#dropBox").html("Some problem occured, please try again.");
                }
            };
       /* } */
    }
}
</script>
</head>
<body>

 <div class="container" style="margin-top:80px;">
 <form style="display:none;">		
        <div id="dropBox" >
           
        </div>
        <input type="file" name="fileInput" id="fileInput"  />
    </form>
    <div class="panel panel-default">
 
       
        <div class="panel-body">
            <div class="text-end">
            <button type="button" id="add" class="btn btn-primary" for-table='#example1'>Add</button> 
</div>
        <table class="table" id="example1" style="border:1px solide red">
    	<tr><th style="display:none;">id</th><th style="display:none;">idtrfdetails</th><th>certificatenumber</th><th>issuebycimac</th><th>moduleselection</th><th>sampleinstore</th><th>filenameaudit</th></tr>
    	<?php while($row = mysqli_fetch_assoc($row1)) {?>
    	<tr>
    		<td style="display:none;"><?php echo $row['idauditdpi'];?></td>
            <td style="display:none;"><?php echo $row['idtrfdetails'];?></td>
    		<td><?php echo $row['certificatenumber']; ?></td>
            <td><?php if ($row['issuebycimac']=='yes') { ?>OK<?php } else { ?>NO<?php } ?></td>
            <td><?php echo $row['moduleselection']; ?></td>
            <td><?php if ($row['sampleinstore']=='yes') { ?>OK<?php } else { ?>NO<?php } ?></td>
            <td><a href="proa.php"><?php if (!empty($row['filenameaudit'])) { ?><a href="proa.php">LINK</a><?php } else { ?><?php } ?></td>
            <td style="display:none;"><?php echo $row['idtrfdetails']; ?></td>
           
    	</tr>
       
    	<?php } ?>
        </table>
        </div>

     

    </div>
  </div>

</body>
</html>