<!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">
                    <div class="select-list">
                        <ul>
                            <li >
                                <p>商品名称：</p>
                                <input type="text" name="name"/>
                            </li>

                            <li th:if="${isSystemUser}">
                                <input name="companyId" type="hidden" id="input_companyId">
                                <p >所属商户：</p>
                                <input name="companyName" onclick="selectTree('company')" id="input_companyTreeName" type="text" placeholder="选择商户"
                                           class="form-control required" readonly="readonly" required>
                            </li>
                            <li>
                                <p>所属类型：</p>
                                <input name="typeId" type="hidden" id="input_typeId">
                                <input name="typeName" onclick="selectTree('type')" id="input_typeTreeName" type="text" placeholder="选择商品分类"
                                       class="form-control required" readonly="readonly" required>
                            </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">
                <a th:if="@{!${isSystemUser}}" class="btn btn-primary" onclick="$.operate.addFull()" shiro:hasPermission="goods:info:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a th:if="@{!${isSystemUser}}" class="btn  btn-success" onclick="putawayAll(1)" shiro:hasPermission="goods:info:putaway">
                    <i class="fa fa-plus"></i> 上架
                </a>
                <a th:if="@{!${isSystemUser}}" class="btn btn-warning" onclick="putawayAll(0)" shiro:hasPermission="goods:info:putaway">
                    <i class="fa fa-plus"></i> 下架
                </a>
            </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 th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('goods:info:edit')}]];
        var putawayFlag = [[${@permission.hasPermi('goods:info:putaway')}]];
        var removeFlag = [[${@permission.hasPermi('goods:info:remove')}]];
        var putawayDict = [[${@dict.getType('goods_putaway')}]];
        var isSystemUser = [[${isSystemUser}]];
        var prefix = ctx + "goods/info";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "商品基础详情",
                columns: [{
                    checkbox: true
                },
                {
                    field : 'id', 
                    title : '上架标志',
                    visible: false
                },
                {
                    field : 'name', 
                    title : '商品名称'
                },
                {
                    field : 'companyId',
                    visible: false
                },
                {
                    field : 'companyName',
                    title : '所属商户'
                },
                {
                    field : 'typeName',
                    title : '所属类型',
                },
                {
                    field : 'typeId', 
                    title : '所属类型id',
                    visible: false
                },
                {
                    field : 'price', 
                    title : '一口价'
                },
                {
                    field : 'cover', 
                    title : '封面图片',
                    formatter: function(value, row, index) {
                        // 图片预览（注意：如存储在本地直接获取数据库路径，如有配置context-path需要使用ctx+路径）
                        // 如：/profile/upload/2019/08/08/3b7a839aced67397bac694d77611ce72.png
                        return $.table.imageView(value,250,250);
                    }
                },
                {
                    field : 'producedTime', 
                    title : '生产日期'
                },
                {
                    field : 'weight', 
                    title : '单重'
                },
                {
                    field : 'putawayFlag',
                    title: '上架标志',
                    width: '10%',
                    align: "left",
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(putawayDict, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];

                        if(!isSystemUser){
                            if(row.putawayFlag=="0"){
                                actions.push('<a class="btn btn-success btn-xs ' + putawayFlag + '" onclick="putaway('+row.id+',1)" shiro:hasPermission="goods:info:putaway"><i class="fa fa-edit"></i> 上架</a> ');
                                actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '"   href="javascript:void(0)" onclick="$.operate.editFull('+row.id+')" shiro:hasPermission="goods:info:putaway"><i class="fa fa-edit"></i> 编辑</a> ');
                                actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '"  onclick="$.operate.remove(\'' + row.id + '\')" shiro:hasPermission="goods:info:remove"><i class="fa fa-remove"></i>删除</a> ');
                            }else if(row.putawayFlag=="1"){
                                actions.push('<a class="btn btn-warning btn-xs ' + putawayFlag + '" onclick="putaway('+row.id+',0)" shiro:hasPermission="goods:info:putaway"><i class="fa fa-download"></i> 下架</a> ');
                                actions.push('<a class="btn btn-primary btn-xs " onclick="$.modal.openFull(\'查看详情\', \''+prefix + '/edit/'+row.id+'?isShow=1\')" shiro:hasPermission="goods:info:putaway"><i class="fa fa-edit"></i>查看详情</a> ');
                            }

                        }else{
                            actions.push('<a class="btn btn-primary btn-xs " onclick="$.modal.openFull(\'查看详情\', \''+prefix + '/edit/'+row.id+'?isShow=1\')"  shiro:hasPermission="goods:info:putaway"><i class="fa fa-edit"></i>查看详情</a> ');
                        }

                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        //批量上下架
        function putawayAll(putawayFlag){
            var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要上/下架选中的" + rows.length + "条数据吗?", function() {
                var url = $.table._option.removeUrl;
                putaway(rows.join(),putawayFlag);
            });

        }
        function putaway(id,putaway){
            var data = new FormData();
            data.append("ids", id);
            data.append("putawayFlag", putaway);
            $.ajax({
                type: "POST",
                url: prefix + "/putaway",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $.table.refresh()
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            })
        }
        /***************选择商户******************/
        function selectTree(name) {
            var url
            var cb;
            if(name=="type"){
                cb = selectType
                url  = ctx + "goods/type/selectTypeTree/0";
            }else if(name=="company"){
                cb= selectCompany
                url  = ctx + "company/tree";
            }

            var options2 = {
                title: '请选择',
                width: "800",
                url: url,
                callBack: cb
            };
            $.modal.openOptions(options2);
        }
        function selectCompany(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#input_companyId").val(body.find('#treeId').val());
            $("#input_companyTreeName").val(body.find('#treeName').val());
            layer.close(index);
            $.table.search()
        }
        function selectType(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#input_typeId").val(body.find('#treeId').val());
            $("#input_typeTreeName").val(body.find('#treeName').val());
            layer.close(index);
            $.table.search()
        }


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