<?php
require_once('dbconnection.php');
session_start();
if (!$_SESSION['emp_email'] || $_SESSION['emp_type'] != 'admin') {  
    header("Location: login.php");  // Redirect to login page if the user is not logged in or not an admin
    exit();  // It's a good practice to add exit after header to ensure the script stops executing
}
$student_id = $_GET['student_id'];
$cid = $_GET['cid'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>گزارش رسیدات محصلین</title>
    <link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <script src="extra_js/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="extra_js/jquery-ui.css">
    <script src="extra_js/jquery-ui.js"></script>
     <script>
  document.addEventListener("keydown", function(event) {
    if (event.key === "Escape") {
      // Use a short delay to ensure smooth behavior
      setTimeout(() => {
        window.history.back();
      }, 100);
    }
  });
</script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
        }
        header {
            background-color: #fff; /* White background */
            color: #333; /* Dark text */
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid #ccc; /* Add bottom border for better separation */
        }
        .container {
            width: 80%;
            margin: 30px auto;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        h2 {
            text-align: center;
            color: #333;
        }
        .form-group {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
        }
        .form-group label {
            font-weight: bold;
            margin-right: 10px;
            width: 20%;
        }
        .form-group input {
            width: 75%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        #exportPDF {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            transition: background-color 0.3s;
        }
        #exportPDF:hover {
            background-color: #45a049;
        }
        #reportResult {
            margin-top: 20px;
            text-align: center;
        }
        .footer {
            text-align: center;
            font-size: 14px;
            color: #888;
            margin-top: 30px;
        }

        /* Custom styling for the date picker */
        .ui-datepicker {
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .ui-datepicker-header {
            background-color: #4CAF50;
            color: white;
            text-align: center;
            padding: 10px;
        }
        .ui-datepicker .ui-datepicker-prev, 
        .ui-datepicker .ui-datepicker-next {
            color: #4CAF50;
        }
        .ui-datepicker .ui-datepicker-prev:hover,
        .ui-datepicker .ui-datepicker-next:hover {
            background-color: #e0e0e0;
            cursor: pointer;
        }
        .ui-datepicker td a {
            color: #333;
        }
        .ui-datepicker td a:hover {
            background-color: #4CAF50;
            color: white;
        }
    </style>
</head>
<body>

    <header>
    <img src="images/logo1.png" alt="" height="70" width="70">
        <h1>گزارش محصلین</h1>
    </header>

    <div class="container" dir="rtl">
        <h2>اتنخاب تاریخ</h2>
        <form id="reportForm">
            <div class="form-group">
                <label for="from_date">از تاریخ</label>
                <input type="text" id="from_date" name="from_date" autocomplete="off" required>
            </div>
            <div class="form-group">
                <label for="to_date">به تاریخ</label>
                <input type="text" id="to_date" name="to_date" autocomplete="off" required>
            </div>

        <input type="hidden" id="student_id" value="<?php echo $student_id ?>" >
        <input type="hidden" id="c_id" value="<?php echo $cid ?>" >
            <button type="button" id="exportPDF">تبدیل به پدیف</button>
        </form>

        <!-- Home page link -->
        <div style="text-align: center; margin-top: 20px;">
            <a href="index.php" style="
                display: inline-block;
                padding: 10px 20px;
                background-color: #2196F3;
                color: white;
                text-decoration: none;
                border-radius: 5px;
                transition: background-color 0.3s;">
                بازگشت به صفحه اصلی
            </a>
        </div>

        <div id="reportResult"></div>
    </div>

    <footer class="footer">
        <p>Copyright © Designed &amp; Developed by <a href="https://dynamic-cryptic.com/" target="_blank">Dynamic & Cryptic</a> 2025</p>
    </footer>

    <script>
    $(document).ready(function(){
        // Activate jQuery UI Datepicker
        $("#from_date, #to_date").datepicker({
            dateFormat: "yy-mm-dd",
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            showOtherMonths: true,
            selectOtherMonths: true
        });

        $("#exportPDF").on("click", function(){
            let fromDate    = $("#from_date").val();
            let toDate      = $("#to_date").val();
            let studentId   = $("#student_id").val();  // Ensure you have a <select> or <input id="student_id">
            let courseId    = $("#c_id").val();        // Ensure you have a <select> or <input id="c_id">

            if(fromDate === "" || toDate === ""){
                alert("لطفاً تاریخ شروع و ختم را انتخاب کنید.");
                return false;
            }

            let url = "export_loan.php?type=pdf"
                    + "&from_date=" + encodeURIComponent(fromDate)
                    + "&to_date="   + encodeURIComponent(toDate)
                    + "&student_id=" + encodeURIComponent(studentId)
                    + "&c_id="       + encodeURIComponent(courseId);

            window.location.href = url;
        });
    });
</script>


</body>
</html>
