<template>
    <Table
        url="/ranger/inspection/api/v1/exemptions"
        rowKey="exemptionId"
        addBtn
        :buttons="buttons"
        ref="table"
        :defaultQuery="defaultQuery"
    >
        <template #search="{ query }">
            <MoreItem :label="$t('table.exemptionType')">
                <a-select
                    class="tw-w-full"
                    v-model="query.exemptionType"
                    :placeholder="$t('select.placeholder')"
                >
                    <a-select-option value="PLACE">{{ $t('select.option.place') }}</a-select-option>
                    <a-select-option value="UNIT">{{ $t('select.option.unit') }}</a-select-option>
                </a-select>
            </MoreItem>
            <MoreItem :label="$t('table.region')">
                <UrlSelect
                    url="/ranger/inspection/api/v1/region/list"
                    v-model="query.regionId"
                    labelFiled="regionName"
                    valueFiled="regionId"
                    :placeholder="$t('select.placeholder')"
                />
            </MoreItem>
            <MoreItem :label="$t('table.place')">
                <UrlSelect
                    v-model="query.placeId"
                    :url="
                        query.regionId ? `/ranger/inspection/api/v1/place/regions/${query.regionId}/list` : ''
                    "
                    labelFiled="placeName"
                    valueFiled="placeId"
                    :placeholder="$t('select.placeholder')"
                />
            </MoreItem>
            <MoreItem :label="$t('table.unit')" v-if="query.exemptionType === 'UNIT'">
                <UrlSelect
                    v-model="query.unitId"
                    :url="query.placeId ? `/ranger/inspection/api/v1/units/places/${query.placeId}/list` : ''"
                    labelFiled="unitName"
                    valueFiled="unitId"
                    :placeholder="$t('select.placeholder')"
                />
            </MoreItem>
        </template>

        <template #drawer="drawer">
            <AddAndEdit v-bind="drawer" />
        </template>

        <a-table-column :title="$t('table.exemptionName')" data-index="exemptionName" />
        <a-table-column :title="$t('table.region')" data-index="regionName" />
        <a-table-column :title="$t('table.type')">
            <template #default="row">
                <span>{{
                    row.exemptionType === 'PLACE' ? $t('select.option.place') : $t('select.option.unit')
                }}</span>
            </template>
        </a-table-column>
        <a-table-column :title="$t('table.time.start')" data-index="startTime" />
        <a-table-column :title="$t('table.time.start')" data-index="endTime" />
        <a-table-column :title="$t('table.reason')" data-index="exemptionReason" />
        <a-table-column :title="$t('table.state')" data-index="exemptionStateName" />
        <a-table-column :title="$t('table.reviser')" data-index="editorName" />
        <a-table-column :title="$t('table.revisionTime')" data-index="editTime" />
    </Table>
</template>

<script>
import Table from '@/components/table/table.vue';
import UrlSelect from '@/components/MySelect/url_select.vue';
import MoreItem from '@/components/table/more_item.vue';
import AddAndEdit from './add_edit.vue';

export default {
    components: { Table, UrlSelect, MoreItem, AddAndEdit },
    data() {
        return {
            defaultQuery: {
                exemptionType: 'PLACE',
            },
            buttons: [
                { label: this.$t('table.edit'), click: this.edit },
                { label: this.$t('table.details'), click: this.view },
                {
                    type: 'confirm',
                    url: (row) => `ranger/inspection/api/v1/exemptions/${row.exemptionId}`,
                    after: this.refresh,
                },
            ],
        };
    },
    methods: {
        refresh() {
            this.$refs.table.getData();
        },
        edit(row) {
            this.$refs.table.show({ row, title: this.$t('table.edit'), type: 'edit' });
        },
        view(row) {
            this.$refs.table.show({ row, title: this.$t('table.details'), type: 'view' });
        },
    },
};
</script>
