<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @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="num">
                            <el-input v-model="formData.num" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project">
                            <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="project_num">
                            <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" readonly />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目负责人" prop="head">
                            <el-input v-model="formData.head" clearable placeholder="请输入项目负责人" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="关联合同" prop="affcontract">
                            <el-input v-model="formData.affcontract_name" clearable placeholder="请输入关联合同" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="行业" prop="industry">
                            <el-select class="flex-1" v-model="formData.industry" clearable placeholder="请选择行业类型">
                                <el-option v-for="(item, index) in dictData.supervision_project_industry" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目回款总额" prop="hk" label-width="120px">
                            <el-input v-model="formData.hk" clearable placeholder="请输入项目回款总额" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="提成比例（%）" prop="rate" label-width="120px">
                            <el-input v-model="formData.rate" clearable placeholder="请输入提成比例（%）" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="应提成总金额" prop="ticheng" label-width="120px">
                            <el-input v-model="formData.ticheng" clearable placeholder="请输入应提成总金额" type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="已支付总额" prop="paid_amount">
                            <el-input v-model="formData.paid_amount" clearable placeholder="请输入已支付总额" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本次支付总额" prop="bczfze" label-width="120px">
                            <el-input v-model="formData.bczfze" clearable placeholder="请输入本次支付总额" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="登记人" prop="djr">
                            <el-input v-model="formData.djr" clearable placeholder="请输入登记人" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="登记日期" prop="apptime">
                            <el-date-picker class="flex-1 !flex" v-model="formData.apptime" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </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-dialog v-model="showDialog" title="选择项目" width="70%">
                    <cost_project @customEvent="customEvent"></cost_project>
                </el-dialog>
                <div v-if="showDialog3">
                    <personnelselector ref="personnel" @confirm="submituser" type="1">
                    </personnelselector>
                </div>
            </el-form>
            <div style="margin-bottom: 30px;">任务明细</div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.project_commission_detail">
                    <el-table-column label="序号" v-if="mode == 'add'" width="150px">
                        <template #default="{ row }">
                            <el-button @click="handleAdd(row)">+</el-button>
                            <el-button @click="handleDelete(row)">-</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="工程师" prop="level" width="200px">
                        <template #default="scope">
                            <el-input v-model="scope.row.engineer" :disabled="mode != 'add'"
                                @click="userclick(scope.$index, 'engineer')" placeholder="点击选择" />
                        </template>
                    </el-table-column>
                    <el-table-column label="任务名称" prop="task_name" width="200px">
                        <template #default="{ row }">
                            <el-input v-model="row.task_name" :disabled="mode != 'add'" />
                        </template>
                    </el-table-column>
                    <el-table-column label="任务类型" prop="task_type" width="200px">
                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.task_type" clearable placeholder="请选择任务类型"
                                :disabled="mode != 'add'">
                                <el-option v-for="(item, index) in dictData.task_type" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                            <!-- {{ dictData.task_type }} -->
                        </template>
                    </el-table-column>
                    <el-table-column label="专业类型" prop="professional_type" width="200px">
                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.professional_type" clearable placeholder="请选择专业类型"
                                :disabled="mode != 'add'">
                                <el-option v-for="(item, index) in dictData.major_type" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </template>
                        <!-- {{ dictData.major_type }} -->
                    </el-table-column>
                    <el-table-column label="造价金额" prop="zj_amount" width="200px">
                        <template #default="scope">
                            <el-input v-model="scope.row.zj_amount" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="收款金额" prop="sk_amount" width="200px">
                        <template #default="scope">
                            <el-input v-model="scope.row.sk_amount" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="其他增减费用" prop="other_fee" width="200px">
                        <template #default="scope">
                            <el-input v-model="scope.row.other_fee" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="提成比例(%)" prop="tc_rate" width="200px">
                        <template #default="scope">
                            <el-input v-model="scope.row.tc_rate" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="应提成金额" prop="tc_amount" width="200px">
                        <template #default="{ row }">
                            <el-input v-model="row.tc_amount" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="本次支付金额" prop="pay_amount" width="200px">
                        <template #default="{ row }">
                            <el-input v-model="row.pay_amount" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" width="200px">
                        <template #default="{ row }">
                            <el-input v-model="row.remark" :disabled="mode != 'add'" type="number" />
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </popup>
    </div>
</template>

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


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

// 表单数据
const formData = reactive({
    id: '',
    dataid: '',
    num: '',
    project_id: '',
    project_name: '',
    project_num: '',
    head: '',
    affcontract: '',
    affcontract_name: "",
    contract: {},
    industry: '',
    hk: '',
    rate: '',
    ticheng: '',
    paid_amount: '',
    bczfze: '',
    djr: '',
    apptime: '',
    remark: '',
    projectInfo: {},
    project_commission_detail: [{
        engineer: '',
        engineer_id: '',
        task_name: '',
        task_type: '',
        professional_type: '',
        zj_amount: '',
        sk_amount: '',
        tc_rate: '',
        tc_amount: '',
        pay_amount: '',
        remark: '',
        other_fee: '',
    }]

})
const customEvent = (e) => {
    console.log(e)
    formData.project_id = e.id
    formData.project_name = e.project_name
    formData.project_num = e.project_num
    formData.head = e.principal
    formData.affcontract = e.contract.id
    formData.affcontract_name = e.contract.contract_name
    showDialog.value = false
}


const handleAdd = () => {
    formData.project_commission_detail.push({
        engineer: '',
        engineer_id: '',
        task_name: '',
        task_type: '',
        professional_type: '',
        zj_amount: '',
        sk_amount: '',
        tc_rate: '',
        tc_amount: '',
        pay_amount: '',
        remark: '',
        other_fee: '',
    })
}


const handleDelete = (row: any) => {
    if (formData.project_commission_detail.length == 1) return
    const index = formData.project_commission_detail.indexOf(row);
    formData.project_commission_detail.splice(index, 1);
}

const tableIndex = ref(0)
const tableKey = ref('')
//打开弹窗
const userclick = async (e: any, key) => {
    tableIndex.value = e
    tableKey.value = key
    showDialog3.value = true
    await nextTick()
    personnel.value.open()
    console.log(tableIndex.value, tableKey.value)

}

//确认
const submituser = (e: any) => {
    formData.project_commission_detail[tableIndex.value][tableKey.value] = e.name
    formData.project_commission_detail[tableIndex.value][tableKey.value + '_id'] = e.id
    showDialog3.value = false
}

// 表单验证
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]
        }
    }
    formData.project_id = formData.projectInfo.id
    formData.project_name = formData.projectInfo.project_name
    formData.project_num = formData.projectInfo.project_num
    formData.head = formData.projectInfo.principal
    formData.affcontract = formData.contract.id
    formData.affcontract_name = formData.contract.contract_name

}

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


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