<?php
/**
 * MUA Insurance Pre-Authorization Form
 * Based on: MUA PRE AUTH FORM.pdf
 */

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

<div class="form-section insurer-specific-section" id="muaFormFields">
    <div class="form-section-title">
        <i class="ri-shield-user-line"></i> MUA Insurance Pre-Authorization
    </div>

    <!-- Section A: Insured's Details -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Section A: Insured's Details</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Name of Insured / Employer</label>
                        <input type="text" class="form-control" name="preauth_data[insured_name]"
                            placeholder="e.g. MUA Corporate">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Policy / Member Number</label>
                        <input type="text" class="form-control" name="preauth_data[policy_number]"
                            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">Relationship to Insured</label>
                        <select class="form-select" name="preauth_data[relationship]">
                            <option value="Self">Self</option>
                            <option value="Spouse">Spouse</option>
                            <option value="Child">Child</option>
                            <option value="Other">Other</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Section B: Clinical Information -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Section B: Clinical Information (Doctor to complete)</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Symptoms & History of Present Illness</label>
                        <textarea class="form-control" name="preauth_data[history]" rows="3" required
                            placeholder="History of 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="Primary Diagnosis"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Management Plan</label>
                        <select class="form-select" name="preauth_data[management_plan]">
                            <option value="Medical">Medical Management</option>
                            <option value="Surgical">Surgical Procedure</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Name of Procedure (if applicable)</label>
                        <input type="text" class="form-control" name="preauth_data[procedure_name]">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Admission Date</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. 2">
                    </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">General</option>
                            <option value="Private">Private</option>
                            <option value="Semi-Private">Semi-Private</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Section C: Estimated Medical Expenses -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0">Section C: Estimated Medical Expenses (KES)</h6>
        </div>
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-3">
                    <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-3">
                    <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-3">
                    <label class="form-label text-muted small">Hospital Fees (Bed)</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">Theatre Fees</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-3">
                    <label class="form-label text-muted small">Pharmacy/Materials</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[pharmacy_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Lab/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-3">
                    <label class="form-label text-muted small">Assistant Surgeon</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[assist_surgeon_fee]" placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Others</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">Estimated Total:</h5>
                        <h4 class="mb-0 text-primary" id="muaTotalDisplay">KES 0.00</h4>
                        <input type="hidden" name="preauth_data[total_breakdown]" id="muaTotal">
                    </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 information given is true and correct. I agree to
                    the terms and conditions of MUA Insurance.
                </label>
            </div>
        </div>
    </div>

</div>

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

        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>