<?php
/**
 * CIC Insurance Pre-Authorization Form Fields
 * Based on CIC PREAUTH.pdf requirements
 */

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

<!-- CIC-Specific Form Fields -->
<div class="form-section insurer-specific-section" id="cicFormFields">
    <div class="form-section-title">
        <i class="ri-shield-check-line"></i> CIC Pre-Authorization Details
    </div>

    <!-- Declaration & Authorization Section -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-file-user-line me-2"></i>Declaration & Authorization</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Declarant Name <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" name="preauth_data[declaration_name]"
                            placeholder="Full name of person making declaration" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Relationship to Patient <span class="text-danger">*</span></label>
                        <select class="form-select" name="preauth_data[declaration_relationship]" required>
                            <option value="">Select Relationship</option>
                            <option value="Self">Self</option>
                            <option value="Spouse">Spouse</option>
                            <option value="Parent">Parent</option>
                            <option value="Child">Child</option>
                            <option value="Guardian">Guardian</option>
                            <option value="Other">Other</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Declaration Date</label>
                        <input type="date" class="form-control" name="preauth_data[declaration_date]"
                            value="<?php echo date('Y-m-d'); ?>">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="preauth_data[declaration_confirmed]"
                            value="1" id="cicDeclarationConfirm" required>
                        <label class="form-check-label" for="cicDeclarationConfirm">
                            I hereby declare that the information provided is true and accurate to the best of my knowledge.
                            I authorize CIC to verify any medical information and to release information to relevant parties.
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Previous Admission History -->
    <div class="card mb-4">
        <div class="card-header bg-light d-flex justify-content-between align-items-center">
            <h6 class="mb-0"><i class="ri-history-line me-2"></i>Previous Admission History</h6>
            <button type="button" class="btn btn-sm btn-outline-primary" onclick="addPreviousAdmission()">
                <i class="ri-add-line"></i> Add Entry
            </button>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="previousAdmissionsTable">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 15%;">Year</th>
                            <th style="width: 25%;">Hospital</th>
                            <th style="width: 30%;">Diagnosis</th>
                            <th style="width: 25%;">Attending Doctor</th>
                            <th style="width: 5%;">Action</th>
                        </tr>
                    </thead>
                    <tbody id="previousAdmissionsBody">
                        <tr class="admission-row">
                            <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][0][year]" placeholder="YYYY"></td>
                            <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][0][hospital]" placeholder="Hospital name"></td>
                            <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][0][diagnosis]" placeholder="Diagnosis"></td>
                            <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][0][doctor]" placeholder="Doctor name"></td>
                            <td><button type="button" class="btn btn-sm btn-outline-danger" onclick="removeAdmissionRow(this)"><i class="ri-delete-bin-line"></i></button></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <small class="text-muted">Add any previous hospital admissions in the last 5 years</small>
        </div>
    </div>

    <!-- Accident Claims Section -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-alarm-warning-line me-2"></i>Accident Claims Information</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Is this claim accident-related?</label>
                        <select class="form-select" name="preauth_data[is_accident_claim]" id="cicAccidentClaim"
                            onchange="toggleAccidentDetails()">
                            <option value="no">No</option>
                            <option value="yes">Yes</option>
                        </select>
                    </div>
                </div>
            </div>

            <div id="accidentDetailsSection" style="display: none;">
                <div class="row">
                    <div class="col-md-12 mb-3">
                        <label class="form-label">Type of Accident</label>
                        <div class="d-flex flex-wrap gap-3">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="preauth_data[accident_type]"
                                    value="RTA" id="accidentRTA">
                                <label class="form-check-label" for="accidentRTA">Road Traffic Accident (RTA)</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="preauth_data[accident_type]"
                                    value="Work" id="accidentWork">
                                <label class="form-check-label" for="accidentWork">Work-related Accident</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="preauth_data[accident_type]"
                                    value="Sports" id="accidentSports">
                                <label class="form-check-label" for="accidentSports">Sports Injury</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="preauth_data[accident_type]"
                                    value="Other" id="accidentOther">
                                <label class="form-check-label" for="accidentOther">Other</label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="mb-3">
                            <label class="form-label">Date of Accident</label>
                            <input type="date" class="form-control" name="preauth_data[accident_date]">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="mb-3">
                            <label class="form-label">Time of Accident</label>
                            <input type="time" class="form-control" name="preauth_data[accident_time]">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="mb-3">
                            <label class="form-label">Police Abstract Number</label>
                            <input type="text" class="form-control" name="preauth_data[police_abstract_no]"
                                placeholder="If applicable">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="mb-3">
                            <label class="form-label">Accident Chronology/Description</label>
                            <textarea class="form-control" name="preauth_data[accident_chronology]" rows="3"
                                placeholder="Describe how the accident occurred"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Radiology Requirements -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-scan-line me-2"></i>Radiology/Imaging Requirements</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12 mb-3">
                    <label class="form-label">Select Required Imaging Studies</label>
                    <div class="d-flex flex-wrap gap-4">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[radiology_mri]"
                                value="1" id="radiologyMRI">
                            <label class="form-check-label" for="radiologyMRI">
                                <i class="ri-scan-2-line me-1"></i> MRI
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[radiology_ct]"
                                value="1" id="radiologyCT">
                            <label class="form-check-label" for="radiologyCT">
                                <i class="ri-scan-line me-1"></i> CT Scan
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[radiology_xray]"
                                value="1" id="radiologyXray">
                            <label class="form-check-label" for="radiologyXray">
                                <i class="ri-pulse-line me-1"></i> X-Ray
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[radiology_ultrasound]"
                                value="1" id="radiologyUltrasound">
                            <label class="form-check-label" for="radiologyUltrasound">
                                <i class="ri-soundcloud-line me-1"></i> Ultrasound
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[radiology_other]"
                                value="1" id="radiologyOther">
                            <label class="form-check-label" for="radiologyOther">Other</label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Imaging Study Details/Justification</label>
                        <textarea class="form-control" name="preauth_data[radiology_details]" rows="2"
                            placeholder="Specify body parts and clinical justification for imaging"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Room & Accommodation -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-hotel-bed-line me-2"></i>Room & Accommodation</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <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="">Select Room Type</option>
                            <option value="General Ward">General Ward</option>
                            <option value="Semi-Private">Semi-Private</option>
                            <option value="Private">Private</option>
                            <option value="ICU">ICU</option>
                            <option value="HDU">HDU</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Room Rate Per Night (KES)</label>
                        <input type="number" step="0.01" class="form-control" name="preauth_data[room_per_night]"
                            placeholder="0.00">
                    </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_days]"
                            min="1" placeholder="0">
                    </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</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Surgeon's Fee (KES)</label>
                        <input type="number" step="0.01" class="form-control" name="preauth_data[surgeon_fee]"
                            placeholder="0.00" onchange="calculateCICTotalFees()">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Physician's Fee (KES)</label>
                        <input type="number" step="0.01" class="form-control" name="preauth_data[physician_fee]"
                            placeholder="0.00" onchange="calculateCICTotalFees()">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Anaesthetist's Fee (KES)</label>
                        <input type="number" step="0.01" class="form-control" name="preauth_data[anaesthetist_fee]"
                            placeholder="0.00" onchange="calculateCICTotalFees()">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Theatre/Procedure Fee (KES)</label>
                        <input type="number" step="0.01" class="form-control" name="preauth_data[theatre_fee]"
                            placeholder="0.00" onchange="calculateCICTotalFees()">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Other Fees (KES)</label>
                        <input type="number" step="0.01" class="form-control" name="preauth_data[other_fees]"
                            placeholder="0.00" onchange="calculateCICTotalFees()">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label"><strong>Total Professional Fees (KES)</strong></label>
                        <input type="number" step="0.01" class="form-control bg-light" name="preauth_data[total_professional_fees]"
                            id="cicTotalProfessionalFees" readonly placeholder="0.00">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Referring Doctor -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-user-heart-line me-2"></i>Referring Doctor Information</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Referring Doctor Name</label>
                        <input type="text" class="form-control" name="preauth_data[referring_doctor_name]"
                            placeholder="Dr. Full Name">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Referring Doctor Contact</label>
                        <input type="text" class="form-control" name="preauth_data[referring_doctor_contact]"
                            placeholder="Phone or email">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Referring Facility</label>
                        <input type="text" class="form-control" name="preauth_data[referring_facility]"
                            placeholder="Hospital/Clinic name">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Other Medical Conditions -->
    <div class="card mb-4">
        <div class="card-header bg-light">
            <h6 class="mb-0"><i class="ri-heart-pulse-line me-2"></i>Other Medical Conditions</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12 mb-3">
                    <label class="form-label">Does the patient have any of the following conditions?</label>
                    <div class="d-flex flex-wrap gap-4">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[condition_diabetes]"
                                value="1" id="conditionDiabetes">
                            <label class="form-check-label" for="conditionDiabetes">Diabetes</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[condition_hypertension]"
                                value="1" id="conditionHypertension">
                            <label class="form-check-label" for="conditionHypertension">Hypertension</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[condition_heart]"
                                value="1" id="conditionHeart">
                            <label class="form-check-label" for="conditionHeart">Heart Disease</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[condition_asthma]"
                                value="1" id="conditionAsthma">
                            <label class="form-check-label" for="conditionAsthma">Asthma</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="preauth_data[condition_other]"
                                value="1" id="conditionOther">
                            <label class="form-check-label" for="conditionOther">Other</label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Please specify other conditions or provide details</label>
                        <textarea class="form-control" name="preauth_data[other_conditions_details]" rows="2"
                            placeholder="Provide details of any other medical conditions"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// CIC Form Specific JavaScript
var cicAdmissionRowCount = 1;

function addPreviousAdmission() {
    var tableBody = document.getElementById('previousAdmissionsBody');
    var newRow = document.createElement('tr');
    newRow.className = 'admission-row';
    newRow.innerHTML = `
        <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][${cicAdmissionRowCount}][year]" placeholder="YYYY"></td>
        <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][${cicAdmissionRowCount}][hospital]" placeholder="Hospital name"></td>
        <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][${cicAdmissionRowCount}][diagnosis]" placeholder="Diagnosis"></td>
        <td><input type="text" class="form-control form-control-sm" name="preauth_data[prev_admissions][${cicAdmissionRowCount}][doctor]" placeholder="Doctor name"></td>
        <td><button type="button" class="btn btn-sm btn-outline-danger" onclick="removeAdmissionRow(this)"><i class="ri-delete-bin-line"></i></button></td>
    `;
    tableBody.appendChild(newRow);
    cicAdmissionRowCount++;
}

function removeAdmissionRow(button) {
    var rows = document.querySelectorAll('#previousAdmissionsBody .admission-row');
    if (rows.length > 1) {
        button.closest('tr').remove();
    } else {
        // Clear the fields instead of removing the last row
        var inputs = button.closest('tr').querySelectorAll('input');
        inputs.forEach(function(input) {
            input.value = '';
        });
    }
}

function toggleAccidentDetails() {
    var isAccident = document.getElementById('cicAccidentClaim').value === 'yes';
    document.getElementById('accidentDetailsSection').style.display = isAccident ? 'block' : 'none';
}

function calculateCICTotalFees() {
    var surgeonFee = parseFloat(document.querySelector('input[name="preauth_data[surgeon_fee]"]').value) || 0;
    var physicianFee = parseFloat(document.querySelector('input[name="preauth_data[physician_fee]"]').value) || 0;
    var anaesthetistFee = parseFloat(document.querySelector('input[name="preauth_data[anaesthetist_fee]"]').value) || 0;
    var theatreFee = parseFloat(document.querySelector('input[name="preauth_data[theatre_fee]"]').value) || 0;
    var otherFees = parseFloat(document.querySelector('input[name="preauth_data[other_fees]"]').value) || 0;

    var total = surgeonFee + physicianFee + anaesthetistFee + theatreFee + otherFees;
    document.getElementById('cicTotalProfessionalFees').value = total.toFixed(2);
}
</script>
