<?php
session_start();
error_reporting(0);
include('includes/config.php');

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

$athrid = intval($_GET['athrid']);

if (isset($_POST['update'])) {
  $author = $_POST['author'];
  $sql = "UPDATE tblauthors SET AuthorName = :author WHERE id = :athrid";
  $query = $dbh->prepare($sql);
  $query->bindParam(':author', $author, PDO::PARAM_STR);
  $query->bindParam(':athrid', $athrid, PDO::PARAM_STR);
  $query->execute();

  $_SESSION['updatemsg'] = "Author info updated successfully";
  header('location:manage-authors.php');
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Edit Author | 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 -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <style>
    .square-card {
      background-color: #f8f9fa;
      border: 1px solid #dee2e6;
      border-radius: 6px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    }
    .header-line {
      font-size: 22px;
      font-weight: 600;
      color: #2258d5;
    }
    .main-content {
      margin-left: 180px;
      margin-top: 50px;
      padding: 20px;
    }
  </style>
</head>
<body>
<div class="d-flex flex-column min-vh-100">
  <?php include('includes/header.php'); ?>

  <div class="main-content flex-grow-1">
    <div class="row mb-4">
      <div class="col-md-12 d-flex justify-content-between align-items-center">
        <h5 class="header-line"><i class="fas fa-user-edit mr-2"></i>Edit Author</h5>
      </div>
    </div>

    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="card square-card mb-4">
          <div class="card-header bg-secondary text-white">
            <i class="fa fa-pencil-alt mr-2"></i> Update Author
          </div>
          <div class="card-body">
            <?php
            $sql = "SELECT * FROM tblauthors WHERE id = :athrid";
            $query = $dbh->prepare($sql);
            $query->bindParam(':athrid', $athrid, PDO::PARAM_STR);
            $query->execute();
            $result = $query->fetch(PDO::FETCH_OBJ);

            if ($result):
            ?>
              <form method="post">
                <div class="form-group">
                  <label for="author" class="font-weight-bold">Author Name</label>
                  <input type="text" name="author" id="author" class="form-control" value="<?= htmlentities($result->AuthorName); ?>" required>
                </div>

                <div class="d-flex justify-content-between">
                  <a href="manage-authors.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Back to List
                  </a>
                  <button type="submit" name="update" class="btn btn-primary">
                    <i class="fas fa-save"></i> Update Author
                  </button>
                </div>
              </form>
            <?php else: ?>
              <p class="text-danger">Invalid Author ID.</p>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<?php include('includes/footer.php'); ?>

<!-- Scripts -->
<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>
