<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" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                    <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="contacts">
                    <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="日期" prop="date" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                    <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="类型" prop="types" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                    <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="admin_id">
                    <el-input v-model="formData.admin_id" 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">
                    <material-picker v-model="formData.annex" />
                </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 type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择下次回访日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
        </popup>

        <el-dialog v-model="showDialog" title="选择客户" width="70%">

            <customDialog @customEvent="customEvent"></customDialog>


            <!-- <el-table style="cursor: pointer;" :data="pager.lists" @current-change="handleCurrentChange">
                <el-table-column label="客户名称" prop="name" show-overflow-tooltip />
                <el-table-column label="客户属性" prop="custom_type">
                    <template #default="{ row }">
                        <dict-value :options="dictData.custom_type" :value="row.custom_type" />
                    </template>
                </el-table-column>
                <el-table-column label="省" prop="province_name" show-overflow-tooltip />
                <el-table-column label="职务" prop="master_position" show-overflow-tooltip />
                <el-table-column label="电话" prop="master_telephone" show-overflow-tooltip />
                <el-table-column label="手机" prop="master_phone" show-overflow-tooltip />
                <el-table-column label="备注" prop="master_notes" show-overflow-tooltip />
                <el-table-column label="负责人" show-overflow-tooltip />
                <el-table-column label="创建时间" prop="create_time" show-overflow-tooltip />
                <el-table-column label="最后跟进" show-overflow-tooltip />
                <el-table-column label="下次回访日期" show-overflow-tooltip />
            </el-table> -->
        </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'

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: '',
    contacts: '',
    date: '',
    types: '',
    admin_id: '',
    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 hh:MM:ss')
    //@ts-ignore
    formData.next_follow_date = timeFormat(formData.next_follow_date, 'yyyy-mm-dd hh:MM:ss')
}

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;
    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>
