@extends('Admin.layouts.master')

@section('content')
    @php
        $currentPage = 'restraunt';
    @endphp
    <style>
        .error-message {
            color: #FF0000;
        }

        form .error-message {
            color: red;
            /* Set your desired color here */
        }

        form .input_class.error-message {
            color: #0e1726;
        }
    </style>
    <div class="layout-px-spacing">
        <div class="middle-content container-xxl p-0">
            <div class="row layout-top-spacing ">
                <div class="top-tabel">
                    <div class="row">
                        <div class="col-md-4">
                            <a class="d-flex align-items-center justify-content-center pl-2"
                                href="{{ route('manage.restaurants') }}">
                                <img class="back-btn" src="{{ asset('public/assets/img/left-arrow.svg') }}">
                                <h6 class="card-title p-0">Edit Details</h6>
                            </a>
                        </div>
                        <div class="col-md-8">

                        </div>
                    </div>
                </div>
                <form method="POST" id="update_voucher" class="form py-5 pb-0" action="javascript:void(0)"
                    enctype="multipart/form-data">
                    {{ csrf_field() }}
                    <input type="hidden" name="id" id="id" value="{{ $restaurantItem->id }}" />
                    <div class="col-xl-12 col-lg-12 col-sm-12  layout-spacing">
                        <div class="widget-content widget-content-area br-8 position-btn h-10">
                            <div class="view-details Article">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Restaurant Name</label>
                                            <input type="text" class="form-control" id="name" name="name"
                                                maxlength="40" value="{{ $restaurantItem->name }}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Restaurant ID</label>
                                            <input type="text" class="form-control" id="restaurant_id"
                                                name="restaurant_id" maxlength="30"
                                                value="{{ $restaurantItem->restaurant_id }}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Image</label>
                                            <input type="file" accept="image/*" class="form-control" name="image"
                                                id="imageInputNormal">
                                            <div id="imageInputPreviewNormal" style="width: 30%;">
                                                <img src="{{ $restaurantItem->image }}" alt="Image Preview"
                                                    style="width: 40%;">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Restaurant Address</label>
                                            <input type="text" class="form-control" id="location_name"
                                                name="location_name" value="{{ $restaurantItem->address }}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Phone Number</label>
                                            <input type="text" class="form-control" id="phone_number" name="phone_number"
                                                value="{{ $restaurantItem->phone_number }}" maxlength="15">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Exclusion</label>
                                            <textarea type="text" class="form-control" id="exclusion" name="exclusion">{!! $restaurantItem->exclusion !!}</textarea>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="location" class="label">Select state</label>
                                        <select class="form-select" aria-label="Default select example" id="single"
                                            name="state_xid">
                                            <option value="">Select State</option>
                                            @foreach ($state as $states)
                                                <option value="{{ $states['id'] }}"
                                                    @if ($states['id'] == $restaurantItem->state_xid) selected @endif>
                                                    {{ $states['name'] }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Latitude</label>
                                            <input type="text" class="form-control" id="latitude" name="latitude"
                                                maxlength="30" value="{{ $restaurantItem->latitude }}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Longitude</label>
                                            <input type="text" class="form-control" id="longitude" name="longitude"
                                                maxlength="30" value="{{ $restaurantItem->longtitude }}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">Bio</label>
                                            <input type="text" class="form-control" name="bio"
                                                value="{{ $restaurantItem->bio }}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label for="company-name" class="label">While at Restaurant be sure to try
                                                :</label>
                                            <input type="text" class="form-control mb-3" name="try_on_1"
                                                value="{{ $restaurantItem->try_on_1 }}">
                                            <input type="text" class="form-control mb-3" name="try_on_2"
                                                value="{{ $restaurantItem->try_on_2 }}">
                                            <input type="text" class="form-control mb-3" name="try_on_3"
                                                value="{{ $restaurantItem->try_on_3 }}">
                                            <input type="text" class="form-control mb-3" name="try_on_4"
                                                value="{{ $restaurantItem->try_on_4 }}">
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label class="label">Operating Hours</label>
                                            @foreach (['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as $day)
                                                <div class="row">
                                                    <div class="col-md-3">
                                                        <label>{{ $day }} Start Time:</label>
                                                        <input type="time" class="form-control"
                                                            name="operating_hours[{{ $day }}][start_time]"
                                                            value="{{ isset($operating_hours[$day]) ? $operating_hours[$day]->start_time : '' }}">
                                                    </div>
                                                    <div class="col-md-3">
                                                        <label>{{ $day }} End Time:</label>
                                                        <input type="time" class="form-control"
                                                            name="operating_hours[{{ $day }}][end_time]"
                                                            value="{{ isset($operating_hours[$day]) ? $operating_hours[$day]->end_time : '' }}">
                                                    </div>
                                                </div>
                                            @endforeach

                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <button class="download-btn "style="width: 30%;" id="update_restaurant">
                                            <span>Submit</span>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>

    <script>
        const imageInput = document.getElementById('imageInputThumb');
        const imagePreview = document.getElementById('imagePreviewThumb');

        imageInput.addEventListener('change', function() {
            console.log("in change kjbck");
            const file = this.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(event) {
                    const img = document.createElement('img');
                    img.src = event.target.result;
                    img.style.maxWidth = '200px';
                    img.style.maxHeight = '200px';
                    imagePreview.innerHTML = '';
                    imagePreview.appendChild(img);
                };
                reader.readAsDataURL(file);
            } else {
                imagePreview.innerHTML = '';
            }
        });


        const imageInputEdit = document.getElementById('imageInputNormal');
        const imagePreviewEdit = document.getElementById('imageInputPreviewNormal');

        imageInputEdit.addEventListener('change', function() {
            const file = this.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(event) {
                    const img = document.createElement('img');
                    img.src = event.target.result;
                    img.style.maxWidth = '200px';
                    img.style.maxHeight = '200px';
                    imagePreviewEdit.innerHTML = '';
                    imagePreviewEdit.appendChild(img);
                };
                reader.readAsDataURL(file);
            } else {
                imagePreviewEdit.innerHTML = '';
            }
        });
    </script>
@endsection
@section('section_script')
    <script>
        $(document).ready(function() {

            $('#update_voucher').validate({
                ignore: [],
                debug: false,
                rules: {
                    name: {
                        required: true
                    },
                    state_xid: {
                        required: true,
                    },
                    rest_id: {
                        required: true,
                    },
                    address: {
                        required: true,
                    },
                    phone_number: {
                        required: true,
                    },
                    latitude: {
                        required: true,
                        number: true,
                        normalizer: function(value) {
                            return $.trim(value);
                        }
                    },
                    longitude: {
                        required: true,
                        number: true,
                        normalizer: function(value) {
                            return $.trim(value);
                        }
                    },
                    bio: {
                        required: true,
                    },
                    try_on_1: {
                        required: true,
                    },
                    try_on_2: {
                        required: true,
                    },
                    try_on_3: {
                        required: true,
                    },
                    try_on_4: {
                        required: true,
                    },
                },
                messages: {
                    name: {
                        required: "Enter restaurant Name",
                    },
                    state_xid: {
                        required: "Please select state",
                    },
                    rest_id: {
                        required: "Enter restaurant Id",
                    },
                    address: {
                        required: "Please enter location",
                    },
                    phone_number: {
                        required: "Please enter phone number",
                    },
                    latitude: {
                        required: "Please enter latitude",
                        number: "Latitude must be a number without spaces",
                    },
                    longitude: {
                        required: "Please enter longitude",
                        number: "Longitude must be a number without spaces",
                    },
                    bio: {
                        required: "Enter Bio",
                    },
                    try_on_1: {
                        required: "Please enter this field",
                    },
                    try_on_2: {
                        required: "Please enter this field",
                    },
                    try_on_3: {
                        required: "Please enter this field",
                    },
                    try_on_4: {
                        required: "Please enter this field",
                    },
                },
                errorClass: 'error-message',
                submitHandler: function(form) {
                    var formData = new FormData(form);

                    $('#update_restaurant').text('Please wait...');
                    $('#update_restaurant').attr('disabled', true);

                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });
                    $.ajax({
                        url: "{{ route('update_restaurant') }}",
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function(result) {

                            if (result.status_code == 200) {
                                toastr.success('Restaurant Updated Sucessfully');
                                setTimeout(function() {
                                    window.location.href =
                                        "{{ route('manage.restaurants') }}";
                                }, 2000);
                            } else {
                                toastr.error('Something Went Wrong');
                                setTimeout(function() {
                                    window.location.href =
                                        "{{ route('manage.restaurants') }}";
                                }, 2000);
                            }
                            $('#update_restaurant').attr('disabled', false);
                            $('#update_restaurant').text('Submit');
                        },

                    });

                    $('#store_coupon').attr('disabled', false);
                    $('#store_coupon').text('Submit');
                }
            });
        });
    </script>
@endsection
