@extends('Admin.layouts.master')

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

    form .error-message {
    color: red; 
    }
</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">
                        <!-- <h6 class="card-title">Edit Manage Customers</h6> -->
                        <a class="d-flex align-items-center justify-content-center pl-2" href="{{ route('restraunt_users') }}">
                            <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>
            <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">
                        <form id="rest_user_form">
                        <input type="hidden" name="rest_id" class="form-control" value="{{$editRest->id}}">

                            <div class="row">
                              
                                <div class="col-md-6">
                                    <div class="form-group ">
                                        <label for="company-name" class="label">First Name</label>
                                        <input type="text" class="form-control" name="first_name" id="first_name" value="{{$editRest->first_name }}">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group ">
                                        <label for="company-name" class="label">Last Name</label>
                                        <input type="text" class="form-control" name="last_name" id="last_name" value="{{$editRest->last_name }}">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group ">
                                        <label for="company-name" class="label">Date of Birth</label>
                                        <input type="date" class="form-control" name="user_birth" id="user_birth" value="{{$editRest->date_of_birth }}">
                                    </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" name="restaurant_phone" id="restaurant_phone" value="{{$editRest->phone_number }}">
                                    </div>
                                </div>
                               
                                <div class="col-md-6">
                                    <button type="submit" id="rest_btn" class="download-btn-custom mt-3 w-25 rest_btn" >
                                        <span>Save</span>
                                    </button>
                                </div>
                            </div>
                        </form>


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

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


@endsection
@section('section_script')
<script src="{{ asset('public/assets/js/admin/manage_restaurant/main.js') }}"></script>



<script>
$(document).on("click", "#rest_btn", function (e) {
// alert('test');

    // Add custom validation method for full name (alphabetic characters only)
    $.validator.addMethod("fullNameCharactersOnly", function(value, element) {
        return /^[a-zA-Z\s]+$/.test(value);
    }, "Please enter only alphabetical characters for the full name");

    $.validator.addMethod("numericCharactersOnly", function(value, element) {
        return /^[0-9]+$/.test(value);
    }, "Please enter only numeric characters");

    $('#rest_user_form').validate({
        rules: {
            first_name: {
                required: true,
                fullNameCharactersOnly: true
            },
            last_name: {
                required: true,
                fullNameCharactersOnly: true
            },
            user_birth: {
                required: true,
            },
        
            restaurant_phone: {
                required: true,
                numericCharactersOnly: true
            },
        },
        messages: {
            first_name: {
                required: "Please enter first name",
            },
            last_name: {
                required: "Please enter last name",
            },
            user_birth: {
                required: "Please enter date of birth",
            },
        
            restaurant_phone: {
                required: "Enter Phone Number",
                numericCharactersOnly: "Please enter only numeric characters"
            },
        },
        errorClass: 'error-message',
        submitHandler: function(form) {
            let base_url = url_path;
            var formData = new FormData(form);
            // console.log(formData);
            // return;
            $('#restaurant_btn').text('Please wait...');
            $('#restaurant_btn').attr('disabled', true);

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                url: base_url + '/update_restaurant_users',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(result) {
                    if (result.status_code == 200) {
                        toastr.success('Restaurant Updated Successfully');
                        setTimeout(function() {
                            window.location.href = base_url + "/restaurant_users";
                        }, 2000);
                    } else {
                        toastr.error('Something Went Wrong');
                        setTimeout(function() {
                            window.location.href = base_url + "/restaurant_users";
                        }, 2000);
                    }
                    $('#restaurant_btn').attr('disabled', false);
                    $('#restaurant_btn').text('Submit');
                },
                error: function() {
                    toastr.error('Something Went Wrong');
                    $('#restaurant_btn').attr('disabled', false);
                    $('#restaurant_btn').text('Submit');
                }
            });
        }
    });

});
</script>




@endsection