@extends('layouts.app')
@section('title')缓存架入库-入库管理@endsection

@section('content')
    <span id="nav2">
        @component('store.menu')@endcomponent
        @component('store.inStorage.menu')@endcomponent
    </span>
    <div class="container-fluid" id="container">
        <div class="row">
            <div class="card offset-md-3 col-md-6 col-sm-12">
                <div class="card-body">
                    <div class="form-group text-center font-weight-bold h4">
                        入库信息
                    </div>
                    <div class="form-group row">
                        <label for="asn" class="col-sm-2 col-3 text-right">ASN号:</label>
                        <input type="text" class="form-control col-8" :class="errors.asn ? 'is-invalid' : ''" id="asn" v-model="info.asn" @blur="checkAsn()" placeholder="只需填写后几位，自动补充">
                        <span class="invalid-feedback offset-2" role="alert" v-if="errors.asn">
                            <strong>@{{ errors.asn[0] }}</strong>
                        </span>
                    </div>
                    <div class="form-group row">
                        <label for="ide" class="col-sm-2 col-3 text-right">料箱号:</label>
                        <input type="text" class="form-control col-8" :class="errors.ide ? 'is-invalid' : ''" id="ide" v-model="info.ide" @blur="checkIde()" placeholder="只需填写后几位，自动补充">
                        <span class="invalid-feedback offset-2" role="alert" v-if="errors.ide">
                            <strong>@{{ errors.ide[0] }}</strong>
                        </span>
                    </div>
                    <div class="input-group row">
                        <label for="barCode" class="col-sm-2 col-3 text-right">条码:</label>
                        <input type="text" class="form-control rounded col-sm-5 col-8 ml-sm-1 ml-2" :class="errors.barCode ? 'is-invalid' : ''" id="barCode" @blur="codeBlur()" @keydown.enter="checkInfo()" v-model="info.barCode">
                        <div class="input-group-append mt-sm-0 mt-4">
                            <span class="input-group-text d-none d-sm-block">@数量</span>
                            <label class="d-sm-none col-4 text-right">数量：</label>
                            <span class="input-group-text p-0 border-0">
                                <input class="form-control" type="number" :class="errors.amount ? 'is-invalid' : ''" step="1" min="1" id="amount" v-model="info.amount">
                            </span>
                        </div>
                        <span class="invalid-feedback offset-2" role="alert" v-if="errors.barCode">
                            <strong>@{{ errors.barCode[0] }}</strong>
                        </span>
                    </div>
                    <div class="input-group row mt-5">
                        <button type="submit" class="btn btn-success offset-2 col-9" @click="checkInfo()">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
@stop

@section('lastScript')
    <script type="text/javascript">
        new Vue({
            el:"#container",
            data:{
                permissionList:[ //允许聚焦许可列表
                    "asn","ide","amount"
                ],
                info:{},
                mount:false,
                before:{
                    asn:"",
                    ide:"",
                },
                errors:{},
            },
            mounted(){
                this.codeFocus();
                this.globalClick();
                this.createBefore();
            },
            methods:{
                //聚焦 白名单
                codeFocus(){
                    if (!this.permissionList.includes(document.activeElement.id)) document.getElementById("barCode").focus();
                },
                //全局点击聚焦
                globalClick(turn = true){
                    if (turn===this.mount)return;
                    this.mount = turn;//防止重复挂载事件
                    if (turn) window.addEventListener("click",this.codeFocus);
                    else window.removeEventListener("click",this.codeFocus);
                },
                codeBlur(){
                    if (this.info.asn && this.info.ide && !this.info.amount){
                        this.globalClick(false);
                        window.tempTip.inputVal('请输入数量：',(amount)=>{
                            this.info.amount = amount;
                            if (this.info.asn && this.info.ide && this.info.barCode && this.info.amount)this.checkInfo();
                            this.globalClick();
                        });
                    }
                },
                checkInfo(){
                    let error = {};
                    if (!this.info.asn)error.asn = ["ASN号必填"];
                    if (this.info.asn && this.info.asn.length!==13)error.asn = ["非法ASN号"];
                    if (!this.info.ide)error.ide = ["料箱号必填"];
                    if (this.info.ide && this.info.ide.length!==10)error.ide = ["非法料箱号"];
                    if (!this.info.barCode)error.barCode = ["商品条码必填"];
                    if (!this.info.amount)error.amount = ["数量必填"];
                    if (JSON.stringify(error)!=='{}'){this.errors = error;return;}
                    this._exeTask();
                },
                _exeTask(){
                    console.log("ok");
                    //入库成功
                    this.info = {};
                    this.errors = {};
                },
                createBefore(){
                    let now = new Date();
                    let yy = now.getFullYear().toString().substr(2, 2);
                    let mm = now.getMonth() + 1;
                    mm = mm <10 ? '0'+mm : mm.toString();
                    let dd = now.getDate();
                    dd = dd <10 ? '0'+dd : dd.toString();
                    this.before.asn = 'ASN'+yy+mm+dd+'000';
                    this.before.ide = 'IDE000000';
                },
                checkAsn(){
                    if(!this.info.asn)return;
                    let len = this.info.asn.length;
                    if (len<13)this.info.asn = this.before.asn.substr(0,13-len)+this.info.asn;
                    if (!this.info.ide)document.getElementById("ide").focus();
                },
                checkIde(){
                    if(!this.info.ide)return;
                    let len = this.info.ide.length;
                    if (len<10)this.info.ide = this.before.ide.substr(0,10-len)+this.info.ide;
                }
            },
        });
    </script>
@stop