<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">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称">
                            <el-input v-model="project_name" clearable placeholder="请选择项目材料预算" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码">
                            <el-input v-model="project_code" clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="申请日期" prop="apply_date">

                            <el-date-picker class="flex-1 !flex" v-model="apply_date" 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="arrival_date">

                            <el-date-picker class="flex-1 !flex" v-model="arrival_date" 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="材料类别">
                            <el-input v-model="material_first_level" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="材料中类">
                            <el-input v-model="material_second_level" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="材料小类">
                            <el-input v-model="material_three_level" disabled clearable placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="材料名称">
                            <el-input v-model="material_name" disabled 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" disabled 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="请输入数量" />
                        </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>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import customDialog from '@/components/custom-dialog/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { toChinesNum } from "@/utils/util";
import { materialrequestDetailAdd, materialrequestDetailEdit, materialrequestDetailDetail } from '@/api/material_purchase_request_detail'
import { getAllProjectTypes } from '@/api/projecttype'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const active = ref(0)

const project_name = ref('')
const project_code = ref('')
const apply_date = ref('')
const arrival_date = 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 material_first_level = ref('')
const material_second_level = ref('')
const material_three_level
    = ref('')
const next = () => {
    if (active.value++ > 3) active.value = 0
}

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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目表' : '新增项目表'
})

// 表单数据
const formData = reactive({
    id: '',
    num: '',
    remark: '',

})


// 表单验证
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
    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_first_level.value = data.material_first_level
    material_second_level.value = data.material_second_level
    material_three_level.value = data.material_three_level
    material_specs.value = data.material_specs
    project_name.value = data.project_name;
    project_code.value = data.project_code
    apply_date.value = data.apply_date

    arrival_date.value = data.arrival_date


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


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

    await formRef.value?.validate()


    const data = { ...formData }
    mode.value == 'edit'
        ? await materialrequestDetailEdit(data)
        : await materialrequestDetailAdd(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>
