<template>
    <el-config-provider :locale="zhCn">
        <el-container class="content">
            <div class="work-area">
                <fieldset class="title-area">
                    <legend>
                        工作台
                    </legend>
                    <div class="title-div">
                        <span class="title-text">非计划领料</span>
                    </div>
                    <div class="station-div">
                        <span class="station-text">工作站：</span>
                    </div>
                    <div class="station-no-div">
                        <span class="station-no-text">{{ standId }}</span>
                    </div>
                </fieldset>
                <fieldset class="main-area">
                    <legend>
                        领料下发
                    </legend>
                    <el-form ref="workFormRef" :model="workFormEntity" :label-position="labelPosition"
                        label-width="150px" style="max-width: 100%" :rules="rules" status-icon>
                        <div style="display: flex;">
                            <div style="display: block; margin: 5px;">
                                <div style="display: flex;">
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">料号</span>
                                        </div>
                                        <div class="display-form-div">
                                            <el-input v-model="workFormEntity.goodsId" size="default" ref="needGoodsId"
                                                @blur="getStockNumOfGoodsIdMethod()" clearable></el-input>
                                        </div>
                                    </div>
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">当前库存数量</span>
                                        </div>
                                        <div style="display: flex; margin-top: 20px;">
                                            <div class="display-form-div-left">
                                                <el-input-number v-model.number="workFormEntity.stockNum"
                                                    controls-position="right" :min="0" disabled />
                                            </div>
                                            <div class="display-form-div-right">
                                                <span class="display-form-text-right">PC</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">需要数量</span>
                                        </div>
                                        <div style="display: flex; margin-top: 20px;">
                                            <div class="display-form-div-left">
                                                <el-input-number v-model.number="workFormEntity.needNum"
                                                    controls-position="right" :min="0" />
                                            </div>
                                            <div class="display-form-div-right">
                                                <span class="display-form-text-right">PC</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div style="display: flex; margin-top: 15px">
                                    <div
                                        style="display: inline-flex; justify-content: center; width: 80px; height: 149px; margin: 5px; padding: 5px; background-color: #CCCCCC;">
                                        <span
                                            style="align-self: center; font-weight: bold;font-size: 25px;">领料<br />原因</span>
                                    </div>
                                    <div>
                                        <div style="display: flex">
                                            <div
                                                style="display: inline-flex; justify-content: center; width: 127px; margin: 5px; padding: 5px; background-color: #00AAE8;">
                                                <span style="align-self: center; font-weight: bold;font-size: 25px;">
                                                    工单#
                                                </span>
                                            </div>
                                            <div
                                                style="display: inline-flex; justify-content: center; width: 410px; margin: 5px; padding: 5px; background-color: #00AAE8;">
                                                <el-input v-model="workFormEntity.workOrder" size="default"
                                                    clearable></el-input>
                                            </div>
                                        </div>
                                        <div style="display: flex; margin-top: 5px">
                                            <div
                                                style="display: inline-flex; justify-content: center; width: 127px; margin: 5px; padding: 5px; background-color: #00AAE8;">
                                                <span style="align-self: center; font-weight: bold;font-size: 25px;">
                                                    小车号
                                                </span>
                                            </div>
                                            <div
                                                style="display: inline-flex; justify-content: center; width: 410px; margin: 5px; padding: 5px; background-color: #00AAE8;">
                                                <el-input v-model="workFormEntity.smallVehicleNo" size="default"
                                                    clearable></el-input>
                                            </div>
                                        </div>
                                        <div style="display: flex; margin-top: 5px">
                                            <div
                                                style="display: inline-flex; justify-content: center; width: 127px; margin: 5px; padding: 5px; background-color: #00AAE8;">
                                                <span style="align-self: center; font-weight: bold;font-size: 25px;">
                                                    描述
                                                </span>
                                            </div>
                                            <div
                                                style="display: inline-flex; justify-content: center; width: 410px; margin: 5px; padding: 5px; background-color: #00AAE8;">
                                                <el-input v-model="workFormEntity.callReason" size="default"
                                                    clearable></el-input>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div style="margin: 10px;">
                                <div class="arrow" @click="createTask()" style="height: 454px;">
                                    <span
                                        style="margin-left: 25px; align-self: center; font-weight: bold;font-size: 45px; writing-mode: vertical-lr;">请求确认</span>
                                </div>
                            </div>
                        </div>
                    </el-form>
                </fieldset>
                <fieldset class="main-area">
                    <legend>
                        领料确认
                    </legend>
                    <el-form ref="confirmFormRef" :model="confirmFormEntity" :label-position="labelPosition"
                        label-width="150px" style="max-width: 100%" :rules="rules" status-icon>
                        <div style="display: flex;">
                            <div style="display: block; margin: 5px;">
                                <div style="display: flex;">
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">箱号</span>
                                        </div>
                                        <div class="display-form-div">
                                            <el-input v-model="confirmFormEntity.vehicleId" size="default"
                                                ref="confirmVehicleId" v-on:keyup.enter="getCurrentGoodsPickNumMethod()"
                                                clearable></el-input>
                                        </div>
                                    </div>
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">料号</span>
                                        </div>
                                        <div class="display-form-div">
                                            <el-input v-model="confirmFormEntity.goodsId" size="default"
                                                ref="confirmGoodsId" v-on:keyup.enter="getCurrentGoodsPickNumMethod()"
                                                clearable></el-input>
                                        </div>
                                    </div>
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">领料数量</span>
                                        </div>
                                        <div style="display: flex; margin-top: 20px;">
                                            <div class="display-form-div-left">
                                                <el-input-number v-model.number="confirmFormEntity.realPickNum"
                                                    ref="confirmRealPickNum" v-on:keyup.enter="getCurrentGoodsPickNumMethod()" controls-position="right" :min="0" />
                                            </div>
                                            <div class="display-form-div-right">
                                                <span class="display-form-text-right">PC</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div style="display: flex; flex-direction: column;">
                                        <div class="display-title-div">
                                            <span class="display-title-text">剩余数量</span>
                                        </div>
                                        <div style="display: flex; margin-top: 20px;">
                                            <div class="display-form-div-left">
                                                <el-input-number v-model.number="confirmFormEntity.remainNum" controls-position="right" :min="0" />
                                            </div>
                                            <div class="display-form-div-right">
                                                <span class="display-form-text-right">PC</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div style="margin: 10px;">
                                <div class="arrow" @click="confirmPick()" style="height: 270px;">
                                    <span
                                        style="margin-left: 25px; align-self: center; font-weight: bold;font-size: 45px; writing-mode: vertical-lr;">回库确认</span>
                                </div>
                            </div>
                        </div>
                    </el-form>
                </fieldset>
            </div>
        </el-container>
    </el-config-provider>
</template>

<script setup>
import store from '@/store'
import { clcNoPlan, clcNoPlanConfirmBack } from '@/api/task'
import { replaceEnglishAndNumberIGAI } from '@/utils/formatter'
import { errorBox, successBox } from '@/utils/myMessageBox.js'
import { getStockNumByGoodsId } from '@/api/stock'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
</script>

<script>
export default {
    name: 'clcNoPlan',
    data() {
        return {
            standId: store.getters.getStandId,
            labelPosition: 'top',
            workFormRef: ref(),
            workFormEntity: reactive({
                goodsId: '',
                stockNum: null,
                needNum: null,
                workOrder: '',
                smallVehicleNo: '',
                callReason: ''
            }),
            confirmFormRef: ref(),
            confirmFormEntity: reactive({
                vehicleId: '',
                goodsId: '',
                realPickNum: null,
                remainNum: null
            }),
            rules: reactive({
                goodsId: [
                    { required: true, message: '请输入料号' }
                ]
            }),
        }
    },
    mounted() {
    },
    beforeUnmount() {
    },
    methods: {
        // 查询库存余量
        getStockNumOfGoodsIdMethod() {
            const request = {
                goodsId: this.workFormEntity.goodsId,
                userName: store.getters.getUserName
            }
            getStockNumByGoodsId(request).then(res => {
                const response = res.data
                if (response.code == 0) {
                    this.workFormEntity.stockNum = response.returnData.remainNum
                } else {
                    this.workFormEntity.stockNum = 0
                    errorBox(response.message)
                }
            }).catch(err => {
                console.log(err)
                this.workFormEntity.stockNum = null
                errorBox('查询库存余量发生错误。')
            })
        },
        getCurrentGoodsPickNumMethod() {
            this.confirmFormEntity.vehicleId = replaceEnglishAndNumberIGAI(this.confirmFormEntity.vehicleId)
            if (this.confirmFormEntity.vehicleId == '' || this.confirmFormEntity.vehicleId == null|undefined) {
                this.$refs.confirmVehicleId.focus()
                return
            }
            if (this.confirmFormEntity.goodsId == '' || this.confirmFormEntity.goodsId == null|undefined) {
                this.$refs.confirmGoodsId.focus()
                return
            }
            if (this.confirmFormEntity.realPickNum == null | undefined) {
                this.$refs.confirmRealPickNum.focus()
                return
            }
        },
        // 重置参数
        resetForms() {
            this.workFormEntity = reactive({
                goodsId: '',
                stockNum: null,
                needNum: null,
                workOrder: '',
                smallVehicleNo: '',
                callReason: ''
            })
            this.$refs.needGoodsId.focus()
        },
        resetConfirmForms() {
            this.confirmFormEntity = reactive({
                vehicleId: '',
                goodsId: '',
                realPickNum: null,
                remainNum: null
            })
        },
        // 确认完成工作
        createTask() {
            if (this.standId == '' || this.workFormEntity.goodsId == '') {
                errorBox('站台号和料号不可缺少')
                return
            }
            const requestParams = {
                goodsId: this.workFormEntity.goodsId,
                stockNum: this.workFormEntity.stockNum,
                needNum: this.workFormEntity.needNum,
                workOrder: this.workFormEntity.workOrder,
                smallVehicleNo: this.workFormEntity.smallVehicleNo,
                callReason: this.workFormEntity.callReason,
                standId: this.standId,
                userName: store.getters.getUserName
            }
            clcNoPlan(requestParams).then(res => {
                const response = res.data
                if (response.code == 0) {
                    successBox(response.message)
                    this.resetForms()
                } else {
                    errorBox(response.message)
                }
            }).catch(err => {
                console.log(err)
                errorBox('请求时发生异常。')
            })
        },
        // 确认回库
        confirmPick() {
            if (this.standId == '' || this.confirmFormEntity.goodsId == '' || this.confirmFormEntity.vehicleId == '' || this.confirmFormEntity.realPickNum == null) {
                errorBox('站台号、箱号、料号、领料数量不可缺少。')
                return
            }
            const confirmParams = {
                standId: this.standId,
                goodsId: this.confirmFormEntity.goodsId,
                vehicleId: this.confirmFormEntity.vehicleId,
                realPickNum: this.confirmFormEntity.realPickNum,
                remainNum: this.confirmFormEntity.remainNum,
                userName: store.getters.getUserName
            }
            clcNoPlanConfirmBack(confirmParams).then(res => {
                const response = res.data
                if (response.code == 0) {
                    ElMessage.success(response.message)
                    this.resetConfirmForms()
                } else {
                    errorBox(response.message)
                }
            }).catch(err => {
                console.log(err)
                errorBox('确认时发生异常。')
            })
        },
    }
}
</script>

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

.work-area {
    width: 100%;
    /* padding: 5px; */
}

.main-area {
    margin: auto;
    min-height: fit-content;
    max-height: 90%;
    margin-bottom: 10px;
    min-width: inherit;
    border: solid 1px;
    border-radius: 10px;
    box-shadow: 0px 15px 10px -15px #000;
    overflow: auto;
}

.title-area {
    display: flex;
    /* min-height: 10%; */
    max-height: max-content;
    margin-bottom: 10px;
    min-width: inherit;
    border: solid 1px;
    border-radius: 10px;
    box-shadow: 0px 15px 10px -15px #000;
    overflow: auto;
}

.arrow {
    background: linear-gradient(-105deg,
            transparent 50%,
            #309330 50%,
            #309330 100%) top right,
        linear-gradient(-75deg,
            transparent 50%,
            #309330 50%,
            #309330 100%) bottom right;
    background-size: 100% 50%;
    /* height: 400px; */
    width: 250px;
    background-repeat: no-repeat;
    display: inline-flex;
    cursor: pointer;
}

.display-form-div :deep(.el-input) {
    width: 195px;
    height: 130px;
    font-size: 25px;
}

.display-form-div-left :deep(.el-input-number) {
    width: 125px;
    height: 130px;
    font-size: 25px;
}

/* :deep(.el-input .el-input__inner) {
    font-size: 60px;
    height: 130px;
}

:deep(.el-input-number .el-input__inner) {
    font-size: 60px;
    height: 130px;
} */

.title-div {
    display: inline-flex;
    width: -webkit-fill-available;
    margin-right: 5px;
    padding: 5px;
    background-color: #CCCCCC;
}

.title-text {
    align-self: center;
    font-weight: bold;
    font-size: 25px;
}

.station-div {
    display: inline-flex;
    justify-content: center;
    width: 150px;
    margin-left: 5px;
    margin-right: 5px;
    padding: 5px;
    background-color: #FFFAAA;
}

.station-text {
    align-self: center;
    font-weight: bold;
    font-size: 25px;
}

.station-no-div {
    display: inline-flex;
    justify-content: center;
    width: 200px;
    padding: 5px;
    border: 5px double #000000;
}

.station-no-text {
    align-self: center;
    font-weight: bold;
    font-size: 25px;
}

.display-title-div {
    display: inline-flex;
    justify-content: center;
    width: 205px;
    height: 80px;
    margin: 5px;
    padding: 5px;
    background-color: #FFFAAA;
}

.display-title-text {
    align-self: center;
    font-weight: bold;
    font-size: 25px;
}

.display-form-div {
    display: inline-flex;
    justify-content: center;
    width: 195px;
    margin: 5px;
    padding: 5px;
    border: 5px double #000000;
    margin-top: 25px;
}

.display-form-div-left {
    display: inline-flex;
    justify-content: center;
    width: 125px;
    margin: 5px;
    padding: 5px;
    border: 5px double #000000;
}

.display-form-div-right {
    display: inline-flex;
    justify-content: center;
    width: 50px;
    margin: 5px;
    padding: 5px;
    background-color: #CCCCCC;
}

.display-form-text-right {
    align-self: center;
    font-weight: bold;
    font-size: 25px;
}
</style>