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

                <el-form-item label="项目名称" prop="labor_budget_id">
                    <el-input v-model="formData.project_name" clearable readonly placeholder="点击选择项目"
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="工种" prop="job_type_id">
                    <el-select v-model="formData.job_type_id" class="flex-1">
                        <el-option :label="item.type_name" :value="item.id" v-for="item in jobList"></el-option>
                    </el-select>
                    <!-- <el-input v-model="formData.job_type_id" clearable placeholder="请输入工种id" /> -->

                </el-form-item>
                <el-form-item label="人工说明" prop="desc">
                    <el-input v-model="formData.desc" clearable placeholder="请输入人工说明" />
                </el-form-item>
                <el-form-item label="单位" prop="unit">
                    <el-input v-model="formData.unit" clearable placeholder="请输入单位" />
                </el-form-item>
                <el-form-item label="数量" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入数量" />
                </el-form-item>
                <el-form-item label="单价" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入单价" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
            </el-form>

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

<script lang="ts" setup name="projectLaborBudgetDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectLaborBudgetDetailAdd, apiProjectLaborBudgetDetailEdit, apiProjectLaborBudgetDetailDetail } from '@/api/project_labor_budget_detail'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import projectDialog from "@/components/project_labor_budget/index.vue"
import { projectjobtypeLists } from "@/api/project_job_type.ts";

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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑人工预算明细' : '新增人工预算明细'
})

// 表单数据
const formData = reactive({
    id: "",
    "labor_budget_id": 0,
    project_name: "",
    "job_type_id": '',
    "desc": "",
    "unit": "",
    "num": 0,
    "price": 0,
    "remark": ""
})

const customEvent = (e) => {
    formData.labor_budget_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

// 工种信息
const jobList = ref([])
const getJobList = async () => {
    let res = await projectjobtypeLists()
    jobList.value = res.lists
}
getJobList()


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    labor_budget_id: [{
        required: true,
        message: '请输入人工预算id',
        trigger: ['blur']
    }],
    job_type_id: [{
        required: true,
        message: '请输入工种id',
        trigger: ['blur']
    }],
    unit: [{
        required: true,
        message: '请输入单位',
        trigger: ['blur']
    }],
    num: [{
        required: true,
        message: '请输入数量',
        trigger: ['blur']
    }],
    price: [{
        required: true,
        message: '请输入单价',
        trigger: ['blur']
    }],
    amount: [{
        required: true,
        message: '请输入金额',
        trigger: ['blur']
    }]
})


// 获取详情
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]
        }
    }


}

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


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