<script>
import { Link, Head, useForm, router } 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, onMounted } from "vue";
import axios from "axios";
import Multiselect from "@vueform/multiselect";
import "@vueform/multiselect/themes/default.css";
import flatPickr from "vue-flatpickr-component";
import "flatpickr/dist/flatpickr.css";
import searchbar from "@/Components/widgets/searchbar.vue";
import { mapGetters } from 'vuex';
import { layoutComputed } from "@/state/helpers";
import { useI18n } from 'vue-i18n';
import { useSharedState } from '@/composables/useSharedState';

export default {
    data() {
        return {
            rightOffcanvas: false,            
            SearchQuery: '',
        };
    },
    components: {
        Layout,
        PageHeader,
        Head,
        Pagination,
        Multiselect,
        flatPickr,
        Link,
        searchbar,
    },
    props: {
        successMessage: String,
        alertMessage: String,
        zones: {
            type: Array,
            required: true,
        },
        vehicleTypes: {
            type: Array,
            required: true,
        },
        show_driver_level_feature:Boolean,
        show_incentive_feature_for_driver: Boolean,
    },

    setup(props) {
        const { t } = useI18n();
        const searchTerm = ref("");
        const filter = useForm({
            service_location_id: null,
            zone_id:[],
            type_id:[],
            limit:10
        });
        const results = ref([]);
        const paginator = ref({});
        const modalShow = ref(false);
        const modalFilter = ref(false);
        const deleteItemId = ref(null);
        const paginatorOption = ref({}); // Spread the results to make them reactive
        const { selectedLocation } = useSharedState();

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

        const zone = ref('');
        const vehicleType = ref('');

        const zoneOptions = ref(props.zones);
        const vehicleTypeOptions = ref(props.vehicleTypes);

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

        // const toggleActiveStatus = async (id, status) => {
        //     try {
        //         await axios.post(`/set-prices/update-status`, { id, status });
        //         const index = results.value.findIndex(item => item.id === id);
        //         if (index !== -1) {
        //             results.value[index].active = status; // Update the active status locally
        //         }
        //     } catch (error) {
        //         if (error.response && error.response.status == 403) {
        //             alertMessage.value = error.response.data.alertMessage;
        //             setTimeout(()=>{
        //                 alertMessage.value = "";
        //             },5000)
        //         }
        //         console.error(t('error_updating_status'), error);
        //     }
        // };


        watch(selectedLocation, (value)=> {
            filter.service_location_id = value;
            fetchDatas();
        });
        const toggleActiveStatus = async (id, status) => {
            Swal.fire({
                title: t('are_you_sure'),
                text: t('you_are_about_to_change_status'),
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#34c38f",
                cancelButtonColor: "#f46a6a",
                confirmButtonText: t('yes'),
                cancelButtonText: t('cancel')
            }).then(async (result) => {
                if (result.isConfirmed) {
                    try {
                        await axios.post(`/set-prices/update-status`, { id: id, status: status });
                        const index = results.value.findIndex(item => item.id === id);
                        if (index !== -1) {
                            results.value[index].active = status; // Update the active status locally
                        }
                        Swal.fire(t('changed'), t('status_updated_successfully'), "success");
                    } catch (error) {
                        console.error(t('error_updating_status'), error);
                        Swal.fire(t('error'), t('failed_to_update_status'), "error");
                    }
                }
            });
        };

        const capitalizeFirstLetter = (value) => {
            return value.charAt(0).toUpperCase() + value.slice(1);
        };

        const rightOffcanvas = ref(false);
        const filterData = () => {
            fetchDatas();
            modalFilter.value = true;
            rightOffcanvas.value = false;
        };

        const clearFilter = () => {
            filter.reset();
            zone.value = ''; // Reset zone filter
            vehicleType.value = ''; // Reset vehicleType filter
            fetchDatas();
            // modalFilter.value = false;
            rightOffcanvas.value = false;
        };

        const closeModal = () => {
            modalShow.value = false;
        };

        const deleteData = async (result) => {
            try {
                await axios.delete(`/set-prices/delete/${result}`);
                const index = results.value.findIndex(data => data.id === result);
                if (index !== -1) {
                    results.value.splice(index, 1);
                }
                modalShow.value = false;
                Swal.fire(t('success'), t('vehicle_price_deleted_successfully'), 'success');
            } catch (error) {
                if (error.response && error.response.status == 403) {
                    alertMessage.value = error.response.data.alertMessage;
                    setTimeout(()=>{
                        alertMessage.value = "";
                    },5000)
                }
                Swal.fire(t('error'), t('failed_to_delete_vehicle_price'), '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 deleteData(itemId);
                    } catch (error) {
                        console.error(t('error_deleting_data'), error);
                        Swal.fire(t('error'), t('failed_to_delete_the_data'), "error");
                    }
                }
            });
        };

        const fetchSearch = async (value) => {
            searchTerm.value = value;
            fetchDatas();
        };

        const fetchDatas = async (page = 1) => {
            try {
                const params = filter.data();
                params.page = page;
                params.zone = zone.value; // Include zone_id in params to apply in Query filter
                params.vehicle_type_id = vehicleType.value; // Include vehicleType in params to apply in Query filter
                params.include = 'vehicleType,zone'; // Add this line to eager-load relationships

                // Only include status parameter if it's not empty (to show all)
                if (filter.status !== "") {
                    params.status = filter.status;
                }
                if(searchTerm.value.length > 0){
                    params.search = searchTerm.value;
                }
                const response = await axios.get(`/set-prices/list`, { params });
                console.log("list-data");
                console.log(response.data.results);

                results.value = response.data.results;
                paginator.value = response.data.paginator;
                updatePaginatorOptions(paginator.value.total);// Update paginator options dynamically
                modalFilter.value = false;
            } catch (error) {
                console.error(t('error_fetching_set_prices'), error);
            }
        };

        const handlePageChanged = async (page) => {
            localStorage.setItem("set-prices/list", page); // Save to localStorage
            fetchDatas(page);
        };

        const editData = async (result) => {
            router.get(`/set-prices/edit/${result.id}`);
        };

        const packagesData = async (result) => {
            router.get(`set-prices/packages/${result.id}`);
        };
        const levelUpData = async (result) => {
            router.get(`drivers-levelup/${result.id}`);
        };
        const incentiveData = async (result) => {
            router.get(`incentives/${result.id}`);
        };
        const surgeData = async (result) => {
            router.get(`set-prices/surge/${result.id}`);
        };
        const updatePaginatorOptions = () => {
            paginatorOption.value = [10, 25, 50, 100,200,500]; // Default static options
        };
        // **Handle per-page changes**
        const changeEntriesPerPage = () => {
            fetchDatas(); // Fetch new data
        };

        return {
            results,
            modalShow,
            deleteItemId,
            successMessage,
            alertMessage,
            filterData,
            deleteModal,
            closeModal,
            dismissMessage,
            searchTerm,
            fetchSearch,
            paginator,
            modalFilter,
            clearFilter,
            fetchDatas,
            filter,
            handlePageChanged,
            editData,
            packagesData,
            surgeData,
            toggleActiveStatus,
            capitalizeFirstLetter,
            zone,
            vehicleType,
            zoneOptions,
            vehicleTypeOptions,
            rightOffcanvas,
            levelUpData,
            incentiveData,
            paginatorOption,
            selectedLocation,
            changeEntriesPerPage
        };
    },
    computed: {
    ...layoutComputed,
    ...mapGetters(['permissions']),
  },
    mounted() {
        this.filter.service_location_id = this.selectedLocation;
        this.fetchDatas();
        const savedPage = localStorage.getItem("set-prices/list");
        if(savedPage){
            this.handlePageChanged(Number(savedPage));
        }
        else{
            this.handlePageChanged(1);
        }
    },
};
</script>

<template>
    <Layout>
        <Head title="Set Prices" />
        <PageHeader :title="$t('set_prices')" :pageTitle="$t('set_prices')" />
        <BRow>
            <BCol lg="12">
                <BCard no-body id="tasksList">
                    <BCardHeader class="border-0">
                        <BRow class="g-2">
                            <BCol md="3">
                                <div class="d-flex align-items-center mt-3">
                                    <label class="me-2 text-muted">{{$t("show")}}</label>
                                    <select v-model="filter.limit" @change="changeEntriesPerPage" class="form-select form-select-sm w-auto">
                                    <option v-for="option in paginatorOption" :key="option" :value="option">
                                        {{ option }}
                                    </option>
                                    </select>
                                    <label class="ms-2 text-muted">{{$t("entries")}}</label>
                                </div>
                            </BCol>
                            <BCol md="3">
                            </BCol>
                            <BCol md="auto" class="ms-auto">
                                <div class="d-flex align-items-center gap-2">
                                    <searchbar @search="fetchSearch"></searchbar>
                                    <BButton variant="danger" @click="rightOffcanvas = true"><i
                                            class="ri-filter-2-line me-1 align-bottom"></i> {{$t("filters")}}</BButton>

                                    <Link href="/set-prices/create" v-if="permissions.includes('add-price')">
                                        <BButton variant="primary" class="float-end"> <i
                                                class="ri-add-line align-bottom me-1"></i> {{$t("add_set_price")}}</BButton>
                                    </Link>
                                </div>
                            </BCol>
                        </BRow>
                    </BCardHeader>
                    <BCardBody class="border border-dashed border-end-0 border-start-0">
                        <div class="table-responsive">
                            <table class="table align-middle position-relative table-nowrap">
                                <thead class="table-active">
                                    <tr>
                                        <th scope="col"> {{$t("zone")}}</th>
                                        <th scope="col"> {{$t("transport_type")}}</th>
                                        <th scope="col"> {{$t("vehicle_type")}}</th>
                                        <th scope="col"> {{$t("status")}}</th>
                                        <th scope="col" class="mx-5"> {{$t("action")}}</th>
                                    </tr>
                                </thead>
                                <tbody v-if="results.length > 0">
                                    <tr v-for="(result, index) in results" :key="index">
                                        <td>{{ result.zone_name }}</td>
                                        <td>
                                            <span v-if="result.transport_type === 'taxi'">{{ $t('taxi') }}</span>
                                            <span v-else-if="result.transport_type === 'delivery'">{{ $t('delivery') }}</span>
                                            <span v-else>{{ $t('all') }}</span>
                                        </td>
                                        <td>{{ result.name }}</td>
                                        <td v-if="permissions.includes('toggle-price')">
                                            <div :class="{
                                                    'form-check': true,
                                                    'form-switch': true,
                                                    'form-switch-lg': true,
                                                    'form-switch-success': result.active,
                                                }">
                                                <input class="form-check-input" type="checkbox" role="switch" @click.prevent="toggleActiveStatus(result.id, !result.active)" :id="'status_' + result.id" :checked="result.active">
                                            </div>
                                        </td>
                                        <td>
                                            <div class="d-flex">
                                                <BButton class="btn btn-soft-warning btn-sm m-2" @click.prevent="editData(result)" v-if="permissions.includes('edit-price')"
                                                    data-bs-toggle="tooltip" v-b-tooltip.hover
                                                    :title="$t('edit')">
                                                    <i class='bx bxs-edit-alt bx-xs'></i>
                                                </BButton>
                                                <BButton @click.prevent="packagesData(result)"
                                                    class="btn btn-soft-success btn-sm m-2"  v-if="permissions.includes('add-package-price')"
                                                    data-bs-toggle="tooltip" v-b-tooltip.hover :title="$t('set_package_prices')">
                                                    <i class='bx bx-gift bx-xs'></i>
                                                </BButton>
                                                <BButton @click.prevent="levelUpData(result)"
                                                    class="btn btn-soft-dark btn-sm m-2"  v-if="permissions.includes('add-package-price') && show_driver_level_feature"
                                                    data-bs-toggle="tooltip" v-b-tooltip.hover :title="$t('drivers-levelup')">
                                                    <i class=' ri-medal-fill bx-xs'></i>
                                                </BButton>
                                                <BButton @click.prevent="incentiveData(result)"
                                                    class="btn btn-soft-secondary btn-sm m-2"  v-if="permissions.includes('add-package-price') && show_incentive_feature_for_driver"
                                                    data-bs-toggle="tooltip" v-b-tooltip.hover :title="$t('incentives')">
                                                    <i class='ri-coins-line bx-xs'></i>
                                                </BButton>
                                                <BButton @click.prevent="surgeData(result)"
                                                    class="btn btn-soft-danger btn-sm m-2"  v-if="permissions.includes('zone-surge')"
                                                    data-bs-toggle="tooltip" v-b-tooltip.hover :title="$t('surge')">
                                                    <i class='ri-flashlight-line bx-xs'></i>
                                                </BButton>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                                <tbody v-else>
                                    <tr>
                                        <td colspan="7" class="text-center">
                                            <img src="@assets/images/search-file.gif" alt="Loading..." style="width:100px" />
                                            <h5>{{ $t("no_data_found") }}</h5>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </BCardBody>
                </BCard>
                <pagination :paginator="paginator" @page-changed="handlePageChanged" />
            </BCol>
        </BRow>
            <!-- Success Message -->
            <div v-if="successMessage" class="custom-alert alert alert-success alert-border-left fade show" data="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" data="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>
        <!-- Filters Modal -->
        <BOffcanvas v-model="rightOffcanvas"  placement="end" @update:show="rightOffcanvas = $event">
            <template #header>
                <h5 class="offcanvas-title" id="offcanvasRightLabel">{{$t("filters")}}</h5>
                <BButton variant="white" class="text-reset p-0" @click="rightOffcanvas = false"><i
                        class="mdi mdi-close"></i></BButton>
            </template>
            <BOffcanvasBody class="p-0 overflow-hidden">
                <div class="p-4">
                    <div class="mb-3">
                        <label class="form-label">{{$t("zone")}}</label>
                        <!-- <Multiselect v-model="zone" :options="zoneOptions" :placeholder="$t('choose_zone')" label="name" /> -->
                        <Multiselect v-model="filter.zone_id" :close-on-select="false" :searchable="true"
                        multiple placeholder="Select Zone" mode="tags" :options="zoneOptions.map(type => ({value:type.id,label:type.name}))" />
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{$t("vehicle_type")}}</label>
                        <!-- <Multiselect v-model="vehicleType" :options="vehicleTypeOptions" :placeholder="$t('choose_vehicle_type')" label="name" /> -->
                        <Multiselect v-model="filter.type_id" :close-on-select="false" :searchable="true"
                        multiple placeholder="Select Vehicle Type"  mode="tags":options="vehicleTypeOptions.map(type => ({value:type.id,label:type.name}))" />
                    </div>
                </div>
                <div class="offcanvas-footer p-4 text-center hstack gap-2">
                    <BButton type="button" variant="light" class="w-100" @click="clearFilter">{{$t("clear_filter")}}</BButton>
                    <BButton type="button" variant="success" class="w-100" @click="filterData">{{$t("filters")}}</BButton>
                </div>
            </BOffcanvasBody>
        </BOffcanvas>

    </Layout>
</template>

<style>
.custom-alert {
    max-width: 600px;
    float: right;
    position: fixed;
    top: 90px;
    right: 20px;
}
.rtl .custom-alert {
  max-width: 600px;
  float: left;
  top: -300px;
  right: 10px;
}
@media only screen and (max-width: 1024px) {
  .custom-alert {
  max-width: 600px;
  float: right;
  position: fixed;
  top: 90px;
  right: 20px;
}
.rtl .custom-alert {
  max-width: 600px;
  float: left;
  top: -230px;
  right: 10px;
}
}
a{
    cursor: pointer;
}


</style>
