<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
    .table-striped-left {
        width: 49%;
        /*margin-right: 4px;*/
    }

    .table-striped-right {
        /*right: -10px;*/
        width: 50%;
        float: right;
    }

    .info_text {
        float: right;
    }

    .info_text li {
        font-size: 16px;
    }

    .info_text li span {
        font-size: 20px;
        font-weight: bold;
        color: rgb(28, 132, 198);
    }
</style>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 select-info">
            <form id="dept-form">
                <div class="select-list">
                    <ul>
                        <li>
                            出库单号:<input type="text" id="code" name="code" th:value="${code}"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" id="list-btn"><i class="fa fa-search"></i>&nbsp;搜索</a>
                        </li>
                    </ul>
                    <ul class="info_text">
                        <li>物料总数:<span id="shipment_length"></span></li>
                        <li>总单据数量:<span id="qty_length"></span></li>
                        <li>已出库数量:<span id="qtyCompleted_length"></span></li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-info table-striped-left" style="padding-top: 20px;">
            <table id="bootstrap-table" data-mobile-responsive="true" class="table table-bordered  table-hover"></table>
        </div>
        <div class="col-sm-12 select-info table-striped-right">
            <ul class="select-list">
                <li style="display: none">id:<input type="text" id="detailId"/></li>
                <li>出库数量:<input type="text" id="shipNum"/></li>
            </ul>
            <table id="bootstrap-table1" data-mobile-responsive="true" class="table table-bordered table-hover"></table>
            <div class="btn-group hidden-xs" id="toolbar1" role="group">
                <a class="btn btn-outline btn-danger btn-rounded" onclick="batchRemoveHead()">
                    <i class="fa fa-trash-o"></i> 取消配盘
                </a>
                <a class="btn btn-outline btn-warning btn-rounded" onclick="batchCreateTask()">
                    <i class="fa fa-edit"></i> 生成任务
                </a>
                <!--<a class="btn btn-outline btn-primary btn-rounded" onclick="floorCombination()">
                    <i class="fa fa-edit"></i> 平库生成任务
                </a>-->
                <a class="menuItem btn btn-outline btn-warning btn-rounded" onclick="jumpReceiptTask()">
                    <i class="fa fa-edit"></i> 跳转任务页面
                </a>
            </div>
            <table id="bootstrap-table2" data-mobile-responsive="true"
                   class="table table-bordered table-hover text-nowrap"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let containerHeaderStatus = [[${@dict.getType('shipmentContainerHeaderStatus')}]];
    let inventoryStatus = [[${@dict.getType('inventorySts')}]];

    let prefix = ctx + "shipment/shippingCombination";

    let list_select = function (code) {
        if ($.common.isEmpty(code)) {
            return
        }
        $.ajax({
            url: prefix + "/listShipmentDetail?code=" + code,
            type: 'get',
            success: function (value) {
                let qty_shipment = 0;
                let qtyCompleted_shipment = 0;
                $("#bootstrap-table").bootstrapTable('removeAll');
                if (value.data) {
                    $("#bootstrap-table").bootstrapTable('load', value.data);
                    for (let i = 0; i < value.data.length; i++) {
                        qty_shipment = qty_shipment + value.data[i].qty;
                        qtyCompleted_shipment = qtyCompleted_shipment + value.data[i].taskQty;

                    }
                    $("#shipment_length").text(value.data.length);
                    $("#qty_length").text(qty_shipment);
                    $("#qtyCompleted_length").text(qtyCompleted_shipment);
                } else {
                    console.log("出库单不存在!")
                }
            }
        });
    };

    $("#bootstrap-table").bootstrapTable({
        contentType: "application/x-www-form-urlencoded",
        // search: true, //搜索
        // showRefresh: true,//刷新
        // showToggle:true, //视图切换
        clickToSelect: true,
        // showColumns:true, //列选择
        // detailView:true,
        // showExport: true, //导出
        // exportDataType: "all", //导出类型basic', 'all', 'selected'.当前页、所有数据、选中数据
        modalName: "出库配盘头",
        // sidePagination: "server",
        pagination: true,   // 是否显示分页(*)
        pageNumber: 1,                                      // 初始化加载第一页,默认第一页
        pageSize: 50,                                       // 每页的记录行数(*)
        pageList: [10, 25, 50],
        onClickRow: function (row, ele, field) {
            let code = $("#code").val();
            $("#detailId").val(row.id);
            $("#shipNum").val(row.qty - row.taskQty);
            $.ajax({
                url: prefix + '/getInventory',
                type: 'post',
                data: {
                    code: code,
                    id: row.id
                },
                success: res => {
                    if (res.code === 200) {
                        $("#bootstrap-table1").bootstrapTable('load', res.data)
                    } else {
                        $.modal.msgWarning(res.msg)
                    }
                }
            })
        },
        columns: [
            {
                field: 'materialCode',
                title: '物料编码'
            },
            {
                field: "materialName",
                title: "物料名称"
            },
            {
                field: 'materialSpec',
                title: "物料规格"
            },
            {
                field: 'materialUnit',
                title: '物料单位'
            },
            {
                field: 'inventorySts',
                title: '库存状态',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(inventoryStatus, value);
                },
            },
            {
                field: 'qty',
                title: '单据数量'
            },
            {
                field: 'taskQty',
                title: '已出数量'
            },
        ]
    });

    $("#bootstrap-table1").bootstrapTable({
        clickToSelect: true,
        showColumns: true, //列选择
        modalName: "库存",
        iconSize: "outline",
        toolbar: "#toolbar",
        contentType: "application/x-www-form-urlencoded",
        pagination: true,   // 是否显示分页(*)
        pageNumber: 1,                                      // 初始化加载第一页,默认第一页
        pageSize: 10,                                       // 每页的记录行数(*)
        pageList: [10, 25, 50],
        columns: [
            {
                field: 'locationCode',
                title: '库位编号'
            },
            {
                field: 'containerCode',
                title: '容器编号'
            },
            {
                field: 'qty',
                title: '数量'
            },
            {
                field: 'materialCode',
                title: '物料编码'
            },
            {
                field: 'materialName',
                title: '物料名称'
            },
            {
                field: 'materialSpec',
                title: '物料规格'
            },
            {
                field: "materialUnit",
                title: "物料单位"
            },
            {
                field: 'inventorySts',
                title: '库存状态',
                align: 'center',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(inventoryStatus, value);
                },
            },
            {
                title: '操作',
                align: 'center',
                events: 'operateEvents',
                formatter: function (value, row, index) {
                    let actions = [];
                    actions.push('<a id="cancel" class="btn btn-success btn-xs" style="background-color: #ac2925" href="#" onclick="combination(\'' + row.id + '\')"><i class="fa fa-edit"></i>配盘</a> ');
                    return actions.join('');
                }
            },
        ]
    });

    $("#bootstrap-table2").bootstrapTable({
        removeUrl: ctx + "shipment/shipmentContainerDetail/remove",
        // search: true, //搜索
        showRefresh: true, //刷新
        showToggle: true, //视图切换
        clickToSelect: true,
        showColumns: true, //列选择
        // detailView:true,
        toolbar: "#toolbar1",
        showExport: true, //导出
        exportDataType: "all",  //导出类型basic', 'all', 'selected'.当前页、所有数据、选中数据
        modalName: "出库配盘",
        iconSize: "outline",
        contentType: "application/x-www-form-urlencoded",
        onRefresh: function () {
            list_shipmentInfo($("#code").val());
        },
        columns: [
            {
                checkbox: true,
                formatter: function (value, row, index) {
                    if (row.status < 10) {
                        return {
                            checked: true,
                            disabled: false
                        }
                    } else {
                        return {
                            checked: false,
                            disabled: true
                        }
                    }
                }
            },
            {
                field: 'containerCode',
                title: '容器编号'
            },
            {
                field: 'locationCode',
                title: '库位编号'
            },
            {
                field: 'materialCode',
                title: '物料编码'
            },
            {
                field: "materialName",
                title: "物料名称"
            },
            {
                field: "materialSpec",
                title: "物料规格"
            },
            {
                field: "materialUnit",
                title: "物料单位"
            },
            {
                field: 'inventorySts',
                title: '库存状态',
                align: 'center',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(inventoryStatus, value);
                },
            },
            {
                field: 'qty',
                title: '数量'
            },
            {
                field: 'status',
                title: '配盘状态',
                align: 'center',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(containerHeaderStatus, value);
                }
            },
            {
                field: 'created',
                title: '创建时间'
            },
            {
                field: 'createdBy',
                title: '创建人'
            },
        ]
    });

    function list_shipmentInfo(code) {
        if ($.common.isEmpty(code)) {
            return
        }
        $.ajax({
            url: prefix + "/getShipmentInfoByCode",
            type: 'post',
            datatype: 'json',
            data: {
                code: code
            },
            error: function (response) {
            },
            success: function (value) {
                $("#bootstrap-table2").bootstrapTable('removeAll');
                if (value.data) {
                    $("#bootstrap-table2").bootstrapTable('load', value.data);
                }
            }
        })
    }

    /* 批量生成任务 */
    function batchCreateTask() {
        let rows = $("#bootstrap-table2").bootstrapTable('getSelections');
        if (rows.length === 0) {
            $.modal.msgWarning("请至少选择一条记录");
            return;
        }
        let ids = "";
        for (let i = 0; i < rows.length; i++) {
            if (ids === "") {
                ids = rows[i].shippingContainerId;
            } else {
                ids = ids + "," + rows[i].shippingContainerId
            }
        }
        chooseStation(ids);
    }

    function floorCombination() {
        let rows = $("#bootstrap-table2").bootstrapTable('getSelections');
        if (rows.length === 0) {
            $.modal.msgWarning("请至少选择一条记录");
            return;
        }
        let ids = "";
        for (let i = 0; i < rows.length; i++) {
            if (ids === "") {
                ids = rows[i].shippingContainerId;
            } else {
                ids = ids + "," + rows[i].shippingContainerId
            }
        }
        let url = ctx + "shipment/shipmentContainerHeader/floorCombination";
        let data = {"ids": ids};
        $.ajax({
            url: url,
            type: 'post',
            data: data,
            success: function (response) {
                if (response.code === web_status.SUCCESS) {
                    $("#bootstrap-table2").bootstrapTable('refresh', {
                        function() {

                        }
                    });
                } else {
                    $.modal.msgError(response.msg);
                }
                $.modal.closeLoading();
            }
        })
    }

    function batchRemoveHead() {
        let rows = $("#bootstrap-table2").bootstrapTable('getSelections');
        if (rows.length === 0) {
            $.modal.msgWarning("请至少选择一条记录");
            return;
        }
        let url = ctx + "shipment/shipmentContainerHeader" + "/remove";
        let ids = "";
        for (let i = 0; i < rows.length; i++) {
            ids = ids + rows[i].shippingContainerId + ","
        }
        let data = {"ids": ids.substring(0, ids.length - 1)};
        $.ajax({
            url: url,
            type: 'post',
            data: data,
            success: function (response) {
                if (response.code === 200) {
                    list_select($("#code").val());
                    list_shipmentInfo($("#code").val());
                    $.modal.msgSuccess('成功');
                } else {
                    $.modal.msg(response.msg)
                }
            }
        })
    }

    function combination(id) {
        let num = $("#shipNum").val();
        let shipmentDetailId = $("#detailId").val();
        $.ajax({
            url: ctx + "shipment/shippingCombination/addCombination",
            type: 'post',
            data: {
                shipmentDetailId: shipmentDetailId,
                inventoryDetailId: id,
                shipQty: num,
            },
            success: function (response) {
                if (response.code === 200) {
                    $.modal.msgSuccess('成功');
                    $("#list-btn").click();
                    list_shipmentInfo($("#code").val());
                } else {
                    $.modal.msg(response.msg)
                }
            }
        })
    }

    function chooseStation(id) {
        let url = ctx + "shipment/shippingCombination/chooseStation2/" + id;
        $.modal.open("选择站台", url, 800 ,400);
    }

    function submit(url, type, dataType, data, content) {
        $.modal.loading("正在处理中,请稍后...");
        let config = {
            url: url,
            type: type,
            dataType: dataType,
            data: data,
            contentType: content,
            success: function (result) {
                if (result.code === web_status.SUCCESS) {
                    $("#list-btn").click();
                } else {
                    $.modal.msgError(result.msg);
                }
                $.modal.closeLoading();
            }
        };
        $.ajax(config)
    }

    function refresh() {
        list_select($("#code").val());
        list_shipmentInfo($("#code").val());
    }

    function initPage() {
        let auto = localStorage.getItem("auto");
        let shipmentCode = localStorage.getItem("shipmentCode");
        $("#code").val(shipmentCode);
        if (parseInt(auto) === 2) {
            let url = prefix + "/autoCombinationAndCreateTask";
            let type = "post";
            let dataType = "json";
            let data = {"shipmentCode": shipmentCode};
            submit(url, type, dataType, data);
        } else if (parseInt(auto) === 1) {
            let url = prefix + "/autoCombination";
            let type = "post";
            let dataType = "json";
            let data = {"shipmentCode": shipmentCode};
            submit(url, type, dataType, data);
        } else {
            $("#list-btn").click();
        }
        localStorage.removeItem("shipmentCode");
        localStorage.removeItem("auto");
        list_shipmentInfo($("#code").val());
    }

    $(function () {
        $("#list-btn").click(function () {
            list_select($("#code").val());
            list_shipmentInfo($("#code").val());
        });
        initPage();
    });

    function jumpReceiptTask() {
        let url = ctx + "task/taskHeader?InternalTaskType=200";
        createMenuItem(url, "出库任务");
    }
</script>
</body>
</html>