<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="工程师" prop="engineer">
                    <el-input v-model="formData.engineer" clearable placeholder="请输入工程师" @click="userclick" />
                </el-form-item>

                <el-form-item label="任务名称" prop="task_name">
                    <el-input v-model="formData.task_name" clearable placeholder="请输入任务名称" />
                </el-form-item>
                <el-form-item label="任务类型" prop="task_type">
                    <el-select class="flex-1" v-model="formData.task_type" clearable placeholder="请选择任务类型">
                        <el-option v-for="(item, index) in dictData.task_type" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="专业类型" prop="professional_type">
                    <el-select class="flex-1" v-model="formData.professional_type" clearable placeholder="请选择专业类型">
                        <el-option v-for="(item, index) in dictData.major_type" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="造价金额" prop="zj_amount">
                    <el-input v-model="formData.zj_amount" clearable placeholder="请输入造价金额" type="number" />
                </el-form-item>
                <el-form-item label="收款金额" prop="sk_amount">
                    <el-input v-model="formData.sk_amount" clearable placeholder="请输入收款金额" type="number" />
                </el-form-item>
                <el-form-item label="提成比例(%)" prop="tc_rate">
                    <el-input v-model="formData.tc_rate" clearable placeholder="请输入提成比例(%)" type="number" />
                </el-form-item>
                <el-form-item label="应提成金额" prop="tc_amount">
                    <el-input v-model="formData.tc_amount" clearable placeholder="请输入应提成金额" type="number" />
                </el-form-item>
                <el-form-item label="本次支付金额" prop="pay_amount">
                    <el-input v-model="formData.pay_amount" clearable placeholder="请输入本次支付金额" type="number" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="其他增减费用" prop="other_fee">
                    <el-input v-model="formData.other_fee" clearable placeholder="请输入其他增减费用" />
                </el-form-item>
                <div v-if="showDialog3">
                    <personnelselector ref="personnel" @confirm="submituser" type="1">
                    </personnelselector>
                </div>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectCommissionDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectCommissionDetailAdd, apiProjectCommissionDetailEdit, } from '@/api/project_commission_detail'
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 mode = ref('add')
const showDialog3 = ref(false)
const personnel = ref<any>()


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑提成明细' : '新增提成明细'
})

// 表单数据
const formData = reactive({
    id: '',
    project_commission_dataid: '',
    engineer: '',
    engineer_id: '',
    task_name: '',
    task_type: '',
    professional_type: '',
    zj_amount: '',
    sk_amount: '',
    tc_rate: '',
    tc_amount: '',
    pay_amount: '',
    remark: '',
    other_fee: '',

})


formData.tc_amount = computed(() => {
    return (Number(formData.sk_amount || 0) + Number(formData.other_fee || 0)) * (formData.tc_rate / 100) || 0
})

//打开弹窗
const userclick = async (e: any) => {
    showDialog3.value = true
    await nextTick()
    personnel.value.open()
}

//确认
const submituser = (e: any) => {
    formData.engineer = e.name
    formData.engineer_id = e.id
    showDialog3.value = false
}

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

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    delete data.tc_amount
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

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


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