<!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('商品库存列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input id="mercId" name="mercId" type="hidden" th:value="${mercId}">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>商品名称：</label>
                                <input type="text" name="prodName"/>
                            </li>
                            <li>
                                <label>属性名称：</label>
                                <input type="text" name="attrName"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <!-- 增加库存区域 -->
     <script id="addInventoryDiv" type="text/template">
         <div class="col-xs-offset-1">
             <form id="formAddInventory">
                 <div class="select-list col-sm-12" style="margin-top: 10px; display: inline-block;">
                     <div style="float: left; width: 100px; line-height: 30px">
                         <label>库存数量：</label>
                     </div>
                     <div style="float: left; width: 200px;">
                         <input type="text" class="form-control" id="quantity" readonly/>
                     </div>
                     <div style="float: left; width: 100px; line-height: 30px; margin-left: 20px;">
                         <label>可售数量：</label>
                     </div>
                     <div style="float: left; width: 200px">
                         <input type="text" class="form-control" id="availQty" readonly/>
                     </div>
                 </div>
                 <div class="col-sm-12 select-list" style="margin-top: 10px; display: inline-block;">
                     <div style="float: left; width: 100px; line-height: 30px">
                        <label>增加库存数量：</label>
                     </div>
                     <div style="float: left; width: 200px">
                         <input type="text" class="form-control" id="addInvQty" name="quantity"/>
                     </div>
                 </div>
             </form>
         </div>
     </script>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('business:prodInventory:edit')}]];
        var prefix = ctx + "business/prodInventory";

        $(function() {
            var options = {
                url: prefix + "/mercInventory/list",
                createUrl: prefix + "/mercInventory/add",
                removeUrl: prefix + "/remove",
                modalName: "商品库存",
                columns: [
                {
                    field: 'prodName',
                    title: '商品名称'
                },
                {
                    field: 'attrName',
                    title: '属性名称'
                },
                {
                    field: 'quantity',
                    title: '库存数量'
                },
                {
                    field: 'availQty',
                    title: '可售数量'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="addInventory(' + row.prodInventoryId + ',' + row.prodId + ',' + row.prodAttrId + ',' + row.quantity + ',' + row.availQty + ')"><i class="fa fa-edit"></i>配置库存</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function addInventory(prodInventoryId, prodId, prodAttrId, quantity, availQty) {
            var content = $('#addInventoryDiv').html();
            layer.open({
                type: 1,
                fix: false,
                maxmin: false,
                shade: 0.3,
                area: ['800px', '480px'],
                title: '添加标签',
                // 弹层外区域关闭
                shadeClose: true,
                success: function(layero, index) {
                    layero.find('#quantity').val(quantity);
                    layero.find('#availQty').val(availQty);
                },
                content: content,
                btn: ['<i class="fa fa-check"></i> 确定', '<i class="fa fa-remove"></i> 关闭'],
                btn1: function() {
                    var addInvQty = $("#addInvQty").val();
                    var url = prefix + "/mercInventory/add";
                    var data = {
                        "prodInventoryId": prodInventoryId,
                        "mercId": $("#mercId").val(),
                        "prodId": prodId,
                        "prodAttrId": prodAttrId,
                        "addInvQty": addInvQty,
                    };
                    $.post(url, data, function(result) {
                        if (result.code === web_status.SUCCESS) {
                            $.modal.closeAll();
                            $.modal.enable();
                            $.modal.alertSuccess("添加成功。");
                            refreshTab();
                        } else if (result.code === web_status.WARNING) {
                            $.modal.closeAll();
                            $.modal.enable();
                            $.modal.alertWarning(result.msg)
                        } else {
                            $.modal.closeAll();
                            $.modal.enable();
                            $.modal.alertError(result.msg);
                        }
                    });
                    layer.closeAll();
                }
            });
        }
    </script>
</body>
</html>