<?php
/**
 * Cigna Pre-Authorization Form
 * Based on: Cigna Claim Form.pdf
 */

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

<div class="form-section insurer-specific-section" id="cigFormFields">
    <div class="form-section-title">
        <i class="ri-globe-line"></i> Cigna Pre-Authorization Details
    </div>

    <!-- Patient & Policy -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-user-line me-2"></i>Patient & Policy Details</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Member ID / Policy Number</label>
                        <input type="text" class="form-control" name="preauth_data[member_id]" required
                            placeholder="e.g. 000xxxxxxx"
                            value="<?php echo isset($patient_data) ? htmlspecialchars($patient_data['policy_number'] ?? '') : ''; ?>">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Plan Name / Employer</label>
                        <input type="text" class="form-control" name="preauth_data[plan_name]"
                            placeholder="e.g. Cigna Global Health Options">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Medical Section -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-hospital-line me-2"></i>Medical Information</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Diagnosis / Condition</label>
                        <textarea class="form-control" name="preauth_data[diagnosis]" rows="2" required
                            placeholder="Primary Diagnosis description"></textarea>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Reason for Visit / Symptoms</label>
                        <textarea class="form-control" name="preauth_data[symptoms]" rows="2"
                            placeholder="Briefly describe symptoms and onset date"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Type of Visit</label>
                        <select class="form-select" name="preauth_data[visit_type]">
                            <option value="Initial Consultation">Initial Consultation</option>
                            <option value="Follow-up">Follow-up</option>
                            <option value="Emergency">Emergency</option>
                            <option value="Routine Checkup">Routine Checkup</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Date of Service / Admission</label>
                        <input type="date" class="form-control" name="preauth_data[service_date]"
                            value="<?php echo date('Y-m-d'); ?>">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Treatment & Fees -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-money-dollar-circle-line me-2"></i>Treatment & Expenses (KES)</h6>
        </div>
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label text-muted small">Consultation Fee</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[consultation_fee]" placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Diagnostic Tests (Lab/X-ray)</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[diagnostics_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Procedures / Surgery</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[procedure_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Medication / Rx</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[rx_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Hospital Services</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-4">
                    <label class="form-label text-muted small">Other</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[other_fee]"
                        placeholder="0.00">
                </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">Total Amount:</h5>
                        <h4 class="mb-0 text-primary" id="cigTotalDisplay">KES 0.00</h4>
                        <input type="hidden" name="preauth_data[total_breakdown]" id="cigTotal">
                    </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>Provider Certification:</strong> I certify that the information provided is accurate and the
                    services are medically necessary.
                </label>
            </div>
        </div>
    </div>
</div>

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

        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>