<?php
session_start();
date_default_timezone_set('Asia/Ho_Chi_Minh');

// Kiểm tra quyền quản trị
if (!isset($_SESSION["tendn"]) || $_SESSION["tendn"] <> "quantri") {
    echo "No accessright";
    exit();
}

require("Connection.php");

// 1. Lấy dữ liệu input an toàn
$truongmn = isset($_POST["truongmn"]) ? $_POST["truongmn"] : "";
$trangthai = isset($_POST["timtt"]) ? $_POST["timtt"] : "";

// 2. Xử lý ngày tháng chuẩn PHP
$tungay = isset($_POST["tungay"]) ? $_POST["tungay"] : "";
if ($tungay == "") {
    // Lấy ngày hôm qua
    $tungay = date("Y-m-d", strtotime("-1 day"));
}

$denngay = isset($_POST["denngay"]) ? $_POST["denngay"] : "";
if ($denngay == "") {
    // Lấy ngày mai
    $denngay = date("Y-m-d", strtotime("+1 day"));
}

// 3. Xây dựng câu Query
$chuoitim = "";

// Escape dữ liệu trước khi ghép chuỗi SQL
$truongmn_safe = mysqli_real_escape_string($link, $truongmn);
$trangthai_safe = mysqli_real_escape_string($link, $trangthai);
$tungay_safe = mysqli_real_escape_string($link, $tungay);
$denngay_safe = mysqli_real_escape_string($link, $denngay);

if ($truongmn <> "")
    $chuoitim .= " a.matruongmn = '" . $truongmn_safe . "' and ";

if ($trangthai <> "")
    $chuoitim .= " a.trangthai = '" . $trangthai_safe . "' and ";

if ($tungay <> "" and $denngay <> "")
    $chuoitim .= " a.ngaydexuat between '" . $tungay_safe . "' and '" . $denngay_safe . "' and ";

// Main Query
if ($chuoitim <> "")
    $sql = "SELECT  a.*,b.*, c.diachi FROM DEXUATMUABANDO a , DANHMUCBANDO b, TRUONGROBOT c where " . $chuoitim . " a.mabando=b.mabando and a.matruongmn=c.matruongmn  order by a.ngaydexuat desc limit 0,500";
else
    $sql = "SELECT  a.*,b.*, c.diachi FROM DEXUATMUABANDO a , DANHMUCBANDO b, TRUONGROBOT c where  a.mabando=b.mabando and a.matruongmn=c.matruongmn  order by a.ngaydexuat desc limit 0,500";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<HEAD>
    <Title>De xuat giao trinh</Title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="css/default.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        </style>
</HEAD>

<body>
    <div align="center"><span class="style7">CẤP PHÁT BẢN ĐỒ ROBOT</Span></div>
    <form name="f1" method="POST">
        <div align="center">
            <table width="82%" border="0" cellpadding="0" cellspacing="0" class="khungbao1">
                <tr>
                    <td width="15%" height="33" class="style5"><div align="right">Trường MN : </div></td>
                    <td width="33%">
                        <select name="truongmn" class="box1">
                            <option value="">Tất cả</option>
                            <?php
                            // --- Query Dropdown Trường ---
                            $sql1 = "select * from TRUONGROBOT order by diachi";
                            $result1 = mysqli_query($link, $sql1);
                            
                            if (mysqli_num_rows($result1) > 0) {
                                while ($row1 = mysqli_fetch_array($result1)) {
                                    $matrmn = $row1["matruongmn"];
                                    $tentruongmn = $row1["diachi"];
                            ?>
                                    <option value="<?php echo $matrmn; ?>" <?php if ($matrmn == $truongmn) echo " selected"; ?>>    <?php echo $tentruongmn; ?>        </option>
                            <?php
                                }
                            }
                            ?>

                        </select>
                        <input name="cmdadd" type="button" value="THÊM TRƯỜNG MỚI" onclick="add()"/>
                        <input name="cmdadd2" type="button" value="XUẤT ROBOT" onclick="addROBOT()"/>
                    </td>
                    <td width="14%"><div align="right" class="style5">Trạng thái:</div></td>
                    <td width="21%">
                        <select name="timtt" class="box1" id="timtt">
                            <option value="">Tất cả</option>
                            <option value="XK" <?php if ($trangthai == "XK") echo " selected"; ?>>XUẤT KHO</option>
                            <option value="DX" <?php if ($trangthai == "DX") echo " selected"; ?>>ĐỀ XUẤT</option>
                            <option value="DG" <?php if ($trangthai == "DG") echo " selected"; ?>>ĐÃ GIAO</option>
                        </select>
                    </td>
                    <td width="17%"><div align="right">
                        <input name="cmd1" type="button" value="Tìm kiếm" onclick="kt()"/>
                    </div></td>
                </tr>

                <tr>
                    <td class="style5"><div align="right">Ngày đề xuất :</div></td>
                    <td colspan="4">
                        <span class="style5"> &nbsp;<span class="style8">từ ngày </span></span>
                        <input name="tungay" type="text" value="<?php echo $tungay; ?>"/>
                        <span class="style5"><span class="style8">đến ngày </span></span>
                        <input name="denngay" type="text" value="<?php echo $denngay; ?>"/>
                    </td>
                </tr>
            </table>
        </div>

        <div align="center">
            <br />
            <input type="hidden" value="<%=malh%>" name="malh" />
        </div>

        <p align="center">&nbsp;</p>
        <table width="100%" cellspacing="0" class="khungbao1">
            <tr>
                <td width="3%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">MãQL</font></p></td>
                <td width="9%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Ngày đề xuất</font></p></td>
                <td width="14%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Cập nhật</font></p></td>
                <td width="7%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Số lượng </font></p></td>
                <td width="36%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Bản đồ đã mua</font></p></td>
                <td width="9%" align="center" class="dkta1"><p align="center"><font color="#800000" size="2" face="Arial">Người đề xuất</font></p></td>
                <td width="9%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Trạng thái</font></p></td>
                <td width="8%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Trường</font></p></td>
            </tr>
            <?php
            // --- LOOP HIỂN THỊ DỮ LIỆU ---
            $result = mysqli_query($link, $sql);
            
            // Check lỗi SQL nếu có
            if (!$result) {
                echo "<tr><td colspan='8'>Lỗi truy vấn: " . mysqli_error($link) . "</td></tr>";
            } else {
                $tongrec = mysqli_num_rows($result);
                $i = 0;
                
                if ($tongrec > 0) {
                    while ($row = mysqli_fetch_array($result)) {
                        $i++;
                        // FIX LỖI COPY PASTE: Code cũ gọi $row1["malopmn"] ở đây là sai (biến $row1 ở vòng lặp trên).
                        // Vì bảng DEXUATMUABANDO không có malopmn (thường là vậy), ta bỏ dòng đó đi để tránh lỗi PHP 8.
                        
                        $ngayhh = date("Y-m-d H:i:s");
            ?>
                        <tr>
                            <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["madx"]; ?></font>&nbsp;</div></td>
                            <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["ngaydexuat"]; ?></font>&nbsp;</div></td>
                            <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["ngaycapnhat"]; ?></font>&nbsp;</div></td>
                            <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["soluong"]; ?></font>&nbsp;</div></td>
                            <td align="left" class="dkta1"><div align="left"><font face="arial" size="2"><?php echo $row["mabando"] . "-" . $row["tenbando"]; ?></font>&nbsp;</div></td>
                            <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["nguoidx"]; ?></font>&nbsp;</div></td>
                            <td align="center" class="dkta1">
                                <div align="center"><strong>
                                    <select name="trangthai<?php echo $row["madx"]; ?>" class="box1">
                                        <option value="XK" <?php if ($row["trangthai"] == "XK") echo " selected"; ?>>XUẤT KHO</option>
                                        <option value="DX" <?php if ($row["trangthai"] == "DX") echo " selected"; ?>>ĐỀ XUẤT</option>
                                        <option value="DG" <?php if ($row["trangthai"] == "DG") echo " selected"; ?>>ĐÃ GIAO</option>
                                    </select>
                                &nbsp;</strong></div>
                            </td>
                            <td align="center" class="dkta1">
                                <div align="center"><font face="arial" size="2"><?php echo $row["diachi"]; ?></font>
                                </div>
                                <input name="ngaycn<?php echo $row["madx"] ?>" type="hidden" class="box1" value="<?php echo $ngayhh; ?>" size="15" maxlength="10" />
                            </td>
                        </tr>
            <?php
                    }
                }
            }
            ?>
        </table>
        <p align="center">
            <input type="button" value="Cập nhật" name="cmd1" onclick="chuyenlop()"/>
            &nbsp;&nbsp;
            <input name="cmd12" type="button" value="Trở về" onclick="javascript:window.location.href='logoutbando.php';" />
        </p>
    </form>
</body>
</HTML>

<script language="javascript">
    function checknum() {
        var a = window.event.keyCode;
        if (a < 48 || a > 57)
            window.event.keyCode = 0;
    }

    function kt() {
        document.f1.action = "capphatbando.php";
        document.f1.submit();
    }
    
    function chuyenlop() {
        document.f1.action = "capnhatcapphatbando.php";
        document.f1.submit();
    }

    function add() {
        window.location.href = "themtruongmoi.php";
    }
    
    function addROBOT() {
        window.location.href = "themrobot.php";
    }
</script>

<?php 
$tcok = isset($_GET["tc"]) ? $_GET["tc"] : "";
if ($tcok == "10") { ?>
    <script language="javascript">
        alert("Cap nhat thanh cong !");
    </script>
<?php } ?>