<!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}">
                                <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="*{loginName}"></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>
                         <li><a href="#goodsOutConfirm" 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" onclick="openGoodsList();" class="form-control input-border-bottom">
                                             </div>
                                         </div>
                                         <div class="col-sm-6 form-group">
                                             <label class="control-label">数量：</label>
                                             <div>
                                                 <input type="number" min="1" id="pickNum" name="pickNum" class="form-control input-border-bottom">
                                             </div>
                                         </div>
                                     </div>
                                     <div class="row">
                                         <div class="col-sm-6 form-group">
                                             <label class="control-label">批次号：</label>
                                             <div>
                                                 <input type="text" id="putinId" name="putinId" autocomplete="off" class="form-control input-border-bottom">
                                             </div>
                                         </div>
                                     </div>
                                 </form>
                                 <div class="row">
                                     <div class="col-sm-12" 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>
                             </div>
                         </div>
                         <div class="tab-pane fade" id="goodsOutConfirm">
                             <form class="m" id="form-goods-out-confirm">
                                 <div class="row">
                                     <div class="col-sm-6 form-group">
                                         <label class="control-label">物料编码：</label>
                                         <div>
                                             <input type="text" id="goodsIdConfirm" name="goodsIdConfirm" placeholder="物料编码" class="form-control input-border-bottom">
                                         </div>
                                     </div>
                                     <div class="col-sm-6 form-group">
                                         <label class="control-label">数量：</label>
                                         <div>
                                             <input type="number" id="pickingNum" name="pickingNum" placeholder="数量" class="form-control input-border-bottom">
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-sm-6 form-group">
                                         <label class="control-label">批次号：</label>
                                         <div>
                                             <input type="text" id="putinIdConfirm" name="putinIdConfirm"  autocomplete="off" placeholder="批次号" 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="ctl" name="ctl" placeholder="托盘号" class="form-control input-border-bottom">
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-sm-6 form-group">
                                         <label class="control-label">生产日期：</label>
                                         <div>
                                             <input type="text" id="productionDate" name="productionDate" placeholder="生产日期" 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="expiryDate" name="expiryDate" placeholder="有效日期" class="form-control input-border-bottom"/>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-sm-6 form-group">
                                         <label class="control-label">创建时间：</label>
                                         <div>
                                             <input type="text" id="wareDate" name="wareDate" placeholder="创建时间" 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="locId" name="locId" placeholder="货位" class="form-control input-border-bottom"/>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-sm-6 form-group">
                                         <label class="control-label">供应商：</label>
                                         <div>
                                             <input type="text" id="providerId" name="providerId" placeholder="供应商" class="form-control input-border-bottom"/>
                                         </div>
                                     </div>
                                 </div>
                             </form>
                             <div class="row" style="padding-left: 10px;padding-right: 10px">
                                 <div class="col-sm-6">
                                     <button type="button" class="btn btn-lg btn-ctl-print" onclick="goodsOutTaskConfirm(4);return false;"><span class="btn-ctl-color">确认</span></button>
                                 </div>
                                 <div class="col-sm-6">
                                     <button type="button" class="btn btn-lg btn-ctl-clear" style="background-color: #e90835 !important;" onclick="goodsOutTaskConfirm(2);return false;"><span class="btn-ctl-color">回库</span></button>
                                 </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 outstatusDatas = [[${@dict.getType('out_status')}]];
        var prefix = ctx + "business/goodsScrap";
        var stockPrefix = ctx + "business/stock";
        var rightPanelRefreshHandler = null;

        $(function() {
            var options = {
                url: prefix + "/list",
                modalName: "报废单明细",
                showSearch: false,
                showToggle: false,
                showRefresh: false,
                showColumns: false,
                search: false,
                queryParams: queryParams,
                columns: [
                {
                    field: 'goodId',
                    title: '物料代码'
                },
                {
                    field: 'putinId',
                    title: '批次号'
                },
                {
                    field: 'ctl',
                    title: '托盘号'
                },
                {
                    field: 'locId',
                    title: '货位'
                },
                {
                    field: 'productionDate',
                    title: '生产日期'
                },
                {
                    field: 'expiryDate',
                    title: '有效日期'
                },
                {
                    field: 'wareDate',
                    title: '创建时间'
                },
                {
                    field: 'pickingNum',
                    title: '数量'
                },
                {
                    field: 'outstatus',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(outstatusDatas, value);
                    }
                }]
            };
            $.table.init(options);
        });

        function queryParams(params) {
            var search = $.table.queryParams(params);
            search.outstatus = "0";
            search.outStand = $("#outStand").val();
            // 查询报废任务
            search.ckType = "1";
            return search;
        }

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

        function rightPanelRefresh() {
            // 页面数据清除
            clear();

            if (rightPanelRefreshHandler != null) {
                clearInterval(rightPanelRefreshHandler);
            }

            // 右侧出库确认页面，每间隔5秒刷新
            rightPanelRefreshHandler =  setInterval(function () {
                $.ajax({
                    type: "GET",
                    url: ctx + "business/goodsScrap/queryByCtl",
                    success: function (res) {
                        if (res.code == "0") {

                            // // 工单号
                            // $("#outId").val(res.data['outId']);
                            // 物料编码
                            $("#goodsIdConfirm").val(res.data['goodId']);
                            // 托盘号
                            $("#ctl").val(res.data['ctl']);
                            // 生产日期
                            $("#productionDate").val(res.data['productionDate']);
                            // 数量
                            $("#pickingNum").val(res.data['pickingNum']);
                            // 有效日期
                            $("#expiryDate").val(res.data['expiryDate']);
                            // 供应商
                            $("#providerId").val(res.data['providerId']);
                            // 创建时间
                            $("#wareDate").val(res.data['wareDate']);
                            // 货位
                            $("#locId").val(res.data['locId']);

                            clearInterval(rightPanelRefreshHandler);
                        }
                    },
                    error: function (e) {
                        console.error(e);
                    }
                });
            }, 5000)
        }

        rightPanelRefresh();

        // 数据清空
        function clear() {
            // 工单号
            // $("#outId").val("");
            // 物料编码
            $("#goodsIdConfirm").val("");
            // 托盘号
            $("#ctl").val("");
            // 批次号
            $("#putinIdConfirm").val("");
            // 生产日期
            $("#productionDate").val("");
            // 数量
            $("#pickingNum").val("");
            // 有效日期
            $("#expiryDate").val("");
            // 供应商
            $("#providerId").val("");
            // 创建时间
            $("#wareDate").val("");
            // 货位
            $("#locId").val("");
        }

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

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

            if($("#pickNum").val() == null || $("#pickNum").val() == "" ||  $("#pickNum").val() == 0) {
                $.modal.msgError('请输入报废数量');
                return;
            }

            if($("#putinId").val() == null || $("#putinId").val() == "") {
                $.modal.msgError('请输入产品批号');
                return;
            }
            goodsOutOrder()

        }

        /**
         *  出库单生成
         */
        function goodsOutOrder () {
            $.ajax({
                type: "post",
                url: ctx + "business/orderdetail/add",
                data: {
                    "goodId": $("#goodsId").val(),
                    "pickNum": $("#pickNum").val(),
                    "outStand": $("#outStand").val(),
                    "putinId": $("#putinId").val(),
                    "ckType": "1"
                },
                success: function (data) {
                    if (data.code == 0) {
                        $.modal.msgSuccess('已提交报废出库任务请求')
                        // 开启页面刷新
                        rightPanelRefresh();
                    } else {
                        $.modal.msgError('发生系统错误，提交报废出库请求失败')
                    }
                },
                error: function () {
                    $.modal.msgError('发生系统错误，提交报废出库请求失败')
                }
            });
        }

        /**
         * 出库物料正确，更新任务状态
         */
        function goodsOutTaskConfirm(result) {
            if($("#goodsIdConfirm").val() == null || $("#goodsIdConfirm").val() == "" ) {
                $.modal.msgError('出库记录为空，不能操作');
                return;
            }

            $.ajax({
                type: "post",
                url: ctx + "business/goodsScrap/updateResult",
                data: {
                    "goodId": $("#goodsIdConfirm").val(),
                    "outstatus": result,
                    "ctl": $("#ctl").val(),
                    "putinId": $("#putinIdConfirm").val(),
                    "outstand": $("#outStand").val()
                },
                success: function (res) {
                    if (res.code == "0") {
                        $.modal.msgSuccess('报废出库结果更新完成')
                        //  右侧出库确认页面刷新
                        rightPanelRefresh();
                    } else {
                        $.modal.msgError('发生系统错误，报废出库结果更新失败')
                    }
                },
                error: function (e) {
                    console.log(e);
                    $.modal.msgError('发生系统错误，报废出库结果更新失败')
                }
            });
        }

        function openGoodsList() {
            var options = {
                title: '选择物料',
                url: stockPrefix + "/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;
            }
            $('#goodsId').val(rows.join());
            $('#goodsId').focus();
            $.modal.close(index);
        }

    </script>
</body>
</html>