<template>
    <el-config-provider :locale="zhCn">
        <el-container class="content">
            <fieldset class="input-area">
                <legend style="font-size: xxx-large;">入库界面</legend>
                <el-form ref="taskInRequestRef" :model="taskInRequestEntity" :label-position="labelPosition"
                    label-width="100px" style="max-width: 100%" :rules="rules" status-icon size="large">
                    <el-row>
                        <el-col :span="22" :offset="1">
                            <el-form-item label="是否入空托" prop="isEmpty">
                                <el-select-v2 v-model="taskInRequestEntity.isEmpty" placeholder="请选择入库类型"
                                    :options="stockTypeOptions"
                                    @change="autoCompleteEmptyInfo(taskInRequestEntity)"></el-select-v2>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1">
                            <el-form-item label="箱号" prop="vehicleNo">
                                <el-input v-model="taskInRequestEntity.vehicleNo" ref="vehicleNo" clearable
                                    style="height: 50px;" v-on:keyup.tab="detectEndInputVehicleNo(taskInRequestEntity)" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1">
                            <el-form-item label="零件号" prop="goodsId">
                                <el-input v-model="taskInRequestEntity.goodsId" ref="goodsId" clearable
                                    :disabled="disabledEmpty" @blur="queryAndAutoComplete(taskInRequestEntity)"
                                    style="height: 50px;" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1">
                            <el-form-item label="零件名称" prop="goodsName">
                                <el-input v-model="taskInRequestEntity.goodsName" clearable readonly
                                    style="height: 50px;" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1">
                            <el-form-item label="零件数量" prop="goodsNum">
                                <el-input-number v-model.number="taskInRequestEntity.goodsNum" ref="goodsNum" clearable
                                    :disabled="disabledEmpty" controls-position="right" :min="1"
                                    @blur="detectEndInputNum(taskInRequestEntity)" style="height: 50px;"></el-input-number>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1">
                            <el-form-item label="零件重量（千克）" prop="weight">
                                <el-input v-model="taskInRequestEntity.weight" clearable readonly style="height: 50px;" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1" v-show="disPlayDateFlag">
                            <el-form-item label="有效期（年）" prop="shelfLife">
                                <!-- // TODO 日期 -->
                                <el-input v-model="taskInRequestEntity.shelfLife" clearable readonly
                                    style="height: 50px;" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="22" :offset="1" v-show="disPlayDateFlag">
                            <el-form-item label="生产日期" prop="productionDate">
                                <el-date-picker v-model="taskInRequestEntity.productionDate" ref="productionDate"
                                    :disabled="disabledEmpty" style="width: 100%;height: 75px;" type="date"
                                    placeholder="请选择生产日期" :editable="false" size="large" clearable />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="4" class="btn-area">
                        <el-col :span="8" :offset="0">
                            <el-form-item>
                                <el-button type="primary" round
                                    @click="addTempTasks(taskInRequestRef, taskInRequestEntity)">绑定信息</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item>
                                <el-button type="success" round
                                    @click="submitGoodsInTask(taskInRequestRef)">下发任务</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item>
                                <el-button type="warning" round @click="callEmptyVehicles(taskInRequestEntity)">请求空箱</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </fieldset>
            <div ref="btnArea"></div>
        </el-container>
    </el-config-provider>
</template>

<script setup>
import store from '@/store'
import { sendGoodsInTask, callEmptyVehicle } from '@/api/task'
import { queryPartInfoByPartNo } from '@/api/goods'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import UploadExcelPart from '@/excel/UploadExcelPart.vue'
const taskInRequestRef = ref()
const taskInRequestEntity = reactive({
    vehicleNo: '',// 载具号
    goodsId: '',// 零件号
    goodsName: '',// 零件名称
    goodsNum: 1,// 零件数量
    weight: 0,// 零件重量
    shelfLife: 0,// 有效期
    productionDate: '',// 生产日期
    isEmpty: '1',
    userName: store.getters.getUserName// 用户名
})
const rules = reactive({
    vehicleNo: [
        { required: true, message: '请输入载具编号' }
    ],
    goodsId: [
        { required: true, message: '请输入物料编号' }
    ],
    goodsNum: [
        { required: true, message: '请输入数量' },
        { type: 'number', message: '请输入数字' }
    ]
})
const labelPosition = "top"
</script>

<script>
export default {
    name: 'goodsInPda',
    components: {
        UploadExcelPart
    },
    data() {
        return {
            tempTasks: [],
            tempVehicleNo: '',
            disPlayDateFlag: false,
            currentPartInfo: {},
            stockTypeOptions: [
                {
                    value: '0',
                    label: '空箱入库'
                },
                {
                    value: '1',
                    label: '带料入库'
                }
            ],
            disabledEmpty: false,
            totalWeight: 0,
            isOverWeight: false
        }
    },
    mounted() {
        this.$refs.vehicleNo.focus()
    },
    methods: {
        // 查询
        queryAndAutoComplete(taskInRequestEntity) {
            this.loading = true
            const param = { material: taskInRequestEntity.goodsId }
            queryPartInfoByPartNo(param).then(res => {
                console.log(res)
                if (res.data.code == 0) {
                    this.currentPartInfo = res.data.returnData
                    taskInRequestEntity.goodsName = this.currentPartInfo.itemDesc
                    this.calWeight(taskInRequestEntity)
                    if (this.currentPartInfo.SLED > 0) {// 判断生产日期区域显示与否
                        this.disPlayDateFlag = true
                        taskInRequestEntity.shelfLife = this.currentPartInfo.SLED
                    } else {
                        this.disPlayDateFlag = false
                        taskInRequestEntity.shelfLife = 0
                        taskInRequestEntity.productionDate = ''
                    }
                    taskInRequestEntity.goodsNum = ''
                    this.$refs.goodsNum.focus()
                } else {
                    taskInRequestEntity.goodsName = ''
                    taskInRequestEntity.goodsNum = ''
                    taskInRequestEntity.weight = 0
                    this.disPlayDateFlag = false
                    taskInRequestEntity.shelfLife = 0
                    taskInRequestEntity.productionDate = ''
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                taskInRequestEntity.goodsName = ''
                taskInRequestEntity.goodsNum = ''
                taskInRequestEntity.weight = 0
                this.disPlayDateFlag = false
                taskInRequestEntity.shelfLife = 0
                taskInRequestEntity.productionDate = ''
                ElMessage.error('查询物料信息错误' + err.message)
            })
            this.loading = false
        },
        calWeight(taskInRequestEntity) {
            taskInRequestEntity.weight = this.currentPartInfo.partWeight * taskInRequestEntity.goodsNum
            this.totalWeight = this.totalWeight + taskInRequestEntity.weight
            if (this.totalWeight > 30) {
                this.isOverWeight = true
                ElMessage({
                    message: '箱子重量超过30kg',
                    type: 'warning',
                })
            }
        },
        autoCompleteEmptyInfo(taskInRequestEntity) {// 自动补全空托入库信息
            if (taskInRequestEntity.isEmpty == '0') {// 空托入库
                taskInRequestEntity.goodsId = '000000000'
                taskInRequestEntity.batchNo = '000000000'
                taskInRequestEntity.goodsNum = 1
                taskInRequestEntity.weight = 0
                taskInRequestEntity.shelfLife = 0
                taskInRequestEntity.productionDate = ''
                this.currentPartInfo = {}
                this.disabledEmpty = true
                this.disPlayDateFlag = false
            } else {
                taskInRequestEntity.goodsId = ''
                taskInRequestEntity.batchNo = ''
                taskInRequestEntity.goodsNum = 1
                this.disabledEmpty = false
            }
            this.$refs.vehicleNo.focus()

        },
        addTempTasks(formEl, formData) {// 生成入库任务
            if (!formEl) return
            formEl.validate((valid) => {
                if (!valid) {
                    ElMessage({
                        message: '请输入必须的入库信息！',
                        type: 'warning',
                    })
                    return
                }
                if (this.tempVehicleNo !== '' && this.tempVehicleNo !== formData.vehicleNo) {
                    ElMessage({
                        message: '载具编号不一致，请确认后重新输入！',
                        type: 'warning',
                    })
                    return
                }
                if (this.tempVehicleNo != '' && formData.isEmpty == '0') {
                    ElMessage({
                        message: '此箱子已经绑定过，请下发。',
                        type: 'warning',
                    })
                    return
                }
                if (this.isOverWeight) {
                    ElMessage({
                        message: '已超重，不可继续绑定，请移除此物料，并下发任务',
                        type: 'error',
                        showClose: true
                    })
                    return
                }
                this.tempVehicleNo = formData.vehicleNo
                formData.userName = store.getters.getUserName
                this.tempTasks.push(Object.assign({}, formData))
                formEl.resetFields()
                this.disabledEmpty = false
                formData.vehicleNo = this.tempVehicleNo
                this.currentPartInfo = {}
                ElMessage({
                    message: '绑定成功！',
                    type: 'success',
                })
            })
        },
        submitGoodsInTask(formEl) {// 下发入库任务
            if (this.tempTasks.length == 0) {
                ElMessage.error("未绑定任何信息")
            }
            sendGoodsInTask(this.tempTasks).then(res => {
                console.log(res)
                if (res.data.code == 0) {
                    this.tempTasks = []
                    this.tempVehicleNo = ''
                    formEl.resetFields()
                    this.disPlayDateFlag = false
                    this.disabledEmpty = false
                    this.currentPartInfo = {}
                    this.totalWeight = 0
                    this.isOverWeight = false
                    ElMessage({
                        message: '下发任务成功',
                        type: 'success'
                    })
                    this.$refs.vehicleNo.focus()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                console.log(err)
                ElMessage.error('创建入库任务错误！' + err.message)
            })
        },
        resetForm(formEl) {// 重置表单信息
            if (!formEl) return
            formEl.resetFields()
            this.tempTasks = []
            this.tempVehicleNo = ''
            this.currentPartInfo = {}
            this.disPlayDateFlag = false
        },
        callEmptyVehicles(taskInRequestEntity) {
            callEmptyVehicle(taskInRequestEntity).then(res => {
                if (res.data.code == 0) {
                    ElMessage({
                        message: res.data.message,
                        type: 'success'
                    })
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                console.log(err)
                ElMessage.error('呼叫空箱错误' + err.message)
            })
        },
        deleteRowTask(row) {// 删除当前行的入库任务
            this.tempTasks.splice(this.tempTasks.indexOf(row), 1)
            if (this.tempTasks.length == 0) {
                this.tempVehicleNo = ''
            }
        },
        detectEndInputVehicleNo(taskInRequestEntity) {
            if (taskInRequestEntity.isEmpty == '0') {// 空托入库
                this.$refs.btnArea.scrollIntoView();
                // this.$refs.vehicleNo.blur()
            } else {
                this.$refs.goodsId.focus()
            }
        },
        detectEndInputNum(taskInRequestEntity) {
            this.calWeight(taskInRequestEntity)
        }
    }
}
</script>

<style scoped>
.content {
    display: flex;
    width: 100%;
}

.el-row {
    margin: 20px 0;
}

:deep(.el-form-item__label) {
    font-size: x-large;
    /* margin: 10px; */
    padding: 10px 0;
}

:deep(.el-select-v2--large .el-select-v2__placeholder) {
    font-size: 25px;
}

.el-row .el-form-item {
    justify-content: center;
}

.el-row .el-form-item .el-select {
    width: 100% !important;
}

.el-row .el-form-item .el-select-v2 {
    width: 100% !important;
}

.el-row .el-form-item .el-input-number {
    width: 100% !important;
}

.el-row .el-form-item .el-button {
    width: 80px;
    height: 50px;
    margin: auto;
    font-size: small;
}

.input-area {
    margin: 10px;
    width: 1080px;
    /* height: 730px; */
    border: solid 1px;
    border-radius: 10px;
    box-shadow: 0px 15px 10px -15px #000;
}
</style>