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

// Bật hiển thị lỗi để debug (tắt đi khi chạy ổn)
// error_reporting(E_ALL); ini_set('display_errors', 1);

if (@$_SESSION["tendn"] <> "quantri") {
    echo "No accessright";
    exit();
}

require("Connection.php");

$truongmn = @$_POST["truongmn"];
$trangthai = @$_POST["timtt"];
$nvpt = @$_POST["nvpt"];
$khlop = @$_POST["khoilopmn"];
$lop = @$_POST["lopmn"];
$nhanvien = @$_POST["nvpt"]; // Trùng với nvpt ở trên

// --- FIX NGÀY THÁNG (Code cũ trừ ngày thủ công sẽ bị lỗi nếu là ngày mùng 1) ---
$tungay = @$_POST["tungay"];
if ($tungay == "") {
    $tungay = date("Y-m-d", strtotime("-1 day")); // Lấy ngày hôm qua chuẩn xác
}

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

// Khởi tạo biến để tránh lỗi Undefined Variable trên PHP 8
$ts = 0; $mn = 0; $nt = 0; $hd = ""; 

$chuoitim = "";

// --- QUERY 1: Thông tin trường ---
if ($truongmn <> "") {
    $chuoitim = $chuoitim . " a.matruongmn = '" . $truongmn . "' and ";
    $sql1 = "select * from truongmn where matruongmn = '" . $truongmn . "'";

    // FIX: mysqli + đảo tham số
    $result1 = mysqli_query($link, $sql1);
    // FIX: mysqli_num_rows
    if (mysqli_num_rows($result1) > 0) {
        while ($row1 = mysqli_fetch_array($result1)) {
            $ts = $row1["tongsolop"];
            $mn = $row1["solopmn"];
            $nt = $row1["solopnt"];
            $hd = $row1["hopdong"];
        }
    }
}

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

if ($lop <> "")
    $chuoitim = $chuoitim . " b.tenlopmn = '" . $lop . "' and ";
if ($khlop <> "")
    $chuoitim = $chuoitim . " b.khoilop = '" . $khlop . "' and ";
if ($nhanvien <> "")
    $chuoitim = $chuoitim . " c.nhanvien = '" . $nhanvien . "' and ";

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

// --- QUERY CHÍNH (MAIN QUERY) ---
if ($chuoitim <> "") {
    $sql = "SELECT a.*,b.khoilop, b.tenlopmn, c.* FROM dexuatgiaotrinh a , lopmn b, truongmn c where " . $chuoitim . " a.malopmn=b.malopmn and a.matruongmn=c.matruongmn  order by a.ngaydexuat desc limit 0,500";
} else {
    $sql = "SELECT a.*,b.khoilop, b.tenlopmn, c.diachi FROM dexuatgiaotrinh a , lopmn b, truongmn c where a.malopmn=b.malopmn 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 GIÁO TRÌNH</Span></div>
    <form name="f1" method="POST">
        <div align="center">
            <table width="70%" border="0" cellpadding="0" cellspacing="0" class="khungbao1">
                <tr>
                    <td width="10%" height="33" class="style5"><div align="right"> Nhân Viên : </div></td>
                    <td width="33%">
                        <select name="nvpt" class="box1" onchange="kt()">
                            <option value="">Tất cả</option>
                            <?php
                            // --- QUERY DROPDOWN NHÂN VIÊN ---
                            $sql_nv = "select * from nhanvien order by tennv";
                            $result_nv = mysqli_query($link, $sql_nv);
                            if (mysqli_num_rows($result_nv) > 0) {
                                while ($row_nv = mysqli_fetch_array($result_nv)) {
                                    $tennv = $row_nv["tennv"];
                            ?>
                                    <option value="<?php echo $tennv; ?>" <?php if ($tennv == $nvpt) echo " selected"; ?>> <?php echo $tennv; ?> </option>
                            <?php
                                }
                            }
                            ?>
                        </select>
                    </td>
                    
                    <td width="17%"><span class="style5">Trạng thái:
                        <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>
                    </span></td>
                    
                    <td width="12%"><div align="right" class="style5">Khối Lớp:</div></td>
                    <td width="8%"><span>
                        <select name="khoilopmn" class="box1" id="khoilopmn">
                            <option value="">Tất cả</option>
                            <option value="Khối Mầm" <?php if ($khlop == "Khối Mầm") echo " selected"; ?>>Khối Mầm</option>
                            <option value="Khối Chồi" <?php if ($khlop == "Khối Chồi") echo " selected"; ?>>Khối Chồi</option>
                            <option value="Khối Lá" <?php if ($khlop == "Khối Lá") echo " selected"; ?>>Khối Lá</option>
                        </select>
                    </span></td>
                    
                    <td width="20%"><span class="style5">Lớp:
                        <select name="lopmn" class="box1">
                            <option value="">Tất cả</option>
                            <?php
                            // --- QUERY DROPDOWN LỚP ---
                            $sql_lop = "select distinct tenlopmn from lopmn order by tenlopmn";
                            $result_lop = mysqli_query($link, $sql_lop);
                            if (mysqli_num_rows($result_lop) > 0) {
                                while ($row_lop = mysqli_fetch_array($result_lop)) {
                                    $lopmn = $row_lop["tenlopmn"];
                            ?>
                                    <option value="<?php echo $lopmn; ?>" <?php if ($lopmn == $lop) echo " selected"; ?>> <?php echo $lopmn; ?> </option>
                            <?php
                                }
                            }
                            ?>
                        </select>
                    </span></td>
                    <td width="17%"><div align="right">
                        <input name="cmd1" type="button" value="Tìm kiếm" onclick="kt()"/>
                    </div></td>
                </tr>

                <tr>
                    <td width="10%" height="33" class="style5"><div align="right">Trường MN : </div></td>
                    <td colspan="5">
                        <select name="truongmn" class="box1">
                            <option value="">Tất cả</option>
                            <?php
                            // --- QUERY DROPDOWN TRƯỜNG ---
                            if ($nvpt <> '')
                                $sql_tr = "select * from truongmn where nhanvien='" . $nvpt . "' order by diachi";
                            else
                                $sql_tr = "select * from truongmn order by diachi";

                            $result_tr = mysqli_query($link, $sql_tr);
                            if (mysqli_num_rows($result_tr) > 0) {
                                while ($row_tr = mysqli_fetch_array($result_tr)) {
                                    $matrmn = $row_tr["matruongmn"];
                                    $tentruongmn = $row_tr["diachi"];
                            ?>
                                    <option value="<?php echo $matrmn; ?>" <?php if ($matrmn == $truongmn) echo " selected"; ?>> <?php echo $tentruongmn; ?> </option>
                            <?php
                                }
                            }
                            ?>
                        </select>
                        <span class="style9">
                            <?php 
                            echo "TS lớp:" . $ts;
                            echo ", MN:" . $mn;
                            echo ", NT:" . $nt;
                            echo ", " . $hd;
                            ?>
                        </span>
                    </td>
                    <td width="17%"><div align="right">
                        <input name="cmd2" type="button" id="cmd2" onclick="kt1()" value="QL Trường- Nhân Viên"/>
                    </div></td>
                </tr>

                <tr>
                    <td class="style5"><div align="right">Ngày đề xuất :</div></td>
                    <td colspan="6">
                        <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="5%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Lớp</font></p></td>
                <td width="7%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Số lượng bé</font></p></td>
                <td width="36%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Giáo trình đã đề xuất</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);
            $tongrec = mysqli_num_rows($result);
            $i = 0;
            if ($tongrec > 0) {
                while ($row = mysqli_fetch_array($result)) {
                    $i++;
                    // FIX LỖI: Code cũ dùng $row1["malopmn"] là sai, phải là $row
                    $malopmn = isset($row["malopmn"]) ? $row["malopmn"] : ''; 
                    
                    $ngayhh = date("Y-m-d H:i:s"); // Lấy giờ hiện tại chuẩn
            ?>
                    <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["tenlopmn"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["soluongbe"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row["noidungdx"]; ?></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='logout.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 kt1() {

        {
            document.f1.action = "nhanvientruong.php";
            document.f1.submit();
        }
    }

    function kt() {

        {
            document.f1.action = "capphatgiaotrinh.php";
            document.f1.submit();
        }
    }
    function chuyenlop() {

        document.f1.action = "capnhatcapphatgt.php";
        document.f1.submit();
    }
</script>
<?php 
$tcok = @$_GET["tc"];
if ($tcok == "10") { ?>
    <script language="javascript">
        alert("Cap nhat thanh cong !");
    </script>
<?php } ?>