<template>
    <el-dialog class="cn-operate-dialog" width="800px" v-model="dialogVisible" :title="title">
      
            <el-form  class="form-two" :model="form" label-width="120px" ref="formRef" :rules="rules">
                <el-form-item label="装置类型:" prop="type">
                    <el-select v-model="form.type" filterable placeholder="请选择"  @change="typeChange">
                        <el-option
                            v-for="item in TypeOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="装置型号:" prop="devType">
                    <el-select v-model="form.devType" placeholder="请选择" filterable @change="devTypeChange">
                        <el-option
                            v-for="item in DevTypeOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="版本号:" prop="versionNo">
                    <el-input v-model="form.versionNo" autocomplete="off" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="版本类型:" prop="versionType">
                    <el-input
                        v-model="form.versionType"
                        autocomplete="off"
                        placeholder="填写特殊类型(不填默认通用类型)"
                    ></el-input>
                </el-form-item>
                <el-form-item label="版本协议:" prop="versionAgreement">
                    <el-input v-model="form.versionAgreement" autocomplete="off" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="版本日期:" prop="versionDate">
                    <el-date-picker
                        v-model="form.versionDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期时间"
                        style="width: 100%"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="描述:" prop="description">
                    <el-input v-model="form.description" autocomplete="off" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="CRC校验:" prop="crcInfo">
                    <el-input v-model="form.crcInfo" autocomplete="off" placeholder="请输入"></el-input>
                </el-form-item>
            </el-form>
            <el-form :inline="true" :model="form" label-width="120px" class="mt10">
                <el-form-item label="升级文件:" prop="file">
                    <el-upload
                        :limit="1"
                        :auto-upload="false"
                        :on-change="fileChange"
                        :on-exceed="fileExceed"
                        :file-list="fileList"
                    >
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </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 TableStore from '@/utils/tableStore'
import { ElMessage } from 'element-plus'
import { queryByCode, queryByid, queryCsDictTree } from '@/api/system-boot/dictTree'
import { addEdData, auditEdData } from '@/api/cs-device-boot/edData'

const TypeOptions = ref()
const DevTypeAllOptions = ref()
const DevTypeOptions = ref()

const fileList = ref()
const tableStore = inject('tableStore') as TableStore
const formRef = ref()
const form = reactive<any>({
    devType: null,
    devTypeName: null,
    versionAgreement: null,
    versionNo: null,
    versionType: null,
    crcInfo: null,
    description: null,
    versionDate: null,
    type: null,
    id: null,
    file: null
})
const rules = {
    type: [{ required: true, message: '装置类型不能为空', trigger: 'blur' }],
    devType: [{ required: true, message: '装置型号不能为空', trigger: 'blur' }],
    versionNo: [{ required: true, message: '版本号不能为空', trigger: 'blur' }],
    versionType: [{ required: true, message: '版本类型不能为空', trigger: 'blur' }],
    versionAgreement: [{ required: true, message: '版本协议不能为空', trigger: 'blur' }],
    versionDate: [{ required: true, message: '版本日期不能为空', trigger: 'blur' }],
    description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
    crcInfo: [{ required: true, message: 'CRC校验不能为空', trigger: 'blur' }],
    file: [{ required: true, message: '升级文件不能为空', trigger: 'blur' }]
}
const typeChange = () => {
    console.log(form.type)
    form.devType = null
    form.devTypeName = null
    queryCsDictTree(form.type).then(res => {
        DevTypeOptions.value = res.data
    })
}
const devTypeChange = (e: any) => {
    console.log(e)
    form.devTypeName = DevTypeOptions.value.filter((item: any) => item.value == e)[0].label
}
const fileChange = (e: any) => {
    console.log('fileChange!')
    console.log(e)
    form.file = e.raw
}
const fileExceed = (e: any) => {
    console.log('fileExceed!')
    console.log(e)
    ElMessage.error('只能上传一个文件')
}
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]
        }
        fileList.value = [
            {
                name: data.filePath,
                url: data.filePath
            }
        ]
    } else {
        for (let key in form) {
            form[key] = null
        }
    }
    queryByCode('Direct_Connected_Device').then(res => {
        queryCsDictTree(res.data.id).then(res => {
            TypeOptions.value = res.data
        })
        queryByid(res.data.id).then(res => {
            DevTypeAllOptions.value = res.data
        })
    })
}
const submit = () => {
    console.log(formRef)
    formRef.value.validate(async (valid: boolean) => {
        console.log(valid)
        if (valid) {
            let submitForm = new FormData()
            for (let key in form) {
                submitForm.append(key, form[key])
            }
            if (form.id) {
                await auditEdData(form)
            } else {
                await addEdData(form)
            }
            ElMessage.success('操作成功')
            tableStore.index()
            dialogVisible.value = false
        }
    })
}

defineExpose({ open })
</script>
