<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Sniff application base url context dynamically
$requestRoot = '';
if (strpos($_SERVER['SCRIPT_NAME'], '/application/') !== false) {
    $requestRoot = explode('/application/', $_SERVER['SCRIPT_NAME'])[0] . '/application';
} else {
    $requestRoot = dirname(dirname(dirname($_SERVER['SCRIPT_NAME'])));
}
$requestRoot = rtrim($requestRoot, '/\\');
?>

<style>
.lis-add-module { 
    width: 100%; 
    display: flex; 
    justify-content: center; 
    align-items: flex-start; 
    animation: fadeIn 0.3s ease-in-out; 
}

.lis-form-container { 
    width: 100%; 
    max-width: 850px; 
    background: #ffffff; 
    border-radius: 12px; 
    padding: 35px; 
    border: 1px solid #e2e8f0; 
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}

.lis-page-title { font-size: 22px; font-weight: 800; color: #0f172a; margin-bottom: 6px; display: flex; align-items: center; gap: 10px; }
.lis-page-title i { color: #2563eb; }
.lis-page-subtitle { font-size: 14px; color: #64748b; margin-bottom: 25px; display: block; border-bottom: 1px solid #e2e8f0; padding-bottom: 16px; }

.lis-section-title { font-size: 13px; font-weight: 800; color: #1e3a8a; margin: 25px 0 16px 0; display: flex; align-items: center; gap: 8px; border-bottom: 1px solid #e2e8f0; padding-bottom: 8px; letter-spacing: 0.5px; text-transform: uppercase; }

/* Form Grid */
.lis-form-row { display: flex; gap: 20px; margin-bottom: 20px; flex-wrap: wrap; }
.lis-form-col { flex: 1; display: flex; flex-direction: column; gap: 8px; min-width: 200px; }
.lis-form-col label { font-size: 11px; font-weight: 800; color: #475569; text-transform: uppercase; letter-spacing: 0.5px; }

/* Inputs */
.lis-input { padding: 11px 14px; border-radius: 8px; border: 1px solid #cbd5e1; font-size: 14px; outline: none; width: 100%; transition: 0.2s; background: #f8fafc; color: #334155; box-sizing: border-box; }
.lis-input:focus { border-color: #3b82f6; background: #ffffff; box-shadow: 0 0 0 3px #eff6ff; }
.lis-big-text { min-height: 80px; resize: vertical; }

/* Dynamic Sub-Panels */
.lis-dynamic-panel { display: none; padding: 20px; border-radius: 8px; margin-bottom: 20px; animation: slideDown 0.2s ease-out; }
#numeric_ranges_panel { background: #f0fdf4; border: 1px solid #bbf7d0; }
#options_panel { background: #fffbeb; border: 1px solid #fde68a; }

/* Buttons */
.lis-btn-primary { background: #2563eb; color: #ffffff; border: none; padding: 12px 24px; border-radius: 8px; font-weight: 700; font-size: 14px; cursor: pointer; transition: 0.2s; display: inline-flex; align-items: center; gap: 8px; }
.lis-btn-primary:hover { background: #1d4ed8; transform: translateY(-1px); }
.lis-btn-primary:disabled { background: #94a3b8; cursor: not-allowed; transform: none; }

.lis-btn-secondary { background: #ffffff; color: #475569; border: 1px solid #cbd5e1; padding: 12px 24px; border-radius: 8px; font-weight: 700; font-size: 14px; cursor: pointer; transition: 0.2s; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; }
.lis-btn-secondary:hover { background: #f8fafc; color: #0f172a; }

/* Alerts */
.lis-alert { padding: 16px; border-radius: 8px; margin-bottom: 24px; font-size: 14px; font-weight: 600; display: none; align-items: center; gap: 10px; }
.lis-alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.lis-alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }

/* Custom Toggle */
.lis-toggle-wrap { display: flex; align-items: center; gap: 14px; background: #f8fafc; border: 1px solid #e2e8f0; padding: 16px; border-radius: 8px; cursor: pointer; transition: 0.2s; user-select: none; }
.lis-toggle-wrap:hover { background: #f1f5f9; border-color: #cbd5e1; }
.lis-toggle-wrap input { width: 18px; height: 18px; cursor: pointer; accent-color: #2563eb; margin: 0; }
.lis-toggle-text { font-size: 14px; font-weight: 700; color: #334155; }
.lis-toggle-desc { font-size: 12px; font-weight: 400; color: #64748b; display: block; margin-top: 2px; }

@keyframes slideDown { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }
</style>

<script>
    window.rimaxAppRoot = '<?php echo $requestRoot; ?>';
</script>

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onload="
    if (typeof window.initAddLabParameter === 'function') {
        window.initAddLabParameter();
    } else {
        const script = document.createElement('script');
        script.src = window.rimaxAppRoot + '/assets/js/AddLabParameters.js?v=' + new Date().getTime();
        script.onload = () => window.initAddLabParameter();
        document.head.appendChild(script);
    }
    this.remove();
" style="display:none;">

<div class="lis-add-module">
    <div class="lis-form-container">
        <div class="lis-page-title"><i class="fa-solid fa-flask-vial"></i> Create Lab Parameter</div>
        <span class="lis-page-subtitle">Configure a new test metric with reference ranges or drop-down criteria to monitor results.</span>

        <div id="addParamErrorAlert" class="lis-alert lis-alert-error"></div>
        <div id="addParamSuccessAlert" class="lis-alert lis-alert-success"></div>

        <form id="addLabParameterForm" novalidate>
            
            <div class="lis-section-title"><i class="fa-solid fa-circle-info"></i> Base Details</div>
            <div class="lis-form-row">
                <div class="lis-form-col" style="flex: 2;">
                    <label for="parameter_name">Parameter Name / Code <span style="color:#dc2626;">*</span></label>
                    <input type="text" name="parameter_name" id="parameter_name" class="lis-input" placeholder="e.g., White Blood Cells (WBC), Hemoglobin" required>
                </div>
                <div class="lis-form-col" style="flex: 1;">
                    <label for="result_type">Result Input Format <span style="color:#dc2626;">*</span></label>
                    <select name="result_type" id="result_type" class="lis-input" required>
                        <option value="Numeric">Numeric (Reference Ranges)</option>
                        <option value="Options">Qualitative Options (Dropdown)</option>
                        <option value="Text">Free Text (Descriptive)</option>
                    </select>
                </div>
            </div>

            <div id="numeric_ranges_panel" class="lis-dynamic-panel">
                <h4 style="margin:0 0 15px 0; color:#15803d; font-size:13px; display:flex; align-items:center; gap:8px;">
                    <i class="fa-solid fa-venus-mars"></i> Standard Reference Ranges (Numeric Values Only)
                </h4>
                <div class="lis-form-row">
                    <div class="lis-form-col">
                        <label>Male Min Value</label>
                        <input type="number" step="0.01" name="min_male" id="min_male" class="lis-input" placeholder="e.g., 4.50" style="border-color:#bbf7d0; background:#fff;">
                    </div>
                    <div class="lis-form-col">
                        <label>Male Max Value</label>
                        <input type="number" step="0.01" name="max_male" id="max_male" class="lis-input" placeholder="e.g., 11.00" style="border-color:#bbf7d0; background:#fff;">
                    </div>
                </div>
                <div class="lis-form-row">
                    <div class="lis-form-col">
                        <label>Female Min Value</label>
                        <input type="number" step="0.01" name="min_female" id="min_female" class="lis-input" placeholder="e.g., 4.00" style="border-color:#bbf7d0; background:#fff;">
                    </div>
                    <div class="lis-form-col">
                        <label>Female Max Value</label>
                        <input type="number" step="0.01" name="max_female" id="max_female" class="lis-input" placeholder="e.g., 10.00" style="border-color:#bbf7d0; background:#fff;">
                    </div>
                </div>
                <div class="lis-form-row" style="margin-bottom:0;">
                    <div class="lis-form-col">
                        <label>Pediatric/Child Min</label>
                        <input type="number" step="0.01" name="min_child" id="min_child" class="lis-input" placeholder="e.g., 5.00" style="border-color:#bbf7d0; background:#fff;">
                    </div>
                    <div class="lis-form-col">
                        <label>Pediatric/Child Max</label>
                        <input type="number" step="0.01" name="max_child" id="max_child" class="lis-input" placeholder="e.g., 13.00" style="border-color:#bbf7d0; background:#fff;">
                    </div>
                </div>
            </div>

            <div id="options_panel" class="lis-dynamic-panel">
                <h4 style="margin:0 0 15px 0; color:#b45309; font-size:13px; display:flex; align-items:center; gap:8px;">
                    <i class="fa-solid fa-list-check"></i> Dropdown Options Setup
                </h4>
                <div class="lis-form-row" style="margin-bottom:0;">
                    <div class="lis-form-col" style="flex: 2;">
                        <label for="options_list">Options Matrix (Comma Separated)</label>
                        <input type="text" name="options_list" id="options_list" class="lis-input" placeholder="e.g., Positive, Negative, Indeterminate" style="border-color:#fcd34d; background:#fff;">
                    </div>
                    <div class="lis-form-col" style="flex: 1;">
                        <label for="expected_value">Default Expected Result</label>
                        <input type="text" name="expected_value" id="expected_value" class="lis-input" placeholder="e.g., Negative" style="border-color:#fcd34d; background:#fff;">
                    </div>
                </div>
            </div>

            <div class="lis-section-title"><i class="fa-solid fa-microscope"></i> Methodology & Measures</div>
            <div class="lis-form-row">
                <div class="lis-form-col" style="flex: 1;">
                    <label for="unit_of_measure">Unit of Measure</label>
                    <input type="text" name="unit_of_measure" id="unit_of_measure" class="lis-input" placeholder="e.g., g/dL, mmol/L, x10^9/L">
                </div>
                <div class="lis-form-col" style="flex: 2;">
                    <label for="default_technique">Default Laboratory Technique</label>
                    <input type="text" name="default_technique" id="default_technique" class="lis-input" placeholder="e.g., Automated Counter, Manual Microscopy, PCR Quantification">
                </div>
            </div>

            <div class="lis-form-row">
                <div class="lis-form-col">
                    <label for="description">Parameter Brief Description</label>
                    <textarea name="description" id="description" class="lis-input lis-big-text" placeholder="Optional notes outlining the medical context of this laboratory indicator..."></textarea>
                </div>
            </div>

            <div class="lis-form-row">
                <div class="lis-form-col">
                    <label for="lab_instructions">Internal Processing Instructions (Technician View)</label>
                    <textarea name="lab_instructions" id="lab_instructions" class="lis-input lis-big-text" placeholder="Type instructions here regarding sample handling, warm-ups, critical mix times, etc."></textarea>
                </div>
            </div>

            <div class="lis-section-title"><i class="fa-solid fa-shield-halved"></i> Report Policy</div>
            <div class="lis-form-row" style="margin-bottom: 30px;">
                <div class="lis-form-col">
                    <label class="lis-toggle-wrap">
                        <input type="checkbox" name="show_in_phr" id="show_in_phr" value="1" checked>
                        <div>
                            <span class="lis-toggle-text">Publish to Patient Health Reports (PHR)</span>
                            <span class="lis-toggle-desc">When checked, this score renders on printable client sheets and patient portal folders.</span>
                        </div>
                    </label>
                </div>
                <div class="lis-form-col"></div>
            </div>

            <div style="display: flex; justify-content: space-between; align-items: center; padding-top: 24px; border-top: 1px solid #e2e8f0;">
                <button type="button" class="lis-btn-secondary" onclick="if(typeof loadContent === 'function'){ loadContent('Laboratory Settings', 'laboratory/manageLabParameters.php', 'Laboratory', 'system_setup', null, true); }">
                    <i class="fa-solid fa-arrow-left"></i> Back to Setup
                </button>
                <button type="submit" id="btnSubmitLabParam" class="lis-btn-primary">
                    <i class="fa-solid fa-floppy-disk"></i> Save Parameter
                </button>
            </div>
        </form>
    </div>
</div>