<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html>
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
    <base href="#springUrl('/assets/adminlte/')"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>工作台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="plugins/ionicons/css/ionicons.min.css">

    <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">

    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
    <!-- Morris chart -->
    <link rel="stylesheet" href="plugins/morris/morris.css">
    <!-- jvectormap -->
    <link rel="stylesheet" href="plugins/jvectormap/jquery-jvectormap-1.2.2.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <link rel="stylesheet"
          href="plugins\bootstrap-fileinput\fileinput.min.css">
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
    <link rel="stylesheet"
          href="plugins/ztree/css/metroStyle/metroStyle.css" type="text/css"/>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>

    <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">


<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">
        <div id="importExcelDiv"></div>

        <section class="content-header">
            <h1>
                促销品管理
                <small>促销品</small>
            </h1>
            <ol class="breadcrumb">
                <li><a><i class="fa fa-dashboard"></i>首页</a></li>
                <li><a class="active">促销品管理列表</a></li>
            </ol>
        </section>


        <!-- Main content -->
        <section class="content">
            <div class="row">
                #if($shiro.lacksRole("Promotional:items:distri"))
                <div class="col-md-2">
                    <div class="box">

                        <div class="box-body">
                            <div class="zTreeDemoBackground left">
                                <ul id="tree" class="ztree"></ul>
                            </div>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /.box -->
                </div>
                #end
                <div

                        #if($shiro.lacksRole("Promotional:items:distri"))
                        class="col-xs-10"
                        #end
                        #if($shiro.hasRole("Promotional:items:distri"))
                        class="col-xs-12"
                        #end
                >
                    <div class="box">
                        <div class="box-header">
                            <form id="seachTableForm" action="#springUrl('/a/appitems/exportExcel')" method="get">
                                <div class="col-xs-2">
                                    <input type="text" class="form-control required" name="name" placeholder="促销品名称">
                                    <input name="typeform" value="$!{typeform}" hidden="true">
                                    <input name="useId" style="display: none;">
                                </div>
                                <div class="col-xs-5">
                                    <a href="javascript:void(0)" class="btn btn-primary search">搜索</a>
<!--                                    <a href="#springUrl('/a/typeAppForm/exportAppitemsExcel?typeform=')$!{typeform}" class="btn btn-primary">导出促销品报表</a>-->
                                    #if($shiro.hasPermission("qy:appitems:supplierStatics"))
                                    <a href="#springUrl('/a/appitems/exportSupplierExcel')" class="btn btn-primary">供应商剩余货量实时统计</a>
                                    #end
                                    #if($shiro.hasPermission("qy:appitems:edit"))
                                    <a href="#springUrl('/a/appitems/form?typeform=')$!{typeform}" class="btn btn-primary">新增</a>
                                    <button  class="btn btn-primary">导出</button>
                                    <a onclick="importExcel();" class="btn btn-primary">导入</a>
                                    #end
                                </div>
                            </form>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <table id="table" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <td hidden="true">Id</td>
                                    <th>编码</th>
                                    <th>申请用途</th>
                                    <th>是否上架</th>
                                    <th>促销品名称</th>
                                    <th>促销品规格</th>
                                    <th>管理员库存</th>
                                    <th>虚拟库存</th>
                                    <th>供应商货量</th>
                                    <th>促销品单价</th>
                                    <th>促销品图片</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="tablebody">
                                </tbody>

                            </table>
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->


    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div><!-- ./wrapper -->
<!-- jQuery 2.1.4 -->
<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/extensions/i18n/lanauage_ch.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script src="plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.min.js"></script>
<!--fileinput js-->
<script src="plugins\bootstrap-fileinput\fileinput.js"></script>
<script src="plugins/bootstrap-fileinput/zh.js"></script>
<script src="plugins/bootstrap-fileinput/zh2.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<script type="text/javascript"
        src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="common/js/cfapp.js"></script>
<script src="plugins/ztree/js/jquery.ztree.all.min.js"
        type="text/javascript"></script>
<script>
    var csrfheader = {name: '_csrf_header', value: '${_csrf.headerName}'};
    var csrftoken = {name: '_csrf', value: '${_csrf.token}'};
</script>
<script>
    function formatDates(now) {
        var now = new Date(now);
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return year + "-" + month + "-" + date + "   " + hour + ":"
            + minute + ":" + second;
    }

    function seachTable() {
        var sSource = "#springUrl('/a/appitems/listData')";
        var aoData = {
            iDisplayStart: 1,
            iDosplayLength: 10
        }
        var retrieveData = function (sSource, aoData, fnCallback) {

            $("#seachTableForm input").each(function () {
                var params = {
                    name: $(this).attr("name"),
                    value: $(this).val()
                };
                aoData.push(params);
            })

            $.ajax({
                "type": "GET",
                "url": sSource,
                "dataType": "json",
                "data": aoData, //以json格式传递
                "success": fnCallback
            });
        };
        //localStorage.clear();
        $('#table').DataTable({
            "lengthChange": false,
            "searching": false,
            "ordering": false,
            "bFiltered": false,
            "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": sSource,
            "fnServerData": retrieveData,
            "pagingType": "full_numbers",
            "initComplete":function(){
                var isSupplier = '$shiro.hasRole("Promotional:items:distri")';
                if(isSupplier=='true'){//供应商管理员隐藏相关列
                    $("#table").dataTable().fnSetColumnVis( 1, false);//编码列
                    $("#table").dataTable().fnSetColumnVis( 2, false);//申请用途
                    $("#table").dataTable().fnSetColumnVis( 3, false);//是否上架
                    $("#table").dataTable().fnSetColumnVis( 6, false);//管理员库存列
                    $("#table").dataTable().fnSetColumnVis( 7, false);//虚拟库存列
                    $("#table").dataTable().fnSetColumnVis( 12, false);//操作列
                }
            },
            "columns": [
                {
                    "data": "id",
                    "title":"id"
                },
                {
                    "data":"number",
                    "title": "编码"
                },
                {
                    "data": "useName",
                    "title":"申请用途"
                },
                {
                    "data": "isOnline",
                    "title":"是否上架"
                },
                {
                    "data": "name",
                    "title":"促销品名称"
                },
                {
                    "data": "specifications",
                    "title":"促销品规格"
                },
                {
                    "data": "authenticStock",
                    "title":"管理员库存"
                },
                {
                    "data": "inventedStock",
                    "title":"虚拟库存"
                },
                {
                    "data": "price",
                    "title":"促销品单价"
                },
                {
                    "data": "disStock",
                    "title":"供应商货量"
                },
                {
                    "data": "cover",
                    "title":"促销品图片"
                },
                {
                    "data": "createTime",
                    "title":"创建时间"
                },
                {
                    "data": "id","title":"操作"
                }],
            "aoColumnDefs": [
                { // set default column settings
                    'visible': false,
                    'targets': [0]
                },

                {
                    "aTargets": [1],
                    "mData": "number",
                    "mRender": function (a, b, c, d) {
                        return '<a href="#springUrl("/a/appitems/form?id=' + c.id + '")" data-id="' + c.id + '" data-action="view">' + a + '</a>';

                    }
                }, {
                    "aTargets": [3],
                    "mData": "isOnline",
                    "mRender": function (a, b, c, d) {
                        if(a==0){
                            return "<font color='red'>未上架</font>";
                        }else{
                            return "<font color='green'>已上架</font>";
                        }

                    }
                },{
                    "aTargets": [10],
                    "mData": "cover",
                    "mRender": function (a, b, c, d) {
                       return "<img width='80' src='"+a+"'/>";
                    }
                },


                {
                    "aTargets": [11],
                    "mData": "createTime",
                    "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                        return formatDates(a, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                {
                    "aTargets": [12],
                    "mData": "id",
                    "mRender": function (a, b, c, d) {//a表示id对应的值，c表示当前记录行对象
                        var html = '#if($shiro.hasPermission("qy:appitems:edit"))';//
                        html += '<a href="#springUrl("/a/appitems/form?id=' + a + '")" data-id="' + a + '" data-action="view" class="btn green">修改</a>';
                        html += '<a href="javascript:void(0);" data-id="' + a + '" data-action="remove" onclick="removeData(' + a + ')" class="btn red">删除</a>';
                        html += '#end';
                        return html;
                    }
                }

            ]
        });
    }

    jQuery(document).ready(function () {
        seachTable();
        $('.datepicker').datepicker({
            show: true,
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: 'zh-CN',
            todayBtn: 'linked',
            clearBtn: 'linked'

        });
        $('.search').click(function () {
            $("#table").dataTable().fnClearTable();
        });
        initMenuTree();
    });

    Cfapp.init();

    function removeData(data) {
        Cfapp.confirm({
            message: "确定要删除吗",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    type: "POST",
                    url: "#springUrl('/a/appitems/delete')",
                    data: {id: data,_csrf_header:csrfheader.value,_csrf:csrftoken.value},
                    dataType: "json",
                    success: function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "删除成功",
                                btntext: "确定",
                                success: function () {
                                    location.href = "#springUrl('/a/appitems/list')";
                                }
                            });
                        }
                    },
                    error: function () {
                    }
                })
            },
            cancel: function () {
                $(".modal-backdrop").fadeOut();
            }
        });
    }

    function importExcel() {
        var templateExcelUrl = "#springUrl('/a/appitems/templateExcel')";
        var importExcelUrl = "#springUrl('/a/appitems/importExcel')?_csrf_header=" + csrfheader.value + "&_csrf=" + csrftoken.value+"&typeform=$!{typeform}";
        Cfapp.importExcel({
            title: '申请项导入',
            importurl: importExcelUrl,
            templateurl: templateExcelUrl,
            cancel: function () {

            },
            success: function () {
                $('.search').click();
            }
        });
    }

    function initMenuTree() {
        var menuTree = $('#tree');
        var articleTreeUri = "#springUrl('/a/appitems/getfetchDataChild')";
        var getMenuTreeData = function () {
            $.ajax({
                "type": "POST",
                "url": articleTreeUri,
                "data": {_csrf_header:csrfheader.value,_csrf:csrftoken.value},
                "dataType": "json",
                "success": buildOrgTree
            });
        };
        var buildOrgTree = function (data) {
            var setting = {
                data: {simpleData: {enable: true}}, callback: {
                    beforeClick: function (id, node) {
                        var menuid = node.id;
                        if (menuid == 0) {
                            $("input[name='useId']").val('');
                        } else {
                            $("input[name='useId']").val(menuid);
                        }
                        $("#table").dataTable().fnClearTable();
                    }
                }
            };
            var zNodes = data.aaData;
            tree = $.fn.zTree.init(menuTree, setting, zNodes);

            tree.expandAll(true);
            tree.expandNode(tree.getNodesByFilter(function (node) {
                return node.level == 0;
            }, true), true, false, false);
        };
        getMenuTreeData();
    }


</script>

</body>
</html>
