<?php
session_start();

date_default_timezone_set('Asia/Ho_Chi_Minh');

require("Connection.php");

// 1. Khởi tạo biến an toàn cho PHP 8
$kieu = isset($_GET["kieu"]) ? $_GET["kieu"] : "";
$madxgt = isset($_GET["madx"]) ? $_GET["madx"] : "";
$makl = isset($_POST["khoilop"]) ? $_POST["khoilop"] : "";
$malh = isset($_POST["malh"]) ? $_POST["malh"] : ""; 

// 2. Logic XÓA (Delete)
if ($kieu == "D" && $madxgt != "") {
    // Ép kiểu int để tránh SQL Injection
    $sql = "delete from dexuatgiaotrinh where madx=" . (int)$madxgt;
    mysqli_query($link, $sql);
}
?>
<!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>
    <form name="f1" method="POST">
        <div align="center">
            <span class="style7">ĐỀ XUẤT GIÁO TRÌNH<br />
            <?php
            // Kiểm tra isset trước khi echo để tránh Warning trên PHP 8
            $s_matruong = isset($_SESSION["matruongmn"]) ? $_SESSION["matruongmn"] : "";
            $s_tendn = isset($_SESSION["tendn"]) ? $_SESSION["tendn"] : "";
            
            $hotengv = "";            
            
            // Nếu session có thì lấy, không thì lấy chuỗi rỗng
            $matruongmn_safe = mysqli_real_escape_string($link, $s_matruong);

            $sql1 = "select tentruongmn from truongmn where matruongmn='" . $matruongmn_safe . "'";
            
            $result1 = mysqli_query($link, $sql1);

            if (!$result1) {
                // Tạm comment dòng die lại để trang không bị chết nếu lỗi query, thay vào đó hiện lỗi nhẹ nhàng
                echo "<br><font color=red>Lỗi kết nối CSDL lấy tên trường</font>";
            } else {
                if (mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_array($result1)) {
                        $hotengv = $row1["tentruongmn"];
                    }
                }
            }
            echo "<font face='Arial'>$hotengv</font>";
            ?>
            </span>
        </div>

        <br />
        <div align="center">Chọn lớp :
            <select name="khoilop" class="box1" onchange="chuyenlop()">
                <option value="">Tất cả</option>
                <?php
                $sql1 = "select distinct(tenlopmn) from lopmn where matruongmn='" . $matruongmn_safe . "'";
                $result1 = mysqli_query($link, $sql1);

                if ($result1 && mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_array($result1)) {
                        $makhoi = $row1["tenlopmn"];
                ?>
                        <option value="<?php echo $makhoi; ?>" <?php if ($makhoi == $makl) echo " selected" ?>>    <?php echo $makhoi; ?>        </option>
                <?php
                    }
                }
                ?>
            </select>

            <br /><br />

            <?php
            $chuoitim = "";
            if ($makl <> "") {
                $makl_safe = mysqli_real_escape_string($link, $makl);
                $chuoitim = $chuoitim . " tenlopmn like N'%" . $makl_safe . "%' and ";
            }

            if ($chuoitim <> "")
                $sql3 = "SELECT * FROM lopmn where matruongmn='" . $matruongmn_safe . "' and " . $chuoitim . " 1=1 order by tenlopmn ";
            else
                $sql3 = "SELECT * FROM lopmn where matruongmn='" . $matruongmn_safe . "'  order by tenlopmn";

            $result3 = mysqli_query($link, $sql3);
            
            // Fix lỗi nếu query rỗng
            $tongrec3 = ($result3) ? mysqli_num_rows($result3) : 0;
            ?>
        </div>

        <div align="center">
            <STRONG><font face="Arial" size="4" color=blue >VUI LÒNG ĐỀ XUẤT KHÔNG QUÁ 8 MÃ BÀI CHO 1 THÁNG!</font></STRONG><br>
        </div>

        <table width="100%" align="center" cellspacing="0" class="khungbao1">
            <tr>
                <td width="9%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Lớp</font></p></td>
                <td width="10%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Số lượng bé</font></p></td>
                <td width="26%" align="center" class="dkta1"><p align="center"><font color="#800000" size="2" face="Arial">Người đề xuất</font></p></td>
                <td width="39%" align="center" class="dkta1"><p align="center"><font color="#800000" size="2" face="Arial">Giáo trình đề xuất</font></p></td>
            </tr>
            <?php
            $i = 0;
            if ($tongrec3 > 0) {
                while ($row3 = mysqli_fetch_array($result3)) {
                    $i++;
                    $currentDate = date("Y-m-d H:i:s");
            ?>
                    <tr>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" color="#0000CC" size="3"><?php echo $row3["tenlopmn"]; ?></font>&nbsp;</div></td>
                        
                        <td align="center" class="dkta1"><div align="center">&nbsp;
                            <input name="soluongbe<?php echo $row3["malopmn"]; ?>" type="text" class="box1" size="7" maxlength="10"  onkeypress="checknum()" />
                        </div></td>

                        <input name="ngaydx<?php echo $row3["malopmn"] ?>" type="hidden" class="box1"  value="<?php echo $currentDate; ?>" size="15" maxlength="10" />

                        <td align="left" class="dkta1"><div align="center">&nbsp;
                            <input name="nguoidx<?php echo $row3["malopmn"]; ?>" type="text" class="box1"  size="33" />
                        </div></td>

                        <td align="center" class="dkta1" ><div align="center">
                            <input name="<?php echo $row3["malopmn"]; ?>" type="hidden" />
                            <textarea name="giaotrinhdx<?php echo $row3["malopmn"]; ?>" cols="50" rows="3" class="box2" ></textarea>
                        </div></td>
                    </tr>
            <?php
                }
            }
            ?>
            <input name="mtmn" type="hidden" value="<?php echo $s_matruong; ?>" />
        </table>

        <p align="center">
            <br />
            <input type="button" value="Cập nhật" name="cmd1" onclick="kt()"/>
            &nbsp;&nbsp;
            <input name="cmd12" type="button" value="Thoát" onclick="javascript:window.location.href = 'logout.php';" />
            <input type="hidden" value="<?php echo $malh; ?>" name="malh" />
        </p>

        <p align="center">&nbsp;</p>
        <p align="center" class="style7"><a name="xemgt" id="xemgt"></a>DANH SÁCH GIÁO TRÌNH ĐÃ ĐỀ XUẤT</p>

        <table width="100%" cellspacing="0" class="khungbao1">
            <tr>
                <td width="5%" align="center" class="dkta1"><p align="center"><font color="#800000" size="2" face="Arial">XÓA</font></p></td>
                <td width="5%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">MãQL</font></p></td>
                <td width="14%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Ngày đề xuất</font></p></td>
                <td width="7%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Lớp</font></p></td>
                <td width="10%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Số lượng bé</font></p></td>
                <td width="41%" 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="10%" align="center" class="dkta1"><p align="center"><font color="#800000" size="2" face="Arial">Người đề xuất</font></p></td>
                <td width="6%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Trạng thái</font></p></td>
                <td width="7%" align="center" class="dkta1"><p align="center"><font color="#800000" face="Arial" size="2">Trường</font></p></td>
            </tr>

            <?php
            $chuoitim = "";
            if ($makl <> "") {
                $makl_safe = mysqli_real_escape_string($link, $makl);
                $chuoitim = $chuoitim . " b.tenlopmn like N'%" . $makl_safe . "%' and ";
            }

            if ($chuoitim <> "")
                $sql4 = "SELECT a.*,b.khoilop, b.tenlopmn FROM dexuatgiaotrinh a , lopmn b where " . $chuoitim . " a.malopmn=b.malopmn and a.matruongmn='" . $matruongmn_safe . "' order by a.ngaydexuat desc limit 0,200";
            else
                $sql4 = "SELECT  a.*,b.khoilop, b.tenlopmn FROM dexuatgiaotrinh a , lopmn b where a.malopmn=b.malopmn and a.matruongmn='" . $matruongmn_safe . "' order by a.ngaydexuat desc limit 0,200";

            $result4 = mysqli_query($link, $sql4);
            $tongrec4 = ($result4) ? mysqli_num_rows($result4) : 0;

            $i = 0;
            if ($tongrec4 > 0) {
                while ($row4 = mysqli_fetch_array($result4)) {
                    $i++;
            ?>
                    <tr>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><a href="#xemgt" onclick="xoadl(<?php echo $row4["madx"]; ?>, '<?php echo $row4["trangthai"]; ?>')">Delete</a></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row4["madx"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2">
                        <?php
                            $date = date_create($row4["ngaydexuat"]);
                            echo date_format($date, 'd/m/Y H:i:s');
                        ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row4["tenlopmn"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row4["soluongbe"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row4["noidungdx"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1"><div align="center"><font face="arial" size="2"><?php echo $row4["nguoidx"]; ?></font>&nbsp;</div></td>
                        <td align="center" class="dkta1" ><div align="center"><strong><font face="arial" color="#990000" size="2"><?php echo $row4["trangthai"]; ?></font>&nbsp;</strong></div></td>
                        <td align="center" class="dkta1" ><div align="center"><font face="arial" size="2"><?php echo $row4["matruongmn"]; ?></font></div></td>
                    </tr>
            <?php
                }
            }
            ?>
        </table>
        <p align="center">&nbsp;</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() {
        if (confirm("Ban co dong y de xuat cac giao trinh nay khong?")) {
            document.f1.action = "capnhatgiaotrinhdx.php";
            document.f1.submit();
        }
    }

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

    function xoadl(a, b) {
        if (b != "DX") {
            alert("Giao trinh nay da phat ban khong duoc xoa!");
            return false;
        }
        if (confirm("Ban co dong y xoa khong?")) {
            window.location.href = "dexuatgiaotrinh.php?kieu=D&madx=" + a;
        }
    }
</script>