<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="160px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目" prop="project_id" @click="showDialog = true" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_name" readonly clearable placeholder="请选择项目" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_code" readonly clearable placeholder="请选择项目" />
                        </el-form-item> </el-col>

                    <el-col :span="8">
                        <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.name" clearable placeholder="请输入姓名" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="上级" prop="superior">
                            <el-input v-model="formData.superior" clearable placeholder="请输入上级" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="部门" prop="department">
                            <el-input v-model="formData.department" clearable placeholder="请输入部门" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="岗位" prop="station">
                            <el-input v-model="formData.station" clearable placeholder="请输入岗位" />
                        </el-form-item> </el-col>
                    <el-col :span="24">
                        <el-form-item label="身份" prop="identity" :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-radio-group v-model="formData.identity" placeholder="请选择身份">
                                <el-radio v-for="(item, index) in dictData.identity" :key="index" :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="电话" prop="phone" :rules="[{ required: true, validator: checkPhone, trigger: 'blur' }]">
                            <el-input v-model="formData.phone" clearable placeholder="请输入电话" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="邮箱" prop="email" :rules="[{ validator: userEmail, trigger: 'blur' }]">
                            <el-input v-model="formData.email" clearable placeholder="请输入邮箱" />
                        </el-form-item> </el-col>

                    <el-col :span="24">
                        <el-form-item label="对我方态度" prop="email">
                            <el-radio-group v-model="formData.attitude" placeholder="请选择对我方态度">
                                <el-radio v-for="(item, index) in dictData.attitude_status" :key="index" :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="工作配合度" prop="email">
                            <el-radio-group v-model="formData.degree_adaptability" placeholder="请选择工作配合度">
                                <el-radio v-for="(item, index) in dictData.degree_adaptability" :key="index" :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                        </el-form-item> </el-col>
                </el-row>

            </el-form>
        </popup>

        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectDialog @customEvent="customEvent"></projectDialog>
        </el-dialog>

    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { apiProjectstakeholderAdd, apiProjectstakeholderEdit, apiProjectstakeholderDetail } from '@/api/project_stakeholder'
import { getAllProjectRoles } from '@/api/project_role_set'
import { timeFormat } from '@/utils/util'
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 personnel = ref<any>()
const mode = ref('add')

const list1 = ref([])
const showDialog = ref(false)
const project_name = ref('')
const project_code = ref('')
const admin_name = ref('')

import feedback from '@/utils/feedback'
import configs from "@/config"

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目干系人设置' : '新增项目干系人设置'
})
const checkPhone = (rule: any, value: any, callback: (arg0: Error) => any) => {
    const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
    if (!value) {
        return callback(new Error('电话号码不能为空'))
    }
    setTimeout(() => {
        if (phoneReg.test(value)) {
            callback()
        } else {
            callback(new Error('电话号码格式不正确'))
        }

    }, 100)
}
const userEmail = (rule: any, value: string, callback: (arg0: Error | undefined) => void) => {

    const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
    if (value && !mailReg.test(value)) {
        callback(new Error('请输入正确的邮箱格式'))
    } else {
        callback()
    }
    // if (!value) {
    //     return callback(new Error('邮箱不能为空'))
    // }
    // setTimeout(() => {
    //     if (mailReg.test(value)) {
    //         callback()
    //     } else {
    //         callback(new Error('请输入正确的邮箱格式'))
    //     }
    // }, 100)
};
// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    name: '',
    identity: '',
    superior: '',
    department: '',
    station: '',
    phone: '',
    email: '',
    attitude: '',
    degree_adaptability: '',
    remark: ''
})

//获取值
const customEvent = (e) => {
    formData.project_id = e.id
    project_name.value = e.name
    project_code.value = e.project_code
    showDialog.value = false

}
// 表单验证
const formRules = reactive<any>({

})
//获取角色

const getlist1 = () => {
    getAllProjectRoles().then((res) => {
        list1.value = res
    })
}

// 获取详情
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]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code

}

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


// 提交按钮
const handleSubmit = async () => {

    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectstakeholderEdit(data)
        : await apiProjectstakeholderAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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

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