<script>
import { Head, useForm } from '@inertiajs/vue3';
import Layout from "@/Layouts/main.vue";
import PageHeader from "@/Components/page-header.vue";
import Pagination from "@/Components/Pagination.vue";
import Swal from "sweetalert2";
import { ref, watch } from "vue";
import axios from "axios";
import { debounce } from 'lodash';
import Multiselect from "@vueform/multiselect";

export default {
    components: {
        Layout,
        PageHeader,
        Head,
        Pagination,
        Multiselect,
    },
    props: {
        successMessage: String,
        alertMessage: String,
    },
    setup(props) {
        const searchTerm = ref("");
        const selectedRole = ref(null);
        const form = useForm({
            name: "",
            description: "",
        });
        const filter = useForm({
            all: "",
            locked: "",
        });
        const roles = ref([]); // Spread the roles to make them reactive
        const paginator = ref({}); // Spread the roles to make them reactive
        const modalShow = ref(false);
        const modalForm = ref(false);
        const modalFilter = ref(false);
        const deleteItemId = ref(null);

        const successMessage = ref(props.successMessage || '');
        const alertMessage = ref(props.alertMessage || '');

        const dismissMessage = () => {
            successMessage.value = "";
            alertMessage.value = "";
        };

        const createRole = () => {
            form.reset();
            selectedRole.value = null;
            modalForm.value = true;
        };

        const filterRole = () => {
            modalFilter.value = true;
        };

        const editRole = (role) => {
            selectedRole.value = role;
            form.name = role.name;
            form.description = role.description;
            form.clearErrors();
            modalForm.value = true;
        };

        const cancelEdit = () => {
            form.reset();
            modalForm.value = false;
            selectedRole.value = null;
        };

        const clearFilter = () => {
            filter.reset();
            fetchRoles();
            modalFilter.value = false;
        };

        const validateForm = () => {
            const { name, description } = form;
            const errors = {};

            const validationRules = {
                name: {
                    test: () => !name,
                    message: 'Role name is required'
                },
                description: {
                    test: () => !description,
                    message: 'Role description is required'
                },
            };

            Object.keys(validationRules).forEach(fieldName => {
                if (validationRules[fieldName].test()) {
                    errors[fieldName] = validationRules[fieldName].message;
                }
            });

            return errors;
        };

        const handleSubmit = async () => {
            const errors = validateForm();
            if (Object.keys(errors).length === 0) {
                try {
                    let response;
                    if (selectedRole.value) {
                        response = await axios.put(`/roles/update/${selectedRole.value.id}`, form.data());
                    } else {
                        response = await axios.post("/roles", form.data());
                    }

                    if (response.status === 200 || response.status === 201) {
                        successMessage.value = response.data.successMessage;
                        const responseData = response.data.roles;
                        if (selectedRole.value) {
                            const index = roles.value.findIndex(role => role.id === selectedRole.value.id);
                            if (index !== -1) {
                                roles.value[index] = responseData;
                            }
                        } else {
                            roles.value.push(responseData);
                        }
                        modalForm.value = false;
                        form.reset();
                    } else {
                        console.error('Unexpected response status:', response.status);
                    }

                } catch (error) {
                    console.error("Error handling form submission:", error);
                    if (error.response && error.response.status === 422) {
                        const errors = error.response.data.errors;
                        form.errors = errors;
                    } else {
                        console.error("An unexpected error occurred:", error);
                    }
                }
            } else {
                form.errors = errors;
            }
        };

        const hideError = (field) => {
            form.errors[field] = '';
        };

        const closeModal = () => {
            modalShow.value = false;
        };
        const deleteRole = async (roleId) => {
            try {
                await axios.delete(`/roles/delete/${roleId}`);
                const index = roles.value.findIndex(role => role.id === roleId);
                if (index !== -1) {
                    roles.value.splice(index, 1);
                }
                modalShow.value = false;
                Swal.fire('Success', 'Role deleted successfully', 'success');
            } catch (error) {
                console.error("Error deleting role:", error);
                Swal.fire('Error', 'Failed to delete role', 'error');
            }
        };

        const deleteModal = async (itemId) => {
            Swal.fire({
                title: "Are you sure?",
                text: "You won't be able to revert this!",
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#34c38f",
                cancelButtonColor: "#f46a6a",
                confirmButtonText: "Yes, delete it!",
            }).then(async (result) => {
                if (result.isConfirmed) {
                    try {
                        await deleteRole(itemId);
                    } catch (error) {
                        console.error("Error deleting role:", error);
                        Swal.fire("Error", "Failed to delete the role", "error");
                    }
                }
            });
        };

        watch(searchTerm,debounce(function(value){
            fetchRoles(searchTerm.value);

        },300));

        const fetchRoles = async (page = 1) => {
            try {
                const params = filter.data();
                params.search = searchTerm.value;
                params.page = page;
                const response = await axios.get(`/roles1/list`, { params });
                roles.value = response.data.roles;
                paginator.value = response.data.paginator;
                modalFilter.value = false;
            } catch (error) {
                console.error("Error fetching roles:", error);
            }
        };

        const handlePageChanged = async (page) => {
            fetchRoles(page);
        };

        return {
            form,
            roles,
            modalShow,
            modalForm,
            deleteItemId,
            selectedRole,
            successMessage,
            alertMessage,
            createRole,
            filterRole,
            editRole,
            cancelEdit,
            handleSubmit,
            deleteModal,
            closeModal,
            deleteRole,
            dismissMessage,
            hideError,
            searchTerm,
            paginator,
            modalFilter,
            clearFilter,
            fetchRoles,
            filter,
            handlePageChanged

        };
    },
    mounted() {
        this.fetchRoles();
    },
};
</script>

<template>
    <Layout>

        <Head title="Approved Drivers" />
        <PageHeader title="Update" pageTitle="Approved Driver" />
        <BRow>
            <BCol lg="12">
                <BCard no-body id="tasksList">

                    <BCardHeader class="border-0">

                    </BCardHeader>
                    <BCardBody class="border border-dashed border-end-0 border-start-0">
                        <form action="javascript:void(0);">
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="select_area" class="form-label">Select Area</label>
                                    <select id="select_area" class="form-select" >
                                        <option disabled value="">Select</option>
                                        <option  value="india">India</option>
                                    </select>
                                    <span v-for="(error, index) in errors.country" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="name" class="form-label">Name</label>
                                    <input type="text" class="form-control" placeholder="Enter name" id="name" 
                                    />
                                    <span v-for="(error, index) in errors.name" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="mobile_number" class="form-label">Enter Mobile Number</label>
                                    <input type="text" class="form-control" placeholder="Enter Mobile Number" id="mobile_number" 
                                    />
                                    <span v-for="(error, index) in errors.name" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="email" class="form-label">Email</label>
                                    <input type="text" class="form-control" placeholder="Enter Email" id="email" 
                                    />
                                    <span v-for="(error, index) in errors.name" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="select_gender" class="form-label">Gender</label>
                                    <select id="select_gender" class="form-select" >
                                        <option disabled value="">Select</option>
                                        <option  value="male">Male</option>
                                        <option  value="female">Female</option>
                                        <option  value="others">others</option>
                                    </select>
                                    <span v-for="(error, index) in errors.country" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="select_transport_type" class="form-label">Transport Type</label>
                                    <select id="select_transport_type" class="form-select" >
                                        <option disabled value="">Select</option>
                                        <option  value="taxi">Taxi</option>
                                        <option  value="delivery">Delivery</option>
                                        <option  value="both">Both</option>
                                    </select>
                                    <span v-for="(error, index) in errors.country" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="select_type" class="form-label">Select Type</label>

                                        <Multiselect class="form-control" v-model="value" :close-on-select="true" :searchable="true"
                                            :create-option="true" :options="[
                                                { value: '', label: 'Select type' },
                                                { value: 'Sedan', label: 'Sedan' },
                                                { value: 'LCV', label: 'LCV' },
                                                { value: 'Scooter', label: 'Scooter' },
                                                { value: 'Bike', label: 'Bike' },
                                                { value: 'SUV', label: 'SUV' },
                                            ]" />
                                    <span v-for="(error, index) in errors.country" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="select_make_car" class="form-label">Make Car</label>
                                    <select id="select_make_car" class="form-select" >
                                        <option disabled value="">Select</option>
                                        <option  value="Acura">Acura</option>
                                        <option  value="Audi">Audi</option>
                                        <option  value="Alfa Romeo">Alfa Romeo</option>
                                        <option  value="Aston Martin">Aston Martin</option>
                                        <option  value="BMW">BMW</option>
                                        <option  value="Bentely">Bentely</option>
                                        <option  value="Buick">Buick</option>
                                        <option  value="cadillac">cadillac</option>
                                        <option  value="Chavrolet">Chavrolet</option>
                                        <option  value="Chrysler">Chrysler</option>
                                        <option  value="FIAT">FIAT</option>
                                        <option  value="Ferrari">Ferrari</option>
                                        <option  value="Fisker">Fisker</option>
                                        <option  value="Ford">Ford</option>
                                        <option  value="GMC">GMC</option>
                                        <option  value="HUMMER">HUMMER</option>
                                        <option  value="Honda">Honda</option>
                                        <option  value="INFINITI">INFINITI</option>
                                        <option  value="Isuzu">Isuzu</option>
                                        <option  value="Jaguar">Jaguar</option>
                                        <option  value="Jeep">Jeep</option>
                                        <option  value="TATA">TATA</option>
                                        <option  value="Dodge">Dodge</option>
                                        <option  value="Hyundai">Hyundai</option>  
                                        <option  value="Ashok Leyland">Ashok Leyland</option>                       
                                    </select>
                                    <span v-for="(error, index) in errors.country" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="select_car_model" class="form-label">Car Model</label>
                                    <select id="select_car_model" class="form-select" >
                                        <option disabled value="">Select</option>
                                        <option  value="lcv_truck">LCV truck</option>
                                        <option  value="light_truck">light truck</option>
                                        <option  value="yodha_pcik_up">Yodha pick-up</option>
                                    </select>
                                    <span v-for="(error, index) in errors.country" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="car_color" class="form-label">Car Color</label>
                                    <input type="text" class="form-control" placeholder="Enter Car Color" id="car_color" 
                                    />
                                    <span v-for="(error, index) in errors.name" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="mb-3">
                                    <label for="car_number" class="form-label">Car Number</label>
                                    <input type="text" class="form-control" placeholder="Enter Car Number" id="car_number" 
                                    />
                                    <span v-for="(error, index) in errors.name" :key="index" class="text-danger">{{ error }}</span>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="mb-3">
                                    <label for="icon" class="form-label d-flex">Profile 
                                        <span><h5 class="text-muted mt-1 mb-0 fs-13">(512px x 512px)</h5></span>
                                    </label>
                                    <!-- Display existing vehicle image if available -->
                                    <div v-if="iconUrl && !form.iconFile">
                                        <img :src="iconUrl" alt="Vehicle Image" style="max-width: 100px; max-height: 100px;" />
                                    </div>
                                    <!-- Image Upload Component -->
                                    <imageUpload v-model="form.iconFile"></imageUpload>
                                    <span v-if="form.errors.icon" class="text-danger">{{ form.errors.icon }}</span>
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="text-end">
                                        <button type="submit" class="btn btn-primary">Update</button>
                                    </div>
                                </div><!--end col-->
                            </div><!--end row-->
                        </form>
                    </BCardBody>
                </BCard>
            </BCol>
        </BRow>

        <div>
            <!-- Success Message -->
            <div v-if="successMessage" class="custom-alert alert alert-success alert-border-left fade show" role="alert"
                id="alertMsg">
                <div class="alert-content">
                    <i class="ri-notification-off-line me-3 align-middle"></i> <strong>Success</strong> - {{
                        successMessage }}
                    <button type="button" class="btn-close btn-close-success" @click="dismissMessage"
                        aria-label="Close Success Message"></button>
                </div>
            </div>

            <!-- Alert Message -->
            <div v-if="alertMessage" class="custom-alert alert alert-danger alert-border-left fade show" role="alert"
                id="alertMsg">
                <div class="alert-content">
                    <i class="ri-notification-off-line me-3 align-middle"></i> <strong>Alert</strong> - {{ alertMessage
                    }}
                    <button type="button" class="btn-close btn-close-danger" @click="dismissMessage"
                        aria-label="Close Alert Message"></button>
                </div>
            </div>
        </div>

        <!-- Create or Edit Form Modal -->
        <BModal v-model="modalForm" hide-footer :title="selectedRole ? 'Update Role' : 'Create Role'"
            class="v-modal-custom">
            <form @submit.prevent="handleSubmit" @keydown="hideError">
                <div class="form-group mt-3">
                    <label for="name" class="form-label">Role Name<span class="text-danger">*</span></label>
                    <input type="text" id="name" v-model="form.name" class="form-control"
                        :class="{ 'is-invalid': form.errors.name }" @focus="hideError('name')" />
                    <div v-if="form.errors.name" class="text-danger">{{ form.errors.name }}</div>
                </div>
                <div class="form-group mt-3">
                    <label for="description" class="form-label">description<span class="text-danger">*</span></label>
                    <input type="text" id="description" v-model="form.description" class="form-control"
                        :class="{ 'is-invalid': form.errors.description }" @focus="hideError('description')" />
                    <div v-if="form.errors.description" class="text-danger">{{ form.errors.description }}</div>
                </div>
                <button type="submit" class="btn btn-success m-2 float-end">{{ selectedRole ? 'Update' : 'Save'
                    }}</button>
                <button v-if="selectedRole" type="button" @click="cancelEdit"
                    class="btn btn-secondary m-2 float-end">Cancel</button>
            </form>
        </BModal>

        <BModal v-model="modalFilter" hide-footer dialog-class="modal-dialog-right" title="Filter"
            class="v-modal-custom " size="sm">
            <form>
                <div class="input-group">
                    <select class="form-select mb-3" aria-label="Default select example" v-model="filter.all">
                        <option selected>Select Status</option>
                        <option value="1">Yes</option>
                        <option value="0">No</option>

                    </select>

                    <select class="form-select mb-3" aria-label="Default select example" v-model="filter.locked">
                        <option selected>Select Status</option>
                        <option value="0">Inactive</option>
                        <option value="1">Active</option>
                    </select>
                </div>
                <BButton variant="primary" class="float-end" @click="fetchRoles"> Apply</BButton>
                <BButton variant="outline-primary" class="float-end mx-2" @click="clearFilter">Clear</BButton>

            </form>
        </BModal>

    </Layout>
</template>
<style>
.custom-alert {
    max-width: 600px;
    float: right;
    position: fixed;
    top: 90px;
    right: 20px;
}

.text-danger {
    padding-top: 5px;
}
</style>
