@extends("layouts.company")
@section('content')
    <div class="row">
        <div class="col-12">
            <div class="page-title-box d-sm-flex align-items-center justify-content-between bg-galaxy-transparent">
                <h4 class="mb-sm-0">Create Staff</h4>

                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="javascript: void(0);">Home</a></li>
                        <li class="breadcrumb-item"><a href="javascript: void(0);">Staff</a></li>
                        <li class="breadcrumb-item active">Create Staff</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
    <!-- end page title -->

    <div class="row">
        <div class="col-lg-12">
            <div class="card" id="orderList">
                <div class="card-body">
                    <form class="tablelist-form" autocomplete="off" name="staffForm" id="staffForm" action="{{ route('company.staff.store') }}" novalidate enctype="multipart/form-data">
                        @csrf
                        <div class="row">
                            <div class="mb-3 col-lg-4">
                                <label for="first_name" class="form-label">First Name</label>
                                <input type="text" id="first_name" class="form-control" placeholder="Enter name" required name="first_name" />
                            </div>

                            <div class="mb-3 col-lg-4">
                                <label for="last_name" class="form-label">Last Name</label>
                                <input type="text" id="last_name" class="form-control" placeholder="Enter name" required name="last_name" />
                            </div>

                            <div class="mb-3 col-lg-4">
                                <label for="orderId" class="form-label w-100">Phone number</label>
                                <div class="row">
                                    <div class="col-4">
                                        <select class="form-select  js-example-basic-single" data-placeholder="Select Dial Code" name="dial_code">
                                            <option></option>
                                            <option value="+971">+971</option>
                                            <option value="+91">+91</option>
                                        </select>
                                    </div>
                                    <div class="col-8">
                                        <input type="text" id="phoneField" class="form-control w-100 number-only" placeholder="Enter Phone number" required name="phone" />
                                    </div>
                                </div>
                            </div>

                            <div class="mb-3 col-lg-4">
                                <label for="email" class="form-label">Email</label>
                                <input type="text" id="email" class="form-control" placeholder="Enter email" required name="email" />
                            </div>
                            <div class="col-lg-4 mb-3">
                                <label for="password" class="form-label">Password</label>
                                <input type="password" class="form-control" placeholder="Enter Password" required name="password" id="password" />
                            </div>
                            <div class="col-lg-4 mb-3">
                                <label for="password_confirmation" class="form-label">Confirm Password</label>
                                <input type="password" id="password_confirmation" class="form-control" placeholder="Enter Confirm Password" required name="password_confirmation" />
                            </div>


                            <div class="col-lg-4 mb-3">
                                <label for="country" class="form-label">Country</label>
                                <select class="form-select js-example-basic-single" data-placeholder="Select Country" name="country" id="country">
                                    <option></option>
                                    @foreach($countries as $country)
                                        <option value="{{ $country->id }}">{{ $country->name }}</option>
                                    @endforeach

                                </select>
                            </div>
                            <div class="col-lg-4 mb-3">
                                <label for="emirates" class="form-label">Emirates</label>
                                <select class="form-select js-example-basic-single" data-placeholder="Select Emirates" name="emirates" id="emirates">
                                    <option></option>

                                </select>
                            </div>
                            <div class="col-lg-4 mb-3">
                                <label for="city" class="form-label">City</label>
                                <select class="form-select js-example-basic-single" data-placeholder="Select City" name="city" id="city">
                                    <option></option>

                                </select>
                            </div>


                            <div class="col-lg-4 mb-3">
                                <label for="status" class="form-label">Status</label>
                                <select class="form-select js-example-basic-single" data-placeholder="Select Status" name="status" id="status">
                                    <option value=""></option>
                                    <option value="1">Active</option>
                                    <option value="0">Inactive</option>
                                </select>
                            </div>
                        </div>



                        <div class="hstack gap-2 justify-content-end">
                            <button type="submit" class="btn btn-success">Create User</button>
                            <!-- <button type="button" class="btn btn-success" id="edit-btn">Update</button> -->
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/additional-methods.min.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {

            $("#country").on('change', function () {
                let country_id = $(this).val();
                $.ajax({
                    url: "{{ route("emirates") }}",
                    type: "POST",
                    headers: {
                        "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content") // CSRF token
                    },
                    data: {
                        _token  : $('meta[name="csrf-token"]').attr('content'), // CSRF token
                        country : country_id,
                    },
                    dataType:"json",
                    success: function (response) {
                        $("#emirates").html(response.emirates);
                    }
                });
            });

            $("#emirates").on('change', function () {
                let emirates = $(this).val();
                $.ajax({
                    url: "{{ route("cities") }}",
                    type: "POST",
                    headers: {
                        "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content") // CSRF token
                    },
                    data: {
                        _token  : $('meta[name="csrf-token"]').attr('content'), // CSRF token
                        emirate : emirates,
                    },
                    dataType:"json",
                    success: function (response) {
                        $("#city").html(response.cities);
                    }
                });
            });

            $("#staffForm").validate({
                rules: {
                    first_name: {
                        required: true,
                        minlength: 3,
                    },
                    last_name: {
                        required: true,
                        minlength: 3,
                    },
                    phone: {
                        required: true,
                        digits: true,
                        minlength: 10,
                        maxlength: 15,
                    },
                    email: {
                        required: true,
                        email: true,
                    },
                    password: {
                        required: true,
                        minlength: 6,
                    },
                    password_confirmation: {
                        required: true,
                        equalTo: "#password",
                    },
                    status: {
                        required: true,
                    },
                    city:{
                        required:true
                    },
                    emirates:{
                        required:true
                    },
                    country:{
                        required:true
                    }
                },
                messages: {
                    name: {
                        required: "Please enter the company name.",
                        minlength: "Company name must be at least 3 characters.",
                    },
                    name_ar: {
                        required: "Please enter the company name in Arabic.",
                        minlength: "Company name (Arabic) must be at least 3 characters.",
                    },
                    phone: {
                        required: "Please enter the phone number.",
                        digits: "Please enter only digits.",
                        minlength: "Phone number must be at least 10 digits.",
                        maxlength: "Phone number cannot exceed 15 digits.",
                    },
                    email: {
                        required: "Please enter an email address.",
                        email: "Please enter a valid email address.",
                    },
                    password: {
                        required: "Please enter a password.",
                        minlength: "Password must be at least 6 characters.",
                    },
                    password_confirmation: {
                        required: "Please confirm the password.",
                        equalTo: "Passwords do not match.",
                    },
                    status: {
                        required: "Please select the status.",
                    },
                },
                errorPlacement: function (error, element) {
                    error.insertAfter(element); // Place error message after the input field
                },
                submitHandler: function (form) {
                    let formData = new FormData(form);

                    // AJAX submission
                    $.ajax({
                        url: form.action,
                        type: "POST",
                        data: formData,
                        processData: false,
                        contentType: false,
                        headers: {
                            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
                        },
                        success: function (response) {
                            // Show success message with Swal
                            if(response.status == "success") {
                                Swal.fire({
                                    text: response.message,
                                    icon: "success",
                                    confirmButtonText: "OK"
                                }).then(() => {
                                    window.location.href = "{{ route('company.staff') }}"; // Redirect to company list
                                });
                            } else {
                                Swal.fire({
                                    text: response.message,
                                    icon: "error",
                                    confirmButtonText: "OK"
                                });
                            }
                        },
                        error: function (xhr) {
                            if (xhr.status === 422) {
                                let errors = xhr.responseJSON.errors;

                                // Display validation errors from the server
                                $.each(errors, function (key, value) {
                                    let field = $(`[name="${key}"]`);
                                    field.addClass("error"); // Add error class to the field
                                    field.after(`<label class="error">${value[0]}</label>`); // Add error message
                                });
                            } else {
                                Swal.fire({
                                    title: "Error!",
                                    text: "Something went wrong. Please try again later.",
                                    icon: "error",
                                    confirmButtonText: "OK"
                                });
                            }
                        }
                    });
                    return false; // Prevent default form submission
                }
            });
        });
    </script>
@endsection

