<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" type="file" />
                        </el-form-item> -->
                        <el-form-item label="附件" prop="field127">
                            <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                                @delFile="delFileFn" />
                        </el-form-item>

                    </el-col>
                </el-row>


                <el-dialog v-model="showDialog" title="选择客户" width="70%">
                    <customDialog @customEvent="customEvent"></customDialog>
                </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 type { PropType } from 'vue'


// 上传文件
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) }


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 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 customEvent = (e: any) => {
    formData.custom_id = e.id;
    formData.custom_name = e.name;
    showDialog.value = false;
};
// 表单验证
const formRules = reactive<any>({
    custom_name: [{
        required: true,
        message: '请输入',
        trigger: ['change']
    }]
})


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


}

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>
