@extends('layouts.app')
@section('title')任务-盘收一体@endsection

@section('content')
<span id="nav2">
    @component('store.menu')@endcomponent
    @component('store.checkingReceive.menu')
        @can('入库管理-盘收一体-盘收')
        <li class="nav-item">
            <a class="nav-link" href="{{URL::current()}}" :class="{active:isActive('mission',3)}">盘收</a>
        </li>@endcan
    @endcomponent
</span>
    <div id="container" class="d-none container-fluid">
        @include('store.checkingReceive._modal')

        @can('入库管理-盘收一体-盘收-编辑')
        <div class="row m-2 card" v-if="is_show">
            <audio src="{{asset('sound/warning_otherBarcode.mp3')}}" controls="controls" preload id="soundWarning" hidden>
            </audio>
            <audio src="{{asset('sound/ding.mp3')}}" controls="controls" preload id="soundDing" hidden>
            </audio>
            <div class="card-body col-12">
                <div class="col-12">
                    <p class="text-muted small" v-if="inputMode=='regular'">常规：可输入效期，相同条码记录不会合并</p>
                    <p class="text-muted small" v-if="inputMode=='increasing'">逐一扫描：处理单一重复商品，每扫一次对应隔口总数量自动递增，扫到不同条码会提示</p>
                    <p class="text-muted small" v-if="inputMode=='multiIncreasing'">边扫边分：处理多种商品，自动将扫到的不同条码数量递增到各自隔口号</p>
                    <ul class="nav nav-tabs mb-4 mt-n3">
                        <li class="nav-item"><a style="cursor: pointer" class="nav-link text-primary" :class="inputMode=='regular'?'active':''"
                            @click="switchMenu('regular')">常规</a></li>
                        <li class="nav-item"><a style="cursor: pointer" class="nav-link text-primary" :class="inputMode=='increasing'?'active':''"
                            @click="switchMenu('increasing')">逐一扫描</a></li>
                        <li class="nav-item"><a style="cursor: pointer" class="nav-link text-primary" :class="inputMode=='multiIncreasing'?'active':''"
                            @click="switchMenu('multiIncreasing')">边扫边分</a></li>
                    </ul>
                </div>
                <div class="col-12 row">
                    <div class="col-6">
                        <div v-if="inputMode=='regular'">
                            <div class="btn btn-sm btn-outline-primary" v-if="status.barcodeDisable" @click="status.barcodeDisable=false">手动输入</div>
                            <div class="btn btn-sm btn-outline-danger" v-if="!status.barcodeDisable" @click="status.barcodeDisable=true">扫描输入</div>
                        </div>
                        <input type="text" id="barcode" class="form-control" @blur="showCommitButton()"
                               :placeholder="status.barcodeDisable ? '扫入条码' : '输入条码'" :disabled="status.barcodeDisable" v-model="inputting.barcode" :class="inputMode=='regular' ? '' : 'mt-4'"
                               @focusin="focusOutDocument" @focusout="focusDocument">

                        <div v-if="inputMode=='regular'">
                            <div class="card-title">生产日期：</div>
                            <input type="date" class="form-control mb-2"
                                   v-model="inputting.produce_date" @focusin="focusOutDocument" @focusout="focusDocument" @keyup="oninputEnter">

                            <div class="card-title">失效日期：</div>
                            <input type="date" class="form-control mb-2"
                                   v-model="inputting.valid_date" @focusin="focusOutDocument" @focusout="focusDocument" @keyup="oninputEnter">
                            <div class="card-title">唯一码：</div>
                            <input type="text" class="form-control mb-2"
                                   v-model="inputting.unique_code" @focusin="focusOutDocument" @focusout="focusDocument" @keyup="oninputEnter">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="card-title" id="amountLabel">输入数量：</div>
                        <div class="input-group mt-n2 mb-2">
                            <input type="number" id="amount" style='height: 40px;font-size: 1.6em;color:blue;font-weight: bolder;padding: 3px;text-align: center' class="form-control" placeholder="" @blur="showCommitButton()"
                                   :disabled="status.amountDisable" v-model="inputting.amount" @focusin="focusOutDocument" @focusout="focusDocument" @keyup="oninputEnter">
                        </div>

                        <div class="card-title">格口号：</div>
                        <input type="number" id="bin_number" class="form-control mt-n2 mb-2" style='height: 100px;font-size: 5em;color:red;font-weight: bolder;padding: 3px;text-align: center'
                               v-model="inputting.bin_number" :disabled="status.binDisable"  @focusin="focusOutDocument" @focusout="focusDocument" @keyup="oninputEnter">

                        <div v-if="inputMode=='regular'" class="mt-4">
                            <div class="card-title">批次号：</div>
                            <input type="text" class="form-control mb-2"
                                   v-model="inputting.batch_number" @focusin="focusOutDocument" @focusout="focusDocument" @keyup="oninputEnter">
                        </div>
                    </div>
                    <div class="col-12" v-if="status.commitButtonVisible && inputMode=='regular'" >
                        <button class="btn btn-success btn form-control" @click="commitGoods">确定</button>
                    </div>
                </div>
            </div>
        </div>
        @endcan
        <div class="ml-2 mt-2">
            <button type="button" class="btn btn-sm btn-outline-dark" @click="exportItem()">导出</button>
            @can('入库管理-盘收一体-盘收-编辑')<button type="button" v-if="storeCheckingReceive.status != '已收货'" class="btn btn-sm btn-outline-danger" @click="isResetAmount()">重新清点</button>@endcan
            @can('入库管理-盘收一体-盘收-编辑')<button type="button" v-if="storeCheckingReceive.status != '已收货'" class="btn btn-sm btn-outline-success" data-toggle="modal" data-target="#asnWindow">匹配ASN单据</button>@endcan
        </div>
        <div class="row text-primary ml-1 mr-2 mt-1 mb-1 w-100" style="background-color: #c3e3b5">
            <span class="ml-md-1">任务ID： <b class="text-dark">@{{ storeCheckingReceive.id }}</b></span>
            <span class="ml-md-3"> &nbsp;货主： <b class="text-dark">@{{ storeCheckingReceive.owner_name }}</b></span>
            <span class="ml-md-3">创建时间： <b class="text-dark">@{{ storeCheckingReceive.created_at }}</b></span>
            <span class="ml-md-3">ASN号： <b class="text-dark">@{{ storeCheckingReceive.asn }}</b></span>
        </div>
        <!-- pc >544 -->
        <div class="d-none d-sm-block">
            <table class="table table-striped table-sm text-nowrap table-hover mt-1 td-min-width-80" id="table">
                <tr v-for="(storeCheckingReceiveItem,i) in storeCheckingReceiveItems" @click="selectedColor(i,$event)">
                    <td><span>@{{ i+1 }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.bin_number }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.commodity_name }}</span></td>
                    <td>
                    <span v-for="commodity_barcode in storeCheckingReceiveItem.commodity_barcodes">
                        <small>@{{ commodity_barcode.code }}</small><br>
                    </span>
                    </td>
                    <td><span>@{{ storeCheckingReceiveItem.imported_amount }}</span></td>
                    <td @click="showInput(storeCheckingReceiveItem.id)" v-if="is_show">
                        <div class="form-inline">
                            <input @focus="showInput(storeCheckingReceiveItem.id)"  @blur="delFocus()" :id = "'counted_amount_'+storeCheckingReceiveItem.id" :value="storeCheckingReceiveItem.counted_amount"
                                    class="form-control form-control-sm" type="number" :disabled="disabledItemId == storeCheckingReceiveItem.id ? false : true">
                            <button v-if="disabledItemId == storeCheckingReceiveItem.id" type="button" class="btn btn-sm btn-success ml-1"
                                @click="updateCountedAmount(storeCheckingReceiveItem)">确定</button>
                            <button v-if="disabledItemId == storeCheckingReceiveItem.id" type="button" class="btn btn-sm btn-danger"
                                @click.stop="disabledItemId = ''">取消</button>
                        </div>
                    </td>
                    <td v-if="!is_show"><span>@{{ storeCheckingReceiveItem.counted_amount }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.asn_amount }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.imported_diff_amount }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.asn_diff_amount }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.produced_at }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.invalid_at }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.batch_code }}</span></td>
                    <td><span>@{{ storeCheckingReceiveItem.unique_code }}</span></td>
                    <td></td>
                </tr>
            </table>
        </div>
        <!-- phone <544 -->
        <div class="d-xl-none">
            <div v-for="(storeCheckingReceiveItem,i) in storeCheckingReceiveItems" class="mt-1 border border-1 rounded"
                @click="openAll(storeCheckingReceiveItem.id)">
                <div class="row">
                    <div class="col-6">
                        <label class="text-muted">序号：</label>@{{ i+1 }}
                    </div>
                    <div class="col-6">
                        <span class="pull-right mr-1 text-primary font-weight-bold small">点击展开</span>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 row">
                        <label class="text-primary col-3 mr-0">条码：</label>
                        <div class="col-9 ml-0">
                            <span v-for="commodity_barcode in storeCheckingReceiveItem.commodity_barcodes">
                                <small>@{{ commodity_barcode.code }}</small><br>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 form-inline">
                        <label class="text-muted w-25">名称：</label><div class="w-75">@{{ storeCheckingReceiveItem.commodity_name }}</div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-6">
                        <label class="text-muted">格口号：</label>@{{ storeCheckingReceiveItem.bin_number }}
                    </div>
                    <div class="col-6">
                        <label class="text-muted">实盘数：</label>@{{ storeCheckingReceiveItem.counted_amount }}
                    </div>
                </div>
                <div class="row">
                    <div class="col-6">
                        <label class="text-muted">导入差异：</label><small class="text-danger font-weight-bold">@{{ storeCheckingReceiveItem.imported_diff_amount }}</small>
                    </div>
                    <div class="col-6">
                        <label class="text-muted">ASN差异：</label><small class="text-danger font-weight-bold">@{{ storeCheckingReceiveItem.asn_amount }}</small>
                    </div>
                </div>
                <div :id="'detail_'+storeCheckingReceiveItem.id" class="up">
                    <div class="row">
                        <div class="col-6">
                            <label class="text-muted">导入数：</label>@{{ storeCheckingReceiveItem.imported_amount }}
                        </div>
                        <div class="col-6">
                            <label class="text-muted">ASN数：</label>@{{ storeCheckingReceiveItem.asn_amount }}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-6">
                            <label class="text-muted">生产日期：</label>@{{ storeCheckingReceiveItem.produced_at }}
                        </div>
                        <div class="col-6">
                            <label class="text-muted">有效日期：</label>@{{ storeCheckingReceiveItem.invalid_at }}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-6">
                            <label class="text-muted">唯一号码：</label>@{{ storeCheckingReceiveItem.unique_code }}
                        </div>
                        <div class="col-6">
                            <label class="text-muted">批次号码：</label>@{{ storeCheckingReceiveItem.batch_code }}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-10 offset-1">
                            @can('入库管理-盘收一体-盘收-编辑')<button @click.stop="destroyItem(storeCheckingReceiveItem,i)" class="btn btn-sm btn-outline-danger w-100">删除</button>@endcan
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@stop

@section('lastScript')
<script type="text/javascript" src="{{mix('js/queryForm/header.js')}}"></script>{{--新版2--}}
<script>
    new Vue({
        el:"#container",
        data:{
            storeCheckingReceive:{id:'{{$storeCheckingReceive->id}}',
                owner_id:'{{$storeCheckingReceive->owner_id}}',
                owner_name:'{{$storeCheckingReceive->owner ? $storeCheckingReceive->owner->name : ''}}',
                                created_at:'{{$storeCheckingReceive->created_at}}',asn:'{{$storeCheckingReceive->asn}}',status:'{{$storeCheckingReceive->status}}'},
            storeCheckingReceiveItems:[
                @foreach($storeCheckingReceive->storeCheckingReceiveItems as $storeCheckingReceiveItem)
                {id:'{{$storeCheckingReceiveItem->id}}',bin_number:'{{$storeCheckingReceiveItem->bin_number}}',
                commodity_name:"{{$storeCheckingReceiveItem->commodity ? $storeCheckingReceiveItem->commodity->name : ''}}",
                commodity_barcodes:{!! $storeCheckingReceiveItem->commodity ? ($storeCheckingReceiveItem->commodity->barcodes ? $storeCheckingReceiveItem->commodity->barcodes : json_encode([])) : json_encode([]) !!},
                imported_amount:'{{$storeCheckingReceiveItem->imported_amount}}',counted_amount:'{{$storeCheckingReceiveItem->counted_amount}}',
                asn_amount:'{{$storeCheckingReceiveItem->asn_amount}}',imported_diff_amount:'{{$storeCheckingReceiveItem->imported_diff_amount}}',
                asn_diff_amount:'{{$storeCheckingReceiveItem->asn_diff_amount}}',produced_at:'{{$storeCheckingReceiveItem->produced_at}}',
                invalid_at:'{{$storeCheckingReceiveItem->invalid_at}}',batch_code:'{{$storeCheckingReceiveItem->batch_code}}',
                unique_code:'{{$storeCheckingReceiveItem->unique_code}}'},
                @endforeach
            ],
            inputMode : 'regular',
            status:{
                barcodeDisable : true,
                commitButtonVisible:false,
                scanEndInputted:false,
                amountDisable : false,
                binDisable : false,
            },
            focusing : 'document',
            inputting:{
                barcode : '',
                amount : '',
                bin_number : '',
                batch_number : '',
                produce_date : '',
                valid_date : '',
                unique_code:'',
            },
            lastScannedBarcode : '',
            asn : '',
            is_show : {!! $is_show !!},
            disabledItemId : "",
            upId:"",
            selectTr:''
        },
        mounted(){
            $(".up").slideUp();
            $('#container').removeClass('d-none');
            $(".tooltipTarget").tooltip({'trigger':'hover'});
            let column = [
                {name:'index',value: '序号', neglect: true},
                {name:'bin_number',value: '格口号'},
                {name:'commodity_name',value: '商品名'},
                {name:'commodity_barcode',value: '商品条码'},
                {name:'imported_amount',value: '导入数量', neglect: true},
                {name:'counted_amount',value: '实盘数量', neglect: true},
                {name:'asn_amount',value: 'ASN数量', neglect: true},
                {name:'imported_diff_amount',value: '导入差异数', neglect: true},
                {name:'asn_diff_amount',value: 'ASN差异数', neglect: true},
                {name:'produced_at',value: '生产日期'},
                {name:'invalid_at',value: '有效期'},
                {name:'batch_code',value: '批次号'},
                {name:'unique_code',value: '唯一码'},
                {name:'operating',value: '操作', neglect: true},
            ];
            new Header({
                el: "table",
                name: "checkingReceive",
                column: column,
                data: this.storeCheckingReceiveItems,
            }).init();
            this.scanListening();
        },
        methods:{
            selectedColor(id,e){
                $('#headerParent tr[class=focusing]').removeClass('focusing')
                if (id==this.selectTr){
                    this.selectTr='';
                    return;
                }
                this.selectTr=id;
                $(e.target).parent('tr').addClass('focusing')
            },
            //显示该条全部信息
            openAll(id){
                if (this.upId == id) {$("#detail_"+id).slideUp(); this.upId = ''}
                else {
                    this.upId = id;
                    $("#detail_"+id).slideDown();
                }
            },
            //获取焦点，取消全局监听
            showInput(id){
                if (this.disabledItemId === id)return;
                this.focusing = "";
                document.getElementById('counted_amount_'+id).focus();
                this.disabledItemId = id;
            },
            //实盘数量输出框失焦
            delFocus(){
                this.focusDocument();
            },
            //执行修改
            updateCountedAmount(item){
                let counted_amount = document.getElementById('counted_amount_'+item.id).value;
                if(!counted_amount || parseInt(counted_amount) != counted_amount){
                    tempTip.setDuration(3000);
                    tempTip.show('请输入正整数！');
                    return;
                }
                axios.post('{{url('store/checkingReceive/updateCountedAmount')}}',{id : item.id,counted_amount : counted_amount})
                    .then((res)=>{
                        if (res.data.success){
                            item.counted_amount = counted_amount;
                            item.imported_diff_amount = res.data.data.imported_diff_amount;
                            item.asn_diff_amount = res.data.data.asn_diff_amount;
                            this.disabledItemId = "";
                            tempTip.setDuration(2000);
                            tempTip.showSuccess("实盘数量成功修改为“"+counted_amount+"”");
                            return;
                        }
                        tempTip.setDuration(3000);
                        tempTip.show(res.data.data);
                    })
                    .catch((err)=>{
                        tempTip.setDuration(3000);
                        tempTip.show('网络错误：'+err);
                    });
            },
            switchMenu(menuName){
                this.inputMode = menuName;
                this.cleanInputs();
                if (menuName === 'regular') {
                    this.status.amountDisable = false;
                    $('#amountLabel').text('输入数量：');
                }else {
                    this.status.amountDisable = true;
                    $('#amountLabel').text('扫入数量：');
                }
            },
            scanListening: function () {
                let _this = this;
                $(document).on('keypress', function (e) {
                    if (!status.barcodeDisable && !_this.focusing && e.keyCode===13 && !_this.inputting.amount){
                        document.getElementById('amount').focus();
                        return;
                    }
                    if(_this.focusing!=='document'){return}
                    if(!_this.status.barcodeDisable){return}
                    if (e.keyCode !== 13) {
                        if(_this.status.scanEndInputted){
                            _this.inputting.barcode='';
                            _this.status.scanEndInputted=false;
                        }
                        _this.inputting.barcode += String.fromCharCode(e.keyCode);
                    } else {
                        if(_this.inputting.barcode.length<=1){
                            window.tempTip.setDuration(4500);
                            window.tempTip.show('未扫入条码，请检查扫码枪设置，尝试调至“直接键盘输出”模式');
                            return;
                        }
                        _this.status.scanEndInputted = true;
                        switch(_this.inputMode){
                            case 'regular' : _this.commitGoodsOnRegularMode();break;
                            case 'increasing': _this.commitGoodsOnIncreasingMode();break;
                            case 'multiIncreasing': _this.commitGoodsOnIncreasingMode(false);break;
                        }
                    }
                });
            },
            showCommitButton: function () {
                if(this.inputting.barcode && this.inputting.amount /*&& this.inputting.bin_number*/){
                    if (this.status.commitButtonVisible){
                        this.commitGoods();
                    }else this.status.commitButtonVisible=true;
                }else{
                    if (this.status.barcodeDisable){
                        this.focusing = '';
                        document.getElementById('amount').focus();
                    }
                }
            },
            _isExist(item){
                if (item.bin_number == this.inputting.bin_number
                    && item.batch_code == this.inputting.batch_number
                    && item.produced_at == this.inputting.produce_date
                    && item.unique_code == this.inputting.unique_code
                    && item.invalid_at == this.inputting.valid_date){
                    return true;
                }
                return false;
            },
            commitGoods: function () {
                window.tempTip.setDuration(3000);
                if (this.inputMode === 'regular'){
                    if (!this.inputting.amount) {window.tempTip.show('请输入数量');return;}
                    let item = this.getItem();
                    if (item){
                        if (item.produced_at && !this.inputting.produce_date){window.tempTip.show('请输入生产日期');return;}
                        if (item.invalid_at && !this.inputting.valid_date){window.tempTip.show('请输入失效日期');return;}
                        if (item.batch_code && !this.inputting.batch_number){window.tempTip.show('请输入批次号');return;}
                        if (this._isExist(item)){
                            if (!confirm('该单盘点已存在是否叠加数量？'))return;
                        }
                    }
                }
                if(!this.inputting.barcode){window.tempTip.show('请扫入条码');return;}
                else if(!this.inputting.bin_number){window.tempTip.show('请输入隔口号');return;}

                window.axios.post('{{url('maintenance/commodity/isExist')}}',{owner_id:this.storeCheckingReceive.owner_id,barcode:this.inputting.barcode})
                    .then(res=>{
                        if (res.data.success) exe();
                        else{
                            window.tempTip.confirm("商品信息不存在，是否录入为新商品",function () {
                                exe();
                            });
                        }
                    }).catch(err=>{
                    window.tempTip.setDuration(3000);
                    window.tempTip.show("网络错误："+err);
                });
                let _this = this;
                function exe(){
                    axios.post('{{url('store/checkingReceive/insertItem')}}',{mission_id : _this.storeCheckingReceive.id,goods : _this.inputting})
                        .then(res=>{
                            if (res.data.success) {
                                _this.lastScannedBarcode = _this.inputting.barcode;
                                if (res.data.type === 'create'){
                                    _this._addStoreCheckingReceiveItem(res.data.data);
                                    window.tempTip.setDuration(2000);
                                    window.tempTip.showSuccess('成功提交：' + _this.inputting.barcode);
                                }else{
                                    if (_this.storeCheckingReceiveItems.length > 0 && res.data.data.id === _this.storeCheckingReceiveItems[0].id){
                                        _this.storeCheckingReceiveItems[0].counted_amount = res.data.data.counted_amount;
                                        _this.storeCheckingReceiveItems[0].imported_diff_amount = res.data.data.imported_diff_amount;
                                        _this.storeCheckingReceiveItems[0].asn_diff_amount = res.data.data.asn_diff_amount;
                                        _this.storeCheckingReceiveItems[0].bin_number = res.data.data.bin_number;
                                    }else{
                                        _this._removeCheckingReceiveItem(res.data.data.id);
                                        _this._addStoreCheckingReceiveItem(res.data.data);
                                    }
                                    _this.inputting.amount = res.data.data.counted_amount;
                                    window.tempTip.setDuration(2000);
                                    window.tempTip.showSuccess('成功增加“'+_this.inputting.barcode+'”实盘数量')
                                }
                                if (_this.status.commitButtonVisible) _this.status.commitButtonVisible=false;
                                _this.audioDing();
                                if (_this.inputMode === 'regular')_this.cleanInputs();
                                if (_this.inputting.bin_number && !_this.status.binDisable)_this.status.binDisable = true;
                                return;
                            }
                            window.tempTip.setDuration(3000);
                            window.tempTip.show(res.data.data);
                        }).catch(err=>{
                        window.tempTip.setDuration(3000);
                        window.tempTip.show("网络错误："+err);
                    });
                }
            },
            _addStoreCheckingReceiveItem(item){
                let storeCheckingReceiveItem = {
                    id:item.id,
                    bin_number:item.bin_number,
                    commodity_name:item.commodity ? item.commodity.name : '',
                    commodity_barcodes:item.commodity ? item.commodity.barcodes : [],
                    imported_amount:item.imported_amount,
                    counted_amount:item.counted_amount,
                    asn_amount:item.asn_amount,
                    imported_diff_amount:item.imported_diff_amount,
                    asn_diff_amount:item.asn_diff_amount,
                    produced_at:item.produced_at,
                    invalid_at:item.invalid_at,
                    batch_code:item.batch_code,
                    unique_code:item.unique_code,
                };
                this.storeCheckingReceiveItems.unshift(storeCheckingReceiveItem);
            },
            _removeCheckingReceiveItem(id){
                this.storeCheckingReceiveItems.some((item,index)=>{
                    if (item.id == id){
                        this.storeCheckingReceiveItems.splice(index,1);
                        return true;
                    }
                });
            },
            cleanInputs: function () {
                this.inputting.barcode='';
                this.inputting.amount='';
                this.inputting.bin_number='';
                this.inputting.produce_date='';
                this.inputting.unique_code='';
                this.inputting.valid_date='';
                this.inputting.batch_number='';
                this.status.commitButtonVisible=false;
                this.status.binDisable=false;
                this.status.barcodeDisable = true;
            },
            autoFillBin: function () {
                let isNotRepeatingBarcode=this.storeCheckingReceiveItems.every(item=>{
                    if (item.commodity_barcodes.length > 0){
                        return  item.commodity_barcodes.every(barcode=> {
                            if (barcode.code === this.inputting.barcode){
                                if (item.bin_number){
                                    this.inputting.bin_number=item.bin_number;
                                    this.status.binDisable=true;
                                }
                                return false;
                            }
                            return true;
                        });
                    }else return true;
                });
                if(isNotRepeatingBarcode) this.status.binDisable=false;
            },
            commitGoodsOnRegularMode(){
                this.showCommitButton();
            },
            commitGoodsOnIncreasingMode: function (signIncreasing = true) {
                let item = this.getItem();
                let _this = this;
                if (!item){
                    _this.focusOutDocument();
                    window.tempTip.setInputType('number');
                    window.tempTip.inputVal('该商品第一件递增请输入隔口号：', function (bin_number) {
                        if (bin_number === '') {
                            window.tempTip.setDuration(2200);
                            window.tempTip.show('未输入隔口号，请重新扫描');
                            _this.alertVibrate();
                            _this.focusDocument();
                            return;
                        }
                        _this.inputting.bin_number = bin_number;
                        _this.inputting.amount = 1;
                        _this.commitGoods();
                        _this.focusDocument();
                    });
                    return;
                }
                _this.status.binDisable=true;
                if (item.bin_number) {
                    _this.inputting.bin_number = item.bin_number;
                    _this.commit(item, signIncreasing);
                }else{
                    _this.lastScannedBarcode = _this.inputting.barcode;
                    _this.focusOutDocument();
                    window.tempTip.setInputType('number');
                    window.tempTip.inputVal('该条码存在但无隔口号，请输入：', function (bin_number) {
                        if (bin_number === '') {
                            window.tempTip.setDuration(2200);
                            window.tempTip.show('未输入隔口号，请重新扫描');
                            _this.alertVibrate();
                            _this.focusDocument();
                            return;
                        }
                        _this.inputting.bin_number = bin_number;
                        _this.commit(item, signIncreasing);
                    });
                }
            },
            commit(item, signIncreasing){
                this.inputting.amount = '';
                this.inputting.produce_date=item.produced_at;
                this.inputting.unique_code=item.unique_code;
                this.inputting.valid_date=item.invalid_at;
                this.inputting.batch_number=item.batch_code;
                if (signIncreasing && this.lastScannedBarcode && this.lastScannedBarcode !== this.inputting.barcode) {
                    this.audioWarning_otherBarcode();
                    this.focusOutDocument();
                    window.tempTip.confirm('扫到其它条码，是否切换至新条码并记录？', ()=>{
                        this.commitGoods();
                        this.focusDocument();
                    }, () => {
                        this.focusDocument();
                    });
                }else{
                    this.commitGoods();
                    this.focusDocument();
                }
            },
            getItem: function () {
                let repeatedGoods=null;
                this.storeCheckingReceiveItems.every(item=>{
                    if (item.commodity_barcodes.length > 0){
                        return  item.commodity_barcodes.every(barcode=> {
                            if (barcode.code === this.inputting.barcode){
                                repeatedGoods = item;
                                return false;
                            }
                            return true;
                        });
                    }else return true;
                });
                return repeatedGoods;
            },
            oninputEnter:function(e){
                if (e.key === 'Enter') {
                    this.focusDocument();
                    this.showCommitButton();
                }
            },
            focusDocument: function () {
                this.focusing = 'document';
            },
            focusOutDocument: function () {
                this.focusing = '';
                this.autoFillBin();
            },
            audioWarning_otherBarcode: function () {
                let audio = document.getElementById('soundWarning');
                audio.currentTime = 0;//重新播放
                if(audio.paused){
                    audio.play();// 播放
                }
                this.alertVibrate();

            },
            audioDing: function () {
                let audio = document.getElementById('soundDing');
                audio.currentTime = 0;//重新播放
                audio.play();// 播放
                //手机震动
                function startVibrate(duration) {
                    if (navigator.vibrate) {
                        navigator.vibrate(duration);
                    } else if (navigator.webkitVibrate) {
                        navigator.webkitVibrate(duration);
                    }
                }
                startVibrate(500);
            },
            alertVibrate: function () {
                function startVibrate(duration) {
                    if (navigator.vibrate) {
                        navigator.vibrate(duration);
                    } else if (navigator.webkitVibrate) {
                        navigator.webkitVibrate(duration);
                    }
                }
                let vibrateInterval = setInterval(function() {
                    startVibrate(150);
                }, 50);
                setTimeout(function() {
                    clearInterval(vibrateInterval)
                }, 2000);
            },
            exportItem(){
                let url = '{{url('store/checkingReceive/mission/export?mission_id=')}}'+this.storeCheckingReceive.id;
                window.open(url);
            },
            isResetAmount(){
                window.tempTip.confirm('是否要进行重新清点，该操作会重置当前任务所有已盘数量！',()=>{
                    this.resetAmount();
                });
            },
            resetAmount(){
              axios.post('{{url('store/checkingReceive/mission/resetAmount')}}',{mission_id:this.storeCheckingReceive.id})
                  .then(res=>{
                      if (res.data.success){
                          this.storeCheckingReceiveItems.forEach(function (item) {
                              item.counted_amount = 0;
                              item.imported_diff_amount = '';
                              item.asn_diff_amount = '';
                              item.bin_number = '';
                          });
                          window.tempTip.setDuration(2000);
                          window.tempTip.showSuccess('重新清点完毕，数量已重置为0');
                          return;
                      }
                      window.tempTip.setDuration(3000);
                      window.tempTip.show(res.data.data);
                  }).catch(err=>{
                      window.tempTip.setDuration(3000);
                      window.tempTip.show("网络错误："+err);
                  });
            },
            matchASN(){
                if (!this.asn){
                    window.tempTip.setIndex(1099);
                    window.tempTip.setDuration(3000);
                    window.tempTip.show('请输入ASN号！');
                    return;
                }
                axios.post('{{url('store/checkingReceive/mission/matchASN')}}',{asn:this.asn,mission_id:this.storeCheckingReceive.id})
                    .then(res=>{
                        if (res.data.success){
                            window.tempTip.setIndex(1099);
                            window.tempTip.setDuration(2000);
                            window.tempTip.showSuccess("匹配成功");
                            location.reload();
                            return;
                        }
                        window.tempTip.setIndex(1099);
                        window.tempTip.setDuration(3000);
                        window.tempTip.show(res.data.data);
                    }).catch(err=>{
                        window.tempTip.setIndex(1099);
                        window.tempTip.setDuration(3000);
                        window.tempTip.show("网络错误："+err);
                    });
            },
            destroyItem(item,index){
                let url = "{{url("store/checkingReceive/destroyItem")}}";
                let param = {id:item.id};
                window.tempTip.confirm("确定要删除商品“"+item.commodity_name+"”的盘点吗?",()=>{
                    window.axios.post(url,param).then((res)=>{
                        if (res.data.success){
                            this.$delete(this.storeCheckingReceiveItems,index);
                            window.tempTip.setDuration(2000);
                            window.tempTip.showSuccess("删除成功");
                            return;
                        }
                        window.tempTip.setDuration(3000);
                        window.tempTip.show(res.data.data);
                    }).catch(err=>{
                        window.tempTip.setDuration(3000);
                        window.tempTip.show("网络错误："+err);
                    })
                });
            }
        },
    });
</script>
@stop
