<?php
/**
 * KCB Medical Scheme Pre-Authorization Form
 * Comprehensive template for KCB Medical Scheme
 */

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

<div class="form-section insurer-specific-section" id="kcbFormFields">
    <div class="form-section-title">
        <i class="ri-shield-check-line"></i> KCB Medical Scheme Pre-Authorization Details
    </div>

    <!-- Member & Policy Details -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-user-follow-line me-2"></i>Member / Policy Details</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Patient's Relationship to Principal Member</label>
                        <select class="form-select" name="preauth_data[relationship]" required>
                            <option value="">Select Relationship</option>
                            <option value="Principal">Principal Member</option>
                            <option value="Spouse">Spouse</option>
                            <option value="Child">Child</option>
                            <option value="Parent">Parent</option>
                            <option value="Other">Other</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Employer / Corporate Scheme Name</label>
                        <input type="text" class="form-control" name="preauth_data[employer_name]"
                            placeholder="e.g. Kenya Power, Teachers Service Commission">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Enhanced Clinical Information -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-stethoscope-line me-2"></i>Clinical Details</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Presenting Complaints & History</label>
                        <textarea class="form-control" name="preauth_data[clinical_history]" rows="3" required
                            placeholder="Chief complaints, duration of symptoms, and relevant history"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Physical Examination Findings</label>
                        <textarea class="form-control" name="preauth_data[exam_findings]" rows="3"
                            placeholder="Key findings from physical examination"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Investigations Done / Results</label>
                        <textarea class="form-control" name="preauth_data[investigations]" rows="2"
                            placeholder="Lab results, Imaging reports, etc."></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Inpatient / Admission Details -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-hospital-line me-2"></i>Admission / Procedure Details</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Room Type Required</label>
                        <select class="form-select" name="preauth_data[room_type]">
                            <option value="">Select Room</option>
                            <option value="General Ward">General Ward</option>
                            <option value="Semi-Private">Semi-Private</option>
                            <option value="Private">Private Wing</option>
                            <option value="HDU">HDU</option>
                            <option value="ICU">ICU</option>
                            <option value="Day Case">Day Case / Outpatient</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Expected Length of Stay (Days)</label>
                        <input type="number" class="form-control" name="preauth_data[expected_stay]" min="0"
                            placeholder="e.g. 3">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Proposed Admission Date</label>
                        <input type="date" class="form-control" name="preauth_data[admission_date]"
                            value="<?php echo date('Y-m-d'); ?>">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Professional Fees Breakdown -->
    <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>Professional Fees Breakdown (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">Assistant Surgeon</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[assistant_surgeon_fee]" placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <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-3">
                    <label class="form-label text-muted small">Theatre / Procedure 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">Accommodation Total</label>
                    <input type="number" step="0.01" class="form-control fee-input"
                        name="preauth_data[accommodation_fee]" placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <label class="form-label text-muted small">Drugs & Consumables</label>
                    <input type="number" step="0.01" class="form-control fee-input" name="preauth_data[consumables_fee]"
                        placeholder="0.00">
                </div>
                <div class="col-md-3">
                    <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_fees]"
                        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="kcbTotalDisplay">KES 0.00</h4>
                        <input type="hidden" name="preauth_data[total_breakdown]" id="kcbTotal">
                    </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">
                    I hereby confirm that the information provided above is accurate and true to the best of my
                    knowledge.
                    I understand that this pre-authorization is subject to the terms and conditions of the
                    KCB Medical Scheme policy.
                </label>
            </div>
        </div>
    </div>
</div>

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

        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);

            // Also update the main form estimated cost if it's empty
            const mainCostInput = document.querySelector('input[name="estimated_cost"]');
            if (mainCostInput && (mainCostInput.value === '' || mainCostInput.value == 0)) {
                mainCostInput.value = total.toFixed(2);
                // Trigger input event to update requested amount
                mainCostInput.dispatchEvent(new Event('input'));
            }
        }

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