
<?php
include 'session_check.php';
if ($_SESSION['role'] !== 'admin') {
    header("Location: index.php");
    exit;
}
include 'config/db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nik = $_POST['nik'];
    $nama = $_POST['nama'];
    $uploadPath = '';

    if (isset($_FILES['hasil_lab']) && $_FILES['hasil_lab']['error'] === 0) {
        $uploadDir = 'uploads/';
        if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);
        $uploadPath = $uploadDir . basename($_FILES['hasil_lab']['name']);
        move_uploaded_file($_FILES['hasil_lab']['tmp_name'], $uploadPath);
    }

    $stmt = $conn->prepare("INSERT INTO pasien (nik, nama, hasil_lab) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $nik, $nama, $uploadPath);
    $stmt->execute();
    header("Location: index.php");
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Tambah Pasien</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body {
            background: #f0f4f8;
        }
        .form-container {
            max-width: 500px;
            margin: 60px auto;
            background: #fff;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .form-title {
            margin-bottom: 25px;
            font-weight: bold;
            text-align: center;
            color: #0d6efd;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="form-container">
        <h4 class="form-title">Tambah Data Pasien</h4>
        <form method="POST" enctype="multipart/form-data">
            <div class="mb-3">
                <label class="form-label"><strong>NIK</strong></label>
                <input type="text" name="nik" class="form-control" required>
            </div>
            <div class="mb-3">
                <label class="form-label"><strong>Nama</strong></label>
                <input type="text" name="nama" class="form-control" required>
            </div>
            <div class="mb-3">
                <label class="form-label"><strong>Upload Hasil Lab</strong></label>
                <input type="file" name="hasil_lab" class="form-control">
            </div>
            <div class="d-grid gap-2">
                <button class="btn btn-primary">Simpan</button>
                <a href="index.php" class="btn btn-secondary">Kembali</a>
</div>
</body>
</html>
