<?php
session_start();

// Sertakan file koneksi database
include_once '../api/db_connect.php'; 

// Periksa apakah pengguna sudah login sebagai jejaring
if (!isset($_SESSION['jejaring_logged_in']) || !$_SESSION['jejaring_logged_in']) {
    header('Location: ../auth/jejaring_login.php'); 
    exit();
}

$loggedInUsername = htmlspecialchars($_SESSION['jejaring_username']);
$loggedInNamaInstansi = htmlspecialchars($_SESSION['jejaring_nama_instansi']);
$message = '';
$message_type = ''; // 'success' atau 'error'

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $nik = $_POST['nik'];
    $nama_pasien = $_POST['nama_pasien'];
    $tanggal_lahir = $_POST['tanggal_lahir'];
    $jenis_kelamin = $_POST['jenis_kelamin'];
    $alamat = $_POST['alamat'];
    $desa = $_POST['desa']; // Data desa diambil dari select
    $sistole = $_POST['sistole'];
    $diastole = $_POST['diastole'];
    $pemeriksaan_gula = $_POST['pemeriksaan_gula'];
    $diagnosa_primer = $_POST['diagnosa_primer'];
    $diagnosa_sekunder = $_POST['diagnosa_sekunder'];
    $terapi_farmakologi = $_POST['terapi_farmakologi'];
    $tanggal_pemeriksaan = $_POST['tanggal_pemeriksaan'];
    $sumber_laporan = $loggedInNamaInstansi; // Menggunakan nama instansi yang login sebagai sumber laporan
    
    // Validasi data input
    // Menambahkan validasi untuk desa
    if (empty($nik) || empty($nama_pasien) || empty($tanggal_lahir) || empty($jenis_kelamin) || empty($alamat) || empty($sistole) || empty($diastole) || empty($tanggal_pemeriksaan) || empty($desa)) {
        $message = "NIK, Nama, Tanggal Lahir, Jenis Kelamin, Alamat, Desa, Sistole, Diastole, dan Tanggal Pemeriksaan wajib diisi.";
        $message_type = 'error';
    } else {
        try {
            // Cek apakah NIK sudah ada
            $checkNikQuery = "SELECT COUNT(*) FROM ptm WHERE nik = :nik";
            $checkStmt = $pdo->prepare($checkNikQuery);
            $checkStmt->bindParam(':nik', $nik);
            $checkStmt->execute();
            $nikExists = $checkStmt->fetchColumn() > 0;

            if ($nikExists) {
                $message = "NIK sudah terdaftar. Data ini tidak dapat dimasukkan.";
                $message_type = 'error';
            } else {
                // Siapkan query SQL untuk memasukkan data
                $sql = "INSERT INTO ptm (nik, nama_pasien, tanggal_lahir, jenis_kelamin, alamat, desa, sistole, diastole, pemeriksaan_gula, diagnosa_primer, diagnosa_sekunder, terapi_farmakologi, tanggal_pemeriksaan, sumber_laporan) 
                        VALUES (:nik, :nama_pasien, :tanggal_lahir, :jenis_kelamin, :alamat, :desa, :sistole, :diastole, :pemeriksaan_gula, :diagnosa_primer, :diagnosa_sekunder, :terapi_farmakologi, :tanggal_pemeriksaan, :sumber_laporan)";
                
                $stmt = $pdo->prepare($sql);
                
                // Bind parameter
                $stmt->bindParam(':nik', $nik);
                $stmt->bindParam(':nama_pasien', $nama_pasien);
                $stmt->bindParam(':tanggal_lahir', $tanggal_lahir);
                $stmt->bindParam(':jenis_kelamin', $jenis_kelamin);
                $stmt->bindParam(':alamat', $alamat);
                $stmt->bindParam(':desa', $desa);
                $stmt->bindParam(':sistole', $sistole);
                $stmt->bindParam(':diastole', $diastole);
                $stmt->bindParam(':pemeriksaan_gula', $pemeriksaan_gula);
                $stmt->bindParam(':diagnosa_primer', $diagnosa_primer);
                $stmt->bindParam(':diagnosa_sekunder', $diagnosa_sekunder);
                $stmt->bindParam(':terapi_farmakologi', $terapi_farmakologi);
                $stmt->bindParam(':tanggal_pemeriksaan', $tanggal_pemeriksaan);
                $stmt->bindParam(':sumber_laporan', $sumber_laporan);
                
                // Jalankan query
                if ($stmt->execute()) {
                    $message = "Data PTM berhasil ditambahkan!";
                    $message_type = 'success';
                } else {
                    $message = "Gagal menambahkan data PTM. Silakan coba lagi.";
                    $message_type = 'error';
                }
            }
        } catch (PDOException $e) {
            $message = "Terjadi kesalahan database: " . $e->getMessage();
            $message_type = 'error';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Entri Laporan PTM - RekamSehat</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        :root {
            --primary-color: #4dab25;
            --secondary-color: #024d52;
            --bg-light: #f4f7f6;
            --text-dark: #333;
            --text-light: #fefefe;
            --border-color: #e0e0e0;
            --sidebar-width: 250px;
            --header-height: 60px;
        }
        body {
            font-family: 'Poppins', sans-serif;
            margin: 0;
            background-color: var(--bg-light);
            color: var(--text-dark);
            overflow-x: hidden;
        }
        .main-wrapper {
            display: flex;
        }
        .header {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: var(--header-height);
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            padding: 0 20px;
            z-index: 1000;
        }
        .header .logo {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--secondary-color);
            margin-right: auto;
        }
        .header .user-info {
            display: flex;
            align-items: center;
            font-size: 0.9rem;
            color: var(--text-dark);
        }
        .header .user-info .user-icon {
            margin-right: 8px;
            color: var(--primary-color);
        }
        .header .user-info .username {
            font-weight: 600;
            margin-right: 15px;
        }
        .header .logout-btn {
            background: none;
            border: none;
            color: var(--secondary-color);
            cursor: pointer;
            font-size: 1rem;
            padding: 5px 10px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }
        .header .logout-btn:hover {
            background-color: rgba(0,0,0,0.05);
        }
        .sidebar {
            width: var(--sidebar-width);
            background: var(--secondary-color);
            color: var(--text-light);
            padding-top: var(--header-height);
            position: fixed;
            top: 0;
            left: 0;
            height: 100%;
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
            z-index: 999;
            overflow-y: auto;
            transition: transform 0.3s ease;
        }
        .sidebar-header {
            padding: 20px;
            text-align: center;
            background-color: var(--primary-color);
            color: var(--text-light);
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        .sidebar ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .sidebar ul li a {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            color: var(--text-light);
            text-decoration: none;
            font-size: 1rem;
            transition: background-color 0.3s ease, color 0.3s ease;
        }
        .sidebar ul li a i {
            margin-right: 10px;
            font-size: 1.2rem;
        }
        .sidebar ul li a:hover,
        .sidebar ul li a.active {
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .sidebar .dropdown-btn {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px 20px;
            color: var(--text-light);
            text-decoration: none;
            font-size: 1rem;
            transition: background-color 0.3s ease, color 0.3s ease;
            cursor: pointer;
        }
        .sidebar .dropdown-btn i {
            margin-right: 10px;
            font-size: 1.2rem;
        }
        .sidebar .dropdown-btn:hover,
        .sidebar .dropdown-btn.active {
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .sidebar .dropdown-container {
            display: none;
            background-color: rgba(0, 0, 0, 0.1);
            padding-left: 20px;
        }
        .sidebar .dropdown-container a {
            padding: 12px 16px;
            text-decoration: none;
            color: var(--text-light);
            display: block;
            transition: background-color 0.3s ease, color 0.3s ease;
        }
        .sidebar .dropdown-container a:hover {
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .sidebar .dropdown-container.show {
            display: block;
        }
        .main-content {
            flex-grow: 1;
            padding: calc(var(--header-height) + 30px) 30px 30px 30px;
            margin-left: var(--sidebar-width);
            transition: margin-left 0.3s ease;
        }
        .sidebar-toggle {
            display: none; /* Default hidden on desktop */
            position: absolute;
            left: 20px; /* Aligned with header padding */
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 5px;
            padding: 8px 12px;
            cursor: pointer;
            z-index: 1001;
            font-size: 1.2rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        .form-container {
            max-width: 800px;
            padding: 20px 0;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 30px;
            margin: auto; /* Center the form container */
        }
        .form-container h2 {
            text-align: left;
            margin-bottom: 20px;
            color: var(--secondary-color);
        }
        .form-row {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 20px;
        }
        .form-group {
            flex: 1 1 calc(50% - 10px);
            display: flex;
            flex-direction: column;
        }
        @media (max-width: 600px) {
            .form-group {
                flex: 1 1 100%;
            }
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
        }
        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid var(--border-color);
            border-radius: 5px;
            font-family: inherit;
            font-size: 1rem;
            box-sizing: border-box;
        }
        .form-group input:focus, .form-group select:focus, .form-group textarea:focus {
            border-color: var(--primary-color);
            outline: none;
        }
        .form-group label .required-symbol {
            color: red;
        }
        .submit-btn {
            width: 100%;
            padding: 12px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            cursor: pointer;
            transition: background-color 0.3s ease;
            margin-top: 20px;
        }
        .submit-btn:hover {
            background-color: #3e8d22;
        }
        .message {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
            text-align: center;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        /* Navigasi Mobile Baru */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                box-shadow: none;
            }
            .main-content {
                margin-left: 0;
            }
            .sidebar-toggle {
                display: block;
            }
            body.nav-active .sidebar {
                transform: translateX(0);
            }
            body.nav-active .overlay {
                display: block;
            }
        }
        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 998; /* Di bawah sidebar, di atas konten utama */
        }
    </style>
</head>
<body>
    <div class="header">
        <button class="sidebar-toggle" id="sidebarToggle">
            <i class="fas fa-bars"></i>
        </button>
        <div class="logo"></div>
        <div class="user-info">
            <span class="username"><i class="fas fa-user-circle user-icon"></i> <?php echo $loggedInNamaInstansi; ?></span>
            <a href="jejaring_logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</a>
        </div>
    </div>
    
    <div class="main-wrapper" id="mainWrapper">
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                Menu Jejaring
            </div>
            <ul>
                <li><a href="../jejaring/"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
                <li><a href="change_password.php"><i class="fas fa-key"></i> Ubah Kata Sandi</a></li>
                <li>
                    <a href="#" class="dropdown-btn active">
                        <i class="fas fa-file-alt"></i> Laporan
                        <i class="fas fa-caret-down"></i>
                    </a>
                    <div class="dropdown-container show">
                        <a href="kehamilan/"><i class="fas fa-baby"></i> Laporan Kehamilan</a>
                        <a href="ptm_entry.php" class="active"><i class="fas fa-heartbeat"></i> Laporan PTM</a>
                        <a href="tb_entry.php"><i class="fas fa-lungs"></i> Entri Terduga TB</a>
                        <a href="kb_entry.php"><i class="fas fa-restroom"></i> Entri KB</a>
                    </div>
                </li>
            </ul>
        </aside>
        <main class="main-content" id="mainContent">
            <div class="form-container">
                <h2>Entri Laporan PTM</h2>
                <?php if ($message): ?>
                    <div class="message <?php echo $message_type; ?>">
                        <?php echo $message; ?>
                    </div>
                <?php endif; ?>
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="nik">NIK<span class="required-symbol">*</span></label>
                            <input type="text" id="nik" name="nik" required pattern="[0-9]{16}" title="NIK harus 16 digit angka">
                        </div>
                        <div class="form-group">
                            <label for="nama_pasien">Nama Pasien<span class="required-symbol">*</span></label>
                            <input type="text" id="nama_pasien" name="nama_pasien" required>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="tanggal_lahir">Tanggal Lahir<span class="required-symbol">*</span></label>
                            <input type="date" id="tanggal_lahir" name="tanggal_lahir" required>
                        </div>
                        <div class="form-group">
                            <label for="jenis_kelamin">Jenis Kelamin<span class="required-symbol">*</span></label>
                            <select id="jenis_kelamin" name="jenis_kelamin" required>
                                <option value="">-- Pilih Jenis Kelamin --</option>
                                <option value="Laki-laki">Laki-laki</option>
                                <option value="Perempuan">Perempuan</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="alamat">Alamat<span class="required-symbol">*</span></label>
                            <input type="text" id="alamat" name="alamat" required>
                        </div>
                        <div class="form-group">
                            <label for="desa">Desa<span class="required-symbol">*</span></label>
                            <select id="desa" name="desa" required>
                                <option value="">-- Pilih Desa --</option>
                                <option value="RONDOKUNING">RONDOKUNING</option>
                                <option value="KREGENAN">KREGENAN</option>
                                <option value="SEMAMPIR">SEMAMPIR</option>
                                <option value="BULU">BULU</option>
                                <option value="SIDOMUKTI">SIDOMUKTI</option>
                                <option value="KRAKSAAN WETAN">KRAKSAAN WETAN</option>
                                <option value="RANGKANG">RANGKANG</option>
                                <option value="KANDANGJATI KULON">KANDANGJATI KULON</option>
                                <option value="KANDANGJATI WETAN">KANDANGJATI WETAN</option>
                                <option value="ALASSUMUR KULON">ALASSUMUR KULON</option>
                                <option value="SUMBERLELE">SUMBERLELE</option>
                                <option value="TAMANSARI">TAMANSARI</option>
                                <option value="ASEMBAKOR">ASEMBAKOR</option>
                                <option value="KEBONAGUNG">KEBONAGUNG</option>
                                <option value="SIDOPEKSO">SIDOPEKSO</option>
                                <option value="PATOKAN">PATOKAN</option>
                                <option value="ASEMBAGUS">ASEMBAGUS</option>
                                <option value="KALIBUNTU">KALIBUNTU</option>
                                <option value="LUAR WILAYAH">LUAR WILAYAH KRAKSAAN</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="tanggal_pemeriksaan">Tanggal Pemeriksaan<span class="required-symbol">*</span></label>
                            <input type="date" id="tanggal_pemeriksaan" name="tanggal_pemeriksaan" required>
                        </div>
                        <div class="form-group">
                            <label for="sistole">Sistole<span class="required-symbol">*</span></label>
                            <input type="number" id="sistole" name="sistole" min="0" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="diastole">Diastole<span class="required-symbol">*</span></label>
                            <input type="number" id="diastole" name="diastole" min="0" required>
                        </div>
                        <div class="form-group">
                            <label for="pemeriksaan_gula">Pemeriksaan Gula Darah</label>
                            <input type="number" step="0.01" id="pemeriksaan_gula" name="pemeriksaan_gula" min="0">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="diagnosa_primer">Diagnosa Primer</label>
                        <input type="text" id="diagnosa_primer" name="diagnosa_primer">
                    </div>
                    
                    <div class="form-group">
                        <label for="diagnosa_sekunder">Diagnosa Sekunder</label>
                        <input type="text" id="diagnosa_sekunder" name="diagnosa_sekunder">
                    </div>
                    
                    <div class="form-group">
                        <label for="terapi_farmakologi">Terapi Farmakologi</label>
                        <textarea id="terapi_farmakologi" name="terapi_farmakologi" rows="4"></textarea>
                    </div>
                    
                    <button type="submit" class="submit-btn">Simpan Laporan PTM</button>
                </form>
            </div>
        </main>
    </div>
    <div class="overlay" id="overlay"></div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const sidebarToggle = document.getElementById('sidebarToggle');
            const overlay = document.getElementById('overlay');
            const dropdownBtn = document.querySelector('.dropdown-btn');
            const dropdownContainer = document.querySelector('.dropdown-container');

            // Fungsi untuk mengaktifkan/menonaktifkan menu
            function toggleNav() {
                document.body.classList.toggle('nav-active');
            }

            // Atur event listener untuk tombol toggle
            if (sidebarToggle) {
                sidebarToggle.addEventListener('click', toggleNav);
            }
            
            // Atur event listener untuk overlay (menutup menu saat klik di luar)
            if (overlay) {
                overlay.addEventListener('click', toggleNav);
            }

            // Atur event listener untuk setiap link di sidebar agar menu tertutup
            const sidebarLinks = document.querySelectorAll('.sidebar ul li a');
            sidebarLinks.forEach(link => {
                link.addEventListener('click', function() {
                    // Tutup menu jika di tampilan mobile
                    if (window.innerWidth <= 768) {
                        document.body.classList.remove('nav-active');
                    }
                });
            });

            // Logika untuk dropdown menu
            if(dropdownBtn){
                dropdownBtn.addEventListener('click', function(event) {
                    event.preventDefault();
                    this.classList.toggle('active');
                    dropdownContainer.classList.toggle('show');
                });
            }
        });
    </script>
</body>
</html>