<?php
/**
 * GA Insurance Pre-Authorization Form
 * Based on: GA Pre- Athorization fom.pdf
 */

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

<div class="form-section insurer-specific-section" id="gaFormFields">
    <div class="form-section-title">
        <i class="ri-shield-check-line"></i> GA Insurance Pre-Authorization
    </div>

    <!-- Part 1: Member's Particulars -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Part 1: Member's Particulars</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Name of Employer / Policyholder</label>
                        <input type="text" class="form-control" name="preauth_data[policyholder]"
                            placeholder="Employer Name">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Membership No.</label>
                        <input type="text" class="form-control" name="preauth_data[membership_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's 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-3">
                    <div class="mb-3">
                        <label class="form-label">Relationship</label>
                        <select class="form-select" name="preauth_data[relationship]">
                            <option value="Principal">Principal</option>
                            <option value="Spouse">Spouse</option>
                            <option value="Child">Child</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-3">
                    <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>

    <!-- Part 2: Medical Practitioner's Report -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Part 2: Medical Practitioner's Report</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Chief Complaints / History</label>
                        <textarea class="form-control" name="preauth_data[complaints]" rows="3" required
                            placeholder="History of present illness"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Diagnosis</label>
                        <textarea class="form-control" name="preauth_data[diagnosis]" rows="2"
                            placeholder="Clinical Diagnosis"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Proposed Procedure / Treatment</label>
                        <input type="text" class="form-control" name="preauth_data[procedure_name]"
                            placeholder="Name of Procedure">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Type of Anaesthesia</label>
                        <select class="form-select" name="preauth_data[anaesthesia_type]">
                            <option value="General">General</option>
                            <option value="Local">Local</option>
                            <option value="Spinal">Spinal / Epidural</option>
                            <option value="Sedation">Sedation</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <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">Expected Stay (Days)</label>
                        <input type="number" class="form-control" name="preauth_data[los]" min="1" placeholder="e.g. 3">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Room Type</label>
                        <select class="form-select" name="preauth_data[room_type]">
                            <option value="General Ward">General Ward</option>
                            <option value="Private">Private</option>
                            <option value="Semi-Private">Semi-Private</option>
                            <option value="ICU/HDU">ICU / HDU</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Part 3: Hospital Estimate of Costs -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Part 3: Hospital Estimate of Costs (KES)</h6>
        </div>
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label text-muted small">Hospital Charges (Bed/Nursing)</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[hospital_charges]" placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Theatre Fee</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[theatre_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Drugs / Pharmacy</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-4">
                    <label class="form-label text-muted small">Surgeon's Fee</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-4">
                    <label class="form-label text-muted small">Anaesthetist's Fee</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-4">
                    <label class="form-label text-muted small">Physician / Pediatrician</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[physician_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Laboratory / Radiology</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[lab_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-4">
                    <label class="form-label text-muted small">Other Charges</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 Estimate:</h5>
                        <h4 class="mb-0 text-primary" id="gaTotalDisplay">KES 0.00</h4>
                        <input type="hidden" name="preauth_data[total_breakdown]" id="gaTotal">
                    </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 above information is correct and complete to the
                    best of my knowledge. I authorize the hospital to release any information required by GA Insurance
                    to process this claim.
                </label>
            </div>
        </div>
    </div>

</div>

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

        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>