<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="160px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="客户名称" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入客户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="客户简称">
                            <el-input v-model="formData.sub_name" clearable placeholder="请输入客户简称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="客户编号">
                            <el-input v-model="formData.code" clearable placeholder="请输入客户编号" v-type="'code'" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="重要等级" prop="important_level">
                            <el-select v-model="formData.important_level" placeholder="请选择重要等级" class=" flex-1">
                                <el-option :label="item.name" :value="parseInt(item.value)"
                                    v-for="item in dictData.custom_important_level">
                                </el-option>
                            </el-select>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="负责部门" prop="dept_id">
                            <el-select v-model="formData.dept_id" placeholder="请选择部门" class="flex-1">
                                <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="客户分类" prop="category">
                            <el-select v-model="formData.category" placeholder="请选择客户分类" class=" flex-1">
                                <el-option :label="item.name" :value="(item.value)"
                                    v-for="item in dictData.custom_category">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票单位">
                            <el-input v-model="formData.invoice_company" clearable placeholder="请输入开票单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="纳税人识别号">
                            <el-input v-model="formData.taxpayer_identification_number" clearable placeholder="请输入纳税人识别号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户银行">
                            <el-input v-model="formData.opening_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票联系人">
                            <el-input v-model="formData.invoice_contact" clearable placeholder="请输入开票联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票单位注册地址">
                            <el-input v-model="formData.invoice_company_address" clearable placeholder="请输入开票单位注册地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票单位电话">
                            <el-input v-model="formData.invoice_company_telephone" clearable placeholder="请输入开票单位电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户账号">
                            <el-input v-model="formData.opening_bank_account" clearable placeholder="请输入开户账号"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="电子邮箱" prop="email">
                            <el-input v-model="formData.email" clearable placeholder="请输入电子邮箱" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="所在省份" prop="province">
                            <el-select v-model="formData.province" clearable placeholder="请选择省" @change="province_change"
                                class="flex-1">
                                <el-option v-for="(item, index) in datas.provinceOptions" :key="index"
                                    :label="item.province_name" :value="(item.province_code)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="所在市区" prop="city">
                            <el-select v-model="formData.city" clearable placeholder="请选择市" class="flex-1">
                                <el-option v-for="(item, index) in datas.cityOptions" :key="index" :label="item.city_name"
                                    :value="(item.city_code)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="邮编">
                            <el-input v-model="formData.post_code" clearable placeholder="请输入邮编" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="电话" prop="telephone">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入电话" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="传真">
                            <el-input v-model="formData.fax" clearable placeholder="请输入传真" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="网址">
                            <el-input v-model="formData.website" clearable placeholder="请输入网址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="地址">
                            <el-input v-model="formData.address" clearable placeholder="请输入地址" />
                        </el-form-item>
                    </el-col>

                    <CreateUserLable :form-data="formData" flag name="录入"></CreateUserLable>
                    <el-col :span="24">
                        <el-form-item label="附件">
                            <UploadAnnex :form-data="formData"></UploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>


                <formTable :formData="formData.detail" :config="tableConfig" :dictData="dictData">
                </formTable>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingCustomEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingCustomAdd, apiMarketingCustomEdit, apiMarketingCustomDetail, apiMarketingCustomContactsLists, apiMarketingCustomContactsDelete } from '@/api/marketing_custom'
import { checkPhone, checkEmail } from "@/utils/validate"
import type { PropType } from 'vue'
import { apiCityList, apiProvinceList } from "@/api/common";


defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    },
    deptList: Array
})
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 datas = reactive({
    provinceOptions: [],
    cityOptions: [],
});

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    sub_name: '',
    code: '',
    important_level: '',
    dept_id: '',
    category: '',
    remark: '',
    invoice_company: '',
    taxpayer_identification_number: '',
    opening_bank: '',
    invoice_contact: '',
    invoice_company_address: '',
    invoice_company_telephone: '',
    opening_bank_account: '',
    email: '',
    province: '',
    city: '',
    post_code: '',
    telephone: '',
    fax: '',
    website: '',
    address: '',
    annex: '',
    create_user: '',
    create_time: '',
    detail: [
        {
            "name": "",
            "dept": "",
            "gender": 0,
            "job": "",
            "mobile": "",
            "telephone": "",
            "email": "",
            "qq": "",
            "remark": ""
        }
    ]
})


const tableConfig = reactive(
    {
        title: "联系人",
        tableConfig: [
            {
                label: "姓名",
                value: 'name',

            },
            {
                label: "部门",
                value: 'dept',

            },
            {
                label: "性别",
                value: 'gender',
                select: [{
                    name: '男',
                    value: 0
                },
                {
                    name: '女',
                    value: 1
                },
                ]

            },
            {
                label: "职务",
                value: 'job',
            },
            {
                label: "手机",
                value: 'mobile',
                check: ['detail', checkPhone]
            },
            {
                label: "电话",
                value: 'telephone',
            },
            {
                label: "邮箱",
                value: 'email',
                check: ['detail', checkEmail]
            },
            {
                label: "qq",
                value: 'qq',
            },
            {
                label: "备注",
                value: 'remark',
            },

        ],
        deleteApi: apiMarketingCustomContactsDelete,
    }
)



// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入客户名称',
        trigger: ['blur']
    }],
    sub_name: [{
        required: true,
        message: '请输入客户简称',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入客户编号',
        trigger: ['blur']
    }],
    important_level: [{
        required: true,
        message: '请输入重要等级',
        trigger: ['blur']
    }],
    dept_id: [{
        required: true,
        message: '请输入负责部门',
        trigger: ['blur']
    }],
    category: [{
        required: true,
        message: '请输入客户分类',
        trigger: ['blur']
    }],

    invoice_company: [{
        required: true,
        message: '请输入开票单位',
        trigger: ['blur']
    }],
    taxpayer_identification_number: [{
        required: true,
        message: '请输入纳税人识别号',
        trigger: ['blur']
    }],
    opening_bank: [{
        required: true,
        message: '请输入开户银行',
        trigger: ['blur']
    }],
    invoice_contact: [{
        required: true,
        message: '请输入开票联系人',
        trigger: ['blur']
    }],
    invoice_company_address: [{
        required: true,
        message: '请输入开票单位注册地址',
        trigger: ['blur']
    }],
    invoice_company_telephone: [{
        required: true,
        message: '请输入开票单位电话',
        trigger: ['blur']
    }],
    opening_bank_account: [{
        required: true,
        message: '请输入开户账号',
        trigger: ['blur']
    }],
    email: [{
        required: true,
        message: '请输入电子邮箱',
        trigger: ['blur']
    },
    {
        validator: checkEmail,
        trigger: ['blur']
    }],
    province: [{
        required: true,
        message: '请输入省份',
        trigger: ['blur']
    }],
    city: [{
        required: true,
        message: '请输入城市',
        trigger: ['blur']
    }],
    post_code: [{
        required: true,
        message: '请输入邮编',
        trigger: ['blur']
    }],
    telephone: [{
        required: true,
        message: '请输入电话',
        trigger: ['blur']
    },
    {
        validator: checkPhone,
        trigger: ['blur']
    }],
    fax: [{
        required: true,
        message: '请输入传真',
        trigger: ['blur']
    }],
    website: [{
        required: true,
        message: '请输入网址',
        trigger: ['blur']
    }],
    address: [{
        required: true,
        message: '请输入地址',
        trigger: ['blur']
    }],
    create_user: [{
        required: true,
        message: '请输入录入人',
        trigger: ['blur']
    }],
    create_time: [{
        required: true,
        message: '请输入录入日期',
        trigger: ['blur']
    }]
})


// 获取详情
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]
        }
    }
    let res = await apiMarketingCustomContactsLists({ custom_id: data.id })
    formData.detail = res.lists
    getCityList();


}

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


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

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

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

const getProvinceList = async () => {
    const data = await apiProvinceList({});
    datas["provinceOptions"] = data;
};
const getCityList = async () => {
    const data = await apiCityList({ province_code: formData.province });
    datas["cityOptions"] = data;
};

//获取省份
function province_change(value: string) {
    getCityList();
}

getProvinceList()

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