<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" :show-checked-strategy="SHOW_PARENT" search-placeholder="请选项" 
                        :dropdownStyle="{ maxHeight: '400px', overflowY: 'auto'}"/>
                </a-form-model-item>
                <a-form-model-item label="识别算法" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="algorithm">
                    <a-select v-model="form.algorithm">
                        <a-select-option :value="1">标准差法</a-select-option>
                    </a-select>
                </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>
                </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/outlierReplaceRules'
import { TreeSelect } from 'ant-design-vue';
const SHOW_PARENT = TreeSelect.SHOW_PARENT;

export default {
    name: "rulesModal",
    components: {
        JCron,
        JSelectMultiple
    },
    data() {
        return {
            SHOW_PARENT,
            title: "操作",
            visible: false,
            model: {},
            confirmLoading: false,
            form: {
                tableName: null,
                field: null,
                algorithm: null,
                strategy: null,
                schemaName:""
            },
            rules: {
                tableName: [{ required: true, message: '请输入表名', trigger: 'blur' }],
                field: [{ required: true, message: '请选择', trigger: 'change' }],
                strategy: [{ required: true, message: '请选择', trigger: 'change' }],
                algorithm: [{ required: true, message: '请选择', trigger: 'change' }]
            },
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            treeData: [],
            checkedList: []
        }
    },
    created() {
        // this.loadData();
    },
    methods: {
        getQuerytableColumns(dataType, tableName) {
            querytableColumns({ dataType: dataType, tableName: tableName }).then(res => {
                if (res.code == 200) {
                    this.treeData = []
                    res.result.forEach(element => {
                        this.treeData.push({
                            title: element,
                            value: element,
                            key: element,
                        })
                    });
                }
            })
        },
        handleChange(value) {
            if (value == 1) {
                this.form.judgmentField = 'ALL'
            } else {

            }
        },
        add(schemaName,tableName, dataType) {
            this.getQuerytableColumns(dataType, tableName)
            this.form = {
                tableName: tableName,
                field: null,
                algorithm: null,
                strategy: null,
                schemaName :schemaName
            }
            this.checkedList = []
            this.visible = true;
        },
        eidt(record, dataType1) {
            if (record.id) {
                queryById({ id: record.id }).then(res => {
                    if (res.code == 200) {
                        this.form = res.result
                    }
                })
            } else {
                this.form = {
                    id: null,
                    tableName: record.tableName,
                    field: null,
                    algorithm: null,
                    strategy: null,
                    schemaName:record.schemaName
                }
            }
            this.getQuerytableColumns(dataType1, record.tableName)
            this.visible = true;
        },
        close() {
            this.visible = false;
        },
        handleOk() {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.confirmLoading = true;
                    this.form.transfFileds = this.checkedList.join(',')
                    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: res.message
                                })
                                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: res.message
                                })
                                this.confirmLoading = false
                            }
                        })
                    }
                }
            });
        },
        handleCancel() {
            this.close()
        },
    }
}
</script>

<style scoped></style>