<template>
    <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk"
        @cancel="handleCancel" okText="保存" cancelText="关闭">

        <a-spin :spinning="confirmLoading">
            <a-form-model ref="ruleForm" :model="form" :rules="rules">
                <a-form-model-item label="表名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tableName">
                    <a-input v-model="form.tableName" placeholder="请输入"></a-input>
                </a-form-model-item>
                <a-form-model-item label="判定字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="field">
                    <a-tree-select v-model="form.field" style="width: 100%"
                        :tree-data="treeData"  search-placeholder="请选项" 
                        :dropdownStyle="{ maxHeight: '400px', overflowY: 'auto'}"/>
                </a-form-model-item>
                <a-form-model-item label="策略" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="strategy">
                    <a-select v-model="form.strategy">
                        <a-select-option :value="1">填充0</a-select-option>
                        <a-select-option :value="2">取均值</a-select-option>
                        <a-select-option :value="3">复制上条数据</a-select-option>
                        <a-select-option :value="4">复制下条数据</a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="最大值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxVal">
                    <a-input v-model="form.maxVal" placeholder="请输入"></a-input>
                </a-form-model-item>
                <a-form-model-item label="最小值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="minVal">
                    <a-input v-model="form.minVal" placeholder="请输入"></a-input>
                </a-form-model-item>
            </a-form-model>
        </a-spin>
    </a-modal>
</template>

<script>
import JCron from "@/components/jeecg/JCron";
import JSelectMultiple from '@/components/jeecg/JSelectMultiple'
import { createRules, updateRules, queryById,querytableColumns } from '@/api/rangeValidatorRules'


export default {
    name: "rulesModal",
    components: {
        JCron,
        JSelectMultiple
    },
    data() {
        return {
            title: "操作",
            visible: false,
            model: {},
            confirmLoading: false,
            dataType: null,
            form: {
                tableName: null,
                strategy: null,
                maxVal: null,
                minVal: null,
                field:null
            },
            rules: {
                tableName: [{ required: true, message: '请输入表名', trigger: 'blur' }],
                strategy: [{ required: true, message: '策略', trigger: 'change' }],
                maxVal: [{ required: true, message: '请选输入最大值', trigger: 'change' }],
                minVal: [{ required: true, message: '请选输入最小值', trigger: 'change' }],
                field: [{ required: true, message: '请选选择字段', trigger: 'change' }]
            },
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            treeData: [],
            checkedList: []
        }
    },
    created() {
    },
    methods: {
        getQuerytableColumns(dataType, tableName,shipModel,shipNumber) {
            querytableColumns({ dataType: dataType, tableName: tableName,shipModel:shipModel,shipNumber:shipNumber }).then(res => {
                if (res.code == 200) {
                    this.treeData = []
                    res.result.forEach(element => {
                        this.treeData.push({
                            title: element,
                            value: element,
                            key: element,
                        })
                    });
                }
            })
        },
        add(schemaName,tableName, dataType,shipModel,shipNumber) {
            this.form =  {
                tableName: tableName,
                strategy: null,
                maxVal: null,
                minVal: null,
                field:null,
                schemaName:schemaName
            },
            this.visible = true;
            this.dataType = dataType
            this.getQuerytableColumns(dataType, tableName,shipModel,shipNumber)
        },
        eidt(record, dataType,shipModel,shipNumber) {
            if (record.id) {
                queryById({ id: record.id }).then(res => {
                    if (res.code == 200) {
                        this.form = res.result
                    }
                })
            } else {
                this.form = {
                    tableName: null,
                    strategy: null,
                    maxVal: null,
                    minVal: null,
                    field:null,
                    tableName: record.tableName,
                    schemaName:record.schemaName
                }
            }
            this.getQuerytableColumns(dataType, record.tableName,shipModel,shipNumber)
            this.visible = true;

        },
        close() {
            this.visible = false;
        },
        handleOk() {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    if(this.form.maxVal < this.form.minVal){
                        this.$notification.error({
                            message: '系统提示',
                            description: '最大值不可小于最小值'
                        })
                        return false;
                    }
                    this.confirmLoading = true;
                    if (!this.form.id) {
                        createRules(this.form).then(res => {
                            if (res.code === 200) {
                                this.$notification.success({
                                    message: '系统提示',
                                    description: res.message
                                })
                                this.confirmLoading = false
                                this.$emit('fatherMethod')
                                this.handleCancel()
                            } else {
                                this.$notification.error({
                                    message: '系统提示',
                                    description: '操作失败'
                                })
                                this.confirmLoading = false
                            }
                        })
                    } else {
                        updateRules(this.form).then(res => {
                            if (res.code === 200) {
                                this.$notification.success({
                                    message: '系统提示',
                                    description: res.message
                                })
                                this.confirmLoading = false
                                this.$emit('fatherMethod')
                                this.handleCancel()
                            } else {
                                this.$notification.error({
                                    message: '系统提示',
                                    description: '操作失败'
                                })
                                this.confirmLoading = false
                            }
                        })
                    }
                }
            });
        },
        handleCancel() {
            this.close()
        },
    }
}
</script>

<style scoped></style>