<?php
/**
 * Sedgwick Pre-Authorization Form
 * Based on: sedgwick claim form & Pre-Admission Forms .pdf
 */

// Prevent direct access
if (!defined('PREAUTH_FORM_INCLUDE')) {
    exit('Direct access not allowed');
}
?>

<div class="form-section insurer-specific-section" id="sedFormFields">
    <div class="form-section-title">
        <i class="ri-shield-cross-line"></i> Sedgwick Pre-Authorization Request
    </div>

    <!-- Section: Member/Patient Info -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Member & Patient Information</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Company / Scheme Name</label>
                        <input type="text" class="form-control" name="preauth_data[company_name]"
                            placeholder="e.g. Sedgwick Scheme">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Membership Number</label>
                        <input type="text" class="form-control" name="preauth_data[member_no]"
                            value="<?php echo isset($patient_data) ? htmlspecialchars($patient_data['policy_number'] ?? '') : ''; ?>">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Patient Name</label>
                        <input type="text" class="form-control"
                            value="<?php echo isset($patient_data) ? htmlspecialchars($patient_data['full_name'] ?? '') : ''; ?>"
                            readonly>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Date of Birth</label>
                        <input type="date" class="form-control"
                            value="<?php echo isset($patient_data) ? htmlspecialchars($patient_data['date_of_birth'] ?? '') : ''; ?>"
                            readonly>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Section: Clinical Details -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Clinical Details (Medical Practitioner)</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Diagnosis / Symptoms</label>
                        <textarea class="form-control" name="preauth_data[diagnosis]" rows="2" required
                            placeholder="Diagnosis and presenting symptoms"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">History of Condition</label>
                        <textarea class="form-control" name="preauth_data[history]" rows="2"
                            placeholder="Onset date and duration"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Procedure Code (if any)</label>
                        <input type="text" class="form-control" name="preauth_data[procedure_code]"
                            placeholder="CPT / CCSD Code">
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="mb-3">
                        <label class="form-label">Procedure Description</label>
                        <input type="text" class="form-control" name="preauth_data[procedure_desc]"
                            placeholder="Description of planned procedure">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Admitting Hospital</label>
                        <input type="text" class="form-control" name="preauth_data[hospital_name]"
                            value="Polakis ENT Centre" readonly>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Date of Admission</label>
                        <input type="date" class="form-control" name="preauth_data[admission_date]"
                            value="<?php echo date('Y-m-d'); ?>">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Duration (Days)</label>
                        <input type="number" class="form-control" name="preauth_data[los]" min="1" placeholder="e.g. 1">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Section: Estimated Fees -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Estimated Professional & Facility Fees (KES)</h6>
        </div>
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-3">
                    <label class="form-label text-muted small">Hospital / Facility</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[hospital_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Surgeon</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[surgeon_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Anaesthetist</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[anaesthetist_fee]" placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Medication</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[drugs_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Investigations</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[investigation_fee]" placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Physiotherapy</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[physio_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-6">
                    <label class="form-label text-muted small">Other (Describe)</label>
                    <div class="input-group">
                        <input type="text" class="form-control" name="preauth_data[other_desc]"
                            placeholder="Description" style="flex: 2;">
                        <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[other_fee]"
                            placeholder="0.00" style="flex: 1;">
                    </div>
                </div>
            </div>
            <div class="row mt-4 pt-3 border-top">
                <div class="col-md-6 offset-md-6">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">Estimated Total:</h5>
                        <h4 class="mb-0 text-primary" id="sedTotalDisplay">KES 0.00</h4>
                        <input type="hidden" name="preauth_data[total_breakdown]" id="sedTotal">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Declaration -->
    <div class="card mb-4 mb-4">
        <div class="card-body bg-light rounded">
            <div class="form-check">
                <input class="form-check-input" type="checkbox" name="preauth_data[declaration]" id="declCheck"
                    required>
                <label class="form-check-label" for="declCheck">
                    <strong>Declaration:</strong> I certify that the services are medically indicated and necessary.
                    (Provider Declaration)
                </label>
            </div>
        </div>
    </div>

</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const feeInputs = document.querySelectorAll('#sedFormFields .fee-input');
        const totalDisplay = document.getElementById('sedTotalDisplay');
        const totalInput = document.getElementById('sedTotal');

        function calculateTotal() {
            let total = 0;
            feeInputs.forEach(input => {
                total += parseFloat(input.value) || 0;
            });

            totalDisplay.textContent = 'KES ' + total.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
            totalInput.value = total.toFixed(2);

            const mainCostInput = document.querySelector('input[name="estimated_cost"]');
            if (mainCostInput && (mainCostInput.value === '' || mainCostInput.value == 0)) {
                mainCostInput.value = total.toFixed(2);
                mainCostInput.dispatchEvent(new Event('input'));
            }
        }

        feeInputs.forEach(input => {
            input.addEventListener('input', calculateTotal);
        });
    });
</script>