<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<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}">
                            <input type="hidden" id="outStand" th:value="*{outStand}"/>
                            <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 box-shadow">
                <div class="ibox-title">
                    <h5>操作界面</h5>
                </div>
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#goodsOut" class="nav-right-tabs-a" data-toggle="tab">品质检验操作</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane active" id="goodsOut">
                        <div class="ibox-content ibox-content-1">
                            <form class="m" id="form-goods-out">
                                <div class="row">
                                    <div class="col-sm-6 form-group">
                                        <label class="control-label">物料编码：</label>
                                        <div>
                                            <input type="text" id="goodsId" name="goodsId" autocomplete="off" class="form-control input-border-bottom" onclick="openGoodsList();">
                                        </div>
                                    </div>
                                    <div class="col-sm-6 form-group">
                                        <label class="control-label">批次号：</label>
                                        <div>
                                            <input type="text" id="batchNo" name="batchNo" class="form-control input-border-bottom" readonly>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-sm-6 form-group">
                                        <label class="control-label">托盘号：</label>
                                        <div>
                                            <input type="text" id="ctl" name="ctl" class="form-control input-border-bottom" readonly>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 form-group">
                                        <label class="control-label">数量：</label>
                                        <div>
                                            <input type="number" id="shelvesNum" name="shelvesNum" class="form-control input-border-bottom" readonly>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <div class="row">
                                <div class="col-sm-6" style="padding-right: 20px;padding-left: 20px;">
                                    <button type="button" class="btn btn-lg btn-ctl-print" onclick="goodsOutConfirm();"><span class="btn-ctl-color">确认检验</span></button>
                                </div>
                                <div class="col-sm-6" style="padding-right: 20px;padding-left: 20px;">
                                    <button type="button" class="btn btn-lg btn-ctl-print-red" onclick="backStock()"><span class="btn-ctl-color">返库</span></button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<style>
    .fixed-table-toolbar {
        display: none !important;
    }
</style>
<script th:inline="javascript">
    var prefix = ctx + "business/qualityInspect";
    var statusDatas = [[${@dict.getType('out_status')}]];

    /**
     * 表格初始化
     */
    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "退货信息",
            showSearch: false,
            showToggle: false,
            showRefresh: false,
            showColumns: false,
            queryParams: queryParams,
            // onClickRow:function(row, $element) {
            //     // 物料编码
            //     $("#goodsId").val(row.goodsId);
            //     return false;
            // },
            // TODO
            columns: [
                {
                    field: 'goodId',
                    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: 'expiryDate',
                    title: '有效日期',
                    sortable: true
                },
                {
                    field: 'mistockNum',
                    title: '数量',
                    sortable: true
                },
                {
                    field: 'pickingNum',
                    title: '出库数量',
                    sortable: true
                },
                {
                    field: 'ctl',
                    title: '托盘号',
                    sortable: true
                },
                {
                    field: 'locId',
                    title: '库位',
                    sortable: true,
                    align: 'center'
                },
                {
                    field: 'outstand',
                    title: '出库站台',
                    sortable: true
                },
                {
                    field: 'outstatus',
                    title: '状态',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                }
            ]
        };
        $.table.init(options);
    });

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.ckType = "5";
        search.outStand = $("#outStand").val();
        return search;
    }

    // 出库任务列表，每间隔5秒刷新
    setInterval(function () {
        console.log('刷新出库任务');
        $.table.refresh();
    }, 1000)

    /**
     * 出库确认,生成出库任务
     */
    function goodsOutConfirm() {

        if($("#goodsId").val() == null || $("#goodsId").val() == "") {
            $.modal.msgError('请选择物料编码');
            return;
        }

        // TODO
        // $.ajax({
        //     type: "post",
        //     url: "http://10.10.90.146:9002/pcba/getPoDetails",
        //     contentType: "application/json",
        //     dataType: "json",
        //     authorization: "",
        //     data: {
        //         "systemParams":{
        //             "requestId":"1",
        //             "application":"NT_WMS",
        //             "sourceSystem":"NT_WMS",
        //             "targetSystem":"EBS",
        //             "serviceName":"geTransactions",
        //             "nonce":1,
        //             "timestamp":1,
        //             "serviceOperation":"S",
        //             "serviceVersion":"1.0",
        //             "token":"fd"
        //         },
        //         "baseQueryParams":{
        //         },
        //         "businessData":[{
        //             "transDate":"",
        //             "poHeaderId":$("#goodsIdProviderIn").val(),
        //             "poLineId":$("#goodsIdProviderIn").val(),
        //             "lineLocationId":$("#goodsIdProviderIn").val(),
        //             "poDistributionId":$("#goodsIdProviderIn").val(),
        //             "itemId":$("#goodsIdProviderIn").val(),
        //             "quantity":$("#goodsIdProviderIn").val(),
        //             "subInventory":$("#goodsIdProviderIn").val(),
        //             "locatorId":$("#goodsIdProviderIn").val(),
        //             "lotNumber":$("#goodsIdProviderIn").val(),
        //             "receiptDate":$("#goodsIdProviderIn").val()
        //         }]
        //     },
        //     success: goodsOutOrder(),
        //     error: function (data){
        //         $.modal.msgError('WMS工单发料写入EBS失败');
        //     }
        // });
        goodsOutOrder()

    }

    /**
     *  出库单生成
     */
    function goodsOutOrder () {
        $.ajax({
            type: "post",
            url: ctx + "business/qualityInspect/add",
            data: {
                "goodId": $("#goodsId").val(),
                "outStand": $("#outStand").val(),
                "putinId": $("#batchNo").val(),
                "barcode": $("#ctl").val(),
                "ckType": "5"
            },
            success: function (data) {
                console.log(data)
                if (data.code == 0) {
                    if (data.msg == "-1") {
                        $.modal.msgError('库存中无该物料')
                    } else if (data.msg == "-2") {
                        $.modal.msgError('当前站台有出库任务未完成')
                    } else {
                        $.modal.msgSuccess('已提交出库任务请求')
                        $("#goodsId").val("")
                        $("#batchNo").val("")
                        $("#ctl").val("")
                        $("#shelvesNum").val("")
                    }
                } else {
                    $.modal.msgError('错误')
                }
            },
            error: function () {
                $.modal.msgError('错误')
            }
        });
    }

    /**
     * 回库
     */
    function backStock() {
        $.ajax({
            type: "post",
            url: ctx + "business/qualityInspect/backStock",
            data: {
                "outstand": $("#outStand").val()
            },
            success: function (data) {
                console.log(data)
                if (data.code == 0) {
                    if (data.msg == "0") {
                        $.modal.msgError('当前站台无可回库任务')
                    } else {
                        $.modal.msgSuccess('已提交回库请求')
                    }
                } else {
                    $.modal.msgError('错误')
                }
            },
            error: function () {
                $.modal.msgError('错误')
            }
        });
    }

    function openGoodsList() {
        var options = {
            title: '选择物料',
            url: prefix + "/modalPage",
            // width: 800,
            // height: 1000,
            callBack: doSubmit2
        };
        $.modal.openOptions(options);
    }

    function doSubmit2(index, layero){
        var rows = layero.find("iframe")[0].contentWindow.getSelections();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        console.log(rows);
        $('#shelvesNum').val(rows.pop());
        $('#ctl').val(rows.pop());
        $('#batchNo').val(rows.pop());
        $('#goodsId').val(rows.pop());
        $.modal.close(index);
    }
</script>
</body>
</html>