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

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="material_budget_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_name" clearable placeholder="请选择项目材料预算" />
                        </el-form-item> </el-col>
                    <!-- <el-col :span="8">
                        <el-form-item label="项目编码" prop="material_budget_id">
                            <el-input v-model="project_code" clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="类型" prop="budget_type">
                            <el-select v-model="formData.budget_type" clearable placeholder="请选择类型">
                                <el-option v-for="(item, index) in dictData.budget_type" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="材料名称" prop="material_id">
                            <el-input v-model="formData.material_name" readonly @click="showDialog1 = true" clearable
                                placeholder="点击选择材料" />
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="8">
                        <el-form-item label="材料编码">
                            <el-input v-model="material_code" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="规格型号">
                            <el-input v-model="material_specs" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="品牌">
                            <el-input v-model="material_brand" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :span="8">
                        <el-form-item label="单位">
                            <el-input v-model="material_unit" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :span="8">
                        <el-form-item label="参数说明">
                            <el-input v-model="material_parameter_description" disabled clearable placeholder="请输入单位" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="单价" prop="price">
                            <el-input v-model="formData.price" clearable placeholder="请输入单价" />
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="8">
                        <el-form-item label="预算总数量">
                            <el-input v-model="material_num" clearable placeholder="请输入预算总数量" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="预算调整数量" prop="num">
                            <el-input v-model="formData.num" clearable placeholder="请输入预算调整数量" class="w-[220px]" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择材料预算" width="70%">
            <projectDialog @customEvent="customEvent"></projectDialog>
        </el-dialog>
        <el-dialog v-model="showDialog1" title="选择材料" width="70%">
            <product @customEvent="customEvent2"></product>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/material_budget/index.vue'
import product from '@/components/product/index.vue'
import { MaterialbudgetDetailLists, MaterialbudgetDetailAdd, MaterialbudgetDetailEdit, MaterialbudgetDetailDetail } from '@/api/project_material_budget_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')
import feedback from '@/utils/feedback'
const showDialog = ref(false)
const showDialog1 = ref(false)
const project_name = ref('')
const project_code = ref('')


const material_name = ref('')
const material_code = ref('')
const material_parameter_description = ref('')
const material_brand = ref('')
const material_unit = ref('')
const material_specs = ref('')



//获取值
const customEvent = (e: any) => {
    formData.material_budget_id = e.id
    project_name.value = e.project_name
    project_code.value = e.project_code
    showDialog.value = false
    budgetDetailLists(e.id)
}
//获取值
const customEvent2 = (e: any) => {
    formData.material_id = e.id
    formData.material_name = e.name
    showDialog1.value = false
    // project_name.value = e.project_name
    // project_code.value = e.project_code
    // showDialog.value = false
    // budgetDetailLists(e.id)
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目材料预算调整' : '新增项目材料预算调整'
})

// 表单数据
const formData = reactive({
    id: '',
    material_budget_id: '',
    material_id: '',
    budget_type: '',
    price: '',
    num: '',
    remark: '',
    material_name: ""
})

// 获取详情
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
    material_name.value = data.material_name
    material_code.value = data.material_code
    material_parameter_description.value = data.material_parameter_description
    material_brand.value = data.material_brand
    material_unit.value = data.material_unit
    material_specs.value = data.material_specs
    formData.budget_type = String(formData.budget_type)

}

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

//获取预算明细列表
const budgetDetailLists = (id) => {











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

    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await MaterialbudgetDetailEdit(data)
        : await MaterialbudgetDetailAdd(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>
