<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_name" class="form-control" placeholder="这里输入关键字">
			</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="datadict">
            <button id="btn_add" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="datadict">
            <button id="btn_batch_delete" type="button" class="btn btn-danger">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除
            </button>
            </shiro:hasPermission>
        </div>
        <table id="table"></table>
    </div>
</section>

<!-- 编辑模态框 -->
<div id="data-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 editFlag = "[[${@perms.hasPerm('datadict')}]]";
    var delFlag = "[[${@perms.hasPerm('datadict')}]]";
    var columns = [
        {checkbox: true },
       	{field: 'datakey', title: '字典Key', align : "left"},
        {field: 'name', title: '字典名称', align : "left"},
        {field: 'description', title: '描述', align : "left"},
        {field: 'isvalid', title: '状态', align : "center",formatter:function(value,row, index){
        	return row.isvalid == '1' ? "有效" : "无效";
     	}},
        {field: 'createtime', title: '创建时间', align : "center"},
        {field: 'updatetime', title: '修改时间', align : "center"},
        {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/datadict/list}]]',
        columns:columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams : queryParams
    }
    Core.initTable(options);
	
	//查询参数
    function queryParams(params) {
        let temp = {
            limit : params.limit, //页面大小
            offset : params.offset, //页码
            name: $("#s_name").val(),
            isvalid: $("#s_isvalid").val()
        };
        return temp;
    }
	
	// 预览
	function previewNews(url) {
		window.open(url, "_blank");
	}
	
	// 修改
    function editPage(id) {
		Core.load("#news-edit-window","[[@{/console/datadict/edit}]]?id="+id,function(){
            $("#data-edit-modal").modal("show");
		}, 2);
	}

	/*删除*/
    function deleteAction(ids,num){
    	var msg = num>1 ? "确定删除选中的"+num+"条记录？" : "确定删除该记录？";
        Core.confirm(msg, function () {
            Core.postAjax("[[@{/console/datadict/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);
            }
        });

	});
</script>