<?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 = '';
$book = null;

$book_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($book_id <= 0) {
    header('Location: books.php');
    exit();
}

// Fetch book data
$stmt = $pdo->prepare("SELECT * FROM library_books WHERE id = ?");
$stmt->execute([$book_id]);
$book = $stmt->fetch();

if (!$book) {
    header('Location: books.php');
    exit();
}

// Handle book update
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_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'];
    $available_copies = (int)$_POST['available_copies'];
    $location = trim($_POST['location']);
    
    if (empty($title) || $total_copies <= 0) {
        $error_message = 'বইয়ের নাম এবং কপি সংখ্যা প্রয়োজন';
    } elseif ($available_copies > $total_copies) {
        $error_message = 'উপলব্ধ কপির সংখ্যা মোট কপির চেয়ে বেশি হতে পারে না।';
    } else {
        try {
            $stmt = $pdo->prepare("
                UPDATE library_books 
                SET title = ?, author = ?, isbn = ?, category = ?, publisher = ?, publication_year = ?, total_copies = ?, available_copies = ?, location = ?
                WHERE id = ?
            ");
            $stmt->execute([$title, $author, $isbn, $category, $publisher, $publication_year, $total_copies, $available_copies, $location, $book_id]);
            
            $success_message = 'বই সফলভাবে আপডেট করা হয়েছে।';
            
            // Refresh book data
            $stmt = $pdo->prepare("SELECT * FROM library_books WHERE id = ?");
            $stmt->execute([$book_id]);
            $book = $stmt->fetch();

        } catch (Exception $e) {
            $error_message = 'বই আপডেট করতে সমস্যা হয়েছে: ' . $e->getMessage();
        }
    }
}

?>

<!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="books.php"><i class="fas fa-arrow-left"></i> বইয়ের তালিকায় ফিরে যান</a>
                <a class="nav-link" href="../logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h5><i class="fas fa-edit"></i> বই সম্পাদনা করুন</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($success_message): ?>
                            <div class="alert alert-success">
                                <?php echo $success_message; ?>
                                <a href="books.php" class="btn-link">বইয়ের তালিকায় ফিরে যান</a>
                            </div>
                        <?php endif; ?>
                        <?php if ($error_message): ?>
                            <div class="alert alert-danger"><?php echo $error_message; ?></div>
                        <?php endif; ?>

                        <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 htmlspecialchars($book['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 htmlspecialchars($book['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 htmlspecialchars($book['isbn']); ?>">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="category" class="form-label">বিভাগ</label>
                                        <input type="text" class="form-control" id="category" name="category" value="<?php echo htmlspecialchars($book['category']); ?>">
                                    </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 htmlspecialchars($book['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 $book['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 $book['total_copies']; ?>" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="available_copies" class="form-label">উপলব্ধ কপি <span class="text-danger">*</span></label>
                                        <input type="number" class="form-control" id="available_copies" name="available_copies" min="0" value="<?php echo $book['available_copies']; ?>" required>
                                    </div>
                                </div>
                            </div>

                            <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 htmlspecialchars($book['location']); ?>">
                            </div>
                            
                            <button type="submit" name="update_book" class="btn btn-primary"><i class="fas fa-save"></i> আপডেট করুন</button>
                            <a href="books.php" class="btn btn-secondary">বাতিল</a>
                        </form>
                    </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>
