<template>
    <el-dialog class="cn-operate-dialog" width="600px" v-model.trim="dialogVisible" :title="title">
        <el-form :model="form" label-width="auto" ref="formRef" :rules="rules">
            <el-form-item label="所属厂站名称" prop="substationName">
                <el-input v-model.trim="form.substationName" placeholder="请输入所属厂站名称"></el-input>
            </el-form-item>
            <el-form-item label="敏感用户名称" prop="name">
                <el-input v-model.trim="form.name" placeholder="请输入敏感用户名称"></el-input>
            </el-form-item>
            <el-form-item label="敏感负荷类型" prop="loadType">
                <el-select v-model.trim="form.loadType" 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="用户协议容量(MVA)" prop="userAgreementCapacity">
                <el-input-number style="width: 100%;" :min="0" maxlength="32" show-word-limit v-model.trim="form.userAgreementCapacity" placeholder="请输入用户协议容量"></el-input-number>
            </el-form-item>
            <el-form-item label="装机容量(MW)" prop="installedCapacity">
                <el-input-number style="width: 100%;" :min="0" maxlength="32" show-word-limit  v-model.trim="form.installedCapacity" placeholder="请输入装机容量"></el-input-number>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number style="width: 100%;" :min="0" maxlength="32" show-word-limit v-model.trim="form.sort" placeholder="请输入排序"></el-input-number>
            </el-form-item>
        </el-form>

        <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 { ElMessage } from 'element-plus'
import TableStore from '@/utils/tableStore'
import { queryByCode, queryCsDictTree } from '@/api/system-boot/dictTree'
import { saveUser, updateUser } from '@/api/cs-device-boot/sensitiveLoadMange'
import { useDictData } from '@/stores/dictData'

const TypeOptions = ref()

const dictData = useDictData()
const DataTypeSelect = dictData.getBasicData('Interference_Source')

const tableStore = inject('tableStore') as TableStore

const formRef = ref()
const form = reactive<any>({
    loadType: [],
    substationName: null,
    installedCapacity: null,
    name: null,
    userAgreementCapacity:null,
    id: null,
    sort:100
})
const rules = {
    substationName: [{ required: true, message: '所属厂站名称不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '敏感用户名称不能为空', trigger: 'blur' }],
    loadType: [{ required: true, message: '请选择敏感负荷类型', trigger: 'blur' }],
    installedCapacity: [{ required: true, message: '用户协议容量不能为空', trigger: 'blur' }],
    userAgreementCapacity: [{ required: true, message: '装机容量不能为空', trigger: 'blur' }],
    sort: [{ 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) {
            form[key] = data[key]
        }
    } else {
        for (let key in form) {
            form[key] = null
            form.sort = 100
        }
    }
}
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            if (form.id) {
                await updateUser(form)
            } else {
                await saveUser(form)
            }
            ElMessage.success('操作成功')
            tableStore.index()
            dialogVisible.value = false
        }
    })
}

defineExpose({ open })
</script>
