<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目" prop="project_id" @click="showDialog = true">
                            <el-input v-model="project_name" readonly clearable placeholder="请选择项目" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id">
                            <el-input v-model="project_code" readonly clearable placeholder="请选择项目" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="主题" prop="theme"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.theme" clearable placeholder="请输入节点名称" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人" prop="contacts">
                            <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                        </el-form-item> </el-col>

                    <el-col :span="8">
                        <el-form-item label="日期" prop="date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable
                                value-format="YYYY-MM-DD " placeholder="选择下次回访日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="类型" prop="follow_type"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="w-[280px]" v-model="formData.follow_type" clearable placeholder="请选择类型">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.follow_type" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>

                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="执行人" prop="executor">
                            <el-input v-model="formData.executor" clearable placeholder="请输入排序号" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="下次回访日期" prop="next_follow_up_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.next_follow_up_date" clearable
                                value-format="YYYY-MM-DD " placeholder="选择下次回访日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="日志详情" prop="content">
                            <el-input v-model="formData.content" type="textarea" clearable placeholder="请输入日志详情" />
                        </el-form-item> </el-col>


                </el-row>

            </el-form>
        </popup>

        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectDialog @customEvent="customEvent"></projectDialog>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { apiProjectlogsAdd, apiProjectlogsEdit, apiProjectlogsDetail } from '@/api/project_logs'

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 showDialog = ref(false)
const project_name = ref('')
const project_code = ref('')
const formDataannex = reactive([])
import feedback from '@/utils/feedback'
import configs from "@/config"
const base_url = configs.baseUrl + configs.urlPrefix
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
// 上传文件
const handleAvatarSuccess_four = (
    response,
    uploadFile
) => {
    if (response.code == 0) {
        feedback.msgError(response.msg);
        return;
    }
    formDataannex.push(
        { uri: response.data.uri, name: response.data.name }

    );
};
// 删除上传的文件
const delFileFn = (index: number) => {
    formDataannex.splice(index, 1)
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目WBS设置' : '新增项目WBS设置'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    theme: '',
    wbs_code: '',
    contacts: '',
    date: '',
    follow_type: '',
    executor: '',
    content: '',
    annex: [],
    next_follow_up_date: []
})

//获取值
const customEvent = (e) => {
    formData.project_id = e.id
    project_name.value = e.name
    project_code.value = e.project_code
    showDialog.value = false

}
// 表单验证
const formRules = reactive<any>({

})




// 获取详情
const setFormData = async (data: Record<any, any>) => {

    if (data.annex && data.annex.length > 0) {
        const arry1 = data.annex.map((item: any, index: any) => {
            return {
                name: `文件${index + 1}`,
                uri: item
            };
        });
        Object.assign(formDataannex, arry1)
    }

    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code

}

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


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item) => item.uri)
    }
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectlogsEdit(data)
        : await apiProjectlogsAdd(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>
