<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="case_number" class="form-control">
            </div>
            <div class="form-group">
                <label>标题: </label>
                <input type="text" id="title" class="form-control">
            </div>
            <div class="form-group">
                <label>状态: </label>
                <select id="status" class="form-control">
                    <option value="">未提交</option>
                    <option value="1">待审核</option>
                    <option value="2">通过</option>
                    <option value="3">未通过</option>
                </select>
            </div>
            <div class="form-group">
                <label>反馈次数: </label>
                <select id="count" class="form-control">
                    <option value="">请选择</option>
                    <option value="0">0</option>
                    <option value="1">非0</option>
                </select>
            </div>
            <div class="form-group">
                <label>标签: </label>
                <select id="tags" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="form-group">
                <label>品牌公司: </label>
                <select id="brandCompany" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="form-group">
                <label>广告公司: </label>
                <select id="advertisingAgency" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="form-group">
                <label>案例用途: </label>
                <select id="purposeCase" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="form-group">
                <label>创意形式: </label>
                <select id="formCreative" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="form-group">
                <label>案例类型: </label>
                <select id="typeCase" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="form-group">
                <label>节日事件: </label>
                <select id="eventsFestival" name="tagLists" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>

            <div class="form-group">
                <label>创建时间：</label>
                <!--指定 date标记-->
                <div class='input-group date' id='createTime'>
                    <input type='text' class="form-control" readonly="readonly" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>

            <div class="form-group">
                <label>至</label>
                <!--指定 date标记-->
                <div class='input-group date' id='createTimeSection'>
                    <input type='text' class="form-control" readonly="readonly" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>


            <div class="form-group">
                <label>修改时间：</label>
                <!--指定 date标记-->
                <div class='input-group date' id='updateTime'>
                    <input type='text' class="form-control" readonly="readonly" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>

            <div class="form-group">
                <label>至</label>
                <!--指定 date标记-->
                <div class='input-group date' id='updateTimeSection'>
                    <input type='text' class="form-control" readonly="readonly" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>

            <div class="form-group">
                <label>上线时间：</label>
                <!--指定 date标记-->
                <div class='input-group date' id='onlineTime'>
                    <input type='text' class="form-control" readonly="readonly" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>

            <div class="form-group">
                <label>至</label>
                <!--指定 date标记-->
                <div class='input-group date' id='onlineTimeSection'>
                    <input type='text' class="form-control" readonly="readonly" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </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>

            <div class="form-group">
                <div class="col-md-1 col-sm-3" style="text-align:left;">
                    <button type="button" id="reset_query" class="btn btn-info">重置</button>
                </div>
            </div>

        </form>
    </div>

    <div class="jax-box jax-box-table">

        <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: 'id', title: '案例编号', align : "center"},
        //{field: 'title', title: '标题', align : "center", width: "155"},
        {field: 'title', title: '标题', align : "center", formatter: function(value, row, index) {
        	if(value!=null && $.trim(value)!='' && value.length>25){
        		value = value.substring(0, 25)+' ......';
        	}
        	return value;
        }},
        {field: 'status', title: '状态', align : "center", width: "60", formatter:function(value,row, index){
                if(row.status === 1){
                    return "待审核";
                }else if(row.status === 2){
                    return "已上线";
                }else if(row.status === 3){
                    return "未通过";
                }else if(row.status === 4){
                    return "已下线";
                }else if(row.status === 5){
                    return "未提交";
                }

            }},
        {field: 'visibility', title: '可见性', align : "center", formatter:function(value,row, index){
                return row.visibility === 1 ? "可见" : "不可见";
            }},
        {field: 'labelFeedback', title: '标签反馈数', align : "center"},
        {field: 'purposeCase', title: '案例用途', align : "center", formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 19){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'formCreative', title: '创意形式', align : "center", formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 20){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'eventsFestival', title: '节日事件', align : "center",formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 21){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'typeCase', title: '案例类型', align : "center",formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 18){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'brandCompany', title: '品牌公司', align : "center", formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 23){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'advertisingAgency', title: '广告公司', align : "center",formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 24){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'collectionNumber', title: '收藏数', align : "center"},
        {field: 'comments', title: '评论数', align : "center"},
        {field: 'createtime', title: '创建时间', align : "center"},
       // {field: 'updatetime', title: '修改时间', align : "center"},
        {field: 'onlineTime', title: '上线时间', align : "center"},
        {field: 'position', title: '关联部门', align : "center", width: "170", formatter:function(value,row, index){
                for(var i = 0; i < row.tagList.length;i++){
                    if(row.tagList[i].id === 26){
                        return row.tagList[i].tagName;
                    }
                }
                return "";
            }},
        {field: 'userName', 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)!=''){
        		var showtxt = value;
        		if(value.length>50)showtxt = value.substring(0, 35)+' ...... '+value.substring(value.length-10, value.length);
        		return '<a href="'+value+'" target="_blank">'+showtxt+'</a>';
        	}
        	return value;
        }},*/
        {field : 'operation', title : '操作', align : "center", formatter: function(value, row, index) {
                let adoptHtml = "";
                let rejectHtml = "";
                let seeHtml = "";
                let editHtml = "";
                if(row.status === 1){
                    adoptHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="operationApplication('+row.applicationId+',2)">发布</a>';
                    rejectHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="operationApplication('+row.applicationId+',3)">驳回</a>';
                    seeHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="seeApplication('+row.id+',1)">查看</a>';
                    editHtml = '&nbsp;<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editPage('+row.id+')">修改</a>';
                }else if(row.status === 2){
                    adoptHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="offlineApplication('+row.id+')">下线</a>';
                    seeHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="seeApplication('+row.id+',1)">查看</a>';
                    editHtml = '&nbsp;<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editPage('+row.id+')">修改</a>';
                }else if(row.status === 3){
                    seeHtml = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="seeApplication('+row.id+',1)">查看</a>';
                }
                return adoptHtml+rejectHtml+editHtml+seeHtml;
            }}
    ];
    var options={
        id:"#table",
        url: '[[@{/console/content/getContentApplication}]]',
        columns:columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams : queryParams
    }
    Core.initTable(options);

    //查询参数
    function queryParams(params) {
        var temp = {
            limit : params.limit, //页面大小
            offset : params.offset, //页码
            title: $("#title").val(),
            id: $("#case_number").val(),
            status: $("#status").val(),
            quantity: $("#count").val(),
            type:$("#typeCase").val(),
            createtime:$("#createTime").find("input").val(),
            createtimeSection:$("#createTimeSection").find("input").val(),
            updatetime:$("#updateTime").find("input").val(),
            updatetimeSection:$("#updateTimeSection").find("input").val(),
            onlineTime:$("#onlineTime").find("input").val(),
            onlineTimeSection:$("#onlineTimeSection").find("input").val(),
            tagLists:
                ""
                +$("#brandCompany").val()+","
                +$("#advertisingAgency").val()+","
                +$("#purposeCase").val()+","
                +$("#formCreative").val()+","
                +$("#typeCase").val()+","
                +$("#eventsFestival").val()+","
                +$("#tags").val()

        };
        return temp;
    }

    // 预览
    function previewNews(url) {
        window.open(url, "_blank");
    }

    // 修改
    function editPage(id) {
        Core.load("#news-edit-window","[[@{/console/content/edit2}]]?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 () {
        getDropDownBox();
        /*查询*/
        $("#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");
            })
        });

        //重置搜索栏
        $("#reset_query").click(function () {
            $("#case_number").val("");
            $("#title").val("");
            $(".form-control").find("option:selected").attr("selected",false);
        });

    });


    function getDropDownBox(){
        //初始化下拉框
        $.getJSON("/cases/console/datadict/getDropDownBox",function (data){
            $.each(data.data, function(i, item){
                if(item.dictId === 18 ){
                    $("#typeCase").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");
                }else if(item.dictId === 19 ){
                    $("#purposeCase").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");

                }else if(item.dictId === 20 ){
                    $("#formCreative").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");

                }else if(item.dictId === 21 ){
                    $("#eventsFestival").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");

                }else if(item.dictId === 23 ){
                    $("#brandCompany").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");

                }else if(item.dictId === 24 ){
                    $("#advertisingAgency").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");
                }else if(item.dictId === 25){
                    $("#tags").append("<option value='"+item.tid+"'>"+item.tagName+"</option>\n");
                }
            });
            //使用refresh方法更新UI以匹配新状态
            $('.selectpicker').selectpicker('refresh');
            //render方法强制重新渲染引导程序
            $('.selectpicker').selectpicker('render');
        });
    }

    //审核案例
    function operationApplication(applicationId,status) {
        var msg = "";
        if(status === 2){
            msg = "确定发布该案例？";
        }else if(status === 3){
            msg = "确定驳回该案例？";
        }
        Core.confirm(msg, function () {
            Core.postAjax("[[@{/console/content/updateApplication}]]",{applicationId:applicationId,status:status},function (data) {
                if(data.ret==0){
                    layer.msg('操作成功！');
                    Core.refreshTable("#table");
                }else{
                    layer.msg(data.msg);
                }
            },"POST");
        })
    }


    //撤回审核
    function withdrawApplication(applicationId){
        var msg = "确定撤回该案例？";
        Core.confirm(msg, function () {
            Core.postAjax("[[@{/console/content/deleteApplication}]]",{applicationId:applicationId},function (data) {
                if(data.ret==0){
                    layer.msg('撤回成功！');
                    Core.refreshTable("#table");
                }else{
                    layer.msg(data.msg);
                }
            },"POST");
        })
    }

    //下线案例
    function offlineApplication(contentId){
        var msg = "确定下线该案例？";
        Core.confirm(msg, function () {
            Core.postAjax("[[@{/console/content/deleteRelease}]]",{contentId:contentId},function (data) {
                if(data.ret==0){
                    layer.msg('下线成功！');
                    Core.refreshTable("#table");
                }else{
                    layer.msg(data.msg);
                }
            },"POST");
        })
    }

    //查看案例
    function seeApplication(contentId,count){
        Core.load("#news-edit-window","[[@{/console/content/edit2}]]?id="+contentId+'&count='+count+'&caseType='+caseType,function(){
            $("#news-edit-modal").modal("show");
        }, 2);
    }

    $('.date').datetimepicker({
        language: 'zh-CN',//显示中文
        format: 'yyyy-mm-dd',//显示格式
        minView: "month",//设置只显示到月份
        initialDate: new Date(),//初始化当前日期
        autoclose: true,//选中自动关闭
        todayBtn: true//显示今日按钮
    })


</script>

