<template>
    <el-dialog class="cn-operate-dialog" draggable width="500px" v-model.trim="dialogVisible" :title="title">
        <el-form :model="form" class="form-one" label-width="auto" ref="formRef" :rules="rules">
           
            <el-form-item label="用户名称" prop="name">
                <el-input maxlength="32" show-word-limit  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="所属厂站名称" prop="substationName">
                <el-input maxlength="32" show-word-limit  v-model.trim="form.substationName" placeholder="请输入所属厂站名称"></el-input>
            </el-form-item>
            <el-form-item label="用户协议容量(MVA)" prop="userAgreementCapacity">
                <el-input-number  style="width: 100%;" :min="0"   v-model.number="form.userAgreementCapacity" placeholder="请输入用户协议容量"></el-input-number>
            </el-form-item>
            <el-form-item label="装机容量(MW)" prop="installedCapacity">
                <el-input-number  style="width: 100%;" :min="0"    v-model.number="form.installedCapacity" placeholder="请输入装机容量"></el-input-number>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number  style="width: 100%;" :min="0"   v-model.number="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" :loading="loading">确定</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 loading = ref(false)
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) => {
    formRef.value?.resetFields()
    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) {
            loading.value = true
            if (form.id) {
                await updateUser(form)
            } else {
                await saveUser(form)
            }
            ElMessage.success('操作成功')
            tableStore.index()
            dialogVisible.value = false
            loading.value = false
        }
    })
}

defineExpose({ open })
</script>
