<?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 từ Form
$matruong = isset($_POST["truongmn"]) ? $_POST["truongmn"] : "";
$tenrobot = isset($_POST["tenrobot"]) ? $_POST["tenrobot"] : "";
$soluong = isset($_POST["soluong"]) ? $_POST["soluong"] : "";
$ngaymua = isset($_POST["ngaymua"]) ? $_POST["ngaymua"] : "";

// 2. Xử lý Thêm mới
if ($matruong <> "") {
    // Escape dữ liệu an toàn
    $matruong_safe = mysqli_real_escape_string($link, $matruong);
    $tenrobot_safe = mysqli_real_escape_string($link, $tenrobot);
    $ngaymua_safe = mysqli_real_escape_string($link, $ngaymua);
    $soluong_safe = (int)$soluong; // Ép kiểu số

    $sql2 = "insert into CHITIETROBOT(matruongmn,tenrobot,soluong,ngaymua)";
    $sql2 .= " values('" . $matruong_safe . "','" . $tenrobot_safe . "'," . $soluong_safe . ",'" . $ngaymua_safe . "')";
    
    // FIX: mysqli + đảo tham số
    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">XUẤT ROBOT CHO CÁC TRƯỜNG</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"><strong><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif">Tên trường</font></strong></div>
                </td>
                <td width="394"><select name="truongmn" class="box1">
                        <option value="">Tất cả</option>
                        <?php
                        $sql1 = "select * FROM TRUONGROBOT  order by diachi";
                        $result1 = mysqli_query($link, $sql1);
                        
                        if ($result1 && 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 == $matruong) echo " selected"; ?>> <?php echo $tentruongmn; ?></option>
                        <?php
                            }
                        }
                        ?>
                    </select></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Tên Robot:</strong></font></div>
                </td>
                <td><select name="tenrobot" id="tenrobot">
                        <option value="GAKEEN">GAKEEN</option>
                        <option value="TEDDY">TEDDY</option>

                    </select></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif"><strong>Số lượng</strong></font></div>
                </td>
                <td><input type="text" maxlength="50" size="10" name="soluong" id="soluong" /></td>
            </tr>
            <tr valign="top">
                <td>
                    <div align="right"><strong><font color="#0033CC" size="2" face="Arial, Helvetica, sans-serif">Ngày mua</font></strong></div>
                </td>
                <td>
                    <?php
                    // Lấy ngày hiện tại chuẩn format Y-m-d
                    $ngayhh = date("Y-m-d");
                    ?>
                    <input name="ngaymua" type="text" id="ngaymua" value="<?php echo $ngayhh; ?>" size="20" maxlength="50" />
                </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ã robot</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">Loại Robot</font></p>
            </td>
            <td width="15%" align="center" class="dkta1">
                <p align="center"><font color="#800000" face="Arial" size="2">Số lượng</font></p>
            </td>
            <td width="18%" align="center" class="dkta1">
                <p align="center"><font color="#800000" size="2" face="Arial">Ngày mua</font></p>
            </td>
        </tr>
        <?php
        $sql = "select a.* , b.diachi from CHITIETROBOT a, TRUONGROBOT b where a.matruongmn=b.matruongmn order by ngaymua desc";
        $result = mysqli_query($link, $sql);
        
        if ($result) {
            $tongrec = mysqli_num_rows($result);
            $i = 0;
            if ($tongrec > 0) {
                while ($row = mysqli_fetch_array($result)) {
                    $i++;
                    // Đã xóa dòng $malopmn thừa thãi ở đây
        ?>
                    <tr>
                        <td align="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["marobot"]; ?></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["tenrobot"]; ?></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="center" class="dkta1">
                            <div align="center"><font face="arial" size="2"><?php echo $row["ngaymua"]; ?></font>&nbsp;</div>
                        </td>
                    </tr>
        <?php
                }
            }
        }
        ?>
    </table>
    <p>&nbsp;</p>
</body>
<script language="javascript">
    function kt() {
        {
            document.f1.action = "themrobot.php";
            document.f1.submit();
        }
    }
</script>