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

// Kiểm tra quyền quản trị
// Dùng isset để tránh lỗi PHP 8
if (!isset($_SESSION["tendn"]) || $_SESSION["tendn"] <> "quantri") {
    echo "No accessright";
    exit();
}

require("Connection.php");

// 1. Lấy dữ liệu từ POST an toàn
$matruong = isset($_POST["matruong"]) ? $_POST["matruong"] : "";
$tentruong = isset($_POST["tentruong"]) ? $_POST["tentruong"] : "";
$tendn = isset($_POST["tendn"]) ? $_POST["tendn"] : "";
$matkhau = isset($_POST["matkhau"]) ? $_POST["matkhau"] : "";
$nvphutrach = isset($_POST["nvphutrach"]) ? $_POST["nvphutrach"] : "";

// 2. Xử lý Thêm mới
if ($matruong <> "") {
    // Escape dữ liệu để tránh lỗi SQL khi có ký tự đặc biệt
    $matruong_safe = mysqli_real_escape_string($link, $matruong);
    $tentruong_safe = mysqli_real_escape_string($link, $tentruong);
    $tendn_safe = mysqli_real_escape_string($link, $tendn);
    $matkhau_safe = mysqli_real_escape_string($link, $matkhau);
    $nvphutrach_safe = mysqli_real_escape_string($link, $nvphutrach);

    $sql2 = "insert into TRUONGROBOT(matruongmn,diachi,tendn,matkhau,nvphutrach)";
    $sql2 .= " values('" . $matruong_safe . "','" . $tentruong_safe . "','" . $tendn_safe . "','" . $matkhau_safe . "','" . $nvphutrach_safe . "')";

    // FIX: mysqli + đảo tham số
    $result2 = mysqli_query($link, $sql2);
}
?>
<!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">
        <center>
            <p><font face="Arial" color="black" size="4"><strong><font color="#009933">THÊM TRƯỜNG MỚI MUA ROBOT</font></strong><br />
                </font></p>
        </center>
        <table width="560" height="91" border="0" align="center" cellpadding="5" cellspacing="0" style="border-color:#339999; border-style:solid; border-width:1px">
            <tr valign="top">
                <td width="144">
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Mã trường:</strong></font></div>
                </td>
                <td width="394"><input type="text" maxlength="30" size="33" name="matruong" /></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Tên trường:</strong></font></div>
                </td>
                <td><input type="text" maxlength="50" size="33" name="tentruong" /></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Tên đăng nhập:</strong></font></div>
                </td>
                <td><input type="text" maxlength="50" size="33" name="tendn" /></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Mật khẩu</strong></font></div>
                </td>
                <td><input type="text" maxlength="50" size="33" name="matkhau" /></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Nhân viên phụ trách</strong></font></div>
                </td>
                <td><select name="nvphutrach">
                        <option value="Bảo">Bảo</option>
                        <option value="Cường">Cường</option>
                        <option value="Tuấn">Tuấn</option>
                        <option value="Yên">Yên</option>
                        <option value="Tùng">Tùng</option>
                    </select> </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div align="center">
                        <input type="button" value="Thêm mới" name="cmd1" onclick="kt()" />
                        <input name="cmd12" type="button" value="Trở về" onclick="javascript:window.location.href='capphatbando.php';" />
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <marquee align="center" direction="left" scrollamount="10" width="100%">
                    </marquee>
                </td>
            </tr>
        </table>
    </form>
    <p>&nbsp;</p>
    <table width="100%" cellspacing="0" class="khungbao1">
        <tr>
            <td width="15%" align="center" class="dkta1">
                <p align="center"><font color="#800000" face="Arial" size="2">Mã trường</font></p>
            </td>
            <td width="34%" 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">
                <p align="center"><font color="#800000" face="Arial" size="2">Tên Đăng Nhập</font></p>
            </td>
            <td width="15%" align="center" class="dkta1">
                <p align="center"><font color="#800000" face="Arial" size="2">Mật khẩu</font></p>
            </td>
            <td width="18%" align="center" class="dkta1">
                <p align="center"><font color="#800000" size="2" face="Arial">Nhân viên phụ trách</font></p>
            </td>
        </tr>
        <?php
        $sql = "SELECT * FROM TRUONGROBOT";
        // FIX: mysqli
        $result = mysqli_query($link, $sql);
        
        if ($result) {
            $tongrec = mysqli_num_rows($result);
            $i = 0;
            if ($tongrec > 0) {
                while ($row = mysqli_fetch_array($result)) {
                    $i++;
                    // Đoạn code cũ lấy $row1["malopmn"] ở đây là sai (copy từ file khác), 
                    // Tớ đã xóa đi vì không dùng đến trong bảng này.
        ?>
                    <tr>
                        <td align="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["matruongmn"]; ?></font>&nbsp;</div>
                        </td>
                        <td align="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["diachi"]; ?></font>&nbsp;</div>
                        </td>
                        <td align="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["tendn"]; ?></font>&nbsp;</div>
                        </td>
                        <td align="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["matkhau"]; ?></font>&nbsp;</div>
                        </td>
                        <td align="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["nvphutrach"]; ?></font>&nbsp;</div>
                        </td>
                    </tr>
        <?php
                }
            }
        }
        ?>
    </table>
    <p>&nbsp;</p>
</body>
<script language="javascript">
    function kt() {
        {
            document.f1.action = "themtruongmoi.php";
            document.f1.submit();
        }
    }
</script>