<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="equipment_budget_id">
                    <!-- <el-input v-model="formData.equipment_budget_id" clearable placeholder="请输入机具预算id" /> -->
                    <el-input v-model="formData.project_name" readonly clearable placeholder="点击选择项目"
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="机具类别" prop="type">
                    <el-input v-model="formData.type" clearable placeholder="请输入机具类别" />
                </el-form-item>
                <el-form-item label="机具名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入机具名称" />
                </el-form-item>
                <el-form-item label="规格型号" prop="spec">
                    <el-input v-model="formData.spec" 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" @change="getAmout">
                    <el-input v-model="formData.num" clearable placeholder="请输入数量" type="number" />
                </el-form-item>
                <el-form-item label="单价" prop="price" @change="getAmout">
                    <el-input v-model="formData.price" clearable placeholder="请输入单价" />
                </el-form-item>
                <el-form-item label="金额" prop="amount">
                    <el-input v-model="formData.amount" disabled 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>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <projectDialog @customEvent="customEvent"></projectDialog>
            </el-dialog>

        </popup>
    </div>
</template>

<script lang="ts" setup name="projectEquipmentBudgetDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectEquipmentBudgetDetailAdd, apiProjectEquipmentBudgetDetailEdit, apiProjectEquipmentBudgetDetailDetail } from '@/api/project_equipment_budget_detail'
import { timeFormat } from '@/utils/util'
import projectDialog from '@/components/project_equipment_budget_detail/index.vue'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑机具预算明细' : '新增机具预算明细'
})

const customEvent = (e) => {
    console.log(e)
    formData.equipment_budget_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}
// 表单数据
const formData = reactive({
    id: '',
    "equipment_budget_id": 0,
    project_name: "",
    "type": "",
    "name": "",
    "spec": "",
    "unit": "",
    "num": "",
    "price": "",
    "remark": "",
    amount: ''
})

const getAmout = () => {
    formData.amount = formData.price * formData.num || 0
}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    equipment_budget_id: [{
        required: true,
        message: '请输入机具预算id',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入机具类别',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入机具名称',
        trigger: ['blur']
    }],
    spec: [{
        required: true,
        message: '请输入规格型号',
        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 apiProjectEquipmentBudgetDetailDetail({
        id: row.id
    })
    setFormData(data)
}


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