<!DOCTYPE html>
<html>
  <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">
    <!-- 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;">
    
     <section class="content-header">
          <h1>
             	菜单管理
            <small>菜单信息</small>
          </h1>

        </section>
    <div id="myModal" class="modal fade">
		<div class="modal-dialog">
			<div class="modal-content">
			<!-- dialog body -->
			<div class="modal-body">
				<button type="button" class="close" data-dismiss="modal">&times;</button>
				菜单同步中,请稍后......
			</div>
			<!-- dialog buttons -->
			</div>
		</div>
	</div>
    
        <!-- Main content -->
        <section class="content">
          <div class="row">
              <div class="col-md-3">
                  <div class="box">

                      <div class="box-body">
                          <div class="zTreeDemoBackground left">
                              <ul id="tree" class="ztree"></ul>
                          </div>
                      </div>
                      <!-- /.box-body -->
                  </div>
                  <!-- /.box -->
              </div>
            <div class="col-xs-9">
              <div class="box">
              <div class="box-header">
                <form id="seachTableForm" action="#springUrl('/a/mpMenu/menuform')" method="get">
<!--                 	<div class="col-xs-2"> -->
<!-- 						<input type="text" class="form-control required" placeholder="图文标题" name="title" value="$!{imageText.title}"> -->
<!-- 					</div>  -->
					<div class="col-xs-5">
						  #if($shiro.hasPermission("mp:menu:edit")) 
                  			 <a href="#springUrl('/a/mpMenu/menuform')"  class="btn bg-olive">新增菜单</a>
                   		  #end
                   		  <a href="javascript:void(0);"  onclick="synMenu()" class="btn bg-olive">同步菜单</a>
					</div>
                    <div class="col-xs-2" style="display: none">
                        <input type="text" class="form-control required"
                               name="id" id="menuid" placeholder="菜单Id">
                    </div>
                </form>
                </div>
                <!-- /.box-header -->
                <div class="box-body">
                  <table id="userlilst" class="table table-bordered table-striped">
                  		<thead>
                  			<th hidden="true">Id</th>
                  			<th>菜单名字</th>
                  			<th>菜单内容</th>
                  			<th>父级菜单名称</th>
                  			<th style="width:200px">返回内容</th>
                  			<th>排序值</th>
                  			<th>创建时间</th>
                  			<th>操作</th>
                  		</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 -->

<!-- END CONTAINER -->
<!-- BEGIN FOOTER -->
<!-- END FOOTER -->
    <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>
    <!-- FastClick -->
    <script src="plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="dist/js/demo.js"></script>
 <script src="plugins/bootbox/bootbox.min.js"></script>
 <script src="plugins/ztree/js/jquery.ztree.all.min.js"
         type="text/javascript"></script>
 <script src="common/js/cfapp.js"></script>

 <script>
     var csrfheader = {name:'_csrf_header',value:'${_csrf.headerName}'};
     var csrftoken  = {name:'_csrf',value:'${_csrf.token}'};
 </script>
<!-- END PAGE LEVEL PLUGINS -->
<script>
	function synMenu(){
        var warningMsg = Cfapp.warning({
            content: '正在同步中，请稍后!',  //
            title: "提示"
        });
		 $.ajax({
             "type": "POST",
             "url": "#springUrl('/a/mpMenu/sync')",
             "dataType": "json",
             "data": {"_csrf_header":csrfheader.value,"_csrf":csrftoken.value}, //以json格式传递
             "success":function(data){
                 warningMsg.destory();
            	if(data.errorNo==0){
            		 Cfapp.alert({
                         message: "同步成功",
                         btntext: "确定",
                         success: function () {
                         }
                     })
            	}else if(data.errorNo==2){
            		Cfapp.alert({
                        message: "一级菜单无回复内容时必须存在子菜单",
                        btntext: "确定",
                        success: function () {
                        }
                    })
            	}else{
            		Cfapp.alert({
                        message: "同步失败",
                        btntext: "确定",
                        success: function () {
                        }
                    })           		
            	}
             }
         });
	}
	
	
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   formatDateTimes(now)   {
	var now = new Date(now);
	 var   month=now.getMonth()+1;     
	    var   date=now.getDate();     
    return  month+"月"+date+"日";     
}


    var menuTreeUri = "#springUrl('/a/mpMenu/getfetchDataChild')";
    function initMenuTree() {
        var menuTree = $('#tree');

        var getMenuTreeData = function () {
            $.ajax({
                "type": "GET",
                "contentType": "application/json",
                "url": menuTreeUri,
                "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) {
                            $('#menuid').val('');
                        } else {
                            $('#menuid').val(menuid);
                        }
                        $("#userlilst").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();
    }



    jQuery(document).ready(function() {
        initMenuTree();//初始化菜单树






        var sSource = "#springUrl('/a/mpMenu/fetchData')";
     	 var aoData = {
   			   iDisplayStart:1,
   			   iDisplayLength:10
   	   		};
     	 var retrieveData = function (sSource, aoData, fnCallback) {
     		$("#seachTableForm input,.select2").each(function() {
				var params = {
					name : $(this).attr("name"),
					value : $(this).val()
				};
				aoData.push(params);
			})
             $.ajax({
                 "type": "GET",
                 "contentType": "application/json",
                 "url": sSource,
                 "dataType": "json",
                 "data": aoData, //以json格式传递
                 "success":fnCallback
             });
         };
     	  
     		      $('#userlilst').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",
     		            "fnDrawCallback":function(){
     		            },
     		            "aoColumns": [
     		                        {"mData": "id"},
     		                        {"mData": "name"},
     		                        {"mData": "sourceType"},
     		                        {"mData": "parentName"},
     		                        {"mData": "sourceContent"},
     		                        {"mData": "sort"},
     		                        {"mData": "createTime"},
     		                        ],
     		                    "aoColumnDefs": [
     		                        {  // set default column settings
     		                            'visible': false,
     		                            'targets': [0]
     		                        },{ 
	     		       					"aTargets" : [ 6 ],
	     		   						"mData" : "createTime",
	     		   						"mRender" : function(a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
	     		   							console.log(c); 
	     		   						return formatDates(a, "yyyy-MM-dd HH:mm:ss");
	     		   						} 
     		   						},{ 
	     		       					"aTargets" : [4 ],
	     		   						"mData" : "msgType",
	     		   						"mRender" : function(a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
	     		   							if(c.msgType=="2"){
                                                var HTML = "";
                                                if(c.newsItem){
                                                    c.newsItem=eval(c.newsItem);
                                                    for(var i =0;i<c.newsItem.length;i++){
                                                        HTML+="<p>"+(i+1)+":<a href='"+c.newsItem[i].url+"' target='_blank'>"+c.newsItem[i].title+"</a></p>";
                                                    }
                                                }
                                                return HTML;
	     		   							}else if(c.msgType=="3"){
	     		   								return "<font color='red'>文章消息</font>"
	     		   							}else{
                                                return c.sourceContent;
                                            }
	     		   						} 
     		   						},{  // set default column settings
     		   								"aTargets" : [ 2 ],
     		   								"mData" : "sourceType",
     		   								"mRender" : function(a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
     		   									if(a=="1"){
     		   										return "发送消息";
     		   									}else{
     		   										return "跳转网页"; 
     		   									}
     		   								}
     		                        },{
	     		       					"aTargets" : [ 7 ], 
	     		   						"mData" : "id",
	     		   						"mRender" : function(a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
	     		   						var viewHTML ="#if($shiro.hasPermission('mp:menu:view'))"+"<a href=#springUrl('/a/mpMenu/menuform?id="+a+"') >修改</a> "+"#end";
	     		     		             var removeHTML="#if($shiro.hasPermission('mp:menu:edit'))"+"<a href='javascript:void(0);' data-id='"+a+" data-action='remove' onclick='removeData("+a+")' class='btn red'>删除</a>"+"#end";
	     		     		             return viewHTML+removeHTML;
	     		   						}
     		   						}]//,
//      		                        {
//      		                            "aTargets": [8],
//      		                            "mData": "id",
//      		                            "mRender": function (a, b, c, d) {//a表示id对应的值，c表示当前记录行对象
     		                            	
//      		                                var viewHTML = '<a href="#" data-id="'+a+'" data-action="view" class="btn green">查看</a>';
//      		                                if(c.delflag==0){
//      		                                	 var removeHTML = '<a href="#" data-id="'+a+'" data-action="remove" class="btn red">删除</a>';
//      		                                }else{
//      		                                	 var removeHTML = '<a href="javascript:void(0)" data-id="'+a+'" data-action="remove" class="btn red" style="color:red">已取消关注</a>';
//      		                                }
//      		                                return viewHTML+removeHTML;
//      		                            }
//      		                        }
     		            
     		        });
     		      
     		      
     		     
     		     
		   		
    });
    function removeData(id){
		   Cfapp.confirm({ 
			 message: "确定要删除吗(包含子菜单)",
			 btnoktext:"确定",
			 btncanceltext:"取消",
			 success:function(){
				 $.ajax({
		              "type": "POST",
		              "url": "#springUrl('/a/mpMenu/remove')",
		              "dataType": "json",
		              "data": {id:id,"_csrf_header":csrfheader.value,"_csrf":csrftoken.value}, //以json格式传递
		              "success": function(data){
		            	  if(data.errorNo==0){
		            		  Cfapp.alert({
		                          message: "删除成功",
		                          btntext: "确定",
		                          success: function () {
		                        	 location.href = "#springUrl('/a/mpMenu/list')";
		                          }
		                      })
		            	  }else{
		            		  Cfapp.alert({
		                          message: "删除失败",
		                          btntext: "确定",
		                          success: function () {
		                        	  location.href = "#springUrl('/a/mpMenu/list')";
		                          }
		                      })
		            	  }
		              }
		          }); 
			 },
			cancel:function(){
				$(".modal-backdrop").fadeOut();

			 }
		   })
	   }
</script>
</body>
<!-- END BODY -->
</html>