<?php
// Connect to database
require_once '../../../dripend/includes/db.php';

$is_success = isset($_GET['status']) && $_GET['status'] == 'success';
$error_msg = isset($_GET['status']) && $_GET['status'] == 'error' ? "Error adding ICD-10 Code. It might already exist in the database or the inputs are invalid." : "";
?>

<style>
/* Embedded Styles for SPA Safety */
.gdrg-add-module { width: 100%; animation: fadeIn 0.3s ease-in-out; font-family: "Segoe UI", system-ui, sans-serif; }
.header-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; }
.header-bar h2 { color: #1e3a8a; margin: 0; font-size: 24px; font-weight: 800; display: flex; align-items: center; gap: 10px; text-transform: uppercase; }
.btn-back { background: #fff; color: #334155; padding: 10px 18px; text-decoration: none; border-radius: 8px; font-weight: bold; border: 1px solid #e2e8f0; cursor: pointer; display: inline-flex; align-items: center; gap: 8px; transition: 0.2s; }
.btn-back:hover { background: #f1f5f9; }
.btn-outline { background: #fff; color: #1e3a8a; padding: 12px 24px; text-decoration: none; border-radius: 8px; font-weight: bold; border: 1px solid #1e3a8a; cursor: pointer; transition: 0.2s; }
.btn-outline:hover { background: #eff6ff; }

/* Form Card Layout */
.form-card { background: #ffffff; border-radius: 12px; margin-bottom: 24px; border: 1px solid #e2e8f0; box-shadow: 0 2px 5px rgba(0,0,0,0.02); overflow: hidden; }
.form-card-header { padding: 18px 24px; color: #1e3a8a; font-weight: 800; font-size: 15px; text-transform: uppercase; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid #f1f5f9; background: #fafaf9; }
.form-card-body { padding: 24px; }

.grid-2 { display: grid; grid-template-columns: 1fr 2fr; gap: 24px; }

.form-group { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.form-group label { font-size: 12px; font-weight: 700; color: #64748b; text-transform: uppercase; letter-spacing: 0.5px; }
.form-control { padding: 12px 16px; border-radius: 8px; border: 1px solid #cbd5e1; outline: none; font-size: 14px; width: 100%; box-sizing: border-box; color: #334155; transition: 0.2s; }
.form-control:focus { border-color: #1e3a8a; box-shadow: 0 0 0 3px #eff6ff; }

.btn-submit { background: #0f172a; color: #fff; border: none; padding: 16px; border-radius: 8px; font-weight: 800; font-size: 15px; cursor: pointer; width: 100%; transition: 0.2s; text-transform: uppercase; display:flex; align-items:center; justify-content:center; gap:10px; }
.btn-submit:hover { background: #1e293b; }

/* SUCCESS MODAL STYLES */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(15, 23, 42, 0.7); display: flex; align-items: center; justify-content: center; z-index: 9999; animation: fadeIn 0.2s ease; }
.modal-content { background: #fff; width: 450px; padding: 40px 30px; border-radius: 16px; text-align: center; box-shadow: 0 20px 40px rgba(0,0,0,0.2); }
.modal-icon { font-size: 60px; color: #10b981; margin-bottom: 20px; animation: scaleIn 0.4s ease-out; }
.modal-content h3 { color: #1e293b; font-size: 22px; font-weight: 800; margin-bottom: 10px; }
.modal-content p { color: #64748b; font-size: 15px; margin-bottom: 30px; line-height: 1.5; }
.modal-actions { display: flex; gap: 15px; justify-content: center; }
@keyframes scaleIn { from { transform: scale(0); } to { transform: scale(1); } }
</style>

<div class="gdrg-add-module">
    
    <!-- THE SUCCESS MODAL (Only shows when insertion is successful) -->
    <?php if ($is_success): ?>
    <div class="modal-overlay">
        <div class="modal-content">
            <div class="modal-icon"><i class="fa-solid fa-circle-check"></i></div>
            <h3>Code Saved!</h3>
            <p>The new ICD-10 diagnosis code has been successfully added to the dictionary.</p>
            <div class="modal-actions">
                <button type="button" class="btn-outline" onclick="if(typeof loadContent === 'function') { loadContent('Add ICD-10', 'gdrg/addIcd.php', 'ICD-10 Dictionary', 'facility', null, true); } else { window.location.href='?cat=facility&page=gdrg/addIcd.php'; }">
                    <i class="fa-solid fa-plus"></i> Add Another
                </button>
                <button type="button" class="btn-submit" style="width: auto; padding: 12px 24px;" onclick="if(typeof loadContent === 'function') { loadContent('ICD-10 Dictionary', 'gdrg/icd10_dict.php', 'Dictionary', 'facility', null, true); } else { window.location.href='?cat=facility&page=gdrg/icd10_dict.php'; }">
                    Return to Dictionary
                </button>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <div class="header-bar">
        <h2><i class="fa-solid fa-book-medical"></i> Add New ICD-10 Code</h2>
        <button onclick="if(typeof loadContent === 'function') { loadContent('ICD-10 Dictionary', 'gdrg/icd10_dict.php', 'Dictionary', 'facility', null, true); } else { window.location.href='?cat=facility&page=gdrg/icd10_dict.php'; }" class="btn-back">
            <i class="fa-solid fa-arrow-left"></i> Back to Dictionary
        </button>
    </div>

    <?php if ($error_msg): ?>
        <div style="background: #fee2e2; color: #991b1b; padding: 15px; border-radius: 8px; margin-bottom: 20px; font-weight: 600;"><i class="fa-solid fa-triangle-exclamation"></i> <?php echo $error_msg; ?></div>
    <?php endif; ?>

    <form method="POST" action="../../dripend/systemSetup/gdrg/processAddIcd.php">
        <div class="form-card">
            <div class="form-card-header">
                <i class="fa-solid fa-file-signature"></i> Diagnosis Details
            </div>
            <div class="form-card-body">
                <div class="grid-2">
                    <div class="form-group">
                        <label>ICD-10 Code</label>
                        <input type="text" name="code" class="form-control" placeholder="e.g., A00.9" required style="text-transform: uppercase;">
                    </div>
                    <div class="form-group">
                        <label>Diagnosis Description</label>
                        <input type="text" name="description" class="form-control" placeholder="e.g., Cholera, unspecified" required>
                    </div>
                </div>
            </div>
        </div>

        <button type="submit" class="btn-submit">
            <i class="fa-solid fa-floppy-disk"></i> Save ICD-10 Code
        </button>
        <div style="height: 50px;"></div>
    </form>
</div>