<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('入库任务单列表')"/>
    <link th:href="@{/ruoyi/css/ry-ui-custom.css?v=4.7.3}" rel="stylesheet"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row pl0 pr0">
        <div class="col-sm-8 pr0 pl0">
            <div class="box-shadow">
                <div class="ibox float-e-margins" style="margin-bottom: 10px;">
                    <div class="ibox-title">
                        <h5>身份信息</h5>
                    </div>
                    <div class="ibox-content ibox-content-1" style="margin: 0;height: 45px">
                        <form th:object="${sysUser}">
                            <div class="col-sm-4 form-horizontal">
                                <label class="control-label">当前操作员：</label>
                                <label class="control-label" style="font-weight: bold" id="currentUser"
                                       th:text="*{userName}"></label>
                            </div>
                            <div class="col-sm-3 form-horizontal">
                                <label class="control-label">角色：</label>
                                <label class="control-label" style="font-weight: bold" id="userRoles"
                                       th:text="*{roleNames}"></label>
                            </div>
                            <div class=" form-horizontal">
                                <label class="control-label">上次登录时间：</label>
                                <label class="control-label" style="font-weight: bold" id="lastLoginTime"
                                       th:text="*{loginDateStr}"></label>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div style="margin-top: 10px;" class="box-shadow">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>入库信息</h5>
                    </div>
                    <div class="ibox-content ibox-content-1">
                        <div class="select-table table-striped">
                            <table id="bootstrap-table"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-4  pl5 pr0">
            <div class="ibox float-e-margins shadow">
                <div class="ibox-title">
                    <h5>入库操作</h5>
                </div>
                <ul class="nav nav-tabs">
                    <li><a href="#noErpIn" class="nav-right-tabs-a" data-toggle="tab">退库入库</a></li>
                    <li><a href="#emptyPalletIn" class="nav-right-tabs-a" data-toggle="tab">空托入库</a></li>
                    <li class="active"><a href="#emptyPalletOut" class="nav-right-tabs-a" data-toggle="tab">空托出库</a>
                    </li>
                    <!--                    <li><a href="#printLable" class="nav-right-tabs-a" data-toggle="tab">打印标签</a></li>-->
                </ul>
                <div class="tab-content">
                    <div class="tab-pane fade" id="noErpIn">
                        <div class="row inner_row_style">
                            <div class="col-sm-12 form-group">
                                <label class="control-label">扫描：</label>
                                <div>
                                    <input type="text" id="scanId" name="scanId"
                                           class="form-control input-border-bottom" onkeydown="scanFunction()">
                                </div>
                            </div>
                        </div>
                        <div class="row inner_row_style">
                            <div class="col-sm-6 form-group">
                                <label class="control-label">订单号：</label>
                                <div>
                                    <input type="text" id="orderNo" name="orderNo" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                            <div class="col-sm-6 form-group">
                                <label class="control-label">物料编码：</label>
                                <div>
                                    <input type="text" id="goodsId" name="goodsId" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                        </div>
                        <div class="row inner_row_style">
                            <div class="col-sm-6 form-group">
                                <label class="control-label">批次号：</label>
                                <div>
                                    <input type="text" id="produclotid" name="produclotid" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                            <div class="col-sm-6 form-group">
                                <label class="control-label">供应商代码：</label>
                                <div>
                                    <input type="text" id="providerId" name="providerId" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                        </div>
                        <div class="row inner_row_style">
                            <div class="col-sm-6 form-group">
                                <label class="control-label">生产日期：</label>
                                <div>
                                    <input type="text" id="productionDate" name="productionDate" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                            <div class="col-sm-6 form-group">
                                <label class="control-label">有效日期：</label>
                                <div>
                                    <input type="text" id="expirationDate" name="expirationDate" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                        </div>
                        <div class="row inner_row_style">
                            <div class="col-sm-6 form-group">
                                <label class="control-label">数量：</label>
                                <div>
                                    <input type="text" id="shelfNum" name="shelfNum"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                        </div>
                        <div class="row inner_row_style" hidden="true">
                            <div class="col-sm-12 form-group">
                                <label class="control-label">重量：</label>
                                <div>
                                    <input type="text" id="noErpWgh" name="noErpWgh" readOnly="readOnly"
                                           class="form-control input-border-bottom">
                                </div>
                            </div>
                        </div>
                        <div class="row tow-button-row-style">
                            <div class="col-sm-6">
                                <button type="button" class="btn btn-lg btn-ctl-print" onclick="clickNoErpIn();"><span
                                        class="btn-ctl-color">确认退库</span></button>
                            </div>
                            <div class="col-sm-6">
                                <button type="button" class="btn btn-lg btn-ctl-print-red"
                                        onclick="finishNoErpIn();"><span
                                        class="btn-ctl-color">返库</span></button>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="emptyPalletIn">
                        <div class="row tow-button-row-style">
                            <div class="col-sm-12">
                                <button type="button" class="btn btn-lg btn-ctl-print" onclick="clickEmptyPalletIn();">
                                    <span class="btn-ctl-color">空托入库</span></button>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane active" id="emptyPalletOut">
                        <div class="row tow-button-row-style">
                            <div class="col-sm-12">
                                <button type="button" class="btn btn-lg btn-ctl-print" onclick="clickEmptyPalletOut();">
                                    <span class="btn-ctl-color">空托出库</span></button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "business/returnStock";
    var access_token = null;
    var defaultWgh = $('#defaultWgh').val();
    var oldBarcode = "";
    var QrCode = null;
    // 当前物料编码
    var currentGoodsId = "";
    // 覆铜板用的
    var currentLotId = null;
    var currentNum = 0;
    // 判断是否是铜箔，如果是铜箔可以允许不同批次
    var isTongbo = 0;
    var tongbo1 = "";
    var tongbo2 = "";
    var tongbo3 = "";
    var tongbo1Num = 0;
    var tongbo2Num = 0;
    var tongbo3Num = 0;

    $(function () {
        var options = {
            url: prefix + "/list",
            exportUrl: prefix + "/export",
            modalName: "入库任务单",
            showSearch: false,
            showToggle: false,
            showRefresh: false,
            showColumns: false,
            search: false,
            columns: [
                {
                    field: 'wmsorderid',
                    title: '订单号',
                    sortable: true
                },
                {
                    field: 'goodsid',
                    title: '物料编码',
                    sortable: true
                },
                {
                    field: 'goodsname',
                    title: '物料描述',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.tooltip(value);
                    }

                },
                {
                    field: 'produclotid',
                    title: '生产批号',
                    sortable: true
                },
                {
                    field: 'productionDate',
                    title: '生产日期',
                    sortable: true
                },
                {
                    field: 'expirationDate',
                    title: '有效日期',
                    sortable: true
                },
                {
                    field: 'providerId',
                    title: '供应商编码',
                    sortable: true
                },
                {
                    field: 'shelfNum',
                    title: '数量',
                    sortable: true
                },
                {
                    field: 'instand',
                    title: '站台号',
                    sortable: true
                },
                {
                    title: '操作',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs' + '" href="javascript:void(0)" onclick="clearCurrRow()"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
        // TODO
        // $.ajax({
        //     type: "post",
        //     url: "http://10.10.90.205:9004/esb/intergrateService",
        //     contentType: "application/json",
        //     dataType: "json",
        //     authorization: "Basic bnBpX2NsaWVudDppcG4xMTIyMjM=",
        //     success: function (res) {
        //         access_token = res.access_token;
        //     }
        // });
    });

    function scanFunction() {
        var keycode = event.keyCode;
        if (keycode == 13) {
            var test = "10910139210287360R17070611707061807051117670335010910139210287360R170706117070618070511176703350 ";
            let scanId = $('#scanId').val();
            let scanInfo = scanId.replace(/(\[GS\])|(\[RS\])|(\[EOT\])/g, " ")
            console.log(scanInfo)
            let s = scanInfo.trim();
            // let s1 = s.substr(2);
            // let arr = s1.split("");
            // let arr = s1.split("[GS]");
            let arr = s.split(" ");
            //录入数据后加入
            // if($('#headProviderId').val !== arr[1]){
            //     $.modal.msgError('该物料非选择的供应商的物料')
            //     return
            // }
            console.log(arr);
            $("#orderNo").val(arr[5]);
            $('#providerId').val(arr[1]);
            $('#goodsId').val(arr[0]);
            $('#produclotid').val(arr[2]);
            $('#productionDate').val(arr[3]);
            $('#expirationDate').val(arr[4]);
            $('#shelfNum').val(arr[6]);

            $.ajax({
                type: "get",
                url: prefix + "/getBarcode",
                success: function (res) {
                    var result = res.data
                    // $("#tasklist").html(data);

                    $('#ctl').val(result.barcode);
                    $('#noErpWgh').val(result.wgh);
                    console.log(result.wgh);
                    if (result.loadmode.trim() != '1') {
                        $.modal.msgError('当前站台无托盘')
                    } else if (oldBarcode != '' && oldBarcode != result.barcode) {
                        $.modal.msgError('机器此时托盘号与上次扫描不同，提交失败')
                    } else {
                        oldBarcode = result.barcode
                        QrCode = new Object();
                        QrCode.ctl = oldBarcode
                        QrCode.orderNo = $("#orderNo").val()
                        QrCode.providerId = $('#providerId').val()
                        QrCode.goodsId = $('#goodsId').val()
                        QrCode.produclotid = $('#produclotid').val()
                        QrCode.productionDate = $('#productionDate').val()
                        QrCode.expirationDate = $('#expirationDate').val()
                        QrCode.plcid = result.plcid
                        // QrCode.wgh = result.wgh
                        QrCode.converyorId = result.converyorId
                        // setTimeout(function (){
                        //     // clickNoErpIn()
                        //     $('#scanId').val('')
                        //     $('#scanId').focus()
                        // }, 1000)
                        // taskCache.push(QrCode)
                    }
                }
            });
        }
    }

    /** 清除所有数据 */
    function clear() {
        $("#" + table.options.id).bootstrapTable('removeAll')
        QrCode = null;
        this.barcode = '';
        currentLotId = null;
        currentNum = 0;
        isTongbo = false;
        tongbo1 = '';
        tongbo2 = '';
        tongbo3 = '';
        tongbo1Num = 0;
        tongbo2Num = 0;
        tongbo3Num = 0;
        currentGoodsId = '';
        clearCodeDate();
    }

    /** 删除当前扫描的数据 */
    function clearCurrRow() {
        $("#" + table.options.id).bootstrapTable('removeAll');
        // 重置当前批次号
        currentLotId = null;
        // 重置当前数量
        currentNum = 0;
        isTongbo = false;
        tongbo1 = '';
        tongbo2 = '';
        tongbo3 = '';
        tongbo1Num = 0;
        tongbo2Num = 0;
        tongbo3Num = 0;
        currentGoodsId = '';
        clearCodeDate();
    }

    /** 清楚扫码数据 */
    function clearCodeDate() {
        $("#orderNo").val('');
        $('#providerId').val('');
        $('#goodsId').val('');
        $('#produclotid').val('');
        $('#productionDate').val('');
        $('#expirationDate').val('');
        $('#shelfNum').val('');
        $('#ctl').val('');
        $('#noErpWgh').val('');
        $("#orderNoProviderIn").val('');
        $('#providerIdProviderIn').val('');
        $('#goodsIdProviderIn').val('');
        $('#produclotidProviderIn').val('');
        $('#productionDateProviderIn').val('');
        $('#expirationDateProviderIn').val('');
        $('#shelfNumProviderIn').val('');
        $('#ctlProviderIn').val('');
        $('#WghProviderIn').val('');
    }

    // 无erp入库
    function clickNoErpIn() {
        if (!$('#scanId').val()) {
            return;
        }
        console.log("无ERP入库任务开始")
        // 扫码清空
        $('#scanId').val('')
        $('#scanId').focus()
        if ($('#noErpWgh').val() >= defaultWgh) {// 每次称重都要大于前一次重量
            if ('106' == QrCode.goodsId.substr(0, 3)) {
                isTongbo = true;
            } else {
                isTongbo = false;
            }
            // 判断是否是同一物料
            if (currentGoodsId != '' && currentGoodsId != QrCode.goodsId) {
                $.modal.msgError('物料编码不一致');
                return;
            } else {
                currentGoodsId = QrCode.goodsId;
            }
            // 覆铜板判断是否同一批次
            if (!isTongbo) {
                if (currentLotId != null && currentLotId != $('#produclotid').val()) {
                    $.modal.msgError('物料批次号不同')
                } else {
                    // 可以更改数量
                    QrCode.shelfNum = Number(currentNum) + Number($('#shelfNum').val());
                    currentNum = QrCode.shelfNum;
                    // 更新当前批次号
                    currentLotId = QrCode.produclotid
                    // 入库数据插入
                    insertTable();
                    // 重量更新
                    defaultWgh = Number(defaultWgh) + Number($('#noErpWgh').val());
                    console.log("无erp入库任务结束")
                }
            } else {
                // 插入第一个批次
                if (tongbo1 == '') {
                    // 第一个批次号
                    tongbo1 = QrCode.produclotid;
                    // 可以更改数量
                    QrCode.shelfNum = Number(tongbo1Num) + Number($('#shelfNum').val());
                    tongbo1Num = QrCode.shelfNum;
                    // 入库数据插入
                    insertTableAllowDiff(0, tongbo1, tongbo1Num);
                } else if (tongbo1 != '' && tongbo1 == QrCode.produclotid) {// 更新第一个批次
                    // 可以更改数量
                    QrCode.shelfNum = Number(tongbo1Num) + Number($('#shelfNum').val());
                    tongbo1Num = QrCode.shelfNum;
                    // 入库数据插入
                    updateTableAllowDiff(0, tongbo1Num);
                } else if (tongbo1 != '' && tongbo1 != QrCode.produclotid && tongbo2 == '') {// 插入第二个批次
                    // 第二个批次号
                    tongbo2 = QrCode.produclotid;
                    // 可以更改数量
                    QrCode.shelfNum = Number(tongbo2Num) + Number($('#shelfNum').val());
                    tongbo2Num = QrCode.shelfNum;
                    // 入库数据插入
                    insertTableAllowDiff(1, tongbo2, tongbo2Num);
                } else if (tongbo1 != '' && tongbo1 != QrCode.produclotid && tongbo2 != '' && tongbo2 == QrCode.produclotid) {// 更新第二个批次
                    // 可以更改数量
                    QrCode.shelfNum = Number(tongbo2Num) + Number($('#shelfNum').val());
                    tongbo2Num = QrCode.shelfNum;
                    // 入库数据插入
                    updateTableAllowDiff(1, tongbo2Num);
                } else if (tongbo1 != '' && tongbo1 != QrCode.produclotid && tongbo2 != '' && tongbo2 != QrCode.produclotid && tongbo3 == '') {// 插入第三个批次
                    // 第三个批次号
                    tongbo3 = QrCode.produclotid;
                    // 可以更改数量
                    QrCode.shelfNum = Number(tongbo3Num) + Number($('#shelfNum').val());
                    tongbo3Num = QrCode.shelfNum;
                    // 入库数据插入
                    insertTableAllowDiff(2, tongbo3, tongbo3Num);
                } else if (tongbo1 != '' && tongbo1 != QrCode.produclotid && tongbo2 != '' && tongbo2 != QrCode.produclotid && tongbo3 != '' && tongbo3 == QrCode.produclotid){// 更新第三个批次
                    // 可以更改数量
                    QrCode.shelfNum = Number(tongbo3Num) + Number($('#shelfNum').val());
                    tongbo3Num = QrCode.shelfNum;
                    // 入库数据插入
                    updateTableAllowDiff(2, tongbo3Num);
                } else {
                    $.modal.msgError('扫描超过三个批次');
                }
                // 重量更新
                defaultWgh = Number(defaultWgh) + Number($('#noErpWgh').val());
                console.log("无erp入库任务结束")
            }
        } else {
            $.modal.msgError('重量不匹配，无法入库')
            console.log("无erp入库任务中止")
        }
    }

    function insertTable() {
        $("#" + table.options.id).bootstrapTable('removeAll');
        $("#" + table.options.id).bootstrapTable('insertRow', {
            index: 0, // 你想插入到哪，0表示第一行
            row: {
                goodsid: QrCode.goodsId,
                providerId: QrCode.providerId,
                shelfNum: currentNum,
                ctl: QrCode.ctl,
                produclotid: currentLotId,
                expirationDate: QrCode.expirationDate,
                productionDate: QrCode.productionDate,
                plcid: QrCode.plcid,
                instand: QrCode.converyorId,
                wmsorderid: QrCode.orderNo,
                onshelfuserid: $('#loginName').text()
            }
        });
    }

    // 铜箔插入入库任务
    function insertTableAllowDiff(row, produclotid, shelfNum) {
        $("#" + table.options.id).bootstrapTable('insertRow', {
            index: row, // 你想插入到哪，0表示第一行
            row: {
                goodsid: QrCode.goodsId,
                providerId: QrCode.providerId,
                shelfNum: shelfNum,
                ctl: QrCode.ctl,
                produclotid: produclotid,
                expirationDate: QrCode.expirationDate,
                productionDate: QrCode.productionDate,
                plcid: QrCode.plcid,
                instand: QrCode.converyorId,
                wmsorderid: QrCode.orderNo,
                onshelfuserid: $('#loginName').text()
            }
        });
    }

    // 铜箔更新入库任务
    function updateTableAllowDiff(row, shelfNum) {
        $("#" + table.options.id).bootstrapTable('updateRow', {
            index: row,
            replace: false,
            row: {
                shelfNum: shelfNum
            }
        });
    }

    /** 无ERP入库 */
    function finishNoErpIn() {
        setTimeout(function (){
            var data = $("#" + table.options.id).bootstrapTable('getData');
            $.ajax({
                type: "post",
                url: prefix + "/finishNoErpIn",
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (data) {
                    // $("#tasklist").html(data);
                    console.log(data)
                    if (data.msg == "操作成功") {
                        $.modal.msgSuccess('提交入库任务成功')
                    } else {
                        $.modal.msgError('提交入库任务失败');
                    }
                    clear();
                    defaultWgh = $('#defaultWgh').val();// 确认入库，重量复位
                },
                error: function (data) {
                    $.modal.msgError('提交入库失败')
                }
            });
        }, 500)
    }

    function clickEmptyPalletIn() {
        console.log("空托入库开始")
        $.ajax({
            type: "get",
            url: ctx + "business/ongoodsshelf/emptyPalletIn",
            success: function (data) {
                console.log(data);
                // $("#tasklist").html(data);
                if (data.code == 0) {
                    if (data.msg == "0") {
                        $.modal.msgError('该ip无站台信息');
                    } else if (data.msg == "-1") {
                        $.modal.msgError('该站台没有托盘');
                    } else {
                        $.modal.msgSuccess('提交空托入库成功');
                    }
                } else {
                    $.modal.msgError('错误');
                }
            }
        });
    }

    function clickEmptyPalletOut() {
        console.log("空托出库开始")
        $.ajax({
            type: "get",
            url: ctx + "business/ongoodsshelf/emptyPalletOut",
            success: function (data) {
                console.log(data);
                // $("#tasklist").html(data);
                if (data.code == 0) {
                    if (data.msg == "0") {
                        $.modal.msgError('当前站台有出库任务未完成');
                    } else {
                        $.modal.msgSuccess('提交空托出库任务成功');
                    }
                } else {
                    $.modal.msgError('错误');
                }
            }
        });
    }
</script>
</body>
</html>
<style>
    .inner_row_style {
        margin-left: 0px;
        margin-right: 0px;
        padding-top: 5px;
    }
</style>