<?php
ini_set('max_execution_time', 300); // 5 minutes

session_start();
// error_reporting(E_ALL);
// ini_set('display_errors', 1);

include('includes/config.php');

if (strlen($_SESSION['alogin']) == 0) {
    header('location:index.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Import Books | Library System</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="icon" type="image/x-icon" href="../<?php echo $_SESSION['logo']; ?>">

    <!-- Bootstrap 4.6 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- FontAwesome 6 -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

    <style>
        .square-card {
            width: 100%;
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
            border-radius: 5px;
        }

        .form-label {
            font-weight: 600;
        }

        .flash-message {
            max-width: 300px;
            margin: 0 auto 15px;
        }

        .main-content {
            margin-left: 180px;
            margin-top: 50px;
            padding: 20px;
        }

        .header-line {
            color: #2258d5;
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 20px;
        }
    </style>
</head>

<body>
    <div class="d-flex flex-column min-vh-100">
        <!-- Include Header -->
        <?php include('includes/header.php'); ?>

        <div class="main-content flex-grow-1">
            <div class="row mb-4">
                <div class="col-md-12">
                    <h5 class="header-line"><i class="fas fa-file-import mr-2"></i>Import Books</h5>
                </div>
            </div>

            <!-- Flash Messages -->
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <?php foreach (['error', 'msg'] as $type): ?>
                        <?php if (!empty($_SESSION[$type])): ?>
                            <div class="alert alert-<?php echo ($type == 'error') ? 'danger' : 'success'; ?> flash-message">
                                <strong><?php echo ucfirst($type); ?> :</strong> <?php echo htmlentities($_SESSION[$type]); ?>
                                <?php $_SESSION[$type] = ""; ?>
                            </div>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            </div>

            <!-- Import Form -->
            <div class="row justify-content-center" style="width:1000px;">
                <div class="col-md-8">
                    <div class="card square-card mb-4">
                        <div class="card-header bg-secondary text-white">
                            <i class="fa fa-upload mr-2"></i> Import Books from Excel
                        </div>
                        <div class="card-body">
                            <form action="import-books-action.php" method="POST" enctype="multipart/form-data">
                                <!-- Department -->
                                <div class="form-group">
                                    <label for="department" class="form-label">Department</label>
                                    <select name="department" id="department" class="form-control">
                                        <option value="">Select Department</option>
                                        <?php
                                        $sql = "SELECT department_id, department_name FROM tbldepartment WHERE is_deleted=0";
                                        $stmt = $dbh->query($sql);
                                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                            ?>
                                            <option value="<?php echo $row['department_id']; ?>">
                                                <?php echo $row['department_name']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>

                                <!-- Category -->
                                <div class="form-group">
                                    <label for="category" class="form-label">Category</label>
                                    <select name="category" id="category" class="form-control">
                                        <option value="">Select Category</option>
                                        <?php
                                        $sql = "SELECT id, CategoryName FROM tblcategory WHERE Status=1 AND is_deleted=0";
                                        $stmt = $dbh->query($sql);
                                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                            ?>
                                            <option value="<?php echo $row['id']; ?>">
                                                <?php echo $row['CategoryName']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>

                                <!-- Degree -->
                                <div class="form-group">
                                    <label for="degree" class="form-label">Degree</label>
                                    <select name="degree" id="degree" class="form-control">
                                        <option value="">Select Degree</option>
                                        <?php
                                        $sql = "SELECT degree_id, degree_name FROM tbldegree WHERE is_deleted=0";
                                        $stmt = $dbh->query($sql);
                                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                            ?>
                                            <option value="<?php echo $row['degree_id']; ?>">
                                                <?php echo $row['degree_name']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>

                                <!-- Shift -->
                                <div class="form-group">
                                    <label for="shift" class="form-label">Shift</label>
                                    <select name="shift" id="shift" class="form-control">
                                        <option value="">Select Shift</option>
                                        <option value="1">Morning</option>
                                        <option value="2">Evening</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="format" class="form-label">Format</label>
                                <select name="format" required>
                                    <option value="">-- Select format --</option>
                                    <option value="new">Format: A..O (New)</option>
                                    <option value="old">Format: A..L (Old)</option>
                                </select>
                                </div>
                            

                                <div class="form-group">
                                    <label for="excel_file" class="form-label">Excel File (.xlsx)</label>
                                    <input type="file" name="excel_file" id="excel_file" class="form-control"
                                        accept=".xlsx" required>
                                </div>

                                <div class="d-flex justify-content-between">
                                    <a href="manage-books.php" class="btn btn-secondary">
                                        <i class="fas fa-arrow-left"></i> Back to Books
                                    </a>
                                    <button type="submit" class="btn btn-success">
                                        <i class="fas fa-file-import"></i> Import Books
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <!-- Include Footer -->
    <?php include('includes/footer.php'); ?>

    <!-- JS -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>