.
<template>
    <el-dialog draggable :title="title" v-model="planAddition" width="950px" :before-close="cancelFn">
        <el-row :gutter="20">
            <el-col :span="10">
                <el-divider content-position="left">基本信息</el-divider>
                <el-form
                    :model="formdata"
                    :disabled="title == '查看计划' || title == '计划审核'"
                    ref="formRef"
                    :rules="rules"
                    label-width="auto"
                    class="form-one"
                >
                    <el-form-item label="普测负责单位:">
                        <Area v-model="formdata.orgNo" disabled />
                    </el-form-item>
                    <el-form-item label="计划开始时间:" prop="planStartTime">
                        <el-date-picker
                            v-model="formdata.planStartTime"
                            type="date"
                            placeholder="选择计划开始时间"
                            value-format="YYYY-MM-DD"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="计划结束时间:" prop="planEndTime">
                        <el-date-picker
                            v-model="formdata.planEndTime"
                            type="date"
                            placeholder="选择计划结束时间"
                            value-format="YYYY-MM-DD"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="普测计划编号:" prop="planNo">
                        <el-input
                            :disabled="title == '编辑计划'"
                            v-model="formdata.planNo"
                            placeholder="请输入普测计划编号"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="计划名称:" prop="planName">
                        <el-input v-model="formdata.planName" placeholder="请输入计划名称"></el-input>
                    </el-form-item>
                    <el-form-item label="测试负责人:" prop="leader">
                        <el-input v-model="formdata.leader" placeholder="请输入测试负责人"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-col :span="14">
                <el-divider content-position="left" style="font-size: 18px; font-weight: bolder">添加电站</el-divider>

                <generalTestTree ref="treeRef" />
            </el-col>
        </el-row>
        <div
            v-if="
                (title == '查看计划' || title == '计划审核') &&
                (title == '计划审核' || formdata.status == 1 || formdata.status == 2 || formdata.status == 3)
            "
        >
            <el-divider content-position="left" style="font-size: 18px; font-weight: bolder">审核意见</el-divider>
            <el-input
                :disabled="title != '计划审核'"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="formdata.checkComment"
            ></el-input>
        </div>
        <template #footer v-if="title != '查看计划' && title != '计划审核'">
            <div>
                <el-button type="primary" @click="submitFn">提交</el-button>
                <el-button type="primary" @click="cancelFn">取消</el-button>
            </div>
        </template>
        <template #footer v-if="title == '计划审核'">
            <div>
                <el-button type="primary" @click="pass(true)">通过</el-button>
                <el-button type="primary" @click="pass(false)">不通过</el-button>
                <el-button type="primary" @click="cancelFn">取消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup lang="ts">
import generalTestTree from '@/components/tree/pqs/generalTestTree.vue'
import { ref, reactive, nextTick } from 'vue'
import { addPlan, checkPlanAudit } from '@/api/process-boot/generalTest'
import Area from '@/components/form/area/index.vue'
import { ElMessage } from 'element-plus'
import { useDictData } from '@/stores/dictData'
const emit = defineEmits(['onsubmit'])
const dictData = useDictData()
const title = ref('')
const planAddition = ref(false)

const formdata: any = ref({
    orgName: '',
    orgNo: dictData.state.area[0].id,
    planStartTime: '',
    planEndTime: '',
    planNo: '',
    planName: '',
    leader: '',
    rgeneralSurveyPlanDetailAddParm: [],
    subCount: '',
    busCount: '',
    subIds: [],
    checkComment: ''
})
const formRef = ref()
const treeRef = ref()
const rules = {
    planStartTime: [{ required: true, message: '选择计划开始时间', trigger: 'change' }],
    planEndTime: [{ required: true, message: '选择计划结束时间', trigger: 'change' }],
    planNo: [{ required: true, message: '请输入普测计划编号', trigger: 'blur' }],
    planName: [{ required: true, message: '请输入计划名称', trigger: 'blur' }],
    leader: [{ required: true, message: '请输入测试负责人', trigger: 'blur' }]
}
const cancelFn = () => {
    formRef.value.resetFields()
    treeRef.value.setKey([])
    planAddition.value = false
}
// 提交
const submitFn = () => {
    formRef.value.validate((valid: any) => {
        if (valid) {
            formdata.value.subIds = treeRef.value.treeRef.getCheckedKeys(false)
            addPlan(formdata.value).then((res: any) => {
                ElMessage.success('新增成功!')
                cancelFn()
                emit('onsubmit')
            })
        }
    })
}
// 通过
const pass = (flag: boolean) => {
    checkPlanAudit({
        checkResult: flag ? 1 : 0,
        planNo: formdata.value.planNo,
        checkComment: formdata.value.checkComment,
        checkPerson: JSON.parse(window.localStorage.getItem('adminInfo')).loginName
    }).then((res: any) => {
        ElMessage.success('审核成功!')
        cancelFn()
        emit('onsubmit')
    })
}

const open = (text: string, row?: any) => {
    formdata.value = Object.assign({
        orgName: '',
        orgNo: dictData.state.area[0].id,
        planStartTime: '',
        planEndTime: '',
        planNo: '',
        planName: '',
        leader: '',
        rgeneralSurveyPlanDetailAddParm: [],
        subCount: '',
        busCount: '',
        subIds: [],
        checkComment: ''
    })
    title.value = text
    planAddition.value = true
    formRef.value?.resetFields()
    nextTick(() => {
        treeRef.value.loadData()
    })
    if (row) {
        formdata.value = JSON.parse(JSON.stringify(row))
        nextTick(() => {
            treeRef.value.loadData()

            treeRef.value.setKey(row.subIds, text)
        })
    }
}

defineExpose({ open })
</script>
<style lang="scss" scoped></style>
