<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li class="active">案例列表</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="formSearch" class="form-inline form-search">
            <div class="form-group">
				<label>标题: </label>
				<input type="text" id="s_title" class="form-control">
			</div>
			<div class="form-group">
				<label>状态: </label>
     			<select id="s_isvalid" class="form-control">
     				<option value="-1"></option>
                    <option value="1">有效</option>
                    <option value="0">无效</option>
                </select>
			</div>
			<div class="form-group">
                <div class="col-md-1 col-sm-3" style="text-align:left;">
                    <button type="button" id="btn_query" class="btn btn-info">查 询</button>
                </div>
			</div>
        </form>
    </div>
    
    <div class="jax-box jax-box-table">
        <div id="toolbar" class="btn-group">
            <shiro:hasPermission name="cases-h5">
            <button id="btn_add" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp;新增
            </button>
            </shiro:hasPermission>
            
            <shiro:hasPermission name="cases-h5">
            <button id="btn_batch_delete" type="button" class="btn btn-danger">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>&nbsp;批量删除
            </button>
            </shiro:hasPermission>
            
            <shiro:hasPermission name="cases-h5">
            <button id="btn_synces" type="button" class="btn btn-info"> 
                <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>&nbsp;同步数据到搜索引擎
            </button>
            </shiro:hasPermission>
        </div>
        <table id="table"></table>
    </div>
</section>

<!-- 编辑模态框 -->
<div id="news-edit-modal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document" style="width: 90%; min-width: 600px; max-width: 1200px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">内容编辑</h4>
            </div>
            <div class="modal-body">
                <div id="news-edit-window"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
	var caseType = '[[${caseType}]]';
	
    var editFlag = "[[${@perms.hasPerm('cases-h5')}]]";
    var delFlag = "[[${@perms.hasPerm('cases-h5')}]]";
    var columns = [
        {checkbox: true },
       	{field: 'createtime', title: '创建时间', align : "center", width: "155"},
        {field: 'isvalid', title: '状态', align : "center", width: "60", formatter:function(value,row, index){
        	return row.isvalid == '1' ? "有效" : "无效";
     	}},
        {field: 'fromName', title: '案例来源', align : "center", width: "70"},
        {field: 'viewno', title: '查看数', align : "center", width: "50"},
        {field: 'sort', title: '排序值', align : "center", width: "50"},
        {field: 'title', title: '标题', align : "left", width: "25%", formatter: function(value, row, index) {
        	if(value!=null && $.trim(value)!='' && value.length>25){
        		value = value.substring(0, 25)+' ......';
        	}
        	return value;
        }},
        {field: 'desct', title: '描述', align : "left", width: "25%", formatter: function(value, row, index) {
        	if(value!=null && $.trim(value)!='' && value.length>25){
        		value = value.substring(0, 25)+' ......';
        	}
        	return value;
        }},
        {field : 'operation', title : '操作', align : "left", width: '102px', formatter: function(value, row, index) {
			let editHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editPage('+row.id+')">修改</a>';
        	let delHtml =  '&nbsp;<a class="table-btn table-btn-danger" href="javascript:void(0)" onclick="deleteAction('+row.id+',1)">删除</a>';
			return editHtml + delHtml;
       }}
	];
    var options={
        id:"#table",
        url: '[[@{/console/content/list}]]?type=[[${caseType}]]',
        columns:columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams : queryParams
    }
    Core.initTable(options);
	
	//查询参数
    function queryParams(params) {
        var temp = {
            limit : params.limit, //页面大小
            offset : params.offset, //页码
            title: $("#s_title").val(),
            isvalid: $("#s_isvalid").val()
        };
        return temp;
    }
	
	// 预览
	function previewNews(url) {
		window.open(url, "_blank");
	}
	
	// 修改
	function editPage(id) {
		Core.load("#news-edit-window","[[@{/console/content/edit-editor}]]?id="+id+'&caseType='+caseType,function(){
            $("#news-edit-modal").modal("show");
		}, 2);
	//	openWindow("[[@{/console/content/edit}]]?id="+id, "编辑新闻", 1200, 600);
	}
	
	function openWindow(url, name, iWidth, iHeight){
  		var iWidth;		// 弹出窗口的宽度;
  		var iHeight;	// 弹出窗口的高度;
  		var iTop = (window.screen.availHeight-30-iHeight)/2;	// 获得窗口的垂直位置;
  		var iLeft = (window.screen.availWidth-10-iWidth)/2;		// 获得窗口的水平位置;
  		window.open(url, name,'height='+iHeight+',innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
	}
	
	/*删除*/
    function deleteAction(ids,num){
    	var msg = num>1 ? "确定删除选中的"+num+"条记录？" : "确定删除该记录？";
        Core.confirm(msg, function () {
            Core.postAjax("[[@{/console/content/delete}]]",{"ids":ids},function (data) {
                if(data.ret==0){
                	layer.msg('删除成功！');
                    Core.refreshTable("#table");
                }else{
	                layer.msg(data.msg);
                }
            },"POST");
        })
	}
	
	$(function () {
		/*查询*/
        $("#btn_query").click(function(){
            Core.refreshTable("#table");
        });
	
		// 新增
		$("#btn_add").click(function(){
        	editPage(-1);
        });
        
        /*批量删除*/
        $("#btn_batch_delete").click(function(){
        	var checkedRows= Core.selectMutiData("#table");
            if(checkedRows){
	            var ids = "";
	            $.each(checkedRows, function (i, item) {
	                ids += (item.id + ",");
	            })
	            if(ids.length > 0)ids = ids.substring(0, ids.length - 1);
	            deleteAction(ids, checkedRows.length);
            }
        });
        
        // 同步数据到搜索引擎
        $("#btn_synces").click(function(){
	        Core.confirm("确定同步全部记录？", function () {
	            Core.mask("#btn_synces");
	            Core.postAjax("[[@{/console/content/syncEsData}]]",{type: '[[${caseType}]]', isReBuild:true},function(data) {
	                Core.unmask("#btn_synces");
	                if(data.ret==0){
	                	layer.msg('同步完成，总记录数为：'+data.data);
	                    Core.refreshTable("#table");
	                }else{
		                layer.msg(data.msg);
	                }
	            },"POST");
	        })
        });
        
	});
	
</script>