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

// Sniff application baseline directories 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: #dc2626; }
.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; }

/* Color Specific Row Alignment */
.lis-color-picker-wrap { display: flex; gap: 10px; align-items: center; }
.lis-color-input { width: 50px; height: 42px; padding: 2px; border-radius: 8px; border: 1px solid #cbd5e1; cursor: pointer; background: #f8fafc; }

/* 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; }
</style>

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

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onload="
    if (typeof window.initAddLabSample === 'function') {
        window.initAddLabSample();
    } else {
        const script = document.createElement('script');
        script.src = window.rimaxAppRoot + '/assets/js/AddLabSample.js?v=' + new Date().getTime();
        script.onload = () => window.initAddLabSample();
        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-vial-circle-check"></i> Register Specimen / Sample Type</div>
        <span class="lis-page-subtitle">Configure diagnostic sample parameters, tube specifications, and default extraction profiles.</span>

        <div id="addSampleErrorAlert" class="lis-alert lis-alert-error"></div>
        <div id="addSampleSuccessAlert" class="lis-alert lis-alert-success"></div>

        <form id="addLabSampleForm" novalidate>
            
            <div class="lis-section-title"><i class="fa-solid fa-fingerprint"></i> Core Descriptions</div>
            <div class="lis-form-row">
                <div class="lis-form-col" style="flex: 2;">
                    <label for="sample_name">Specimen Category Name <span style="color:#dc2626;">*</span></label>
                    <input type="text" name="sample_name" id="sample_name" class="lis-input" placeholder="e.g., Whole Blood, Serum, Plasma, Mid-Stream Urine, CSF" required>
                </div>
                <div class="lis-form-col" style="flex: 1;">
                    <label for="default_source">Extraction Method / Source</label>
                    <input type="text" name="default_source" id="default_source" class="lis-input" placeholder="e.g., Venous Puncture, Swab, Catheter">
                </div>
            </div>

            <div class="lis-section-title"><i class="fa-solid fa-box-tissue"></i> Container & Phlebotomy Details</div>
            <div class="lis-form-row">
                <div class="lis-form-col" style="flex: 2;">
                    <label for="container_type">Collection Container / Tube Spec</label>
                    <input type="text" name="container_type" id="container_type" class="lis-input" placeholder="e.g., EDTA Tube, Lithium Heparin, Plain Top, Sterile Cup">
                </div>
                <div class="lis-form-col" style="flex: 1;">
                    <label for="container_color">Visual Cap Color</label>
                    <div class="lis-color-picker-wrap">
                        <input type="color" name="container_color" id="container_color" class="lis-color-input" value="#cbd5e1">
                        <span style="font-size: 12px; color: #64748b;">Tap to match tube cap color</span>
                    </div>
                </div>
            </div>

            <div class="lis-form-row">
                <div class="lis-form-col" style="flex: 1;">
                    <label for="required_volume">Required Volumetric Quantity</label>
                    <input type="text" name="required_volume" id="required_volume" class="lis-input" value="Standard" placeholder="e.g., 3-5 mL, 20 mL, 1 Drop">
                </div>
                <div class="lis-form-col" style="flex: 1;"></div>
            </div>

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