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

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="subpackage_budget_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_name" readonly clearable placeholder="请输入项目名称" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="subpackage_budget_id">
                            <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工作类型" prop="work_type">
                            <el-input v-model="formData.work_type" clearable placeholder="请输入工作类型" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工作内容" prop="work_content">
                            <el-input v-model="formData.work_content" clearable placeholder="请输入工作内容" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目特征" prop="project_features">
                            <el-input v-model="formData.project_features" clearable placeholder="请输入项目特征" />
                        </el-form-item>
                    </el-col> <el-col :span="8">
                        <el-form-item label="单位" prop="unit">
                            <el-input v-model="formData.unit" clearable placeholder="请输入单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工作量" prop="num">
                            <!-- <el-input-number v-model="formData.num" :max="10000" placeholder="请输入工作量"
                                oninput="if(formData.num.length>4)formData.num=formData.num.slice(0,4)" /> -->
                            <el-input v-model="formData.num" clearable placeholder="请输入工作量" @change="numberChange" />
                        </el-form-item>
                    </el-col> <el-col :span="8">
                        <el-form-item label="单价" prop="price">
                            <el-input v-model="formData.price" clearable placeholder="请输入单价" @change="numberChange" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <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>

    </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/subpackage_budget/index.vue'
import { MaterialbudgetDetailLists } from '@/api/project_material_budget_detail'
import prodctTable from '@/components/product/index.vue'
import { subpackagdetailAdd, subpackagdetailEdit, subpackagdetailDetail } from '@/api/project_subpackage_budget_detail'
import customDialog from '@/components/project_personnel/index.vue'
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 person_name = ref('')
const idcard = ref('')
const tableData = ref([{}])

import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const formDataannex = reactive([])

const numberChange = () => {
    if (formData.num > 10000) formData.num = 10000
    if (formData.price > 10000) formData.price = 10000
}

//获取值
const customEvent = (e) => {
    formData.subpackage_budget_id = e.id
    project_name.value = e.project_name
    project_code.value = e.project_code
    showDialog.value = false

}

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


// 表单数据
const formData = reactive({
    id: '',
    subpackage_budget_id: '',
    work_type: '',
    work_content: '',
    project_features: '',
    unit: '',
    num: 0,
    price: 0,
    remark: '',
    project_id: "",
})


// 表单验证
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]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code


}

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


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

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