<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <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="单位类别" prop="type">
                            <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择单位类别">
                                <el-option v-for="(item, index) in dictData.unit_type" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质等级" prop="qualification_grade">
                            <el-input v-model="formData.qualification_grade" clearable placeholder="请输入资质等级" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系电话" prop="telephone">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="责任范围" prop="duty">
                            <el-input v-model="formData.duty" clearable placeholder="请输入责任范围" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <formTable :formData="formData.contacts" :config="tableConfig"></formTable>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable @customEvent="customEvent" :config="manage_project" />
            </el-dialog>

        </popup>
    </div>
</template>

<script lang="ts" setup name="manageCompanyEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiManageCompanyAdd, apiManageCompanyEdit, apiManageCompanyDetail, apimanage_company_contacts, apimanage_company_contacts_delete } from '@/api/manage_company'
import { manage_project } from "@/components/dialogTable/dialogTableConfig"
import { checkPhone, checkEmail } from "@/utils/validate"

import type { PropType } from 'vue'
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: '',
    project_id: '',
    project_name: "",
    name: '',
    type: '',
    qualification_grade: '',
    telephone: '',
    duty: '',
    contacts: []
})


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

            },
            {
                label: "职务",
                value: 'duties'
            },
            {
                label: "办公电话",
                value: 'telephone',
            },
            {
                label: "手机号码",
                value: 'mobile',
                check: true
            },
            {
                label: "电子邮箱",
                value: 'email',
                check: true

            },
            {
                label: "传真",
                value: 'fax',
            },

        ],
        deleteApi: apimanage_company_contacts_delete,
    }
)


const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入单位名称',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入单位类别',
        trigger: ['blur']
    }],
    qualification_grade: [{
        required: true,
        message: '请输入资质等级',
        trigger: ['blur']
    }],
    telephone: [

        {
            required: true,
            message: '请输入联系电话',
            trigger: ['blur']
        },
        {
            validator: checkPhone,
            trigger: ['blur']
        }
    ],
    mobile: [
        {
            required: true,
            message: '请输入联sd系电话',
            trigger: ['blur']
        },
        {
            validator: checkPhone,
            trigger: ['blur']
        }
    ],
    email: [
        {
            validator: checkEmail,
            trigger: ['blur']
        }
    ],
    duty: [{
        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 apimanage_company_contacts({ company_id: formData.id })
    formData.contacts = res.lists
}

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


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