<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" clearable disabled placeholder="系统自动填写" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="角色名称" prop="project_role_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="w-[280px]" v-model="formData.project_role_id" clearable
                                placeholder="请选择角色名称">

                                <el-option v-for="(item, index) in list1" :key="index" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item></el-col>
                    <el-col :span="8">
                        <el-form-item label="人员名称" prop="admin_id" @click="userclick"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="admin_name" clearable readonly placeholder="请输入人员名称" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="人工单价" prop="working_unit_price"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.working_unit_price" clearable placeholder="请输入人工单价" />
                        </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>
        <personnelselector ref="personnel" @confirm="submituser" type="1"></personnelselector>
    </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 personnelselector from '@/components/personnelselector/index.vue'
import { apiProjectmemberAdd, apiProjectmemberEdit, apiProjectmemberDetail } from '@/api/project_member'
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 formData = reactive({
    id: '',
    project_id: '',
    admin_id: '',
    working_unit_price: '',
    remark: '',
    project_role_id: '',

})

//获取值
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 userclick = () => {
    personnel.value.open()

}
//确认
const submituser = (e: any) => {
    formData.admin_id = e.id
    admin_name.value = e.name
}

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

}

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


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

    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectmemberEdit(data)
        : await apiProjectmemberAdd(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>
