<template>
    <el-dialog class="cn-operate-dialog" draggable v-model.trim="dialogVisible" width="800px" :title="title" @close="emit('close')">
        <el-scrollbar>
            <div style="padding-left: 50px">
                <el-divider content-position="center">基础数据</el-divider>
            </div>
            <el-form :model="form" label-width="130px" ref="formRef" class="form-two" :rules="rules">
                <el-form-item label="数据分类" prop="dataType">
                    <el-select v-model.trim="form.dataType" filterable clearable placeholder="请选择数据分类">
                        <el-option
                            v-for="item in DataTypeSelect"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数据名称" prop="name">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.name"
                        autocomplete="off"
                        placeholder="请输入数据名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="别名" prop="otherName">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.otherName"
                        autocomplete="off"
                        placeholder="请输入别名"
                    ></el-input>
                </el-form-item>
                <el-form-item label="展示名称" prop="showName">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.showName"
                        autocomplete="off"
                        placeholder="请输入展示名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="相别" prop="phase">
                    <el-select v-model.trim="form.phase" filterable clearable placeholder="请选择相别">
                        <el-option
                            v-for="item in phaseSelect"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="单位">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.unit"
                        autocomplete="off"
                        placeholder="请输入单位"
                    ></el-input>
                </el-form-item>
                <el-form-item label="基础数据类型">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.type"
                        autocomplete="off"
                        placeholder="请输入基础数据类型"
                    ></el-input>
                </el-form-item>
                <el-form-item label="开始结束次数">
                    <el-slider v-model.trim="form.harm" range  :max="50" style="width: 90%" />
                </el-form-item>
                <el-form-item label="统计方法">
                    <el-select
                        v-model.trim="form.statMethod"
                        multiple
                        collapse-tags
                        collapse-tags-tooltip
                        filterable
                        clearable
                        placeholder="请选择统计方法"
                    >
                        <el-option
                            v-for="item in StatMethodSelect"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数据存储" prop="classId">
                    <el-select v-model.trim="form.classId" filterable clearable placeholder="请选择数据存储">
                        <el-option
                            v-for="item in DataSelect"
                            :key="item.code"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="报表数据来源">
                    <el-select v-model.trim="form.resourcesId" filterable clearable placeholder="请选择报表数据来源">
                        <el-option
                            v-for="item in ResourcesIdSelect"
                            :key="item.code"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="序号" prop="sort">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        type="number"
                        v-model.trim="form.sort"
                        autocomplete="off"
                        placeholder="请输入序号"
                    ></el-input>
                </el-form-item>
                <el-form-item label="限值名称" prop="limitName">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        type="text"
                        v-model.trim="form.limitName"
                        autocomplete="off"
                        placeholder="请输入限值名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="限值表名" prop="limitTable">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        type="text"
                        v-model.trim="form.limitTable"
                        autocomplete="off"
                        placeholder="请输入限值表名"
                    ></el-input>
                </el-form-item>
                <el-form-item label="超标判断方式" prop="formula">
                    <el-select v-model.trim="form.formula" filterable clearable placeholder="请选择超标判断方式">
                        <el-option
                            v-for="item in FormulaSelect"
                            :key="item.code"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div style="padding-left: 50px">
                <el-divider content-position="center">拓展数据</el-divider>
            </div>
            <el-form class="form-two" :model="form" label-width="130px" ref="formRef2">
                <el-form-item label="告警码(缺省值)">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.defaultValue"
                        autocomplete="off"
                        placeholder="请输入告警码(缺省值)"
                    ></el-input>
                </el-form-item>
                <el-form-item label="事件类别">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.eventType"
                        autocomplete="off"
                        placeholder="请输入事件类别"
                    ></el-input>
                </el-form-item>

                <el-form-item label="设置最大值">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        type="number"
                        v-model.trim="form.maxNum"
                        autocomplete="off"
                        placeholder="请输入设置最大值"
                    ></el-input>
                </el-form-item>
                <el-form-item label="设置最小值">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        type="number"
                        v-model.trim="form.minNum"
                        autocomplete="off"
                        placeholder="请输入设置最小值"
                    ></el-input>
                </el-form-item>
                <el-form-item label="枚举序列">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.setValue"
                        autocomplete="off"
                        placeholder="请输入枚举序列"
                    ></el-input>
                </el-form-item>
                <el-form-item label="字符串长度上限">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        type="number"
                        v-model.trim="form.strlen"
                        autocomplete="off"
                        placeholder="请输入字符串长度上限"
                    ></el-input>
                </el-form-item>
                <el-form-item label="上送规则">
                    <el-input maxlength="32" show-word-limit 
                        
                        
                        v-model.trim="form.tranRule"
                        autocomplete="off"
                        placeholder="请输入上送规则"
                    ></el-input>
                </el-form-item>
                <el-form-item label="是否可远程控制">
                    <el-radio v-model.trim="form.ctlSts" :label="1">是</el-radio>
                    <el-radio v-model.trim="form.ctlSts" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="是否需遥控校验">
                    <el-radio v-model.trim="form.curSts" :label="1">是</el-radio>
                    <el-radio v-model.trim="form.curSts" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="是否存储">
                    <el-radio v-model.trim="form.storeFlag" :label="1">是</el-radio>
                    <el-radio v-model.trim="form.storeFlag" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="是否加密">
                    <el-radio v-model.trim="form.storeFlag" :label="1">是</el-radio>
                    <el-radio v-model.trim="form.storeFlag" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="数据是否上送">
                    <el-radio v-model.trim="form.tranFlag" :label="1">是</el-radio>
                    <el-radio v-model.trim="form.tranFlag" :label="0">否</el-radio>
                </el-form-item>
            </el-form>
        </el-scrollbar>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click=";(dialogVisible = false), emit('close')">取消</el-button>
                <el-button type="primary" @click="submit" :loading="loading">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject } from 'vue'
import { reactive } from 'vue'
import TableStore from '@/utils/tableStore'
import { ElMessage } from 'element-plus'
import { useDictData } from '@/stores/dictData'
import { addCsDictData, updateCsDictData } from '@/api/system-boot/csDictData'
const emit = defineEmits(['close'])
const formRef = ref()
const formRef2 = ref()
const loading = ref(false)
const dictData = useDictData()
const DataSelect = dictData.getBasicData('Data')
const DataTypeSelect = dictData.getBasicData('Cs_Data_Type')
const ResourcesIdSelect = dictData.getBasicData('Data_Day')
const FormulaSelect = dictData.getBasicData('Limit_Value_Judgment')
const phaseSelect = [
    {
        name: 'A相',
        id: 'A'
    },
    {
        name: 'B相',
        id: 'B'
    },
    {
        name: 'C相',
        id: 'C'
    },
    {
        name: '无相别',
        id: 'T'
    },
    {
        name: 'AB相',
        id: 'AB'
    },
    {
        name: 'BC相',
        id: 'BC'
    },
    {
        name: 'CA相',
        id: 'CA'
    },
    // {
    //     name: '无相别',
    //     id: 'M'
    // },
]
const StatMethodSelect = [
    {
        name: '平均值',
        id: 'avg'
    },
    {
        name: '最大值',
        id: 'max'
    },
    {
        name: '最小值',
        id: 'min'
    },
    {
        name: 'CP95值',
        id: 'cp95'
    }
]
const tableStore = inject('tableStore') as TableStore
const form = reactive<any>({
    id: null,
    classId: null,
    ctlSts: null,
    curSts: null,
    dataType: null,
    defaultValue: null,
    eventType: null,
    harm: [0, 50],
    harmEnd: null,
    harmStart: null,
    maxNum: null,
    minNum: null,
    name: null,
    otherName: null,
    phase: null,
    resourcesId: null,
    setValue: null,
    showName: null,
    sort: null,
    statMethod: null,
    storeFlag: null,
    strlen: null,
    systemType: null,
    tranFlag: null,
    tranRule: null,
    type: null,
    unit: null,
    formula: null,
    limitName: null,
    limitTable: 'pq_overlimit'
})
const rules = {
    dataType: [{ required: true, message: '请输入数据分类', trigger: 'change' }],
    name: [{ required: true, message: '请输入数据名称', trigger: 'blur' }],
    otherName: [{ required: true, message: '请输入别名', trigger: 'blur' }],
    showName: [{ required: true, message: '请输入展示名称', trigger: 'blur' }],
    phase: [{ required: true, message: '请选择相别', trigger: 'change' }],
    unit: [{ required: true, message: '请输入单位', trigger: 'blur' }],
    type: [{ required: true, message: '请输入基础数据类型', trigger: 'blur' }],
    harmStart: [{ required: true, message: '请填写开始结束次数', trigger: 'change' }],
    statMethod: [{ required: true, message: '请选择统计方法', trigger: 'blur' }],
    classId: [{ required: true, message: '请选择数据存储', trigger: 'blur' }],
    resourcesId: [{ required: true, message: '请选择报表数据来源', trigger: 'blur' }],
    sort: [{ required: true, message: '请填写序号', trigger: 'blur' }],
    defaultValue: [
        { required: true, message: '请输入参数缺省值', trigger: 'blur' },
        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
    ],
    eventType: [{ required: true, message: '请输入事件类别', trigger: 'blur' }],
    maxNum: [{ type: 'date', required: true, message: '请设置最大值', trigger: 'blur' }],
    minNum: [{ type: 'date', required: true, message: '请设置最小值', trigger: 'blur' }],
    setValue: [{ required: true, message: '请输入枚举序列', trigger: 'blur' }],
    strlen: [{ required: true, message: '请输入字符串长度上限', trigger: 'blur' }],
    tranRule: [{ required: true, message: '请填写上送规则', trigger: 'blur' }],
    ctlSts: [{ required: true, message: '请选择是否可远程控制', trigger: 'blur' }],
    storeFlag: [{ required: true, message: '请选择是否需遥控校验', trigger: 'blur' }],
    tranFlag: [{ required: true, message: '请选择是否存储||是否加密', trigger: 'blur' }]
}
const dialogVisible = ref(false)
const title = ref('')
const open = (text: string, data?: anyObj) => {
    title.value = text
    dialogVisible.value = true
    if (data) {
        for (let key in form) {
            if (key == 'statMethod') {
                form[key] = data[key] == '/' ? [] : data[key].split(',')
            }  else {
                form[key] = data[key] === '/' ? null : data[key]
            }
        }
        form.harm = [form.harmStart, form.harmEnd]
    } else {
        for (let key in form) {
            form[key] = null
        }
        form.pq_overlimit = 'pq_overlimit'
        form.harm = [0, 50]
    }
    // console.log(form)
}
const submit = async () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            loading.value = true
            form.harmStart = form.harm[0]
            form.harmEnd = form.harm[1]
            if (form.id) {
                await updateCsDictData(form)
            } else {
                await addCsDictData(form)
            }
            ElMessage.success('保存成功')
            tableStore.index()
            dialogVisible.value = false
            emit('close')
            loading.value = false
        }
    })
}

defineExpose({ open })
</script>
