<?php
session_start();
require_once '../config/database.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit();
}

$success_message = '';
$error_message = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add_seat']) || isset($_POST['edit_seat'])) {
        $exam_id = $_POST['exam_id'];
        $subject_id = $_POST['subject_id'];
        $student_id = $_POST['student_id'];
        $room_number = $_POST['room_number'];
        $seat_number = $_POST['seat_number'];
        $row_number = $_POST['row_number'];
        $column_number = $_POST['column_number'];
        $exam_date = $_POST['exam_date'];

        if (isset($_POST['edit_seat'])) {
            $id = $_POST['id'];
            $stmt = $pdo->prepare("UPDATE seat_arrangements SET exam_id=?, subject_id=?, student_id=?, room_number=?, seat_number=?, row_number=?, column_number=?, exam_date=? WHERE id=?");
            $result = $stmt->execute([$exam_id, $subject_id, $student_id, $room_number, $seat_number, $row_number, $column_number, $exam_date, $id]);
            if ($result) {
                $success_message = "সীট সফলভাবে আপডেট করা হয়েছে!";
            } else {
                $error_message = "ত্রুটি: সীট আপডেট করা যায়নি।";
            }
        } else {
            $stmt = $pdo->prepare("INSERT INTO seat_arrangements (exam_id, subject_id, student_id, room_number, seat_number, row_number, column_number, exam_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
            $result = $stmt->execute([$exam_id, $subject_id, $student_id, $room_number, $seat_number, $row_number, $column_number, $exam_date]);
            if ($result) {
                $success_message = "সীট সফলভাবে যোগ করা হয়েছে!";
            } else {
                $error_message = "ত্রুটি: সীট যোগ করা যায়নি।";
            }
        }
    } elseif (isset($_POST['delete_seat'])) {
        $id = $_POST['id'];
        $stmt = $pdo->prepare("DELETE FROM seat_arrangements WHERE id = ?");
        $result = $stmt->execute([$id]);
        if ($result) {
            $success_message = "সীট সফলভাবে মুছে ফেলা হয়েছে!";
        } else {
            $error_message = "ত্রুটি: সীট মুছে ফেলা যায়নি।";
        }
    }
}

// Handle edit request
$edit_seat = null;
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $stmt = $pdo->prepare("SELECT * FROM seat_arrangements WHERE id = ?");
    $stmt->execute([$id]);
    $edit_seat = $stmt->fetch();
}

// Handle delete confirmation
$delete_id = null;
if (isset($_GET['delete'])) {
    $delete_id = $_GET['delete'];
}

// Get all seat arrangements
$stmt = $pdo->query("
    SELECT sa.*, e.name as exam_name, s.name as subject_name, st.first_name, st.last_name, st.roll_number
    FROM seat_arrangements sa
    LEFT JOIN exams e ON sa.exam_id = e.id
    LEFT JOIN subjects s ON sa.subject_id = s.id
    LEFT JOIN students st ON sa.student_id = st.id
    ORDER BY sa.exam_date, sa.room_number, sa.row_number, sa.column_number
");
$seat_arrangements = $stmt->fetchAll();

// Get all exams for dropdown
$exams = $pdo->query("SELECT id, name FROM exams ORDER BY name")->fetchAll();

// Get all subjects for dropdown
$subjects = $pdo->query("SELECT id, name FROM subjects ORDER BY name")->fetchAll();

// Get all students for dropdown
$students = $pdo->query("SELECT id, first_name, last_name, roll_number FROM students ORDER BY roll_number")->fetchAll();
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>সীট ব্যবস্থাপনা - স্কুল ম্যানেজমেন্ট সিস্টেম</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Hind Siliguri', sans-serif;
            background-color: #f8f9fa;
        }
        .bengali-text {
            font-family: 'Hind Siliguri', sans-serif;
        }
    </style>
</head>
<body class="bengali-text">
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="../index.php">
                <i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম
            </a>
            
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="../index.php">
                    <i class="fas fa-home"></i> হোম
                </a>
                <a class="nav-link" href="routine.php">
                    <i class="fas fa-calendar"></i> পরীক্ষার রুটিন
                </a>
                <a class="nav-link" href="seat_card.php">
                    <i class="fas fa-chair"></i> সীট কার্ড
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5><i class="fas fa-chair"></i> সীট ব্যবস্থাপনা</h5>
                        <a href="seat_arrangement_generator.php" class="btn btn-sm btn-warning">
                            <i class="fas fa-magic"></i> রোল ভিত্তিক সীট জেনারেটর
                        </a>
                    </div>
                    <div class="card-body">
                        <?php if ($success_message): ?>
                            <div class="alert alert-success alert-dismissible fade show">
                                <i class="fas fa-check-circle"></i> <?php echo $success_message; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>

                        <?php if ($error_message): ?>
                            <div class="alert alert-danger alert-dismissible fade show">
                                <i class="fas fa-exclamation-triangle"></i> <?php echo $error_message; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>

                        <!-- Add/Edit Form -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h6>
                                    <i class="fas fa-plus-circle"></i> 
                                    <?php echo $edit_seat ? 'সীট আপডেট করুন' : 'নতুন সীট যোগ করুন'; ?>
                                </h6>
                            </div>
                            <div class="card-body">
                                <form method="POST" action="">
                                    <?php if ($edit_seat): ?>
                                        <input type="hidden" name="id" value="<?php echo $edit_seat['id']; ?>">
                                        <input type="hidden" name="edit_seat" value="1">
                                    <?php else: ?>
                                        <input type="hidden" name="add_seat" value="1">
                                    <?php endif; ?>
                                    
                                    <div class="row">
                                        <div class="col-md-4">
                                            <label for="exam_id" class="form-label">পরীক্ষা</label>
                                            <select class="form-select" id="exam_id" name="exam_id" required>
                                                <option value="">পরীক্ষা নির্বাচন করুন</option>
                                                <?php foreach ($exams as $exam): ?>
                                                    <option value="<?php echo $exam['id']; ?>" 
                                                        <?php echo (isset($edit_seat) && $edit_seat['exam_id'] == $exam['id']) ? 'selected' : ''; ?>>
                                                        <?php echo $exam['name']; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        
                                        <div class="col-md-4">
                                            <label for="subject_id" class="form-label">বিষয়</label>
                                            <select class="form-select" id="subject_id" name="subject_id" required>
                                                <option value="">বিষয় নির্বাচন করুন</option>
                                                <?php foreach ($subjects as $subject): ?>
                                                    <option value="<?php echo $subject['id']; ?>" 
                                                        <?php echo (isset($edit_seat) && $edit_seat['subject_id'] == $subject['id']) ? 'selected' : ''; ?>>
                                                        <?php echo $subject['name']; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        
                                        <div class="col-md-4">
                                            <label for="student_id" class="form-label">ছাত্র/ছাত্রী</label>
                                            <select class="form-select" id="student_id" name="student_id" required>
                                                <option value="">ছাত্র/ছাত্রী নির্বাচন করুন</option>
                                                <?php foreach ($students as $student): ?>
                                                    <option value="<?php echo $student['id']; ?>" 
                                                        <?php echo (isset($edit_seat) && $edit_seat['student_id'] == $student['id']) ? 'selected' : ''; ?>>
                                                        <?php echo $student['first_name'] . ' ' . $student['last_name'] . ' (রোল: ' . $student['roll_number'] . ')'; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    <div class="row mt-3">
                                        <div class="col-md-3">
                                            <label for="room_number" class="form-label">রুম নম্বর</label>
                                            <input type="text" class="form-control" id="room_number" name="room_number" 
                                                value="<?php echo isset($edit_seat) ? $edit_seat['room_number'] : ''; ?>" required>
                                        </div>
                                        
                                        <div class="col-md-3">
                                            <label for="seat_number" class="form-label">সীট নম্বর</label>
                                            <input type="text" class="form-control" id="seat_number" name="seat_number" 
                                                value="<?php echo isset($edit_seat) ? $edit_seat['seat_number'] : ''; ?>" required>
                                        </div>
                                        
                                        <div class="col-md-3">
                                            <label for="row_number" class="form-label">সারি নম্বর</label>
                                            <input type="number" class="form-control" id="row_number" name="row_number" 
                                                value="<?php echo isset($edit_seat) ? $edit_seat['row_number'] : ''; ?>" min="1" required>
                                        </div>
                                        
                                        <div class="col-md-3">
                                            <label for="column_number" class="form-label">কলাম নম্বর</label>
                                            <input type="number" class="form-control" id="column_number" name="column_number" 
                                                value="<?php echo isset($edit_seat) ? $edit_seat['column_number'] : ''; ?>" min="1" required>
                                        </div>
                                    </div>
                                    
                                    <div class="row mt-3">
                                        <div class="col-md-6">
                                            <label for="exam_date" class="form-label">পরীক্ষার তারিখ</label>
                                            <input type="date" class="form-control" id="exam_date" name="exam_date" 
                                                value="<?php echo isset($edit_seat) ? $edit_seat['exam_date'] : ''; ?>" required>
                                        </div>
                                    </div>
                                    
                                    <div class="mt-3">
                                        <button type="submit" class="btn btn-success">
                                            <i class="fas fa-save"></i> 
                                            <?php echo $edit_seat ? 'আপডেট করুন' : 'যোগ করুন'; ?>
                                        </button>
                                        <a href="seat_arrangements.php" class="btn btn-secondary">
                                            <i class="fas fa-times"></i> বাতিল করুন
                                        </a>
                                    </div>
                                </form>
                            </div>
                        </div>

                        <!-- Confirm Delete Modal -->
                        <?php if ($delete_id): ?>
                        <div class="modal fade show d-block" tabindex="-1" style="background-color: rgba(0,0,0,0.5);">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title">নিশ্চিত করুন</h5>
                                    </div>
                                    <div class="modal-body">
                                        <p>আপনি কি নিশ্চিত যে আপনি এই সীটটি মুছে ফেলতে চান?</p>
                                    </div>
                                    <div class="modal-footer">
                                        <form method="POST" action="" class="d-inline">
                                            <input type="hidden" name="delete_seat" value="1">
                                            <input type="hidden" name="id" value="<?php echo $delete_id; ?>">
                                            <button type="submit" class="btn btn-danger">হ্যাঁ, মুছে ফেলুন</button>
                                        </form>
                                        <a href="seat_arrangements.php" class="btn btn-secondary">বাতিল করুন</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>

                        <!-- Seat Arrangements Table -->
                        <div class="card">
                            <div class="card-header">
                                <h6><i class="fas fa-list"></i> সীট তালিকা</h6>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered">
                                        <thead class="table-dark">
                                            <tr>
                                                <th>#</th>
                                                <th>পরীক্ষা</th>
                                                <th>বিষয়</th>
                                                <th>ছাত্র/ছাত্রী</th>
                                                <th>রুম</th>
                                                <th>সীট</th>
                                                <th>সারি</th>
                                                <th>কলাম</th>
                                                <th>তারিখ</th>
                                                <th>কার্যক্রম</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($seat_arrangements as $index => $seat): ?>
                                                <tr>
                                                    <td><?php echo $index + 1; ?></td>
                                                    <td><?php echo htmlspecialchars($seat['exam_name']); ?></td>
                                                    <td><?php echo htmlspecialchars($seat['subject_name']); ?></td>
                                                    <td><?php echo htmlspecialchars($seat['first_name'] . ' ' . $seat['last_name'] . ' (রোল: ' . $seat['roll_number'] . ')'); ?></td>
                                                    <td><?php echo htmlspecialchars($seat['room_number']); ?></td>
                                                    <td><?php echo htmlspecialchars($seat['seat_number']); ?></td>
                                                    <td><?php echo $seat['row_number']; ?></td>
                                                    <td><?php echo $seat['column_number']; ?></td>
                                                    <td><?php echo date('d/m/Y', strtotime($seat['exam_date'])); ?></td>
                                                    <td>
                                                        <a href="?edit=<?php echo $seat['id']; ?>" class="btn btn-sm btn-primary">
                                                            <i class="fas fa-edit"></i> সম্পাদনা
                                                        </a>
                                                        <a href="?delete=<?php echo $seat['id']; ?>" class="btn btn-sm btn-danger">
                                                            <i class="fas fa-trash"></i> মুছুন
                                                        </a>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                            
                                            <?php if (empty($seat_arrangements)): ?>
                                                <tr>
                                                    <td colspan="10" class="text-center">কোন সীট পাওয়া যায়নি</td>
                                                </tr>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>