<template>
    <el-dialog class="cn-operate-dialog" v-model="dialogVisible" :title="title">
        <el-scrollbar>
            <div style="padding-left: 50px">
                <el-divider content-position="center">基础数据</el-divider>
            </div>
            <el-form :inline="true" :model="form" label-width="140px" ref="formRef" :rules="rules">
                <el-form-item label="数据分类:" prop="dataType">
                    <el-select v-model="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 v-model="form.name" autocomplete="off" placeholder="请输入数据名称"></el-input>
                </el-form-item>
                <el-form-item label="别名:" prop="otherName">
                    <el-input v-model="form.otherName" autocomplete="off" placeholder="请输入别名"></el-input>
                </el-form-item>
                <el-form-item label="展示名称:" prop="showName">
                    <el-input v-model="form.showName" autocomplete="off" placeholder="请输入展示名称"></el-input>
                </el-form-item>
                <el-form-item label="相别:" prop="phase">
                    <el-select v-model="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="单位:" prop="unit">
                    <el-input v-model="form.unit" autocomplete="off" placeholder="请输入单位"></el-input>
                </el-form-item>
                <el-form-item label="基础数据类型:" prop="type">
                    <el-input v-model="form.type" autocomplete="off" placeholder="请输入基础数据类型"></el-input>
                </el-form-item>
                <el-form-item label="开始结束次数:" prop="harmStart">
                    <el-slider v-model="form.harm" range show-stops :max="50" />
                </el-form-item>
                <el-form-item label="统计方法:" prop="statMethod">
                    <el-select v-model="form.statMethod" multiple 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="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="报表数据来源:" prop="resourcesId">
                    <el-select v-model="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 type="number" v-model="form.sort" autocomplete="off" placeholder="请输入序号"></el-input>
                </el-form-item>
                <el-form-item label="限值名称:" prop="limitName">
                    <el-input
                        type="text"
                        v-model="form.limitName"
                        autocomplete="off"
                        placeholder="请输入限值名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="限值表名:" prop="limitTable">
                    <el-input
                        type="text"
                        v-model="form.limitTable"
                        autocomplete="off"
                        placeholder="请输入限值表名"
                    ></el-input>
                </el-form-item>
                <el-form-item label="超标判断方式:" prop="formula">
                    <el-select v-model="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 :inline="true" :model="form" label-width="140px" ref="formRef2" :rules="rules">
                <el-form-item label="参数缺省值:" prop="defaultValue">
                    <el-input v-model="form.defaultValue" autocomplete="off" placeholder="请输入参数缺省值"></el-input>
                </el-form-item>
                <el-form-item label="事件类别:" prop="eventType">
                    <el-input v-model="form.eventType" autocomplete="off" placeholder="请输入事件类别"></el-input>
                </el-form-item>

                <el-form-item label="设置最大值:" prop="maxNum">
                    <el-input
                        type="number"
                        v-model="form.maxNum"
                        autocomplete="off"
                        placeholder="请输入设置最大值"
                    ></el-input>
                </el-form-item>
                <el-form-item label="设置最小值:" prop="minNum">
                    <el-input
                        type="number"
                        v-model="form.minNum"
                        autocomplete="off"
                        placeholder="请输入设置最小值"
                    ></el-input>
                </el-form-item>
                <el-form-item label="枚举序列:" prop="setValue">
                    <el-input v-model="form.setValue" autocomplete="off" placeholder="请输入枚举序列"></el-input>
                </el-form-item>
                <el-form-item label="字符串长度上限:" prop="strlen">
                    <el-input
                        type="number"
                        v-model="form.strlen"
                        autocomplete="off"
                        placeholder="请输入字符串长度上限"
                    ></el-input>
                </el-form-item>
                <el-form-item label="上送规则:" prop="tranRule">
                    <el-input v-model="form.tranRule" autocomplete="off" placeholder="请输入上送规则"></el-input>
                </el-form-item>
                <el-form-item label="是否可远程控制:" prop="ctlSts">
                    <el-radio v-model="form.ctlSts" :label="1">是</el-radio>
                    <el-radio v-model="form.ctlSts" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="是否需遥控校验:" prop="curSts">
                    <el-radio v-model="form.curSts" :label="1">是</el-radio>
                    <el-radio v-model="form.curSts" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="是否存储:" prop="storeFlag">
                    <el-radio v-model="form.storeFlag" :label="1">是</el-radio>
                    <el-radio v-model="form.storeFlag" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="是否加密:" prop="storeFlag">
                    <el-radio v-model="form.storeFlag" :label="1">是</el-radio>
                    <el-radio v-model="form.storeFlag" :label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="数据是否上送:" prop="tranFlag">
                    <el-radio v-model="form.tranFlag" :label="1">是</el-radio>
                    <el-radio v-model="form.tranFlag" :label="0">否</el-radio>
                </el-form-item>
            </el-form>
        </el-scrollbar>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submit">确认</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 formRef = ref()
const formRef2 = ref()
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: 'T相',
        id: 'T'
    }
]
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: 'blur' },
        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
    ],
    name: [{ required: true, message: '请输入数据名称', trigger: 'blur' }],
    otherName: [{ required: true, message: '请输入别名', trigger: 'blur' }],
    showName: [{ required: true, message: '请输入展示名称', trigger: 'blur' }],
    phase: [{ type: 'array', required: true, message: '请选择相别', trigger: 'blur' }],
    unit: [{ required: true, message: '请输入单位', trigger: 'blur' }],
    type: [{ required: true, message: '请输入基础数据类型', trigger: 'blur' }],
    harmStart: [{ required: true, message: '请填写开始结束次数', trigger: 'blur' }],
    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].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 () => {
    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
}

defineExpose({ open })
</script>
