<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"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="project_name" readonly @click="showDialog = true" clearable
                                placeholder="点击选择项目" :disabled="project" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="project_code" disabled clearable placeholder="系统自动填写" />
                        </el-form-item> </el-col><el-col :span="8">
                        <el-form-item label="客户名称" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="custom_name" disabled clearable placeholder="系统自动填写" />
                        </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="contacts">
                            <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                        </el-form-item> </el-col><el-col :span="8">
                        <el-form-item label="联系方式" prop="contact_information"
                            :rules="[{ validator: checkPhone, trigger: 'blur' }]">
                            <el-input v-model="formData.contact_information" clearable placeholder="请输入联系方式" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目角色" prop="project_role">
                            <el-input v-model="formData.project_role" clearable placeholder="请输入项目角色" />
                        </el-form-item> </el-col><el-col :span="8">
                        <el-form-item label="职位" prop="position">
                            <el-input v-model="formData.position" clearable placeholder="请输入职位" />
                        </el-form-item> </el-col><el-col :span="8">
                        <el-form-item label="跟进日期" prop="follow_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.follow_date" clearable type="date"
                                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: 'change' }]">
                            <el-select class="flex-1" v-model="formData.follow_type" clearable placeholder="请选择跟进类型">
                                <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="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="action_description">
                            <el-input v-model="formData.action_description" clearable placeholder="请输入行动描述" />
                        </el-form-item> </el-col><el-col :span="8">
                        <el-form-item label="项目把握度" prop="project_assurance"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-select class="flex-1" v-model="formData.project_assurance" clearable placeholder="请选择项目把握度">
                                <el-option v-for="(item, index) in dictData.project_assurance" :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="follow_status"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-select class="flex-1" v-model="formData.follow_status" clearable placeholder="请选择跟进状态">
                                <el-option v-for="(item, index) in dictData.follow_status" :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="follow_stage"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-select class="flex-1" v-model="formData.follow_stage" clearable placeholder="请选择跟进状态">
                                <el-option v-for="(item, index) in dictData.follow_stage" :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="notes">
                            <el-input v-model="formData.notes" 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
                                type="date" value-format="YYYY-MM-DD" placeholder="选择下次回访日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="附件" prop="field127">
                            <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                                @delFile="delFileFn" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectTable @customEvent="customEvent"></projectTable>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="projectFollowUpEdit">
import type { FormInstance } from 'element-plus'
import projectTable from "@/components/project/index.vue"
import Popup from '@/components/popup/index.vue'
import { apiProjectFollowUpAdd, apiProjectFollowUpEdit, apiProjectFollowUpDetail } from '@/api/project_follow_up'
import { useDictData } from '@/hooks/useDictOptions'

// 上传文件
import annexUpload from "@/components/annexUpload/index.vue"
const handleAvatarSuccess_four = (response: any) => {
    // @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 props = defineProps({
    project: Object
})
const { dictData } = useDictData('project_assurance,follow_status,follow_type,follow_stage')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const project_name = ref('')
const project_code = ref('')
const custom_name = ref('')
const formDataannex = reactive([])
// dialog
const showDialog = ref(false)

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目跟进记录' : '新增项目跟进记录'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    executor: '',
    contacts: '',
    contact_information: '',
    project_role: '',
    position: '',
    follow_date: '',
    follow_type: '',
    theme: '',
    action_description: '',
    project_assurance: '',
    follow_status: '',
    follow_stage: '',
    notes: '',
    next_follow_up_date: '',
    ceate_time: '',
    annex: []
})

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


if (props.project) {
    customEvent(props.project)
}

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

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    custom_name.value = data.custom_name
    project_name.value = data.project_name
    project_code.value = data.project_code
    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]
        }
    }

    //@ts-ignore
    // formData.follow_date = timeFormat(formData.follow_date, 'YYYY-MM-DD ')
    //@ts-ignore
    // formData.next_follow_up_date = timeFormat(formData.next_follow_up_date, 'YYYY-MM-DD ')
}

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

const checkPhone = (rule: any, value: any, callback: (arg0: Error) => any) => {

    if (value && !/^1\d{10}$/.test(value)) {
        callback(new Error('请输入正确的手机号码'));
    } else {
        callback()
    }

}
// 提交按钮
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 apiProjectFollowUpEdit(data)
        : await apiProjectFollowUpAdd(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>
