<template>
    <Table
        url="/ranger/inspection/api/v1/region/page"
        rowKey="regionId"
        addBtn
        :buttons="buttons"
        ref="table"
    >
        <template #search="{ query }">
            <MoreItem :label="$t('table.organization')">
                <UrlSelect
                    v-model="query.organizationId"
                    url="/api/v1/users/organizations"
                    labelFiled="orgName"
                    valueFiled="orgId"
                />
            </MoreItem>
            <MoreItem :label="$t('table.regionName')">
                <a-input v-model="query.regionName" :placeholder="$t('input.placeholder')" />
            </MoreItem>
        </template>

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

        <a-table-column :title="$t('table.regionName')" data-index="regionName" />
        <a-table-column :title="$t('table.organization')" data-index="organizationName" />
        <a-table-column :title="$t('table.remark')" data-index="remark" />
        <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 {
            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/region/${row.regionId}`,
                    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>
