<x-admin.layout>
    <x-slot name="title">Issuance of death certificate/ मृत्यू दाखला मिळविणेबाबत.</x-slot>
    <x-slot name="heading">Issuance of death certificate/ मृत्यू दाखला मिळविणेबाबत.</x-slot>

    <div class="row" id="addContainer">
        <div class="col-sm-12">
            <div class="card">
                <form class="theme-form" name="addForm" id="addForm" enctype="multipart/form-data">
                    @csrf

                    <div class="card-header">
                        <h4 class="card-title">Edit Details</h4>
                    </div>

                    <div class="card-body">
                        <div class="mb-3 row">
                        <input type="hidden" model-id="id" value="{{ $status->id }}">
                            {{-- Applicant Details --}}
                            <h5 class="mb-3">Details of Applicant / अर्जदाराची माहिती</h5>

                            <div class="col-md-4">
                                <label for="surname">Surname / आडनाव<span class="text-danger">*</span></label>
                                <input type="text" name="surname" id="surname" class="form-control" placeholder="Enter applicant surname" value="{{ $insuranceofdeathreading->surname }}" readonly>
                                <span class="text-danger is-invalid surname_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="name">Name / नाव <span class="text-danger">*</span></label>
                                <input type="text" name="name" id="name" class="form-control" placeholder="Enter applicant name" value="{{ $insuranceofdeathreading->name }}" readonly>
                                <span class="text-danger is-invalid name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="father_name">Father Name / वडिलांचे नाव <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="father_name" name="father_name" placeholder="Enter Father Name" value="{{ $insuranceofdeathreading->father_name }}" readonly>
                                <span class="text-danger is-invalid father_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="address">Address / पत्ता <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="address" name="address" placeholder="Enter Mother Name" value="{{ $insuranceofdeathreading->address }}" readonly>
                                <span class="text-danger is-invalid address_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="society_name">Society Name/ सोसायटीचे नाव<span class="text-danger">*</span></label>
                                <input type="text" name="society_name" id="society_name" class="form-control" placeholder="Enter society_name" value="{{ $insuranceofdeathreading->society_name }}" readonly>
                                <span class="text-danger is-invalid society_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="block_no">Block Number / ब्लॉक क्रमांक<span class="text-danger">*</span></label>
                                <input type="text" name="block_no" id="block_no" class="form-control" placeholder="Enter block_no" value="{{ $insuranceofdeathreading->block_no }}" readonly>
                                <span class="text-danger is-invalid block_no_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="road_name">Road Name / रस्त्याचे नाव<span class="text-danger">*</span></label>
                                <input type="text" name="road_name" id="road_name" class="form-control" placeholder="Enter road_name" value="{{ $insuranceofdeathreading->road_name }}" readonly>
                                <span class="text-danger is-invalid road_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="place">Place / जागा<span class="text-danger">*</span></label>
                                <input type="text" name="place" id="place" class="form-control" placeholder="Enter place" value="{{ $insuranceofdeathreading->place }}" readonly>
                                <span class="text-danger is-invalid place_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="ward">Ward Area / प्रभाग क्षेत्र<span class="text-danger">*</span></label>
                                <select class="form-select" name="ward" id="ward" disabled>
                                    <option value="">Select Ward Area</option>
                                    @foreach($wards as $ward)
                                        <option value="{{ $ward->name }}" {{ ($insuranceofdeathreading->ward_no == $ward->id) ? 'selected' : '' }}>
                                            {{ $ward->name }}
                                        </option>
                                    @endforeach
                                </select>
                                <span class="text-danger is-invalid ward_err"></span>
                            </div>

                             <div class="col-md-4">
                                <label for="mbmc_elec_ward_no">Mbmc electric ward number / Mbmc इलेक्ट्रिक वार्ड क्रमांक<span class="text-danger">*</span></label>
                                <input type="text" name="mbmc_elec_ward_no" id="mbmc_elec_ward_no" class="form-control" placeholder="Enter pincode number" value="{{ $insuranceofdeathreading->mbmc_elec_ward_no }}" readonly>
                                <span class="text-danger is-invalid mbmc_elec_ward_no_err"></span>
                            </div>

                             <div class="col-md-4">
                                <label for="mobile_no">Contact Number/संपर्क क्रमांक<span class="text-danger">*</span></label>
                                <input type="text" name="mobile_no" id="mobile_no" class="form-control" placeholder="Enter contact no" value="{{ $insuranceofdeathreading->mobile_no }}" readonly>
                                <span class="text-danger is-invalid mobile_no_err"></span>
                            </div>

                             {{-- <div class="col-md-4">
                                <label for="mbmc_elec_ward_no">MBMC Electric Ward Number / MBMC इलेक्ट्रिक वार्ड क्रमांक <span class="text-danger">*</span></label>
                                <input type="text" name="mbmc_elec_ward_no" id="mbmc_elec_ward_no" class="form-control" placeholder="Enter mbmc_elec_ward_no" value="{{ $insuranceofdeathreading->mbmc_elec_ward_no }}" readonly>
                                <span class="text-danger is-invalid mbmc_elec_ward_no_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="contact_no">Contact Number/संपर्क क्रमांक<span class="text-danger">*</span></label>
                                <input type="text" name="contact_no" id="contact_no" class="form-control" placeholder="Enter contact_no" value="{{ $insuranceofdeathreading->contact_no }}">
                                <span class="text-danger is-invalid contact_no_err"></span>
                            </div> --}}


                            {{-- Property Details --}}
                            <h5 class="mt-4 mb-3">Deatails Of Deceased / मृतांचा तपशील</h5>

                            <div class="col-md-4">
                                <label for="date_of_death">Date of Death/मृत्यूची तारीख<span class="text-danger">*</span></label>
                                <input class="form-control" name="date_of_death" id="date_of_death" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->date_of_death }}" readonly>
                                <span class="text-danger is-invalid date_of_death_err"></span>
                            </div>

                            {{-- <div class="col-md-6">
                                <label for="father_full_name">Father Full Name<span class="text-danger">*</span></label>
                                <input class="form-control" name="father_full_name" id="father_full_name" rows="2" placeholder="Enter property type" vlaue="{{ $insuranceofdeathreading->father_full_name }}">
                                <span class="text-danger is-invalid father_full_name_err"></span>
                            </div> --}}

                            <div class="col-md-4">
                                <label for="name_of_deceased">Name Of Deceased / मृत व्यक्तीचे नाव<span class="text-danger">*</span></label>
                                <input class="form-control" name="name_of_deceased" id="name_of_deceased" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->name_of_deceased }}" readonly>
                                <span class="text-danger is-invalid name_of_deceased_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label for="name_in_english">Name In English/इंग्रजीत नाव<span class="text-danger">*</span></label>
                                <input class="form-control" name="name_in_english" id="name_in_english" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->name_in_english }}" readonly>
                                <span class="text-danger is-invalid name_in_english_err"></span>
                            </div>
                            {{-- <div class="col-md-6">
                                <label for="birth_place">Birth Place/जन्मस्थान<span class="text-danger">*</span></label>
                                <input class="form-control" name="birth_place" id="birth_place" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->birth_place }}">
                                <span class="text-danger is-invalid birth_place_err"></span>
                            </div>
                            <div class="col-md-6">
                                <label for="birth_date">Birth Date/जन्मतारीख<span class="text-danger">*</span></label>
                                <input class="form-control" name="birth_date" id="birth_date" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->birth_date }}">
                                <span class="text-danger is-invalid birth_date_err"></span>
                            </div> --}}
                            <div class="col-md-4">
                                <label for="gender">Gender/लिंग<span class="text-danger">*</span></label>
                                <select class="form-select" name="gender" id="gender" disabled>
                                    <option value="" disabled>-- Select --</option>
                                    <option value="Female" {{ $insuranceofdeathreading->gender == 'Female' ? 'selected' : '' }}>Female</option>
                                    <option value="Male" {{ $insuranceofdeathreading->gender == 'Male' ? 'selected' : '' }}>Male</option>
                                    <option value="Other" {{ $insuranceofdeathreading->gender == 'Other' ? 'selected' : '' }}>Other</option>
                                </select>
                                <span class="text-danger is-invalid gender_err"></span>
                            </div>
                             <div class="col-md-4">
                                <label for="age">Age/वय<span class="text-danger">*</span></label>
                                <input class="form-control" name="age" id="age" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->age }}" readonly>
                                <span class="text-danger is-invalid age_err"></span>
                            </div>
                            <div class="col-md-4">
                                <label for="place_of_death">Place Of Death/मृत्यूचे ठिकाण<span class="text-danger">*</span></label>
                                <input class="form-control" name="place_of_death" id="place_of_death" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->place_of_death }}" readonly>
                                <span class="text-danger is-invalid place_of_death_err"></span>
                            </div>
                             <div class="col-md-4">
                                <label for="permanenent_address">Place Of Death Address/मृत्यूचे ठिकाण / पत्ता<span class="text-danger">*</span></label>
                                <input class="form-control" name="permanenent_address" id="permanenent_address" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->permanenent_address }}" readonly>
                                <span class="text-danger is-invalid permanenent_address_err"></span>
                            </div>
                              {{-- <div class="col-md-6">
                                <label for="no_of_certificate">Upload No. Of Certificate/प्रमाणपत्राची संख्या अपलोड करा<span class="text-danger">*</span></label>
                                <input class="form-control" name="no_of_certificate" id="no_of_certificate" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->no_of_certificate }}">
                                <span class="text-danger is-invalid no_of_certificate_err"></span>
                            </div> --}}


                            {{-- Document Uploads --}}
                            <h5 class="mt-4 mb-3">Document Uploads / दस्तऐवज अपलोड करा</h5>

                            <div class="col-md-4">
                                <label for="no_of_certificate">No. Of Certificate/प्रमाणपत्राची संख्या <span class="text-danger">*</span></label>
                                <input class="form-control" name="no_of_certificate" id="no_of_certificate" rows="2" placeholder="Enter property type" value="{{ $insuranceofdeathreading->no_of_certificate }}" readonly>
                                <span class="text-danger is-invalid no_of_certificate_err"></span>
                            </div>
                            <!-- <div class="col-md-6" id="upload_section" style="display:block;">
                                    <label class="col-form-label" for="uploaded_applications">Upload Application In Prescribed Format / विहित नमुन्यातील अर्ज अपलोड करा <span class="text-danger">*</span></label>
                                    @if($insuranceofdeathreading->uploaded_application)
                                     <a href="{{ asset('storage/' . $insuranceofdeathreading->uploaded_application) }}" target="_blank" title="View Plan Copy">View Doc</a>
                                    @endif
                                    <input class="form-control" id="uploaded_applications" name="uploaded_applications" type="file" >
                                    <span class="text-danger is-invalid uploaded_applications_err"></span>
                            </div> -->
                            <div class="col-md-6">
                                    <label for="upload_section">Document Uploaded<span class="text-danger">*</span></label>
                                    @if($insuranceofdeathreading->uploaded_application)
                                        {{-- CASE 1: Document is uploaded - Show View Button --}}
                                        <div class="d-flex align-items-center">
                                            <a href="{{ asset('storage/' . $insuranceofdeathreading->uploaded_application) }}" target="_blank" class="btn btn-sm btn-success">
                                                View Document</a></div>
                                        <input type="hidden" name="existing_upload_section" value="{{ $insuranceofdeathreading->uploaded_application }}">
                                    @else
                                        <input type="file" name="upload_section" id="upload_section" class="form-control" accept="image/*">
                                        <p class="mt-1 mb-0 text-danger">No Document Uploaded</p>
                                    @endif
                                    <span class="text-danger is-invalid upload_section_err"></span>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <label for="remark">Remark<span class="text-danger">*</span></label>
                                <textarea id="Remark" name="remark" maxlength="255" class="form-control" rows="4" required></textarea>
                            </div>

                            {{-- <div class="col-md-3">
                                <label for="side_visit_geotag_pic">Site Visit Geotag Photo / साईट व्हिजिट फोटो <span class="text-danger">*</span></label>
                                <input type="file" name="side_visit_geotag_pic" id="side_visit_geotag_pic" class="form-control" accept="image/*">
                                @if($insuranceofdeathreading->side_visit_geotag_pic)
                                    <a href="{{ asset('storage/' . $insuranceofdeathreading->side_visit_geotag_pic) }}" target="_blank" title="Site Visit Photo">View Doc</a>
                                @endif
                                <span class="text-danger is-invalid side_visit_geotag_pic_err"></span>
                            </div>

                            <div class="col-md-3">
                                <label for="property_doc">Property Document / मालमत्तेचे दस्तऐवज<span class="text-danger">*</span></label>
                                <input type="file" name="property_doc" id="property_doc" class="form-control" accept="image/*,.pdf">
                                @if($insuranceofdeathreading->property_doc)
                                    <a href="{{ asset('storage/' . $insuranceofdeathreading->property_doc) }}" target="_blank" title="Property Doc">View Doc</a>
                                @endif
                                <span class="text-danger is-invalid property_doc_err"></span>
                            </div> --}}

                            {{-- Declaration --}}
                            {{-- <div class="col-md-12 mt-4">
                                <label for="is_correct_info">Declaration / घोषणापत्र:</label>
                                <div class="form-check d-flex align-items-start">
                                    <input type="checkbox" class="form-check-input mt-1" id="is_correct_info" name="is_correct_info" checked value="yes">
                                    <label class="form-check-label ms-2" for="is_correct_info">
                                        "All information provided above is correct and I shall be fully responsible for any discrepancy. <br>
                                        वरील पुरविलेली सर्व माहिती ही अचूक असून, त्यात कुठल्याही प्रकारची तफावत आढळल्यास त्यास मी पूर्णतः जबाबदार असेन."
                                    </label>
                                </div>
                                <span class="text-danger is-invalid is_correct_info_err"></span>
                            </div> --}}

                        </div>
                    </div>

                    <div class="card-footer">
                         <button type="button" class="btn btn-primary approveBtn" data-id="{{ $status->id }}">Approve</button>
                        <button type="button" class="btn btn-danger rejectBtn" data-id="{{ $status->id }}">Reject</button>
                        <button id="btnCancel" class="btn btn-warning">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</x-admin.layout>


<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
{{-- update --}}
<script>

    document.getElementById('btnCancel').addEventListener('click', function() {
        window.location.href = "{{ route('pendinglist') }}";
    });


    $(document).ready(function() {
        $('.approveBtn').on('click', function() {
            let statusId = $(this).data('id');
            let remark = $('#Remark').val();

            if (remark.trim() !== '') {
                $.ajax({
                    url: '/approve-item/' + statusId,
                    type: 'POST',
                    data: {
                        _token: '{{ csrf_token() }}',
                        remark: remark
                    },
                    success: function(response) {
                        Swal.fire({
                            icon: 'success',
                            title: 'Success!',
                            text: response.message,
                            confirmButtonText: 'OK'
                        }).then(() => {
                            window.location.href = response.redirect_url;
                        });
                    },
                    error: function(xhr) {
                        Swal.fire({
                            icon: 'error',
                            title: 'Error!',
                            text: 'Error: ' + xhr.responseText,
                            confirmButtonText: 'OK'
                        });
                    }
                });
            } else {
                Swal.fire({
                    icon: 'warning',
                    title: 'Warning!',
                    text: 'Please Add Remark',
                    confirmButtonText: 'OK'
                });
            }
        });
    });


    $(document).ready(function() {
        $('.rejectBtn').on('click', function() {
            var statusId = $(this).data('id');
            let remark = $('#Remark').val();

            if (remark !== null && remark.trim() !== '') {
                $.ajax({
                    url: '/reject-item/' + statusId,
                    type: 'POST',
                    data: {
                        _token: '{{ csrf_token() }}',
                        remark: remark
                    },
                    success: function(response) {
                        Swal.fire({
                            icon: 'success',
                            title: 'Success!',
                            text: response.message,
                            confirmButtonText: 'OK'
                        }).then(() => {
                            window.location.href = response.redirect_url;
                        });
                    },
                    error: function(xhr) {
                        Swal.fire({
                            icon: 'error',
                            title: 'Error!',
                            text: 'Error: ' + xhr.responseText,
                            confirmButtonText: 'OK'
                        });
                    }
                });
            } else {
                Swal.fire({
                    icon: 'warning',
                    title: 'Warning!',
                    text: 'Please Add Remark',
                    confirmButtonText: 'OK'
                });
            }
        });
    });


    $("#addForm").submit(function(e) {
        e.preventDefault();
        $("#addSubmit").prop('disabled', true);


        var formdata = new FormData(this);
        var updateUrl = '{{ route('death-insurance.update', $insuranceofdeathreading->id) }}'; // 👈 FIXED
        formdata.append('_method', 'PUT');
        $.ajax({
            url: updateUrl,
            type: 'POST',
            data: formdata,
            contentType: false,
            processData: false,
            beforeSend: function() {
                $('#preloader').css('opacity', '0.5');
                $('#preloader').css('visibility', 'visible');
            },
            success: function(data) {
                $("#addSubmit").prop('disabled', false);
                if (!data.error)
                    swal("Successful!", data.success, "success")
                    .then((action) => {
                        window.location.href = '{{ route('my-application') }}';
                    });
                else
                    swal("Error!", data.error, "error");
            },
            statusCode: {
                422: function(responseObject, textStatus, jqXHR) {
                    $("#addSubmit").prop('disabled', false);
                    resetErrors();
                    printErrMsg(responseObject.responseJSON.errors);
                },
                500: function(responseObject, textStatus, errorThrown) {
                    $("#addSubmit").prop('disabled', false);
                    swal("Error occured!", "Something went wrong please try again", "error");
                }
            },
            complete: function() {
                $('#preloader').css('opacity', '0');
                $('#preloader').css('visibility', 'hidden');
            },
        });

    });
</script>
