<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="90px" :rules="formRules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="客户名称" prop="custom_name">
                            <el-input v-model="formData.custom_name" clearable placeholder="请选择客户"
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入姓名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="职位" prop="position">
                            <el-input v-model="formData.position" clearable placeholder="请输入职位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="手机" prop="phone">
                            <el-input v-model="formData.phone" clearable placeholder="请输入手机" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="电话" prop="telephone">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="formData.email" clearable placeholder="请输入邮箱" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="notes">
                            <el-input v-model="formData.notes" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="附件" prop="annex">
                            <material-picker v-model="formData.annex" />
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-dialog v-model="showDialog" title="选择客户" width="70%">
                    <el-table style="cursor: pointer;" :data="pager.lists" @current-change="handleCurrentChange">
                        <!-- <el-table-column type="selection" width="55" /> -->
                        <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>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="customContactsEdit">
import { usePaging } from '@/hooks/usePaging'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import customDialog from '@/components/custom-dialog/index.vue'
import { apiCustomContactsAdd, apiCustomContactsEdit, apiCustomContactsDetail } from '@/api/custom_contacts'
import { apiCustomLists } from '@/api/custom'

import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const showDialog = ref(false);
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

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

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑客户联系人' : '新增客户联系人'
})


const popTableList = reactive([])
// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiCustomLists,
})
getLists()
// 表单数据
const formData = reactive({
    id: '',
    custom_id: '',
    custom_name: '',
    name: '',
    position: '',
    phone: '',
    telephone: '',
    email: '',
    notes: '',
    annex: '',
    status: '',
})


// 表单验证
const formRules = reactive<any>({
    custom_name: [{
        required: true,
        message: '请输入',
        trigger: ['change']
    }]
})


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

    console.log(data, 5666)

    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 apiCustomContactsDetail({
        id: row.id
    })
    setFormData(data)
}


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

// 选择客户
const handleCurrentChange = (e: any) => {
    formData.custom_name = e.name
    formData.custom_id = e.id
    console.log(e)

    showDialog.value = false
}

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

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



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