<?php
$data=urldecode($_REQUEST['data']);$d=explode("|",$data);?>
<script>
document.getElementById('navBox').innerHTML =
    '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Client List</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $_REQUEST['clientnumber']?>&view=asset"><?= getClientName($_REQUEST['clientnumber'])?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Incomplete Reports</li>';
</script>

<ul class="nav nav-tabs md-tabs nav-justified bg-white mt-n5 mt-md-0" role="tablist">
    <li class="nav-item"><a id="reptab" class="nav-link active btn-info text-light" data-toggle="tab" href="#repcard" role="tab">Incomplete reports for <strong><?= getClientName($_REQUEST['clientnumber'])?></strong></a></li>
</ul>

<div class="tab-content card mt-4">

    <div class="tab-pane fade show active" id="repcard" role="tabpanel" aria-labelledby="reptab">
        <?php
            $stmt = $GLOBALS['con']->prepare("SELECT a.clientnumber, a.repitem, a.repnumber, a.reptype, a.itemnumber, a.insdate, a.adefect, a.bdefect, b.itemnumber, b.plantnumber, b.desc, a.result FROM reportitems_temp a INNER JOIN itemdetails b ON b.itemnumber=a.itemnumber WHERE a.clientnumber=? ORDER BY abs(repitem) DESC")or die(mysqli_error($GLOBALS['con']));
            $stmt->bind_param('i',$_REQUEST['clientnumber']);$stmt->execute();$r=$stmt->get_result();$stmt->close();
            $rnum=mysqli_num_rows($r);
            if($rnum<1){?>
        <div class="alert alert-default text-center"><i class="fad fa-exclamation-triangle text-danger" aria-hidden="true"></i> No reports on file <i class="fad fa-exclamation-triangle text-danger" aria-hidden="true"></i><br><br>
        </div>
        <?php }
            else{?>
        <div class="row d-flex flex-row flex-wrap">
            <?php
            while($dta=mysqli_fetch_array($r)){
                $tag="";
                //$conv=strtotime($dta['insdate']);
                $date=date('d-m-Y',$dta['insdate']);
                $adefect=explode("|",$dta['adefect']);
                if($dta['result']=="DEFECTS" ){$col="#F4AA09";$title="DEFECTS<br>".$dta['bdefect'];$file="file-exclamation";}
                if($dta['result']=="URGENT" ){$col="#e96152";$title="URGENT<br>".$adefect[0];$file="file-excel";}
                if($dta['result']=="PASS" ){$col="#007E33";$tag="Pass"; $title="Pass";$file="file-powerpoint";}
                if($dta['result']=="OBS" ){$col="#333333";$tag="Obs"; $title="OBS<br>".$dta['obs'];$file="file-alt";}
                if($dta['result']=="PNA" ){$col="#cccccc";$tag="PNA"; $title="PNA";$file="file";}
                if($dta['result']=="" ){$col="#9211E8";$tag="P2UD"; $title="P2UD";$file="file";}
                ?>
            <div id="R<?= $dta['repitem']?>" class="col-6 col-md-2 h-100">
                <div class="card card-body fileIcon text-center mb-4">
                    <a href="javascript:void(0)"><i style="color:<?= $col?>" class="fad fa-file-signature" aria-hidden="true"></i></a><br />
                    <p><strong><?= $dta['desc']?></strong><br><?= $dta['plantnumber']?><br><small><?= getReportDesc($dta['reptype'],'formtype')?></small><br><?= $date?><br></p>
                    <div class="justify-content-center">
                        <a href="index.php?t=includes/master&p=formgen.php&mode=continue&formnumber=<?= $dta['reptype']?>&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['clientnumber']?>|37|<?= $dta['itemnumber']?>&savemode=edit"
                           class="btn btn-secondary me-4" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Edit Report"><i class="fad fa-pencil"></i></a>

                        <a href="javascript:void(0)" onclick="removeRep('<?= $dta['repitem']?>')" class="btn btn-danger" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Delete Report"><i
                               class="fad fa-trash"></i></a>
                    </div>
                </div>
            </div>

            <?php }?>
        </div>
        <?php }?>
    </div>

</div>
<script>
function getChecks() {
    var selected = ''
    $('input[type=checkbox]').each(function() {
        if ($(this).is(":checked")) {
            selected = selected + ($(this).attr('name') + '|');
        }

    });
    //alert(selected)
}

function removeRep(r) {
    
      const target = event.target.dataset.href;
        var dest = '<p class="text-center">Are you sure you wish to remove this incomplete report?</p>';
        loadModalContent(dest, "killBox", "modal-xl", 1060, "true", "Remove incomplete report",'cancelConfirm', () => confirmRemove(r));
 
}

function confirmRemove(r) {
    $.post('<?=ROOT_URL?>/includes/master/items.php', {
        mode: 'removeReport',
        id: r,
        tenant:TENANT_URL
    }, function(ret) {
        $("#R" + r).fadeOut();
        showResponse('success', 'Report removed from the system successfully')
        killModal('killBox')
    })
}
</script>