<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="auto" :rules="formRules">

                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id" @click="showDialog2 = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_name" clearable readonly placeholder="请输入项目名称" :disabled="project" />
                        </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 placeholder="请输入项目编码" disabled />
                        </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="idcard"
                            :rules="[{ required: true, validator: checkIDcard, trigger: 'blur' }]">
                            <el-input v-model="formData.idcard" clearable placeholder="请输入身份证号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="手机号" prop="mobile"
                            :rules="[{ required: true, validator: checkPhone, trigger: 'blur' }]">
                            <el-input v-model="formData.mobile" clearable placeholder="请输入手机号" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="工种" prop="work_type"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-select v-model="formData.work_type" clearable placeholder="请选择工种" class="flex-1">
                                <el-option v-for="(item, index) in allJobType" :key="index" :label="item.type_name"
                                    :value="parseInt(item.id)" />
                            </el-select>
                        </el-form-item>

                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="人工单价" prop="unit_price"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.unit_price" clearable placeholder="请输入人工单价" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="每日生活费" prop="daily_living_expenses"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.daily_living_expenses" clearable placeholder="请输入每日生活费"
                                type="number" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="身份证正面">
                            <material-picker v-model="formData.idcard_front" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="身份证反面">
                            <material-picker v-model="formData.idcard_backend" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="银行卡照片">
                            <material-picker v-model="formData.bank_card" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="银行卡号" prop="bank_no">
                            <el-input v-model="formData.bank_no" clearable placeholder="请输入银行卡号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户银行" prop="deposit_bank">
                            <el-input v-model="formData.deposit_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="初期收入" prop="opening_income">
                            <el-input v-model="formData.opening_income" clearable placeholder="请输入初期收入" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>

                </el-row>







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

            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { projectpersonnelAdd, projectpersonnelEdit, projectpersonnelDetail } from '@/api/project_personnel'
import { useDictData } from '@/hooks/useDictOptions'
import { projectjobtypeAll } from "@/api/project_job_type.ts"
const { dictData } = useDictData('work_type')

let props = defineProps({
    project: Object
})
const project_name = ref('')
const project_code = ref('')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog2 = ref(false)



const allJobType = reactive([])
const getallJobType = async () => {
    let res = await projectjobtypeAll()
    Object.assign(allJobType, res)
}
getallJobType()

//验证
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 checkIDcard = (rule: any, value: any, callback: (arg0: Error) => any) => {
    const Reg = /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[Xx\d]$/
    if (!value) {
        return callback(new Error('身份证号码不能为空'))
    }
    setTimeout(() => {
        if (Reg.test(value)) {
            callback()
        } else {
            callback(new Error('身份证号码格式不正确'))
        }

    }, 100)
}


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目人员表' : '新增项目人员表'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: "",
    name: "",
    idcard: "",
    mobile: "",
    work_type: "",
    unit_price: "",
    daily_living_expenses: "",
    idcard_front: "",
    idcard_backend: "",
    bank_card: "",
    bank_no: "",
    deposit_bank: "",
    remark: "",
    opening_income: "",

})

const customEvent2 = (e: any) => {
    formData.project_id = e.id;
    project_name.value = e.name;
    project_code.value = e.project_code;
    showDialog2.value = false;
};

if (props.project) customEvent2(props.project)

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

})



// 获取详情
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 projectpersonnelDetail({
        id: row.id
    })
    setFormData(data)
}


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

    await formRef.value?.validate()

    const data = { ...formData }
    mode.value == 'edit'
        ? await projectpersonnelEdit(data)
        : await projectpersonnelAdd(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>
