<x-admin.layout>
    <x-slot name="title">Civil Local Self-Government Nomination Certificate for State Food License/राज्य खाद्य परवान्या
        करिता नागरी स्थानिक स्वराज्य संस्थांचा नाहरकत दाखला</x-slot>
    <x-slot name="heading">Civil Local Self-Government Nomination Certificate for State Food License/राज्य खाद्य परवान्या
        करिता नागरी स्थानिक स्वराज्य संस्थांचा नाहरकत दाखला</x-slot>

    <!-- Add Form -->
    <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="col-md-4">
                            <label class="col-form-label" for="zone">Zone / झोन<span class="text-danger">*</span></label>
                            <select class="form-select" name="zone" id="zone" >
                                <option value="">Select Zone</option>
                                @foreach ($zones as $zone)
                                    <option @if ($changeNursingLicense->zone == $zone->name) selected @endif value="{{ $zone->name }}">{{ $zone->name }}</option>
                                @endforeach
                            </select>
                            <span class="text-danger is-invalid zone_err"></span>
                        </div> --}}
                        <div class="mb-3 row">
                            <input type="hidden" model-id="id" value="{{ $status->id }}">

                            <div class="col-md-4">
                                <label class="col-form-label" for="first_name">First Name <span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="first_name" name="first_name" type="text"
                                    placeholder="Enter First Name" value="{{ $civillocalselfgovernment->first_name }}"
                                    disabled>
                                <span class="text-danger is-invalid first_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label class="col-form-label" for="middle_name">Middle Name <span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="middle_name" name="middle_name" type="text"
                                    placeholder="Enter Middle Name" value="{{ $civillocalselfgovernment->middle_name }}"
                                    disabled>
                                <span class="text-danger is-invalid middle_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label class="col-form-label" for="last_name">Last Name <span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="last_name" name="last_name" type="text"
                                    placeholder="Enter Last Name" value="{{ $civillocalselfgovernment->last_name }}"
                                    disabled>
                                <span class="text-danger is-invalid last_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label class="col-form-label" for="marathi_first_name">प्रथम नाव (मराठी) <span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="marathi_first_name" name="marathi_first_name"
                                    type="text" placeholder="नाव प्रविष्ट करा प्रथम"
                                    value="{{ $civillocalselfgovernment->marathi_first_name }}" disabled>
                                <span class="text-danger is-invalid marathi_first_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label class="col-form-label" for="marathi_middle_name">मधले नाव (मराठी)<span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="marathi_middle_name" name="marathi_middle_name"
                                    type="text" placeholder="प्रविष्ट करा मधले नाव"
                                    value="{{ $civillocalselfgovernment->marathi_middle_name }}" disabled>
                                <span class="text-danger is-invalid marathi_middle_name_err"></span>
                            </div>

                            <div class="col-md-4">
                                <label class="col-form-label" for="marathi_last_name">आडनाव (मराठी) <span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="marathi_last_name" name="marathi_last_name"
                                    type="text" placeholder="आडनाव प्रविष्ट करा"
                                    value="{{ $civillocalselfgovernment->marathi_last_name }}" disabled>
                                <span class="text-danger is-invalid marathi_last_name_err"></span>
                            </div>


                            <div class="col-md-4">
                                <label class="col-form-label" for="mobile_num">Mobile Number</label>
                                <input class="form-control" id="mobile_num" name="mobile_num" type="text"
                                    oninput="this.value = this.value.replace(/\D/g, '')" maxlength="10" minlength="10"
                                    placeholder="Enter Mobile Number"
                                    value="{{ $civillocalselfgovernment->mobile_num }}" disabled>
                                <span class="text-danger is-invalid mobile_num_err"></span>
                            </div>
                            <div class="col-md-4">
                                <label class="col-form-label" for="adhar_num">Aadhar Card No</label>
                                <input class="form-control" id="adhar_num" name="adhar_num" type="text"
                                    oninput="this.value = this.value.replace(/\D/g, '')" maxlength="12"
                                    minlength="12" placeholder="Enter Aadhar  Card Number"
                                    value="{{ $civillocalselfgovernment->adhar_num }}" disabled>
                                <span class="text-danger is-invalid adhar_num_err"></span>
                            </div>
                            <div class="col-md-4">
                                <label class="col-form-label" for="email">Email<span
                                        class="text-danger">*</span></label>
                                <input class="form-control" id="email" name="email" type="email"
                                    placeholder="Enter Email" value="{{ $civillocalselfgovernment->email }}" disabled>
                                <span class="text-danger is-invalid email_err"></span>
                            </div>
                            <div class="col-md-5">
                                <label class="col-form-label" for="address"> Address <span
                                        class="text-danger">*</span></label>
                                <textarea class="form-control" name="address" id="address" cols="30" rows="2"
                                    placeholder="Enter Address" disabled>{{ $civillocalselfgovernment->address }}</textarea>
                                <span class="text-danger is-invalid address_err"></span>
                            </div>

                            <div class="col-md-5">
                                <label class="col-form-label" for="marathi_address"> पत्ता (मराठी) <span
                                        class="text-danger">*</span></label>
                                <textarea class="form-control" name="marathi_address" id="marathi_address" cols="30" rows="2"
                                    placeholder="पत्ता" value="" disabled>{{ $civillocalselfgovernment->marathi_address }}</textarea>
                                <span class="text-danger is-invalid marathi_address_err"></span>
                            </div>


                            <div class="col-md-5">
                                <label class="col-form-label" for="purpose">Purpose <span
                                        class="text-danger">*</span></label>
                                <textarea class="form-control" name="purpose" id="purpose" cols="30" rows="2"
                                    placeholder="Enter purpose" value="" disabled>{{ $civillocalselfgovernment->purpose }}</textarea>
                                <span class="text-danger is-invalid purpose_err"></span>
                            </div>

                            <div class="col-md-5">
                                <label class="col-form-label" for="marathi_purpose">उद्देश (मराठी) <span
                                        class="text-danger">*</span></label>
                                <textarea class="form-control" name="marathi_purpose" id="marathi_purpose" cols="30" rows="2"
                                    placeholder="उद्देश" value="" disabled>{{ $civillocalselfgovernment->marathi_purpose }}</textarea>
                                <span class="text-danger is-invalid marathi_purpose_err"></span>
                            </div>

                            <div class="col-md-4 mb-2">
                                <label class="col-form-label" for="prescribed_formats">Upload Application In
                                    Prescribed Format / विहित नमुन्यातील अर्ज अपलोड करा <span
                                        class="text-danger">*</span></label>
                                <div><a href="{{ asset('storage/' . $civillocalselfgovernment->prescribed_format) }}"
                                        target="_blank">View Document</a></div>
                                <span class="text-danger is-invalid prescribed_formats_err"></span>
                            </div>

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

                            {{-- <label class="col-form-label" for="is_correct_info">Declaration / घोषणापत्र:</label>
                            <div class="col-md-12">
                                <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>
<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'
                });
            }
        });
    });
</script>


{{-- Add --}}
<script>
    $("#addForm").submit(function(e) {
        e.preventDefault();
        $("#addSubmit").prop('disabled', true);


        var formdata = new FormData(this);
        var updateUrl = '{{ route('civil-local-self-government.update', $civillocalselfgovernment->id) }}';
        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>
