<?php
include_once("../../lib/konfig.php");
session_start();
$mulai = $_GET['mulai'];
$hingga = $_GET['hingga'];
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);

$mesin = "SELECT kd_mesin,mesin FROM tb_mesin;";

$lot = "SELECT A.id,A.shift,A.tgl,date_format(A.tgl,'%d %b %Y') as tanggal,A.kd_mesin,C.mesin,A.nomor,A.sku,B.barang,
        ifnull(D.cari_gudang,0) as cari_gudang
        FROM tb_lot A 
        Left join tb_fg B on B.sku = A.sku
        Left join tb_mesin C on C.kd_mesin = A.kd_mesin
        Left join(select id_produksi,count(id_produksi) as cari_gudang from tb_fg_gudang group by id_produksi)D on D.id_produksi = A.id 
        where A.tgl between '$mulai' and '$hingga'
        order by A.tgl,A.shift;";

$brg = "SELECT sku,barang,jenis FROM tb_fg;";

$resultmesin = $MySQLi->query($mesin);
$resultlot = $MySQLi->query($lot);
$brglot = $MySQLi->query($brg);
?>
<table class="table table-sm table-bordered">
    <thead>
        <tr>
            <th>#</th>
            <th>Sku</th>
            <th>Barang Jadi</th>
            <th width="50px">Tanggal</th>
            <th>Shift</th>
            <th>No Mesin</th>
            <th width="100px">Jumlah FG</th>
            <th>Cetak</th>
            <th>Tranfer ke gudang</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        foreach ($resultlot as $l) {
            echo "<tr>";
            echo "<td>" . $i . "</td>";
            echo "<td>" . $l['sku'] . "</td>";
            echo "<td>" . $l['barang'] . "</td>";
            echo "<td>" . $l['tanggal'] . "</td>";
            echo "<td>" . $l['shift'] . "</td>";
            echo "<td>" . $l['mesin'] . "</td>";
            echo "<td>" . $l['nomor'] . "</td>";
            echo "<td><button type='button' class='btn btn-warning btn-sm' onclick='cetaklot(" . $l['id'] . ")'><i class='bi bi-receipt-cutoff'></i></button></td>";
            if ($l['cari_gudang'] == 0) {
                echo "<td><button type='button' class='btn btn-info btn-sm' onclick='tflot(" . $l['id'] . ")'><i class='bi bi-box-arrow-right'></i></button></td>";
            } else {
                echo "<td>Transfer</td>";
            }

            //echo "<td>" . $l['cari_gudang'] . "</td>";
            echo "</tr>";
            $i++;
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="3"><select class="form-control" id="brglot">
                    <?php
                    foreach ($brglot as $b) {
                        echo "<option value='" . $b['sku'] . "'>" . $b['barang'] . "</option>";
                    }
                    ?>
                </select></th>
            <th><input type="date" id="tgllot" class="form-control"></th>
            <th><select class="form-control" id="shiftlot">
                    <option value="1">Shift 1</option>
                    <option value="2">Shift 2</option>
                    <option value="3">Shift 3</option>
                </select></th>
            <th><select class="form-control" id="mesinlot">
                    <?php
                    foreach ($resultmesin as $m) {
                        echo "<option value='" . $m['kd_mesin'] . "'>" . $m['mesin'] . "</option>";
                    }
                    ?>
                </select></th>
            <th><input type="number" id="qtylot" class="form-control"></th>
            <th colspan="2"><button type="button" onclick="simpanlot()" class="btn btn-primary btn-sm">simpan</button></th>
        </tr>
    </tfoot>
</table>