<!-- <template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="项目id" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                </el-form-item>
                <el-form-item label="文档名称" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入文档名称" />
                </el-form-item>
                <el-form-item label="目录id" prop="directory_id">
                    <el-input v-model="formData.directory_id" clearable placeholder="请输入目录id" />
                </el-form-item>
                <el-form-item label="子目录" prop="subdirectory_id">
                    <el-input v-model="formData.subdirectory_id" clearable placeholder="请输入子目录" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="consultScenePrepareEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiConsultScenePrepareAdd, apiConsultScenePrepareEdit, apiConsultScenePrepareDetail } from '@/api/consult_scene_prepare'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目咨询--现场准备' : '新增项目咨询--现场准备'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    title: '',
    directory_id: '',
    subdirectory_id: '',
    remark: '',
    annex: '',
})


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    title: [{
        required: true,
        message: '请输入文档名称',
        trigger: ['blur']
    }],
    directory_id: [{
        required: true,
        message: '请输入目录id',
        trigger: ['blur']
    }],
    subdirectory_id: [{
        required: true,
        message: '请输入子目录',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiConsultScenePrepareDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiConsultScenePrepareEdit(data)
        : await apiConsultScenePrepareAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script> -->

<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" @click="showDialog = true"
                        readonly />
                </el-form-item>
                <el-form-item label="文档名称" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入文档名称" />
                </el-form-item>
                <el-form-item label="目录" prop="directory_id">
                    <el-select class="flex-1" v-model="formData.directory_id" clearable placeholder="请选择目录"
                        @change="getSubdirectory">
                        <el-option v-for="(item, index) in DirectoryList" :key="index" :label="item.name"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="子目录" prop="subdirectory_id">
                    <el-select class="flex-1" v-model="formData.subdirectory_id" clearable placeholder="请选择子目录">
                        <el-option v-for="(item, index) in SubdirectoryList" :key="index" :label="item.name"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>

                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable @customEvent="customEvent" :config="consult_project" />
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="consultDemandEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiConsultScenePrepareAdd, apiConsultScenePrepareEdit } from '@/api/consult_scene_prepare'
import type { PropType } from 'vue'
import { consult_project } from "@/components/dialogTable/dialogTableConfig"
import { apiConsultSubdirectoryLists } from '@/api/consult_subdirectory'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    },
    DirectoryList: Array

})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目咨询--需求研究及管理' : '新增项目咨询--需求研究及管理'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    title: '',
    directory_id: '',
    subdirectory_id: '',
    remark: '',
    annex: [],
})


const handleAvatarSuccess_four = (response: any) => {
    formData.annex ||= []
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};
// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }

const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}


// 获取目录

const SubdirectoryList = ref([])
// 根据目录获取子目录
const getSubdirectory = async () => {
    formData.subdirectory_id = ''
    let res = await apiConsultSubdirectoryLists({ directory_id: formData.directory_id })
    SubdirectoryList.value = res.lists
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    title: [{
        required: true,
        message: '请输入文档名称',
        trigger: ['blur']
    }],
    directory_id: [{
        required: true,
        message: '请输入目录id',
        trigger: ['blur']
    }],
    subdirectory_id: [{
        required: true,
        message: '请输入子目录',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    let res = await apiConsultSubdirectoryLists({ directory_id: formData.directory_id })
    SubdirectoryList.value = res.lists

}



// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiConsultScenePrepareEdit(data)
        : await apiConsultScenePrepareAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
})
</script>
