<template>
    <Table
        url="/ranger/inspection/api/v1/items"
        rowKey="itemId"
        addBtn
        :buttons="buttons"
        ref="table"
        :scroll="{ x: true }"
    >
        <template #search="{ query }">
            <MoreItem label="地区">
                <UrlSelect
                    url="/ranger/inspection/api/v1/region/list"
                    v-model="query.regionId"
                    labelFiled="regionName"
                    valueFiled="regionId"
                />
            </MoreItem>
            <MoreItem label="地点">
                <UrlSelect
                    v-model="query.placeId"
                    :url="
                        query.regionId ? `/ranger/inspection/api/v1/place/regions/${query.regionId}/list` : ''
                    "
                    labelFiled="placeName"
                    valueFiled="placeId"
                />
            </MoreItem>
            <MoreItem label="单元">
                <UrlSelect
                    v-model="query.unitId"
                    :url="query.placeId ? `/ranger/inspection/api/v1/units/places/${query.placeId}/list` : ''"
                    labelFiled="unitName"
                    valueFiled="unitId"
                />
            </MoreItem>
            <MoreItem label="项目名称">
                <a-input v-model="query.inputValue" placeholder="请输入项目名称" />
            </MoreItem>
        </template>

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

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

        <a-table-column title="项目名称" data-index="itemName" />
        <a-table-column title="所属单元" data-index="unitName" />
        <a-table-column title="数据类型">
            <template #default="row">
                <span> {{ dataType[row.dataType] }}</span>
            </template>
        </a-table-column>
        <a-table-column title="项目描述">
            <template #default="row">
                <span v-if="row.dataType === 'TEXT'"> {{ row.defaultText }}</span>
                <span v-if="row.dataType === 'OPTION'">
                    {{ `正常值:${row.normalValue}  异常值:${row.abnormalValue}` }}</span
                >
                <span v-if="row.dataType === 'NUMERIC'">
                    {{
                        `默认值:${row.defaultValue} 上限值:${row.upperLimitValue}  下限值:${row.lowerLimitValue}`
                    }}</span
                >
            </template>
        </a-table-column>
        <a-table-column title="修改人" data-index="editorName" />
        <a-table-column title="修改时间" 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: '编辑', click: this.edit },
                { label: '详情', click: this.view },
                {
                    type: 'confirm',
                    url: (row) => `/ranger/inspection/api/v1/items/${row.itemId}`,
                    after: this.refresh,
                },
            ],
            dataType: {
                TEXT: '文本类型',
                OPTION: '选项类型',
                NUMERIC: '数值类型',
            },
        };
    },
    methods: {
        refresh() {
            this.$refs.table.getData();
        },
        edit(row) {
            this.$refs.table.show({ row, title: '编辑', type: 'edit' });
        },
        view(row) {
            this.$refs.table.show({ row, title: '查看', type: 'view' });
        },
    },
};
</script>
