<template>
    <div class="mx-auto max-w-7xl dx-viewport">
        <div class="flex flex-col p-4 bg-white rounded-lg drop-shadow-sm min-h-96">
            <nav class="flex flex-wrap gap-4 mb-4" aria-label="Tabs">
                <a v-for="(tab, index) in tabs" :key="tab.name" @click="changeTab(index)"
                    :class="[tabChecked === index ? 'bg-primary-100 text-primary-900' : 'text-gray-500 hover:text-gray-700', 'cursor-pointer px-3 py-2 font-medium text-sm rounded-md']"
                    :aria-current="tabChecked === index ? 'page' : undefined">{{ tab.name }}</a>
            </nav>

            <!-- Filters -->
            <section aria-labelledby="filter-heading">
                <h2 id="filter-heading" class="sr-only">Filters</h2>

                <div class="pb-4 bg-white">
                    <div class="flex flex-wrap items-center px-0 mx-auto lg:justify-between max-w-7xl">
                        <Menu as="div"
                            class="relative flex-1 inline-block w-full mb-2 mr-2 text-left lg-2 lg:mb-0 md:flex-none lg:w-fit">
                            <MenuButton
                                class="flex inline-flex items-center justify-between w-full px-2 py-2 text-xs font-medium text-gray-700 border border-gray-100 rounded-md whitespace-nowrap group hover:text-gray-900">
                                Cari dengan : <b>{{ searchOptions.find(option => option.current === true)?.name }}</b>
                                <ChevronDownIcon
                                    class="flex-shrink-0 w-3 h-3 ml-1 -mr-1 text-gray-400 group-hover:text-gray-500"
                                    aria-hidden="true" />
                            </MenuButton>

                            <transition enter-active-class="transition duration-100 ease-out"
                                enter-from-class="transform scale-95 opacity-0"
                                enter-to-class="transform scale-100 opacity-100"
                                leave-active-class="transition duration-75 ease-in"
                                leave-from-class="transform scale-100 opacity-100"
                                leave-to-class="transform scale-95 opacity-0">
                                <MenuItems
                                    class="absolute left-0 z-10 w-40 mt-2 origin-top-left bg-white rounded-md shadow-2xl ring-1 ring-black ring-opacity-5 focus:outline-none">
                                    <div class="py-1">
                                        <MenuItem v-for="(option, index) in searchOptions" :key="option.name"
                                            v-slot="{ active }">
                                        <a @click="handleChangeSearchOption(index)"
                                            :class="[option.current ? 'font-medium text-gray-900' : 'text-gray-500', active ? 'bg-gray-100' : '', 'block px-4 py-2 text-xs']">{{
                                                option.name }}</a>
                                        </MenuItem>
                                    </div>
                                </MenuItems>
                            </transition>
                        </Menu>

                        <PopoverGroup class="items-center flex-1 mb-2 sm:mr-2 lg:mb-0 md:flex-none lg:w-fit">
                            <Popover class="relative inline-block w-full px-2 text-left">
                                <PopoverButton
                                    class="flex inline-flex items-center justify-between w-full px-2 py-2 text-xs text-gray-700 border border-gray-100 rounded-md whitespace-nowrap group">
                                    <span class="flex-1 text-left">Order by</span>
                                    <span v-if="orderByOptions.filter(option => option.checked).length > 0"
                                        class="ml-1.5 rounded bg-gray-100 py-0.2 px-1.5 text-[10px] flex-none font-bold tabular-nums text-gray-500">
                                        {{ orderByOptions.filter(option => option.checked).length }}
                                    </span>
                                    <ChevronDownIcon
                                        class="flex-none flex-shrink-0 w-3 h-3 ml-1 -mr-1 text-gray-400 group-hover:text-gray-500"
                                        aria-hidden="true" />
                                </PopoverButton>

                                <transition enter-active-class="transition duration-100 ease-out"
                                    enter-from-class="transform scale-95 opacity-0"
                                    enter-to-class="transform scale-100 opacity-100"
                                    leave-active-class="transition duration-75 ease-in"
                                    leave-from-class="transform scale-100 opacity-100"
                                    leave-to-class="transform scale-95 opacity-0">
                                    <PopoverPanel
                                        class="absolute right-0 z-10 p-4 mt-2 origin-top-right bg-white rounded-md shadow-2xl ring-1 ring-black ring-opacity-5 focus:outline-none">
                                        <form class="space-y-4">
                                            <div v-for="(option, optionIdx) in orderByOptions" :key="option.value"
                                                class="flex items-center">
                                                <input :value="option.value" type="checkbox" :checked="option.checked"
                                                    @change="handleChangeOrderBy(optionIdx, !option.checked)"
                                                    class="w-4 h-4 text-indigo-600 border-gray-300 rounded focus:ring-indigo-500" />
                                                <label
                                                    class="pr-6 ml-3 text-sm font-medium text-gray-900 whitespace-nowrap">
                                                    {{ option.label }}
                                                </label>
                                            </div>
                                        </form>
                                    </PopoverPanel>
                                </transition>
                            </Popover>
                        </PopoverGroup>

                        <div class="relative inline-block mb-2 mr-2 text-left lg:mb-0 grow xs:mt-0 md:flex-1">
                            <div class="w-full md:max-w-[220px]">
                                <vue-tailwind-datepicker v-model="dateValue" i18n="id" :options="datePickerOptions"
                                    :formatter="formatter" placeholder="Filter Tanggal" :auto-apply="false"
                                    input-classes="bg-white text-gray-900 border rounded-md border-gray-100 text-xs px-2 py-2 focus:ring-0 focus:border-gray-100"
                                    :disabled="false" as-single use-range separator=" s/d " />
                            </div>
                        </div>

                        <form @submit.prevent="handleSubmitSearch"
                            class="flex lg:mt-0 items-center w-full !py-1 border-0 border-gray-100 rounded-lg bg-gray-50 flex-between lg:w-fit">
                            <!-- <ButtonDropdown button-style="bg-primary-50" text-style="text-xs" :data="searchBy"
                                :checked="searchByChecked" @update:checked="handleSearchByChange" />

                            <div class="h-5 mx-2 border-r-2 border-gray-100"></div>

                            <ButtonPrimary label="Order By"
                                class-name="bg-primary-50 mt-0 py-2 text-xs !px-2 !text-dark font-normal rounded-md mr-4" /> -->

                            <InputText class-name="flex-1 bg-transparent text-xs !py-0 px-2 placeholder:text-gray-600"
                                placeholder="Masukan Pencarian" />

                            <ButtonPrimary type="submit"
                                class-name="mr-1 flex flex-row bg-primary-500 !mt-0 text-xs !px-2 !text-white font-normal rounded-md">
                                <MagnifyingGlassIcon class="w-4 h-4 lg:mr-2" aria-hidden="true" />
                                <span class="hidden lg:block">Cari</span>
                            </ButtonPrimary>
                        </form>
                    </div>
                </div>

                <!-- Active filters -->
                <div class="border-gray-100 bg-gray-50 border-x border-y"
                    v-if="orderByOptions.filter((e) => e.checked).length > 0">
                    <div class="px-2 py-3 mx-auto max-w-7xl sm:flex sm:items-center sm:px-2 lg:px-4">
                        <h3 class="text-xs font-medium text-gray-700">
                            Order by
                            <span class="sr-only">, active</span>
                        </h3>

                        <div aria-hidden="true" class="hidden w-px h-5 bg-gray-300 sm:ml-4 sm:block" />

                        <div class="mt-2 sm:mt-0 sm:ml-4">
                            <div class="flex flex-wrap items-center -m-1">
                                <span v-for="(order, index) in orderByOptions.filter((e) => e.checked)" :key="order.value"
                                    class="m-1 inline-flex items-center rounded-full border border-gray-100 bg-white py-1.5 pl-3 pr-2 text-xs font-medium text-gray-900">
                                    <span>{{ order.label }}</span>
                                    <button @click="handleChangeOrderBy(index, !order.checked)" type="button"
                                        class="inline-flex flex-shrink-0 w-4 h-4 p-1 ml-1 text-gray-400 rounded-full hover:bg-gray-200 hover:text-gray-500">
                                        <span class="sr-only">Remove filter for {{ order.label }}</span>
                                        <svg class="w-2 h-2" stroke="currentColor" fill="none" viewBox="0 0 8 8">
                                            <path stroke-linecap="round" stroke-width="1.5" d="M1 1l6 6m0-6L1 7" />
                                        </svg>
                                    </button>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <DxDataGrid :data-source="dataSource" :remote-operations="false" :allow-column-reordering="true"
                :row-alternation-enabled="true" :show-borders="true" @content-ready="onContentReady">
                <!-- <DxColumn :group-index="0" data-field="Product" /> -->
                <DxColumn data-field="Amount" caption="Sale Amount" data-type="number" format="currency"
                    alignment="right" />
                <DxColumn :allow-grouping="false" data-field="Discount" caption="Discount %" data-type="number"
                    format="percent" alignment="right" cell-template="discountCellTemplate" css-class="bullet" />
                <DxColumn data-field="SaleDate" data-type="date" />
                <DxColumn data-field="Region" data-type="string" />
                <DxColumn data-field="Sector" data-type="string" />
                <DxColumn data-field="Channel" data-type="string" />
                <DxColumn :width="150" data-field="Customer" data-type="string" />

                <!-- <DxGroupPanel :visible="true" /> -->
                <!-- <DxFilterRow :visible="true" /> -->
                <!-- <DxSearchPanel :visible="true" :highlight-case-sensitive="true" /> -->
                <DxGrouping :auto-expand-all="false" />
                <DxPager :allowed-page-sizes="pageSizes" :show-page-size-selector="true" />
                <DxPaging :page-size="10" />
                <DxSelection mode="single" />
            </DxDataGrid>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, type PropType } from 'vue';
import VueTailwindDatepicker from 'vue-tailwind-datepicker'
import {
    Dialog,
    DialogPanel,
    Disclosure,
    DisclosureButton,
    DisclosurePanel,
    Menu,
    MenuButton,
    MenuItem,
    MenuItems,
    Popover,
    PopoverButton,
    PopoverGroup,
    PopoverPanel,
    TransitionChild,
    TransitionRoot,
} from '@headlessui/vue'

import {
    DxDataGrid,
    DxColumn,
    DxGrouping,
    DxGroupPanel,
    DxPager,
    DxPaging,
    DxSearchPanel,
    DxFilterRow,
    DxSelection,
} from 'devextreme-vue/data-grid';
import DataSource from 'devextreme/data/data_source';
import 'devextreme/data/odata/store';
import ButtonDropdown from '@/components/Buttons/ButtonDropdown.vue';
import ButtonPrimary from '@/components/Buttons/ButtonPrimary.vue';
import InputText from '@/components/InputText.vue';
import { MagnifyingGlassIcon, ChevronDownIcon } from '@heroicons/vue/24/solid';
import { XMarkIcon } from '@heroicons/vue/24/outline'

const tabs = [
    { name: 'Gangguan' },
    { name: 'Keluhan' },
    { name: 'Gangguan Diperhatikan' },
]
const tabChecked = ref(0)

function changeTab(index: number) {
    tabChecked.value = index
}

const handleSubmitSearch = (e: any) => {
    e.preventDefault()
}

let collapsed = false;
const dataSource = new DataSource({
    store: {
        type: 'odata',
        url: 'https://js.devexpress.com/Demos/SalesViewer/odata/DaySaleDtoes',
        key: 'Id',
        beforeSend(request) {
            const year = new Date().getFullYear() - 1;
            request.params.startDate = `${year}-05-10`;
            request.params.endDate = `${year}-5-15`;
        },
    },
});

const pageSizes = [10, 25, 50, 100];
const onContentReady = (e: any) => {
    if (!collapsed) {
        e.component.expandRow(['EnviroCare']);
        collapsed = true;
    }
};

const searchOptions = ref([
    { name: 'Nama Lapor', current: true },
    { name: 'Nama', current: false },
    { name: 'Alamat', current: false },
])
function handleChangeSearchOption(index: number) {
    searchOptions.value.forEach((option, i) => {
        if (i === index) {
            option.current = true
        } else {
            option.current = false
        }
    })
}

const orderByOptions = ref([
    { value: 'status', label: 'Status', checked: false },
    { value: 'tanggal', label: 'Tanggal Lapor', checked: false },
])
function handleChangeOrderBy(index: number, data: boolean) {
    orderByOptions.value.forEach((option, i) => {
        if (i === index) {
            option.checked = data
        }
    })
}


type ModelType = string | [Date, Date] | { start: string | Date; end: string | Date; } | { startDate: string | Date; endDate: string | Date; };
const emptyDate: ModelType = '';
const dateValue = ref(emptyDate)

const formatter = ref({
    date: 'DD MMMM YYYY',
    month: 'MMMM'
})

const datePickerOptions = ref({
    shortcuts: {
        today: 'Hari ini',
        yesterday: 'Kemarin',
        past: (period: any) => period + ' hari terakhir',
        currentMonth: 'Bulan ini',
        pastMonth: 'Bulan lalu'
    },
    footer: {
        apply: 'Terapkan',
        cancel: 'Batal'
    },
})

</script>
