<template>
    <Table
        url="/ranger/inspection/api/v1/place/page"
        rowKey="placeId"
        :selected.sync="selected"
        addBtn
        :buttons="buttons"
        ref="table"
    >
        <template #search="{ query }">
            <MoreItem :label="$t('select.region')">
                <UrlSelect
                    v-model="query.regionId"
                    url="/ranger/inspection/api/v1/region/list"
                    labelFiled="regionName"
                    valueFiled="regionId"
                    :placeholder="$t('select.placeholder')"
                />
            </MoreItem>
            <MoreItem :label="$t('input.search')">
                <a-input v-model="query.inputValue" :placeholder="$t('input.search.one.placeholder')" />
            </MoreItem>
        </template>

        <template #operation>
            <a-button @click="download">{{ $t('table.export') }}</a-button>
        </template>

        <template #drawer="drawer">
            <AddAndEdit v-if="drawer.type !== 'exempt'" v-bind="drawer" />
            <Exempt v-else v-bind="drawer" />
        </template>

        <a-table-column :title="$t('table.placeName')" data-index="placeName" />
        <a-table-column :title="$t('table.regionName')" data-index="regionName" />
        <a-table-column :title="$t('table.QrCode')" data-index="qrCode" />
        <a-table-column :title="$t('table.ElectronicTags')" data-index="electronicTag" />
        <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 { downloadFileByUrl } from '@/utils';
import AddAndEdit from './add_edit.vue';
import Exempt from './exempt.vue';

export default {
    components: { Table, UrlSelect, MoreItem, AddAndEdit, Exempt },
    data() {
        return {
            buttons: [
                { label: this.$t('table.edit'), click: this.edit },
                { label: this.$t('table.details'), click: this.view },
                { label: this.$t('table.exemption'), click: this.exempt },
                {
                    type: 'confirm',
                    url: (row) => `/ranger/inspection/api/v1/place/${row.placeId}`,
                    after: this.refresh,
                },
            ],
            selected: {},
        };
    },
    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' });
        },
        exempt(row) {
            this.$refs.table.show({ row, title: this.$t('table.exemption'), type: 'exempt' });
        },
        download() {
            if (!this.selected?.keys?.length) return this.$message.error(this.$t('table.export.tip'));
            downloadFileByUrl(
                `/ranger/inspection/api/v1/place/exportQrCodeData?placeIdList=${this.selected.keys.join(
                    ',',
                )}`,
            );
        },
    },
};
</script>
