<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../koneksi.php';

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$query = mysqli_query($koneksi, "SELECT * FROM tujuan_pembelajaran WHERE id = '$id'");
$data = mysqli_fetch_array($query);

if (!$data) {
    die("Data tidak ditemukan!");
}

// Proses Update
if (isset($_POST['update'])) {
    $tp = $_POST['tp'];
    $desk = mysqli_real_escape_string($koneksi, $_POST['deskripsi']);
    $update = mysqli_query($koneksi, "UPDATE tujuan_pembelajaran SET tp_kode='$tp', deskripsi_tp='$desk' WHERE id='$id'");
    
    if ($update) {
        echo "<script>alert('Berhasil Update'); window.location.href='tujuan_pembelajaran.php';</script>";
    } else {
        echo "Error Update: " . mysqli_error($koneksi);
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Edit TP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light p-5">
<div class="container">
    <div class="card shadow">
        <div class="card-header bg-primary text-white"><h5>Edit Tujuan Pembelajaran</h5></div>
        <div class="card-body">
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Kode TP</label>
                    <input type="text" name="tp" class="form-control" value="<?= htmlspecialchars($data['tp_kode']) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Deskripsi</label>
                    <textarea name="deskripsi" class="form-control" rows="3"><?= htmlspecialchars($data['deskripsi_tp']) ?></textarea>
                </div>
                <a href="tujuan_pembelajaran.php" class="btn btn-secondary">Batal</a>
                <button type="submit" name="update" class="btn btn-success">Update Data</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>