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

// Kiểm tra quyền truy cập
if (@$_SESSION["tendn"] <> "quantri") {
    echo "No accessright";
    exit();
}

require("Connection.php");

$nhanvien = @$_POST["nhanvien"];

// Query chính để lấy danh sách trường
if ($nhanvien <> "")
    $sql = "SELECT * from truongmn where nhanvien= '" . $nhanvien . "' order by diachi";
else
    $sql = "SELECT * from truongmn order by diachi";

?>
<!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">QUẢN LÝ TRƯỜNG - NHÂN VIÊN</Span></div>
    <form name="f1" method="POST">
        <div align="center">
            <table align="center" width="43%" border="0" cellpadding="0" cellspacing="0" class="khungbao1">
                <tr>
                    <td width="21%" height="33" class="style5"><div align="right">Nhân viên : </div></td>
                    <td width="40%">
                        <select name="nhanvien" class="box1" id="nhanvien">
                            <option value="">Tất cả</option>
                            <?php
                            // --- QUERY 1: Dropdown tìm kiếm (Top) ---
                            $sqlnv = "select * from nhanvien order by tennv";
                            $resultnv = mysqli_query($link, $sqlnv);
                            
                            if (mysqli_num_rows($resultnv) > 0) {
                                while ($rownv = mysqli_fetch_array($resultnv)) {
                                    $tennv = $rownv["tennv"];
                            ?>
                                    <option value="<?php echo $tennv; ?>" <?php if ($tennv == $nhanvien) echo " selected"; ?>>    <?php echo $tennv; ?>        </option>
                            <?php
                                }
                            }
                            ?>
                        </select>
                        <input name="cmd1" type="button" value="Tìm kiếm" onclick="kt()"/>
                    </td>
                </tr>
            </table>
        </div>

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

        <p align="center">&nbsp;</p>
        <table width="63%" align="center" cellspacing="0" class="khungbao1">
            <tr>
                <td width="26%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2"> Tên trường</font></p></td>
                <td width="18%" align="center" class="dkta1"><font color="#800000" face="Arial" size="2">GT đề xuất</font></td>
                <td width="15%" align="center" class="dkta1"><font color="#800000" face="Arial" size="2">Số lớp MN </font></td>
                <td width="16%" align="center" class="dkta1"><font color="#800000" face="Arial" size="2">Số lớp NT </font></td>
                <td width="10%" align="center" class="dkta1"><font color="#800000" face="Arial" size="2">Tổng Số lớp</font></td>
                <td width="15%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Nhân viên phụ trách</font></p></td>
            </tr>

            <?php
            // --- QUERY 2: Main List ---
            $result = mysqli_query($link, $sql);
            $tongrec = mysqli_num_rows($result);
            
            if ($tongrec > 0) {
                while ($row = mysqli_fetch_array($result)) {
                    // FIX LỖI: Code cũ dùng $row1["malopmn"] là sai, ở đây đang loop biến $row
                    // Và bảng TRUONGMN thường dùng matruongmn, không phải malopmn.
                    // Tớ sửa lại lấy matruongmn cho an toàn.
                    $matruongmn = $row["matruongmn"]; 
            ?>
                    <tr>
                        <td align="center" class="dkta1"><div align="left"><font face="arial" size="2"><?php echo $row["diachi"]; ?></font>&nbsp;</div></td>

                        <td align="center" class="dkta1"><strong>
                                <select name="hopdong<?php echo $row["matruongmn"]; ?>" class="box1">
                                    <option value="" <?php if ($row["hopdong"] == "") echo " selected"; ?>> <?php echo ""; ?> </option>
                                    <option value="Theo Lớp" <?php if ($row["hopdong"] == "Theo Lớp") echo " selected"; ?>> <?php echo "Theo Lớp"; ?> </option>
                                    <option value="Theo Trẻ" <?php if ($row["hopdong"] == "Theo Trẻ") echo " selected"; ?>> <?php echo "Theo Trẻ"; ?> </option>
                                </select>
                            </strong></td>
                        <td align="center" class="dkta1"><input name="solopmn<?php echo $row["matruongmn"]; ?>" type="text" value="<?php echo $row["solopmn"]; ?>" size="5" /></td>
                        <td align="center" class="dkta1"><input name="solopnt<?php echo $row["matruongmn"]; ?>" type="text" value="<?php echo $row["solopnt"]; ?>" size="5" /></td>
                        <td align="center" class="dkta1"><input name="tongsolop<?php echo $row["matruongmn"]; ?>" type="text" value="<?php echo $row["tongsolop"]; ?>" size="5" /></td>
                        <td align="center" class="dkta1">
                            <div align="center"><strong>
                                    <select name="nvpt<?php echo $row["matruongmn"]; ?>" class="box1">
                                        <option value="" <?php if ($row["nhanvien"] == "") echo " selected"; ?>>    <?php echo ""; ?>        </option>
                                        <?php
                                        // --- QUERY 3: Nested Dropdown (Nhân viên cho từng dòng) ---
                                        // Lưu ý: Query trong vòng lặp (N+1) làm chậm web, nhưng để giữ nguyên logic cũ của bạn chạy được, tớ vẫn giữ nguyên cấu trúc này.
                                        $sqlnv_sub = "select * from nhanvien order by tennv";
                                        $resultnv_sub = mysqli_query($link, $sqlnv_sub);
                                        
                                        if (mysqli_num_rows($resultnv_sub) > 0) {
                                            while ($rownv_sub = mysqli_fetch_array($resultnv_sub)) {
                                                $tennv_sub = $rownv_sub["tennv"];
                                        ?>
                                                <option value="<?php echo $tennv_sub; ?>" <?php if ($row["nhanvien"] == $tennv_sub) echo " selected"; ?>>    <?php echo $tennv_sub; ?>        </option>
                                        <?php
                                            }
                                        }
                                        ?>
                                    </select>
                                    &nbsp;</strong></div>
                        </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 kt() {
        document.f1.action = "nhanvientruong.php";
        document.f1.submit();
    }

    function chuyenlop() {
        document.f1.action = "capnhatnhanvientruong.php";
        document.f1.submit();
    }
</script>

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