<?php
session_start();
include '../config/koneksi.php';

if(!isset($_SESSION['admin'])){
    header("Location: ../login.php");
    exit;
}

$id = $_GET['id'];

$data = mysqli_query($conn,"SELECT * FROM pendaftar WHERE id='$id'");
$d = mysqli_fetch_array($data);

if(isset($_POST['update'])){

    $nama     = $_POST['nama'];
    $nisn     = $_POST['nisn'];
    $asal     = $_POST['asal_sekolah'];
    $jurusan  = $_POST['jurusan'];
    $hp       = $_POST['no_hp'];
    $alamat   = $_POST['alamat'];

    // Upload KK
    if($_FILES['foto_kk']['name'] != ""){

        $foto_kk = $_FILES['foto_kk']['name'];
        $tmp_kk  = $_FILES['foto_kk']['tmp_name'];

        move_uploaded_file($tmp_kk,
        "../assets/img/kk/".$foto_kk);

    }else{
        $foto_kk = $d['foto_kk'];
    }

    // Upload KTP
    if($_FILES['foto_ktp']['name'] != ""){

        $foto_ktp = $_FILES['foto_ktp']['name'];
        $tmp_ktp  = $_FILES['foto_ktp']['tmp_name'];

        move_uploaded_file($tmp_ktp,
        "../assets/img/ktp/".$foto_ktp);

    }else{
        $foto_ktp = $d['foto_ktp'];
    }

    // Upload Akte
    if($_FILES['foto_akte']['name'] != ""){

        $foto_akte = $_FILES['foto_akte']['name'];
        $tmp_akte  = $_FILES['foto_akte']['tmp_name'];

        move_uploaded_file($tmp_akte,
        "../assets/img/akte/".$foto_akte);

    }else{
        $foto_akte = $d['foto_akte'];
    }

    mysqli_query($conn,"UPDATE pendaftar SET

    nama='$nama',
    nisn='$nisn',
    asal_sekolah='$asal',
    jurusan='$jurusan',
    no_hp='$hp',
    alamat='$alamat',
    foto_kk='$foto_kk',
    foto_ktp='$foto_ktp',
    foto_akte='$foto_akte'

    WHERE id='$id'
    ");

    echo "
    <script>
    alert('Data berhasil diupdate');
    window.location='data_pendaftar.php';
    </script>
    ";

}
?>

<!DOCTYPE html>
<html lang="id">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Edit Pendaftar</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">

<style>

body{
    background:#f4f6f9;
    font-family:'Segoe UI',sans-serif;
}

.card-form{
    border:none;
    border-radius:25px;
    overflow:hidden;
    box-shadow:0 10px 30px rgba(0,0,0,0.08);
}

.card-header{
    background:linear-gradient(45deg,#0d6efd,#0b5ed7);
    color:white;
    padding:20px;
}

.form-control,
.form-select{
    border-radius:12px;
    height:50px;
}

textarea.form-control{
    height:120px;
}

.preview-img{
    width:120px;
    height:120px;
    object-fit:cover;
    border-radius:15px;
    border:3px solid #ddd;
}

</style>

</head>
<body>

<div class="container py-5">

<div class="card card-form">

<div class="card-header">

<h3 class="mb-0">
<i class="fa fa-edit"></i>
Edit Data Pendaftar
</h3>

</div>

<div class="card-body p-4">

<form method="POST"
enctype="multipart/form-data">

<div class="row">

<div class="col-md-6 mb-3">
<label>Nama Lengkap</label>
<input type="text"
name="nama"
class="form-control"
value="<?= $d['nama'] ?>"
required>
</div>

<div class="col-md-6 mb-3">
<label>NISN</label>
<input type="text"
name="nisn"
class="form-control"
value="<?= $d['nisn'] ?>"
required>
</div>

<div class="col-md-6 mb-3">
<label>Asal Sekolah</label>
<input type="text"
name="asal_sekolah"
class="form-control"
value="<?= $d['asal_sekolah'] ?>">
</div>

<div class="col-md-6 mb-3">
<label>Jurusan</label>

<select name="jurusan"
class="form-select">

<option <?= $d['jurusan']=="ATPH" ? "selected" : "" ?>>
ATPH
</option>

<option <?= $d['jurusan']=="ATU" ? "selected" : "" ?>>
ATU
</option>

<option <?= $d['jurusan']=="PPLG" ? "selected" : "" ?>>
PPLG
</option>

<option <?= $d['jurusan']=="DKV" ? "selected" : "" ?>>
DKV
</option>

<option <?= $d['jurusan']=="TSM" ? "selected" : "" ?>>
TSM
</option>

<option <?= $d['jurusan']=="TPTUP" ? "selected" : "" ?>>
TPTUP
</option>

</select>

</div>

<div class="col-md-6 mb-3">
<label>No HP</label>
<input type="text"
name="no_hp"
class="form-control"
value="<?= $d['no_hp'] ?>">
</div>

<div class="col-md-6 mb-3">
<label>Alamat</label>
<textarea name="alamat"
class="form-control"><?= $d['alamat'] ?></textarea>
</div>

<!-- KK -->
<div class="col-md-4 mb-3">

<label>Foto KK</label>

<input type="file"
name="foto_kk"
class="form-control">

<div class="mt-2">
<img src="../assets/img/kk/<?= $d['foto_kk'] ?>"
class="preview-img">
</div>

</div>

<!-- KTP -->
<div class="col-md-4 mb-3">

<label>Foto KTP</label>

<input type="file"
name="foto_ktp"
class="form-control">

<div class="mt-2">
<img src="../assets/img/ktp/<?= $d['foto_ktp'] ?>"
class="preview-img">
</div>

</div>

<!-- Akte -->
<div class="col-md-4 mb-3">

<label>Foto Akte</label>

<input type="file"
name="foto_akte"
class="form-control">

<div class="mt-2">
<img src="../assets/img/akte/<?= $d['foto_akte'] ?>"
class="preview-img">
</div>

</div>

</div>

<div class="mt-4 d-flex gap-2">

<button type="submit"
name="update"
class="btn btn-primary px-4 rounded-pill">

<i class="fa fa-save"></i>
Update Data

</button>

<a href="data_pendaftar.php"
class="btn btn-secondary px-4 rounded-pill">

<i class="fa fa-arrow-left"></i>
Kembali

</a>

</div>

</form>

</div>

</div>

</div>

</body>
</html>