<template>
    <Table
        url="/ranger/inspection/api/v1/items"
        rowKey="itemId"
        addBtn
        :buttons="buttons"
        ref="table"
        :scroll="{ x: true }"
    >
        <template #search="{ query }">
            <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')">
                <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>
            <MoreItem :label="$t('table.itemName')">
                <a-input v-model="query.inputValue" :placeholder="$t('input.placeholder')" />
            </MoreItem>
        </template>

        <!-- <template #operation>
            <a-button @click="download">导出</a-button>
        </template> -->

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

        <a-table-column :title="$t('table.itemName')" data-index="itemName" />
        <a-table-column :title="$t('table.unitName')" data-index="unitName" />
        <a-table-column :title="$t('table.dataType')">
            <template #default="row">
                <span> {{ $t(`other.${row.dataType}`) }}</span>
            </template>
        </a-table-column>
        <a-table-column :title="$t('table.describe')">
            <template #default="row">
                <span v-if="row.dataType === 'TEXT'"> {{ row.defaultText }}</span>
                <span v-if="row.dataType === 'OPTION'">
                    {{
                        `${$t('other.normal')}:${row.normalValue}  ${$t('other.outliers')}:${
                            row.abnormalValue
                        }`
                    }}</span
                >
                <span v-if="row.dataType === 'NUMERIC'">
                    {{
                        `${$t('other.defaultValue')}:${row.defaultValue} 
                        ${$t('other.upperLimit')}:${row.upperLimitValue}  
                        ${$t('other.lowerLimit')}:${row.lowerLimitValue}`
                    }}</span
                >
            </template>
        </a-table-column>
        <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/items/${row.itemId}`,
                    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>
