<!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" style="height: auto !important;">
        <div class="col-sm-12 pr0 pl0 shadow">
            <div class="ibox float-e-margins ">
                <div class="ibox-title">
                    <div class="col-sm-10  pl0 pr0">
                        <h8>货位库存表</h8>
                    </div>
                    <div class="col-sm-2 pl20">
                        <a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="stsDescribe()">货位使用情况</a>
                        <a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.table.exportExcel()"
                           shiro:hasPermission="business:inreport:export">导出货位库存表</a>
                    </div>
                </div>
                <div class="ibox-content ibox-content-1" style="margin: 0; height: 200px">
                    <div class="btn-group-sm" id="toolbar" role="group">
                        <form id="formId">
                            <div class="select-list">
                                <ul>
                                    <li>
                                        选择库存状态：
                                        <select id="sts" name="sts" th:with="type=${@dict.getType('stock_sts')}"
                                                onchange="$.table.search('formId')">
                                            <option text="" value="" selected></option>
                                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                                    th:value="${dict.dictValue}"></option>
                                        </select>
                                    </li>
                                    <li>
                                        <label>物料编码：</label>
                                        <input type="text" name="goodsId"/>
                                    </li>
                                    <li>
                                        <a class="btn btn-primary btn-rounded btn-sm"
                                           onclick="$.table.search('formId')"><i
                                                class="fa fa-search"></i>&nbsp;搜索</a>
                                        <a class="btn btn-warning btn-rounded btn-sm"
                                           onclick="$.form.reset('formId')"><i
                                                class="fa fa-refresh"></i>&nbsp;重置</a>
                                    </li>
                                </ul>
                            </div>
                        </form>
                    </div>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('business:stockReport:edit')}]];
    var removeFlag = [[${@permission.hasPermi('business:stockReport:remove')}]];
    var prefix = ctx + "business/stockReport";
    var statusDatas = [[${@dict.getType('stock_sts')}]];

    /**
     * 表格初始化
     */
    $(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: true,
            search: false,
            // TODO
            columns: [
                {
                    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: 'shelvesNum',
                    title: '数量',
                    sortable: true
                },
                {
                    field: 'ctl',
                    title: '托盘号',
                    sortable: true
                },
                {
                    field: 'locationId',
                    title: '库位',
                    sortable: true
                },
                {
                    field: 'providerId',
                    title: '供应商编码',
                    sortable: true
                },
                {
                    field: 'sts',
                    title: '状态',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (value == "0") {// 合格品
                            actions.push('<button id="scan" class="btn btn-default btn-xs" onclick="changeStatus(' + row.goodsId + ', \'' + row.produclotid + '\', ' + row.ctl + ', ' + value + ')">合格</button>');
                        } else if (value == "1") {// 不合格品
                            actions.push('<button id="scan" class="btn btn-danger btn-xs" onclick="changeStatus(' + row.goodsId + ', \'' + row.produclotid + '\', ' + row.ctl + ', ' + value + ')">不合格</button>');
                        } else if (value == "2") {// 延期
                            actions.push('<button id="scan" class="btn btn-warning btn-xs" onclick="changeStatus(' + row.goodsId + ', \'' + row.produclotid + '\', ' + row.ctl + ', ' + value + ')">延期</button>');
                        } else if (value == "3") {// 报废
                            actions.push('<button id="scan" class="btn btn-success btn-xs">报废</button>');
                        } else if (value == "4") {// 锁定
                            actions.push('<button id="scan" class="btn btn-info btn-xs">锁定</button>');
                        } else if (value == "5") {// 待检
                            actions.push('<button id="scan" class="btn btn-primary btn-xs" onclick="changeStatus(' + row.goodsId + ', \'' + row.produclotid + '\', ' + row.ctl + ', ' + value + ')">待检</button>');
                        }
                        return actions.join('');
                    },
                    sortable: true
                },
                {
                    title: '延期可出工单',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.sts == "2") {
                            actions.push('<button id="scan" class="btn btn-warning btn-xs" onclick="checkGoodsOut(' + row.goodsId + ', \'' + row.produclotid + '\', ' + row.ctl + ')">填写工单</button>');
                        }
                        return actions.join('');
                    },
                    align: 'center'
                }
            ]
        };
        $.table.init(options);
    });

    /**
     * 货位使用情况
     */
    function stsDescribe() {
        // TODO
        $.modal.open(
            "货位使用情况",
            prefix + "/stsDescribe",
            500,
            400,
            null);
    }

    /**
     * 更改库存状态
     */
    function changeStatus(goodsId, produclotid, ctl, sts) {
        let stock = encodeURIComponent(JSON.stringify({
            "goodsId": goodsId,
            "produclotid": produclotid,
            "ctl": ctl,
            "sts": sts
        }));
        var options = {
            width: '550',
            height: '400',
            title: '更改库存状态',
            url: prefix + "/changeStockSts/" + stock,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero){
        var body = $.modal.getChildFrame(index);
        var mark = body.find('#mark').val();
        var goodsId = body.find('#goodsId').val();
        var produclotid = body.find('#produclotid').val();
        var sts = body.find("input[name='sts']:checked").val();
        var expirationDate = body.find("#expirationDate").val();
        var ctl = body.find('#ctl').val();
        const requestData = {
            "goodsId": goodsId,
            "produclotid": produclotid,
            "ctl": ctl,
            "sts": sts,
            "expirationDate": expirationDate,
            "mark": mark
        }
        $.ajax({
            type: "post",
            url: prefix + "/updateStockSts",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(requestData),
            success: function (data) {
                if (data.code == 0) {
                    $.modal.msgSuccess(data.msg)
                } else {
                    $.modal.msgError(data.msg)
                }
                $.table.search('formId');
            },
            error: function () {
                $.modal.msgError('状态更新错误')
                $.table.search('formId');
            }
        });
        $.modal.close(index);
    }
</script>
</body>
</html>
<style>
    .inner_row_style {
        margin-left: 0px;
        margin-right: 0px;
        padding-top: 5px;
    }
</style>