<?php $_REQUEST['src'] = 'ext2';
require_once('../../functions/global.php'); ?>
<div id="loadingBox" style="position: fixed; top:50%; width:100%">
    <h1 class="text-center"><i class="fad fa-flux-capacitor fa-spin fa-3x fa-fw"
            style="--fa-primary-color: #0055A6; --fa-secondary-color: gray;"></i><br>Loading Data <br><small>Please
            wait</small></h1>
</div>


<div class="row mb-5">
    <div class="col-12">
        <h4>Item overview - <span id="name"><?= getClientName($_REQUEST['clientnumber']) ?></span> <span
                class="float-end"><a class="btn btn-sm btn-secondary me-2" onclick="filterStack()"><i
                        class="fad fa-filter"></i> Filter by range</a> <a class="btn btn-sm btn-secondary"
                    onclick="printLabel()"><i class="fad fa-print"></i> print</a></span></h4>
    </div>
</div>
<section id="start" class="dark-grey-text hidden">

    <div class="row">

        <div class="col-12 col-md-6 mb-4">
            <a href="javascript:void(0)" class="card hoverable">
                <div class="card-body">
                    <div class="media">
                        <span
                            class="card-img-100 d-inline-flex justify-content-center align-items-center rounded-circle grey lighten-3 me-4">
                            <i class="fad fa-calendar-alt fa-2x text-info"></i>
                        </span>
                        <div class="media-body">
                            <h5 class="dark-grey-text mb-3">Examiniation period start</h5>
                            <p class="font-weight-light text-muted mb-0"><input class="form-control datepicker"
                                    type="text" name="date_start" id="date_start" placeholder="Click or tap to begin" />
                            </p>
                        </div>
                    </div>
                </div>
            </a>
        </div>

        <div class="col-12 col-md-6 mb-4">
            <a href="javascript:void(0)" class="card hoverable">
                <div class="card-body">
                    <div class="media">
                        <span
                            class="card-img-100 d-inline-flex justify-content-center align-items-center rounded-circle grey lighten-3 me-4">
                            <i class="fad fa-calendar-alt fa-2x  purple-text"></i>
                        </span>
                        <div class="media-body">
                            <h5 class="dark-grey-text mb-3">Examiniation period end</h5>
                            <p class="font-weight-light text-muted mb-0"><input class="form-control datepicker"
                                    type="text" name="date_end" id="date_end" placeholder="END DATE"
                                    value="<?= date('d-m-Y') ?>" /></p>
                        </div>
                    </div>
                </div>
            </a>
        </div>

    </div>
    <div class="col-md-12 text-center mt-4"><a class="btn btn-info" href="javascript:void(0)" onClick="loadData()">Load
            Data</a></div>
</section>

<table id="tableData" class="table table-striped tavble-condensed table-sm" style="display:none">
    <thead class="p-0">
        <tr class="p-0">
            <th class="printhead py-0">Item</th>
            <th class="printhead py-0">Code</th>
            <th class="printhead py-0">Count</th>
            <th class="printhead py-0">Frequency</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $unitsTotal = 0;
        $sdcTotal = 0;

        $sp = mysqli_query($GLOBALS['con'], "SELECT DISTINCT `desc`,`code` FROM `itemdetails` WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `statuslive`='1'");
        while ($sd = mysqli_fetch_array($sp)) {

            $spc = mysqli_query($GLOBALS['con'], "SELECT `itemnumber`,`code` FROM `itemdetails` WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `statuslive`='1'  AND `desc`='" . $sd['desc'] . "' AND `code`='" . $sd['code'] . "'") or die(mysqli_error($GLOBALS['con']));
            $sdc = mysqli_num_rows($spc);
            $sdx = mysqli_fetch_array($spc);

            $iuc = mysqli_query($GLOBALS['con'], "SELECT `units`,`frequency`,`code` FROM `dd_plant` WHERE `code`='" . $sdx['code'] . "'") or die(mysqli_error($GLOBALS['con']));
            $iuc = mysqli_fetch_array($iuc);

            $units = $iuc['units'] * $sdc;
            $sdcTotal += $sdc;
            $unitsTotal += $units;
            echo "<tr><th>" . $sd['desc'] . "</th><th>" . $iuc['code'] . "</th><td>" . $sdc . "</td><td>" . $iuc['frequency'] . "</td></tr>";
        }; ?>
    </tbody>

    <tfoot class="bg-info text-white">
        <tr>
            <th class="py-0"></th>
            <th class="py-0"></th>
            <td class="printhead py-0"><?= $sdcTotal ?></td>
            <td class="printhead py-0"></td>
        </tr>
    </tfoot>
</table>
</div>

<script>
function loadData() {
    $('#loadingBox').css('display', 'block');
    $('#tableData').css('display', 'none');
    $('#start').css('display', 'none');
    var client = '<?= $_REQUEST['clientnumber'] ?>';
    var location = '<?= $_REQUEST['locnumber'] ?>';
    var s = $('#date_start').val();
    var e = $('#date_end').val();
    var n = $('#client').find(':selected').data('name')
    $.post('includes/master/itemSummary_functions.php', {
        FCN: 'load',
        locnumber: location,
        clientnumber: client,
        start: s,
        end: e
    }, function(ret) {
        var d = ret.split('|');
        $('#tableData tbody').html($.trim(d[0]));
        $('#tableData tfoot').html($.trim(d[1]));
        $('#loadingBox').fadeOut();
        $('#tableData').fadeIn();
        $('#name').text(n);
    })
}

function filterStack() {
    $('#start').fadeIn();
}

$(document).ready(function() {
    $('#loadingBox').fadeOut();
    $('#tableData').fadeIn();
})

function printLabel() {
    var title = $('#name').text();
    $("#tableData").print({
        addGlobalStyles: true,
        stylesheet: 'src/css/print.css',
        rejectWindow: true,
        noPrintSelector: ".no-print",
        iframe: true,
        prepend: '<div class="col-12 mb-4"><h4>Item summary for: <strong>' + title + '</strong></h4></div>'
    });
}

$(".datepicker").pickadate({
    format: "dd-mm-yyyy",
    formatSubmit: "dd-mm-yyyy",
    selectMonths: true,
    max: new Date(),
});
</script>