<template>
    <div class="ele-body">
        <a-card :bordered="false">
            <a-modal
                v-model:visible="showEdit"
                :title="form.surfaceWaterEvaluationStandardId !== undefined ? '修改' : '添加'"
                :confirm-loading="loading"
                :width="500"
                :body-style="{ paddingBottom: '8px' }"
                @ok="save"
            >
                <a-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    :label-col="{ md: { span: 6 }, sm: { span: 24 } }"
                    :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }"
                >
                    <a-form-item label="指标名称" name="minLeq">
                        <a-input v-model:value="form.indicatorName" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="类别" name="category">
                        <a-select v-model:value="form.category">
                            <a-select-option v-for="item in categoryOptions" :key="item.value">{{item.label}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="比较符号" name="comparisonSymbol">
                        <a-select v-model:value="form.comparisonSymbol">
                            <a-select-option v-for="item in comparOptions" :key="item.value">{{item.label}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="指数类别值" name="categoryValue">
                        <a-input type="number" v-model:value="form.categoryValue"  />
                    </a-form-item>


                </a-form>
            </a-modal>
            <!-- 表格 -->
            <ele-pro-table
                v-model:selection="selectionList"
                ref="table"
                row-key="surfaceWaterEvaluationStandardId"
                :datasource="url"
                :columns="columns"
                :where="where"
                :need-page="false"
                :scroll="{ x: 'max-content' }"
            >
                <template #categoryValue="{ record }">
                    {{comparOptions.find(item=>item.value == record.comparisonSymbol).label}}
                    {{record.categoryValue}}
                </template>
                <template #toolbar>
                    <a-space>
                        <a-button @click="openEdit" type="primary">新增</a-button>
                        <a-popconfirm
                            :disabled="selectionList.length == 0"
                            :title="`确认删除${selectionList.length}条数据吗?`"
                            ok-text="Yes"
                            cancel-text="No"
                            @confirm="removeBatch"
                        >
                            <a-button
                                :disabled="selectionList.length == 0"
                                type="primary"
                                ghost
                                danger
                            >删除
                            </a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
                <template #action="{ record }">
                    <a-space>
                        <a-button
                            @click="openEdit(record)"
                            type="primary"
                            shape="round"
                            size="small"
                        >修改</a-button
                        >
                        <a-popconfirm
                            :title="`确认删除这条数据吗?`"
                            ok-text="Yes"
                            cancel-text="No"
                            @confirm="remove(record)"
                        >
                            <a-button type="primary" danger shape="round" size="small"
                            >删除</a-button
                            >
                        </a-popconfirm>
                    </a-space>
                </template>
            </ele-pro-table>
        </a-card>
    </div>
    <!-- 编辑弹窗 -->
</template>

<script>
// import _ from "lodash";
import {
    pageSurfaceWaterStandardAllUrl,
    saveSurfaceWaterStandard,
    updateSurfaceWaterStandard,
    removeBatchSurfaceWaterStandard,
    removeSurfaceWaterStandard
} from "@/api/ecology/water/surface-water-standard";
// import moment from "moment";
export default {
    name: "SurfaceWaterStandard",
    components: {},
    data() {
        return {
            data: [],
            // 表格数据接口
            url: pageSurfaceWaterStandardAllUrl,
            selection: [],
            // 表格列配置
            columns: [
                {
                    title: "指标名称",
                    dataIndex: "indicatorName",
                },
                {
                    title: "类别",
                    dataIndex: "category",
                },
                {
                    title: "指数类别值",
                    slots: {
                        customRender: "categoryValue",
                    },
                },
                {
                    title: "操作",
                    key: "action",
                    width: 150,
                    align: "center",
                    fixed: "right",
                    slots: {
                        customRender: "action",
                    },
                },
            ],
            comparOptions:[
                {label:"=",value:"eq"},
                {label:"≤",value:"le"},
                {label:"≥",value:"ge"},
                {label:"<",value:"lt"},
                {label:">",value:"gt"},
            ],
            categoryOptions:[
                {label:"I",value:"I"},
                {label:"II",value:"II"},
                {label:"III",value:"III"},
                {label:"IV",value:"IV"},
                {label:"V",value:"V"},
            ],


            // 表格搜索条件
            where: {},
            // 表格选中数据
            selectionList: [],
            // 是否显示编辑弹窗
            showEdit: false,
            // 表单数据
            form: {},
            loading: false,
            rules: {
                indicatorName:[{required: true,message: '请输入指标名称'}],
                category: [{required: true,message: '请选择类别',},],
                comparisonSymbol: [{required: true,message: '请选择比较符',},],
                categoryValue: [{required: true,message: '请输入类别值',},],
            },
        };
    },
    mounted() {
    },
    methods: {
        minLeqChange(e){
            console.log(e)
        },
        /* 刷新表格 */
        reload() {

            this.$refs.table.reload({
                where: this.where,
            });
        },
        /* 重置搜索 */
        reset() {
            this.where = {};
            this.reload();
        },
        /* 打开编辑弹窗 */
        openEdit(row) {
            // cloneRecord.mi
            this.form = Object.assign({}, row);
            this.showEdit = true;
            this.$nextTick(() => {
                this.$refs.form.clearValidate(); // 清除表单验证信息
            });
        },
        async save() {
            await this.$refs.form.validate();
            const hide = this.$message.loading('请求中..', 0);
            const form = this.form;
            if (form.surfaceWaterEvaluationStandardId) {
                updateSurfaceWaterStandard(form)
                    .then((res) => {
                        if (res.data.code == 0) {
                            this.showEdit = false;
                            this.$message.success(res.data.msg);
                            this.reload();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    })
                    .catch((error) => {
                        this.$message.error(error.message);
                    })
                    .finally(() => {
                        hide();
                    });
            } else {
                saveSurfaceWaterStandard(form)
                    .then((res) => {
                        if (res.data.code == 0) {
                            this.showEdit = false;
                            this.$message.success(res.data.msg);
                            this.reload();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    })
                    .catch((error) => {
                        this.$message.error(error.message);
                    })
                    .finally(() => {
                        hide();
                    });
            }
        },
        /* 删除单个 */
        remove(row) {
            const hide = this.$message.loading('请求中..', 0);
            removeSurfaceWaterStandard(row.surfaceWaterEvaluationStandardId).then((res) => {
                hide();
                if (res.data.code === 0) {
                    this.$message.success(res.data.msg);
                    this.reload();
                } else {
                    this.$message.error(res.data.msg);
                }
            }).catch(e => {
                hide();
                this.$message.error(e.message);
            })
        },
        /* 批量删除 */
        removeBatch() {
            const hide = this.$message.loading('请求中..', 0);
            const ids = this.selectionList.map((item) => item.surfaceWaterEvaluationStandardId);
            removeBatchSurfaceWaterStandard(ids).then((res) => {
                if (res.data.code === 0) {
                    this.$message.success(res.data.msg);
                    this.reload();
                } else {
                    this.$message.error(res.data.msg);
                }
            }).catch(e => {
                this.$message.error(e.message);
            }).finally(() => {
                hide();
            })
        }

    },
};
</script>

<style scoped lang="less">
</style>