<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="70vw" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="采购单位" prop="company_name">
                            <el-input v-model="formData.company_name" clearable placeholder="点击选择采购单位" readonly
                                @click="showDialog2 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进场主题" prop="theme">
                            <el-input v-model="formData.theme" clearable placeholder="请输入进场主题" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进场时间" prop="enter_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.enter_time" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择进场时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="共同参与人" prop="co_participant">
                            <el-input v-model="formData.co_participant" clearable placeholder="请输入共同参与人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进场结果" prop="enter_result">
                            <el-radio-group v-model="formData.enter_result" placeholder="请选择骑缝盖章">
                                <el-radio :label="0">进场</el-radio>
                                <el-radio :label="1">进场后复检</el-radio>
                                <el-radio :label="2">退检</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否平行检验" prop="parallel_test">
                            <el-radio-group v-model="formData.parallel_test" placeholder="请选择骑缝盖章">
                                <el-radio :label="0">否</el-radio>
                                <el-radio :label="1">是</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <supervision_project @customEvent="customEvent"></supervision_project>
                </el-dialog>
                <div v-if="showDialog2">
                    <el-dialog v-model="showDialog2" width=" 70%" title="选择施工单位">
                        <supervision_participating_units @customEvent="customEvent1" :project_id="formData.project_id"
                            :unit_type="6">
                        </supervision_participating_units>
                    </el-dialog>
                </div>
                <el-dialog v-model="showDialog3" title="选择材料信息" width="70%">
                    <supervision_material_equipment_info @customEvent="customEvent2">
                    </supervision_material_equipment_info>
                </el-dialog>
            </el-form>
            <div style="margin-bottom: 30px;">材料信息 <el-button type="primary" @click="handleAdd"
                    v-if="formData.entry_detail.length == 0" style="float: right;">新增</el-button></div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.entry_detail">
                    <el-table-column label="序号" width="150px">
                        <template #default="{ row }">
                            <el-button @click="handleAdd()">+</el-button>
                            <el-button @click="handleDelete(row)">-</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="材料名称" prop="name">

                        <template #default="scope">
                            <el-input v-model="scope.row.name" @click="showDialog3 = true, tableIndex = scope.$index" />
                        </template>
                    </el-table-column>
                    <el-table-column label="材料品牌" prop="brand">

                        <template #default="scope">
                            <el-input v-model="scope.row.brand" />
                        </template>
                    </el-table-column>
                    <el-table-column label="单位" prop="unit">

                        <template #default="{ row }">
                            <el-input v-model="row.unit" />
                        </template>
                    </el-table-column>
                    <el-table-column label="是否合同约定品牌" prop="contract_brand">

                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.contract_brand" clearable placeholder="请选择">
                                <el-option label="否" :value="0" />
                                <el-option label="是" :value="1" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="型号" prop="model">

                        <template #default="{ row }">
                            <el-input v-model="row.model" />
                        </template>
                    </el-table-column>
                    <el-table-column label="进场数量" prop="entry_number">

                        <template #default="{ row }">
                            <el-input v-model="row.entry_number" />
                        </template>
                    </el-table-column>
                </el-table>
            </div>


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

<script lang="ts" setup name="supervisionMaterialEntryEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionMaterialEntryAdd, apiSupervisionMaterialEntryEdit, apiSupervisionMaterialEntryDetail, apisupervision_material_entry_detail, apisupervision_material_entry_detail_delete } from '@/api/supervision_material_entry'
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')
const showDialog = ref(false)
const showDialog2 = ref(false)
const showDialog3 = ref(false)
const tableIndex = ref(0)



// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--材料进场' : '新增工程监理--材料进场'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: 0,
    project_name: "",
    company_id: '',
    company_name: "",
    theme: '',
    code: '',
    enter_time: '',
    co_participant: '',
    enter_result: '',
    parallel_test: '',
    remark: '',
    create_user: '',
    entry_detail: [{
        "name": "",
        "brand": "",
        "model": "",
        "unit": "",
        "contract_brand": 0,
        "entry_number": 0
    }]
})


const handleAdd = () => {
    formData.entry_detail.push({
        "name": "",
        "brand": "",
        "model": "",
        "unit": "",
        "contract_brand": 0,
        "entry_number": 0
    })
}
const handleDelete = async (row: any) => {
    // if (formData.entry_detail.length == 1) return
    if (row.id) {
        await apisupervision_material_entry_detail_delete({ id: row.id })
        const index = formData.entry_detail.indexOf(row);
        formData.entry_detail.splice(index, 1);
    } else {
        const index = formData.entry_detail.indexOf(row);
        formData.entry_detail.splice(index, 1);
    }

}



const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    formData.company_id = ''
    formData.company_name = ''

}
const customEvent1 = (e) => {
    formData.company_id = e.id
    formData.company_name = e.project_name
    showDialog2.value = false
}
const customEvent2 = (e) => {
    console.log(e)
    for (let key in formData.entry_detail[tableIndex.value]) {
        (formData.entry_detail[tableIndex.value])[key] = e[key]
    }
    showDialog3.value = false
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入采购单位id',
        trigger: ['blur']
    }],
    theme: [{
        required: true,
        message: '请输入进场主题',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入编号',
        trigger: ['blur']
    }],
    enter_time: [{
        required: true,
        message: '请输入进场时间',
        trigger: ['blur']
    }],
    co_participant: [{
        required: true,
        message: '请输入共同参与人',
        trigger: ['blur']
    }],
    enter_result: [{
        required: true,
        message: '请输入进场结果 0-进场 1-进场后复检 2-退检',
        trigger: ['blur']
    }],
    parallel_test: [{
        required: true,
        message: '请输入是否平行检验 0-否 1-是',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }],
    create_user: [{
        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]
        }
    }
    let res = await apisupervision_material_entry_detail({ material_entry_id: formData.id })
    formData.entry_detail = res.lists
}

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


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