<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="主题" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入主题" />
                </el-form-item>
                <el-form-item label="客户名称" prop="custom_name">
                    <el-input v-model="formData.custom_name" @click="showDialog = true" clearable placeholder="请选择客户" />
                </el-form-item>
                <el-form-item label="联系人" prop="custom_master_name">
                    <el-input v-model="formData.custom_master_name" clearable placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="日期" prop="date">
                    <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-form-item label="类型" prop="types">
                    <el-select class="flex-1" v-model="formData.types" 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-form-item label="执行人" prop="executor">
                    <el-input v-model="formData.executor" clearable placeholder="请输入执行人" />
                </el-form-item>
                <el-form-item label="行动描述" prop="description">
                    <el-input v-model="formData.description" clearable placeholder="请输入行动描述" />
                </el-form-item>
                <el-form-item label="附件/现场照片" prop="annex">
                    <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                        @delFile="delFileFn" />
                </el-form-item>
                <el-form-item label="位置" prop="coordinate">
                    <el-input v-model="formData.coordinate" clearable placeholder="请输入位置" />
                </el-form-item>
                <el-form-item label="下次回访日期" prop="next_follow_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.next_follow_date" clearable
                        value-format="YYYY-MM-DD" placeholder="选择下次回访日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
        </popup>

        <el-dialog v-model="showDialog" title="选择客户" width="70%">
            <customDialog @customEvent="customEvent"></customDialog>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="customFollowEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCustomFollowAdd, apiCustomFollowEdit, apiCustomFollowDetail } from '@/api/custom_follow'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { usePaging } from '@/hooks/usePaging'
import { apiCustomLists } from '@/api/custom'
// 上传文件
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) }
// <annexUpload : annex = "formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
// @delFile="delFileFn" />

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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑客户跟进记录' : '新增客户跟进记录'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    custom_id: '',
    custom_name: '',
    custom_master_name: '',
    date: '',
    types: '',
    executor: '',
    description: '',
    annex: [],
    coordinate: '',
    next_follow_date: '',
    status: '',
})

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

})
// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiCustomLists,

})
getLists()
// 获取详情
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]
        }
    }

    // //@ts-ignore
    // formData.date = timeFormat(formData.date, 'yyyy-mm-dd')
    // //@ts-ignore
    // formData.next_follow_date = timeFormat(formData.next_follow_date, 'yyyy-mm-dd')
}

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

const customEvent = (e: any) => {
    formData.custom_id = e.id;
    formData.custom_name = e.name;
    formData.custom_master_name = e.master_name;
    showDialog.value = false;
};

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiCustomFollowEdit(data)
        : await apiCustomFollowAdd(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>
