<?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=<?= $d['0']?>&view=asset"><?= getClientName($d[0])?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Completed Reports: <?= getItemName($d[2])?></li>';
</script>

<ul class="nav nav-tabs nav-justified bg-white mt-0" id="myTab" role="tablist">
    <li class="nav-item" role="presentation">
        <a data-mdb-tab-init id="reptab" class="nav-link active btn-info text-light" data-mdb-toggle="tab" href="#repcard" role="tab" aria-controls="repcard" aria-selected="true">Active Reports</a>
    </li>
    <li class="nav-item" role="presentation">
        <a data-mdb-tab-init id="wstab" class="nav-link btn-info text-light" data-mdb-toggle="tab" href="#wscard" role="tab" aria-controls="wscard" aria-selected="false">Archived Reports</a>
    </li>
</ul>

<div class="tab-content card">
    <div class="tab-pane fade" id="wscard" role="tabpanel" aria-labelledby="wstab">
        <?php 
            $stmt = $GLOBALS['con']->prepare("SELECT `repitem`,`repnumber`,`reptype`,`itemnumber`,`insdate`,`adefect`,`bdefect`,`delivery` FROM `archiveitems` WHERE `itemnumber`=? ORDER BY abs(insdate) DESC")or die(mysqli_error($GLOBALS['con']));
            $stmt->bind_param('i',$d[2]);$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="col-md-12 blockquote py-0">
            <p>Archived reports for <strong><?= getItemName($d[2])?></strong><br><small>Hover over the icons for additional information</small></p>
        </div>


        <?php
                while($dta=mysqli_fetch_array($r)){
                    if($dta['reptype']==""){}
                    else{
                $tag="";
                $clientcall = mysqli_query($GLOBALS['con'],"SELECT * FROM `clientdetails` WHERE `clientnumber`='".$dta['clientnumber']."'");
                $cd=mysqli_fetch_array($clientcall);
                //$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['repnumber']?>" class="">
            <div class="card card-body fileIcon text-center mb-4">
                <a href="index.php?t=includes/master&p=items.php&view=view&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['data']?>&mode=reportitems" data-mdb-tooltip-init
                   data-mdb-placement="bottom" data-mdb-trigger="hover" data-mdb-html="true" title="<?= $title?>"><i style="color:<?= $col?>" class="fad fa-<?= $file?>" aria-hidden="true"></i></a><br />
                <p><strong><?= $dta['repnumber']?></strong><br><?= getReportDesc($dta['reptype'],'formtype')?><br><?= $date?><br>
                </p>
                <div class="justify-content-center">

                    <a href="index.php?t=includes/master&p=items.php&view=view&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['data']?>&mode=archiveitems" class="btn btn-secondary" data-mdb-tooltip-init
                       data-mdb-placement="bottom"
                       data-mdb-trigger="hover"
                       title="View  Report"><i class="fad fa-eye"></i></a>
                    <?php if($dta['delivery']=="0" || $dta['delivery']==""){?>
                    <button type="button" class="btn btn-secondary" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Report not delivered"><i class="fad fa-paper-plane"
                           aria-hidden="true"> </i></button>
                    <?php }
                        else {?>
                    <button type="button" class="btn btn-info" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Report delivered <?= date('d-m-y',$dta['delivery'])?>"><i class="fad fa-paper-plane"
                           aria-hidden="true"> </i></button>
                    <?php }?>

                    <a href="index.php?t=includes/master&p=formgen.php&mode=archiveReload&formnumber=<?= $dta['reptype']?>&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['data']?>&savemode=edit" class="btn btn-secondary"
                       data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Edit  Report"><i class="fad fa-pencil"></i></a>

                    <button type="button" class="btn btn-secondary" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Report Qr" onclick="loadQR(<?=$dta['repnumber']?>)"><i class="fad fa-qrcode"
                           aria-hidden="true"> </i></button>
                </div>
            </div>
        </div>

        <?php }
    }?>

        <?php }?>
    </div>

    <div class="tab-pane fade show active" id="repcard" role="tabpanel" aria-labelledby="reptab">
        <?php
            $stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`repitem`,`repnumber`,`reptype`,`itemnumber`,`insdate`,`adefect`,`bdefect`,`obs`,`delivery`,`result` FROM `reportitems` WHERE `itemnumber`=? AND `clientnumber`=? ORDER BY abs(insdate) DESC")or die(mysqli_error($GLOBALS['con']));
            $stmt->bind_param('ii',$d[2],$d[0]);$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="col-md-12 blockquote py-0">
            <p>Completed reports for <strong><?= getItemName($d[2])?></strong><br><small>Hover over the icons for additional information</small></p>
        </div>

        <div class="row row-cols-1 row-cols-md-5 g-4">
            <?php
        $c=1;
        while($dta=mysqli_fetch_array($r)){
        $tag="";
        $clientcall = mysqli_query($GLOBALS['con'],"SELECT * FROM `clientdetails` WHERE `clientnumber`='".$dta['clientnumber']."'");
         $cd=mysqli_fetch_array($clientcall);
        //$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="#00695c";$tag="PNA"; $title="PNA";$file="file-minus";}
        if($dta['result']=="" ){$col="#9211E8";$tag="P2UD"; $title="P2UD";$file="file";}
        $email_values = [
            0 => $dta['clientnumber'], //client
            1 => $cd['pri_contact_name'], //type
            2 => $cd['email'], //recipient
            3 => 'emailing documents', //subject
            4 => '', //message
            5 => 'output', //attachment
            6 => 'reports/'.$dta['clientnumber'].'/'.$dta['repnumber'].'_'.$dta['itemnumber'].'.pdf' //source
           
        ];
        $email64_values = base64_encode(json_encode($email_values));

        ?>
            <div id="R<?= $dta['repnumber']?>">
                <div class="card card-body fileIcon text-center mb-4 border border-info hover-info">

                    <a href="index.php?t=includes/master&p=items.php&view=view&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['data']?>&mode=reportitems"
                       data-mdb-tooltip-init
                       data-mdb-placement="bottom" data-mdb-trigger="hover" data-mdb-html="true" title="<?= $title?>"><i style="color:<?= $col?>" class="fad fa-<?= $file?>" aria-hidden="true"></i></a><br />
                    <p><strong><?= $dta['repnumber']?></strong><br><?= getReportDesc($dta['reptype'],'formtype')?><br><?= $date?><br>
                    </p>
                    <div class="justify-content-center">
                        <a href="javascript:void(0)" class="btn btn-secondary noprint emailButton"
                           data-values="<?=$email64_values?>"
                           data-href="<?=ROOT_URL?>/includes/master/mailer.php" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" title="Email Client"><i class="fad fa-envelope"></i></a>
                        <a href="index.php?t=includes/master&p=items.php&view=view&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['data']?>&mode=reportitems" class="btn btn-secondary" data-mdb-tooltip-init
                           data-mdb-placement="bottom"
                           data-mdb-trigger="hover"
                           title="View  Report"><i class="fad fa-eye"></i></a>
                        <?php if($dta['delivery']=="0"){?>
                        <button type="button" class="btn btn-secondary" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Report not delivered"><i class="fad fa-paper-plane"
                               aria-hidden="true"> </i></button>
                        <?php }
                        else {?>
                        <button type="button" class="btn btn-info" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Report delivered <?= date('d-m-y',$dta['delivery'])?>"><i
                               class="fad fa-paper-plane"
                               aria-hidden="true"> </i></button>
                        <?php }?>

                        <a href="index.php?t=includes/master&p=formgen.php&mode=reload&formnumber=<?= $dta['reptype']?>&repnumber=<?= $dta['repnumber']?>&data=<?= $_REQUEST['data']?>&savemode=edit" class="btn btn-secondary"
                           data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Edit  Report"><i class="fad fa-pencil"></i></a>

                        <button type="button" class="btn btn-secondary" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" title="Report Qr" onclick="loadQR(<?=$dta['repnumber']?>)"><i class="fad fa-qrcode"
                               aria-hidden="true"> </i></button>
                    </div>
                    <?php if(($_SESSION['access']==1 || $_SESSION['access']==2 || $_SESSION['access']==999) && $c==1){?>
                    <div style="position:absolute; top:3px; right:3px">
                        <button onclick="delRep('<?= $dta['repitem']?>','<?= $_REQUEST['data']?>')" type="button" class="btn btn-sm btn-danger m-0 px-3 py-2 " data-mdb-tooltip-init data-mdb-placement="bottom"
                                data-mdb-trigger="hover"
                                title="Delete Report"><i class="fad fa-times" aria-hidden="true" style="--fa-secondary-opacity: 1.0"> </i></button>
                    </div>

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

            <?php $c++;}?>
        </div>
        <?php }?>
    </div>

</div>
<script>
function loadQR(repnumber) {
    var target = ROOT_URL + '/includes/asset/asset_qr.php'
    var dest = encodeURI(target + "?repnumber=" + repnumber + "&tenant=" + TENANT_URL);
    loadModalContent(dest, "qrBox", "modal-xl", 1060, "true", "Report Security QR");
}

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

    });
    // alert(selected)
}

function delRep(r, d) {
    var dest = '<p></p>Are you sue you want to remove this inspection registration?</p>';
    loadModalContent(dest, "popupBox", "modal-xl", 1060, "true", 'Inspection Removal', "cancelConfirm", () => delConfirm(r, d));
}

function delConfirm(r, d) {
    $('#R' + r).fadeOut()
    $.post('<?=ROOT_URL?>/includes/master/item_functions.php', {
        FUNCTION: 'remove',
        rep: r,
        dataSet: d,
        tenant: TENANT_URL
    }, function(ret) {
        //debug(ret)
        window.location.href = "index.php?t=includes/master&p=items.php&data=<?= $_REQUEST['data']?>&view=list"
    })
}

document.body.addEventListener('click', function(event) { // Event delegation on body
    if (event.target.classList.contains('emailButton')) {
        const target = event.target.dataset.href;
        const string = event.target.dataset.values;

        var dest = encodeURI(target + "?string=" + string + "&mode=popup&tenant=" + TENANT_URL);
        loadModalContent(dest, "popupBox", "modal-xl", 1060, "true");

    }
});
</script>