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

// Check if user is logged in and has permission
if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] != 'admin' && $_SESSION['user_role'] != 'teacher')) {
    header('Location: ../login.php');
    exit();
}

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

// Handle book addition
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_book'])) {
    $title = trim($_POST['title']);
    $author = trim($_POST['author']);
    $isbn = trim($_POST['isbn']);
    $category = trim($_POST['category']);
    $publisher = trim($_POST['publisher']);
    $publication_year = !empty($_POST['publication_year']) ? (int)$_POST['publication_year'] : null;
    $total_copies = (int)$_POST['total_copies'];
    $location = trim($_POST['location']);
    
    if (empty($title) || $total_copies <= 0) {
        $error_message = 'বইয়ের নাম এবং কপি সংখ্যা প্রয়োজন';
    } else {
        try {
            // Check if ISBN already exists
            if (!empty($isbn)) {
                $stmt = $pdo->prepare("SELECT COUNT(*) FROM library_books WHERE isbn = ?");
                $stmt->execute([$isbn]);
                
                if ($stmt->fetchColumn() > 0) {
                    $error_message = 'এই ISBN নম্বরের বই ইতিমধ্যে বিদ্যমান';
                } else {
                    $stmt = $pdo->prepare("
                        INSERT INTO library_books (title, author, isbn, category, publisher, publication_year, total_copies, available_copies, location) 
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
                    ");
                    $stmt->execute([$title, $author, $isbn, $category, $publisher, $publication_year, $total_copies, $total_copies, $location]);
                    
                    $success_message = 'বই সফলভাবে যোগ করা হয়েছে';
                    $_POST = array();
                }
            } else {
                $stmt = $pdo->prepare("
                    INSERT INTO library_books (title, author, isbn, category, publisher, publication_year, total_copies, available_copies, location) 
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
                ");
                $stmt->execute([$title, $author, $isbn, $category, $publisher, $publication_year, $total_copies, $total_copies, $location]);
                
                $success_message = 'বই সফলভাবে যোগ করা হয়েছে';
                $_POST = array();
            }
        } catch (Exception $e) {
            $error_message = 'বই যোগ করতে সমস্যা হয়েছে: ' . $e->getMessage();
        }
    }
}

// Handle book issue
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['issue_book'])) {
    $book_id = (int)$_POST['book_id'];
    $student_id = (int)$_POST['student_id'];
    $issue_date = $_POST['issue_date'];
    $due_date = $_POST['due_date'];
    
    if ($book_id > 0 && $student_id > 0) {
        try {
            $pdo->beginTransaction();
            
            // Check if book is available
            $stmt = $pdo->prepare("SELECT available_copies FROM library_books WHERE id = ? AND status = 'active'");
            $stmt->execute([$book_id]);
            $available_copies = $stmt->fetchColumn();
            
            if ($available_copies <= 0) {
                throw new Exception('এই বইয়ের কোন কপি উপলব্ধ নেই');
            }
            
            // Check if student already has this book
            $stmt = $pdo->prepare("
                SELECT COUNT(*) FROM library_issues 
                WHERE book_id = ? AND student_id = ? AND status = 'issued'
            ");
            $stmt->execute([$book_id, $student_id]);
            
            if ($stmt->fetchColumn() > 0) {
                throw new Exception('এই ছাত্র/ছাত্রীর কাছে ইতিমধ্যে এই বই রয়েছে');
            }
            
            // Issue the book
            $stmt = $pdo->prepare("
                INSERT INTO library_issues (book_id, student_id, issue_date, due_date, issued_by) 
                VALUES (?, ?, ?, ?, ?)
            ");
            $stmt->execute([$book_id, $student_id, $issue_date, $due_date, $_SESSION['user_id']]);
            
            // Update available copies
            $stmt = $pdo->prepare("UPDATE library_books SET available_copies = available_copies - 1 WHERE id = ?");
            $stmt->execute([$book_id]);
            
            $pdo->commit();
            $success_message = 'বই সফলভাবে ইস্যু করা হয়েছে';
            
        } catch (Exception $e) {
            $pdo->rollBack();
            $error_message = $e->getMessage();
        }
    } else {
        $error_message = 'বই এবং ছাত্র/ছাত্রী নির্বাচন করুন';
    }
}

// Get books for dropdown
$stmt = $pdo->query("
    SELECT id, title, author, available_copies 
    FROM library_books 
    WHERE status = 'active' AND available_copies > 0 
    ORDER BY title
");
$available_books = $stmt->fetchAll();

// Get students for dropdown
$student_query = "
    SELECT s.id, s.student_id, s.first_name, s.last_name, c.name as class_name, c.section 
    FROM students s 
    JOIN classes c ON s.class_id = c.id 
    WHERE s.status = 'active'";

$params = [];

if (!empty($_GET['session'])) {
    $student_query .= " AND s.session = ?";
    $params[] = $_GET['session'];
}

if (!empty($_GET['class_id'])) {
    $student_query .= " AND s.class_id = ?";
    $params[] = (int)$_GET['class_id'];
}

if (!empty($_GET['group_id'])) {
    $group_id = (int)$_GET['group_id'];
    $stmt_group = $pdo->prepare("SELECT group_name FROM student_groups WHERE id = ?");
    $stmt_group->execute([$group_id]);
    $group_name = $stmt_group->fetchColumn();
    if ($group_name) {
        $student_query .= " AND s.group_name = ?";
        $params[] = $group_name;
    }
}

$student_query .= " ORDER BY s.first_name, s.last_name";

$stmt = $pdo->prepare($student_query);
$stmt->execute($params);
$students = $stmt->fetchAll();

// Get recent books
$stmt = $pdo->prepare("
    SELECT * FROM library_books 
    WHERE status = 'active' 
    ORDER BY created_at DESC 
    LIMIT 10
");
$stmt->execute();
$recent_books = $stmt->fetchAll();

// Get recent issues
$stmt = $pdo->prepare("
    SELECT li.*, lb.title, lb.author, s.first_name, s.last_name, s.student_id,
           c.name as class_name, c.section
    FROM library_issues li
    JOIN library_books lb ON li.book_id = lb.id
    JOIN students s ON li.student_id = s.id
    JOIN classes c ON s.class_id = c.id
    ORDER BY li.created_at DESC
    LIMIT 10
");
$stmt->execute();
$recent_issues = $stmt->fetchAll();

// Get library statistics
$stmt = $pdo->query("
    SELECT 
        COUNT(*) as total_books,
        SUM(total_copies) as total_copies,
        SUM(available_copies) as available_copies,
        (SELECT COUNT(*) FROM library_issues WHERE status = 'issued') as issued_books,
        (SELECT COUNT(*) FROM library_issues WHERE status = 'overdue') as overdue_books
    FROM library_books 
    WHERE status = 'active'
");
$library_stats = $stmt->fetch();
?>

<!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">
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <!-- 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="../logout.php">
                    <i class="fas fa-sign-out-alt"></i> লগআউট
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <!-- Library Statistics -->
        <div class="row mb-4">
            <div class="col-md-2">
                <div class="card bg-primary text-white">
                    <div class="card-body text-center">
                        <h4><?php echo $library_stats['total_books'] ?: 0; ?></h4>
                        <p class="mb-0">মোট বই</p>
                    </div>
                </div>
            </div>
            <div class="col-md-2">
                <div class="card bg-info text-white">
                    <div class="card-body text-center">
                        <h4><?php echo $library_stats['total_copies'] ?: 0; ?></h4>
                        <p class="mb-0">মোট কপি</p>
                    </div>
                </div>
            </div>
            <div class="col-md-2">
                <div class="card bg-success text-white">
                    <div class="card-body text-center">
                        <h4><?php echo $library_stats['available_copies'] ?: 0; ?></h4>
                        <p class="mb-0">উপলব্ধ</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-warning text-white">
                    <div class="card-body text-center">
                        <h4><?php echo $library_stats['issued_books'] ?: 0; ?></h4>
                        <p class="mb-0">ইস্যুকৃত</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-danger text-white">
                    <div class="card-body text-center">
                        <h4><?php echo $library_stats['overdue_books'] ?: 0; ?></h4>
                        <p class="mb-0">মেয়াদোত্তীর্ণ</p>
                    </div>
                </div>
            </div>
        </div>

        <?php if ($success_message): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <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" role="alert">
                <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; ?>

        <div class="row">
            <!-- Add Book Form -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5><i class="fas fa-book-medical"></i> নতুন বই যোগ</h5>
                    </div>
                    <div class="card-body">
                        <form method="POST" action="">
                            <div class="mb-3">
                                <label for="title" class="form-label">বইয়ের নাম <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="title" name="title" 
                                       value="<?php echo isset($_POST['title']) ? htmlspecialchars($_POST['title']) : ''; ?>" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="author" class="form-label">লেখক</label>
                                <input type="text" class="form-control" id="author" name="author" 
                                       value="<?php echo isset($_POST['author']) ? htmlspecialchars($_POST['author']) : ''; ?>">
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="isbn" class="form-label">ISBN</label>
                                        <input type="text" class="form-control" id="isbn" name="isbn" 
                                               value="<?php echo isset($_POST['isbn']) ? htmlspecialchars($_POST['isbn']) : ''; ?>">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="category" class="form-label">বিভাগ</label>
                                        <select class="form-select" id="category" name="category">
                                            <option value="">নির্বাচন করুন</option>
                                            <option value="পাঠ্যবই">পাঠ্যবই</option>
                                            <option value="গল্প">গল্প</option>
                                            <option value="উপন্যাস">উপন্যাস</option>
                                            <option value="কবিতা">কবিতা</option>
                                            <option value="বিজ্ঞান">বিজ্ঞান</option>
                                            <option value="ইতিহাস">ইতিহাস</option>
                                            <option value="ভূগোল">ভূগোল</option>
                                            <option value="ধর্ম">ধর্ম</option>
                                            <option value="অন্যান্য">অন্যান্য</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="publisher" class="form-label">প্রকাশক</label>
                                        <input type="text" class="form-control" id="publisher" name="publisher" 
                                               value="<?php echo isset($_POST['publisher']) ? htmlspecialchars($_POST['publisher']) : ''; ?>">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="publication_year" class="form-label">প্রকাশ সাল</label>
                                        <input type="number" class="form-control" id="publication_year" name="publication_year" 
                                               min="1900" max="<?php echo date('Y'); ?>"
                                               value="<?php echo isset($_POST['publication_year']) ? $_POST['publication_year'] : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="total_copies" class="form-label">কপি সংখ্যা <span class="text-danger">*</span></label>
                                        <input type="number" class="form-control" id="total_copies" name="total_copies" 
                                               min="1" value="<?php echo isset($_POST['total_copies']) ? $_POST['total_copies'] : '1'; ?>" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="location" class="form-label">অবস্থান</label>
                                        <input type="text" class="form-control" id="location" name="location" 
                                               placeholder="যেমন: শেলফ A-1"
                                               value="<?php echo isset($_POST['location']) ? htmlspecialchars($_POST['location']) : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            
                            <button type="submit" name="add_book" class="btn btn-success">
                                <i class="fas fa-save"></i> বই যোগ করুন
                            </button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Issue Book Form -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5><i class="fas fa-hand-holding"></i> বই ইস্যু করুন</h5>
                    </div>
                    <div class="card-body">
                        <!-- Student Search Form -->
                        <form method="GET" action="" class="mb-4">
                            <div class="row">
                                <div class="col-md-4">
                                    <label for="session" class="form-label">সেশন</label>
                                    <select class="form-select" id="session" name="session">
                                        <option value="">সব সেশন</option>
                                        <?php 
                                        $stmt = $pdo->query("SELECT DISTINCT session FROM students ORDER BY session DESC");
                                        while ($row = $stmt->fetch()) {
                                            $selected = (isset($_GET['session']) && $_GET['session'] == $row['session']) ? 'selected' : '';
                                            echo "<option value='{$row['session']}' {$selected}>{$row['session']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label for="class_id" class="form-label">ক্লাস</label>
                                    <select class="form-select" id="class_id" name="class_id">
                                        <option value="">সব ক্লাস</option>
                                        <?php
                                        $stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name, section");
                                        while ($row = $stmt->fetch()) {
                                            $selected = (isset($_GET['class_id']) && $_GET['class_id'] == $row['id']) ? 'selected' : '';
                                            echo "<option value='{$row['id']}' {$selected}>{$row['name']} - {$row['section']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label for="group_id" class="form-label">গ্রুপ</label>
                                        <select class="form-select" id="group_id" name="group_id">
                                        <option value="">সব গ্রুপ</option>
                                        <?php
                                        $stmt = $pdo->query("SELECT id, group_name FROM student_groups WHERE status = 'active' ORDER BY group_name");
                                        while ($row = $stmt->fetch()) {
                                            $selected = (isset($_GET['group_id']) && $_GET['group_id'] == $row['id']) ? 'selected' : '';
                                            echo "<option value='{$row['id']}' {$selected}>{$row['group_name']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <button type="submit" class="btn btn-info mt-3 w-100">
                                <i class="fas fa-search"></i> ছাত্র/ছাত্রী খুঁজুন
                            </button>
                        </form>
                        <hr>

                        <!-- Issue Book Form -->
                        <form method="POST" action="">
                            <div class="mb-3">
                                <label for="book_id" class="form-label">বই নির্বাচন করুন</label>
                                <select class="form-select" id="book_id" name="book_id" required>
                                    <option value="">বই নির্বাচন করুন</option>
                                    <?php foreach ($available_books as $book): ?>
                                        <option value="<?php echo $book['id']; ?>">
                                            <?php echo htmlspecialchars($book['title']); ?>
                                            <?php if ($book['author']): ?>
                                                - <?php echo htmlspecialchars($book['author']); ?>
                                            <?php endif; ?>
                                            (উপলব্ধ: <?php echo $book['available_copies']; ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            
                            <div class="mb-3">
                                <label for="student_id" class="form-label">ছাত্র/ছাত্রী নির্বাচন করুন</label>
                                <select class="form-select" id="student_id" name="student_id" required>
                                    <option value="">
                                        <?php echo (isset($_GET['session']) || isset($_GET['class_id']) || isset($_GET['group_id'])) ? 'ফলাফল থেকে ছাত্র/ছাত্রী নির্বাচন করুন' : 'প্রথমে ছাত্র/ছাত্রী খুঁজুন'; ?>
                                    </option>
                                    <?php foreach ($students as $student): ?>
                                        <option value="<?php echo $student['id']; ?>">
                                            <?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?>
                                            (<?php echo $student['student_id']; ?>) - 
                                            <?php echo htmlspecialchars($student['class_name'] . '-' . $student['section']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="issue_date" class="form-label">ইস্যুর তারিখ</label>
                                        <input type="date" class="form-control" id="issue_date" name="issue_date" 
                                               value="<?php echo date('Y-m-d'); ?>" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="due_date" class="form-label">ফেরতের তারিখ</label>
                                        <input type="date" class="form-control" id="due_date" name="due_date" 
                                               value="<?php echo date('Y-m-d', strtotime('+14 days')); ?>" required>
                                    </div>
                                </div>
                            </div>
                            
                            <button type="submit" name="issue_book" class="btn btn-primary">
                                <i class="fas fa-hand-holding"></i> বই ইস্যু করুন
                            </button>
                        </form>
                    </div>
                </div>

                <!-- Recent Issues -->
                <div class="card mt-4">
                    <div class="card-header">
                        <h5><i class="fas fa-history"></i> সাম্প্রতিক ইস্যু</h5>
                    </div>
                    <div class="card-body">
                        <?php if (empty($recent_issues)): ?>
                            <p class="text-muted">কোন ইস্যু তথ্য নেই</p>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead>
                                        <tr>
                                            <th>বই</th>
                                            <th>ছাত্র</th>
                                            <th>ইস্যু</th>
                                            <th>ফেরত</th>
                                            <th>অবস্থা</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recent_issues as $issue): ?>
                                            <tr>
                                                <td>
                                                    <small>
                                                        <strong><?php echo htmlspecialchars($issue['title']); ?></strong>
                                                        <?php if ($issue['author']): ?>
                                                            <br><?php echo htmlspecialchars($issue['author']); ?>
                                                        <?php endif; ?>
                                                    </small>
                                                </td>
                                                <td>
                                                    <small>
                                                        <?php echo htmlspecialchars($issue['first_name'] . ' ' . $issue['last_name']); ?>
                                                        <br><?php echo htmlspecialchars($issue['class_name'] . '-' . $issue['section']); ?>
                                                    </small>
                                                </td>
                                                <td><small><?php echo date('d/m/Y', strtotime($issue['issue_date'])); ?></small></td>
                                                <td><small><?php echo date('d/m/Y', strtotime($issue['due_date'])); ?></small></td>
                                                <td>
                                                    <?php
                                                    $status_class = '';
                                                    $status_text = '';
                                                    switch ($issue['status']) {
                                                        case 'issued':
                                                            $status_class = 'bg-primary';
                                                            $status_text = 'ইস্যুকৃত';
                                                            break;
                                                        case 'returned':
                                                            $status_class = 'bg-success';
                                                            $status_text = 'ফেরত';
                                                            break;
                                                        case 'overdue':
                                                            $status_class = 'bg-danger';
                                                            $status_text = 'মেয়াদোত্তীর্ণ';
                                                            break;
                                                        case 'lost':
                                                            $status_class = 'bg-warning';
                                                            $status_text = 'হারিয়ে গেছে';
                                                            break;
                                                    }
                                                    ?>
                                                    <span class="badge <?php echo $status_class; ?>"><?php echo $status_text; ?></span>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Recent Books -->
        <div class="row mt-4">
            <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-books"></i> সাম্প্রতিক বই</h5>
                        <div>
                            <a href="books.php" class="btn btn-sm btn-primary">
                                <i class="fas fa-eye"></i> সব বই দেখুন
                            </a>
                            <a href="return.php" class="btn btn-sm btn-success">
                                <i class="fas fa-undo"></i> বই ফেরত
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <?php if (empty($recent_books)): ?>
                            <div class="text-center py-4">
                                <i class="fas fa-book fa-3x text-muted mb-3"></i>
                                <p class="text-muted">কোন বই নেই</p>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>বইয়ের নাম</th>
                                            <th>লেখক</th>
                                            <th>বিভাগ</th>
                                            <th>প্রকাশক</th>
                                            <th>মোট কপি</th>
                                            <th>উপলব্ধ</th>
                                            <th>অবস্থান</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recent_books as $book): ?>
                                            <tr>
                                                <td>
                                                    <strong><?php echo htmlspecialchars($book['title']); ?></strong>
                                                    <?php if ($book['isbn']): ?>
                                                        <br><small class="text-muted">ISBN: <?php echo $book['isbn']; ?></small>
                                                    <?php endif; ?>
                                                </td>
                                                <td><?php echo htmlspecialchars($book['author']) ?: '-'; ?></td>
                                                <td>
                                                    <?php if ($book['category']): ?>
                                                        <span class="badge bg-info"><?php echo $book['category']; ?></span>
                                                    <?php else: ?>
                                                        <span class="text-muted">-</span>
                                                    <?php endif; ?>
                                                </td>
                                                <td><?php echo htmlspecialchars($book['publisher']) ?: '-'; ?></td>
                                                <td><span class="badge bg-primary"><?php echo $book['total_copies']; ?></span></td>
                                                <td>
                                                    <span class="badge <?php echo ($book['available_copies'] > 0) ? 'bg-success' : 'bg-danger'; ?>">
                                                        <?php echo $book['available_copies']; ?>
                                                    </span>
                                                </td>
                                                <td><?php echo htmlspecialchars($book['location']) ?: '-'; ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../assets/js/script.js"></script>
    <script>
        // Auto-calculate due date (14 days from issue date)
        document.getElementById('issue_date').addEventListener('change', function() {
            const issueDate = new Date(this.value);
            const dueDate = new Date(issueDate);
            dueDate.setDate(dueDate.getDate() + 14);
            
            document.getElementById('due_date').value = dueDate.toISOString().split('T')[0];
        });
    </script>
</body>
</html>