<template>
    <el-dialog class="cn-operate-dialog" width="600px" 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="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="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="版本类型" prop="name">
                <el-input v-model.trim="form.name" placeholder="请输入版本类型"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="name">
                <el-input
                    maxlength="32"
                    :autosize="{ minRows: 2, maxRows: 4 }"
                    type="textarea"
                    show-word-limit
                    v-model.trim="form.remark"
                    placeholder="请输入描述"
                ></el-input>
            </el-form-item>
            <el-form-item label="上传升级文件" prop="name">
                <el-upload
                    :limit="1"
                    accept=".bin"
                    :auto-upload="false"
                    :on-change="fileChange"
                    :on-exceed="fileExceed"
                    :on-remove="fileRemove"
                    :file-list="fileList"
                >
                    <el-button type="primary">点击上传</el-button>
                </el-upload>
            </el-form-item>
            <el-form-item label="CRC校验" prop="name">
                <el-input v-model.trim="form.name" placeholder="请输入版本日期"></el-input>
            </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 fileList: any = ref([])
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 fileRemove = (e: any) => {
    form.file = null
    fileList.value = []
}
const fileChange = (e: any) => {
    if (!beforeUpload(e.raw)) return
    form.file = e.raw
    fileList.value = [e.raw]
}
const fileExceed = (e: any) => {
    ElMessage.error('只能上传一个文件')
}
// 处理上传前检查
const beforeUpload = (file: any) => {
    const isWord = file.name.endsWith('.bin')

    if (!isWord) {
        ElMessage.error('请上传(.bin)格式文件!')
        fileList.value = []
        return false
    }

    // 校验通过后允许上传，交由 http-request 处理
    return true
}

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>
