<%@ page import="com.fadu.app.util.SystemCache" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>

<link rel="stylesheet" href="${contextPath}/static/assets/css/jquery-ui.css" />
<link rel="stylesheet" href="${contextPath}/static/assets/css/datepicker.css" />
<link rel="stylesheet" href="${contextPath}/static/assets/css/ui.jqgrid.css" />

<style>
    #modal-excel-body{
        font-size: 20px;
    }
    .a-upload {
        padding: 4px 10px;
        height: 30px;
        line-height: 20px;
        position: relative;
        top:7px;
        cursor: pointer;
        color: #888;
        background: #fafafa;
        border: 1px solid #ddd;
        border-radius: 6px;
        overflow: hidden;
        display: inline-block;
        *display: inline;
        *zoom: 1
    }

    .a-upload  input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
        filter: alpha(opacity=0);
        cursor: pointer
    }

    .a-upload:hover {
        color: #444;
        background: #eee;
        border-color: #ccc;
        text-decoration: none
    }
</style>

<div style="background: rgba(1,1,2,0.05)">
    <br>
    关键字:<input id="keywords" placeholder="输入关键字" class="input-medium search-query" type="text" />
    &nbsp;选题类型：<select id="subType">
    </select>
    &nbsp;内容类型：<select id="subCon">
    </select>
    &nbsp;选题领域：<select id="subTree">
    </select>
    &nbsp;是否上线：<select id="isLivea">
    <option selected value="">全部</option>
    <option value="1">是</option>
    <option value="0">否</option>
    </select>
    &nbsp;<button id="searchd" type="button" class="btn" >搜索</button>
    &nbsp;<button id="addAcc" type="button" onclick="preAddSub()" class="btn" >添加选题</button>
    &nbsp;<button id="exportExcel" type="button" onclick="exportExcel()" class="btn" >导出数据</button>
    &nbsp;<button id="importExcel" type="button" onclick="importExcel()" class="btn" >批量导入</button>
    <br>
    <br>
</div>

<div class="row" >
    <div class="col-xs-6" >
        <table id="grid-table"></table>
        <div id="grid-pager"></div>
        <script type="text/javascript">
            var $path_base = "${contextPath}/static";//in Ace demo this will be used for editurl parameter
        </script>
        <!-- PAGE CONTENT ENDS -->
    </div><!-- /.col -->
</div><!-- /.row -->

<div id="modal-add-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    添加选题
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-add-tip" class="red clearfix"></div>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label col-sm-3">标题(字数限制20)：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-7" maxlength="20" type="text" id="add-title">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label col-sm-3">选题文案：</label>
                    <div class="col-sm-8 input-group">
                        <textarea class="control-label col-sm-7" rows="3" cols="60" id="add-documents"></textarea>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">内容形式：</label>
                    <div class="col-sm-8 input-group">
                        <select id="add-content" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">领域：</label>
                    <div class="col-sm-8 input-group">
                        <select id="add-territory" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">类型：</label>
                    <div class="col-sm-8 input-group">
                        <select id="add-type" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-add-success" type="button" class="btn btn-sm btn-primary"
                            onclick="addSub();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div id="modal-edit-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    修改选题
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-edit-tip" class="red clearfix"></div>
                <input style="display: none" class="control-label" type="text" id="subId">
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label col-sm-3">标题(字数限制20)：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-7" maxlength="20" type="text" id="edit-title">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label col-sm-3">选题文案：</label>
                    <div class="col-sm-8 input-group">
                        <textarea class="control-label col-sm-7" rows="3" cols="60" id="edit-documents"></textarea>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">内容形式：</label>
                    <div class="col-sm-8 input-group">
                        <select id="edit-content" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">领域：</label>
                    <div class="col-sm-8 input-group">
                        <select id="edit-territory" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">类型：</label>
                    <div class="col-sm-8 input-group">
                        <select id="edit-type" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">是否上线：</label>
                    <div class="col-sm-8 input-group">
                        <select id="edit-isLive" class="col-sm-5">
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-edit-success" type="button" class="btn btn-sm btn-primary"
                            onclick="editSub();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<%--excel导入框--%>
<div id="modal-excel-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    上传Excel导入选题
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-excel-tip" class="red clearfix"></div>
                <form name="xx" action="${contextPath}/xmt/sub/uploadFile"
                      method="post" enctype="multipart/form-data" id="formId"
                      class="pageForm required-validate" >
                    <div id="upload">
                        <div>
                            <span for="excelFile">&nbsp;&nbsp;选择文件：</span>
                            <a class="a-upload" id="up1">
                                <input id="excelFile" type="file" name="filename">点击上传
                            </a>
                            <a href="https://duowen-test.oss-cn-shenzhen.aliyuncs.com/duowenadmin/%E9%80%89%E9%A2%98%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx">下载导入模板</a>
                            <span class="showFileName"></span>
                            <span class="fileerrorTip"></span>
                        </div>
                    </div>
                </form>
                <br>
                <div class="modal-footer no-margin-top">
                    <div class="text-center">
                        <button id="mod-excel-success" type="button" class="btn btn-sm btn-primary"
                                onclick="javasrcipt:_submit();">
                            确定
                        </button>
                        <button class="btn btn-sm" data-dismiss="modal">
                            取消
                        </button>
                    </div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
</div>

<div id="modal-addsub-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    添加账号数据
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-addsub-tip" class="red clearfix"></div>
                <input id="mod-addsub-id" hidden = hidden>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">选题标题：</label>
                    <span id="mod-addsub-title"></span>
                </div>
                <%--<div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-7">选择账号：</label>
                    <input>
                </div>--%>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-7">账号Id(输入账号名称模糊查询)：</label>
                    <input type="text" id="update-acc" class="searchIpt form-control">
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-addsub-success" type="button" class="btn btn-sm btn-primary"
                            onclick="addSubAcc();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div id="modal-sub-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    选题详情
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-sub-tip" class="red clearfix"></div>
                <div id="subbb" class="form-group" style="margin-left: 30px;margin-bottom: 20px;">

                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <%--<button id="mod-sub-success" type="button" class="btn btn-sm btn-primary"
                            onclick="addSubAcc();">
                        确定
                    </button>--%>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<script type="text/javascript">


    var myGrid = null;
    var scripts = [ null,  "${contextPath}/static/assets/js/date-time/bootstrap-datepicker.js", "${contextPath}/static/assets/js/date-time/locales/bootstrap-datepicker.zh-CN.js", "${contextPath}/static/assets/js/jqGrid/jquery.jqGrid.js", "${contextPath}/static/assets/js/jqGrid/i18n/grid.locale-cn.js", null ]

    $('.page-content-area').ace_ajax('loadScripts', scripts, function() {
        jQuery(function ($) {

            var grid_selector = "#grid-table";
            var pager_selector = "#grid-pager";

            /*搜索框  主要对Jquery grid进行刷新 带参数刷新    */
            $("#searchd").click(function(){
                var keywords = $("#keywords").val();
                var subType = $("#subType").val();
                var subTree = $("#subTree").val();
                var subCon = $("#subCon").val();
                var isLivea = $("#isLivea").val();
                myGrid.setGridParam({postData:{keywords:keywords,subType:subType,
                        subTree:subTree,subCon:subCon,isLive:isLivea,page:1},
                    url : '${contextPath}/xmt/sub/getAllSub',page:1}).trigger('reloadGrid');
            });

            $('#add-coopDate').datepicker({
                language: 'zh-CN',
                pickTime: false,
                todayBtn: true,
                autoclose: true,
                minView: '2',
                forceParse: false,
                format:"yyyy-mm-dd"
            });
            $('#edit-coopDate').datepicker({
                language: 'zh-CN',
                pickTime: false,
                todayBtn: true,
                autoclose: true,
                minView: '2',
                forceParse: false,
                format:"yyyy-mm-dd"
            });


            $(document).keydown(function (event) {
                var key = window.event ? event.keyCode : event.which;
                if (key == 13) {
                    $("#searchd").click();
                }
            });


            myGrid=	jQuery(grid_selector).jqGrid({
                subGrid : false,
                url : "${contextPath}/xmt/sub/getAllSub",
                datatype : "json",
                mtype:'POST',//设置http方式为post以解决乱码问题
                colNames : ['id','选题标题','选题文案','内容形式','领域','类型', '上线状态','选题入库时间',
                    '平台总播放量','操作'],
                colModel : [{
                    name : 'id',
                    index : 'id',
                    width : 150,
                    search : false,
                    editable : false,
                    hidden : true
                    //sorttype : 'date'
                },{
                    name : 'title',
                    index : 'title',
                    width : 150,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'documents',
                    index : 'documents',
                    width : 220,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'content',
                    index : 'content',
                    width : 70,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'territory',
                    index : 'territory',
                    width : 70,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'type',
                    index : 'type',
                    width : 70,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'isLive',
                    index : 'isLive',
                    width : 70,
                    search : false,
                    editable : false,
                    // sorttype : 'date'
                },{
                    name : 'createDate',
                    index : 'createDate',
                    width : 150,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'bfCountAll',
                    index : 'bfCountAll',
                    width : 120,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : '',
                    index : '',
                    width : 300,
                    editable : false,
                    search : false,
                    sortable : false,
                    fixed:true,
                    formatter : optionFormatter
                }],
                sortname : "createDate",
                sortorder : "desc",
                viewrecords : true,
                rowNum : 10,
                rowList : [ 10, 20, 30 ],
                pager : pager_selector,
                altRows : true,
                height:"auto",//自动根据每页条数弹性调整表格高度
                loadComplete : function() {
                    var table = this;
                    setTimeout(function(){
                        /*styleCheckbox(table);
                        updateActionIcons(table);*/
                        updatePagerIcons(table);
                        enableTooltips(table);
                    }, 0);
                }
            });

            function updatePagerIcons(table) {
                var replacement = {
                    'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
                    'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
                    'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
                    'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
                };
                $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function() {
                    var icon = $(this);
                    var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                    if ($class in replacement)
                        icon.attr('class', 'ui-icon ' + replacement[$class]);
                })
            }

            function enableTooltips(table) {
                $('.navtable .ui-pg-button').tooltip({
                    container : 'body'
                });
                $(table).find('.ui-pg-div').tooltip({
                    container : 'body'
                });
            };

            function optionFormatter(cellvalue, options, cell) {
                console.log(cell);
                var template = "<button data-toggle='modal'  onclick='showEdit(\""+cell.id+"\")' class='btn btn-minier btn-yellow'>编辑数据</button>";
                if (cell.isLive === "是"){
                    template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='subEdit(\""+cell.id+"\")' class='btn btn-minier btn-info'>选题详情</button>";
                    template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='preAddAcc(\""+cell.id+"\",\""+cell.title+"\")' class='btn btn-minier btn-grey'>添加账号</button>";
                }
                template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='predelete(\""+cell.id+"\")' class='btn btn-minier btn-danger'>删除数据</button>";
                return template;
            }


        });
    });

    function getAlltype(type) {
        var sec = document.getElementById('add-type');
        var sss = document.getElementById('edit-type');
        var ccc = document.getElementById('subType');
        sec.innerHTML = "<option value='' selected>请选择</option>";
        sss.innerHTML = "<option value='' selected>请选择</option>";
        ccc.innerHTML = "<option value='' selected>全  部</option>";
        $.getJSON("${contextPath}/xmt/sub/getAlltype",function(result){
            $.each(result, function(i, v){
                if (type === 1){
                    sec.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else if (type === 2){
                    sss.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else{
                    ccc.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }
            });
        });
    }

    function getAllterritory(type) {
        var sec = document.getElementById('add-territory');
        var sss = document.getElementById('edit-territory');
        var ccc = document.getElementById('subTree');
        sec.innerHTML = "<option value='' selected>请选择</option>";
        sss.innerHTML = "<option value='' selected>请选择</option>";
        ccc.innerHTML = "<option value='' selected>全  部</option>";
        $.getJSON("${contextPath}/xmt/sub/getAllterritory",function(result){
            $.each(result, function(i, v){
                if (type === 1){
                    sec.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else if (type === 2){
                    sss.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else{
                    ccc.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }
            });
        });
    }

    function getAllcontent(type) {
        var sec = document.getElementById('add-content');
        var sss = document.getElementById('edit-content');
        var ccc = document.getElementById('subCon');
        sec.innerHTML = "<option value='' selected>请选择</option>";
        sss.innerHTML = "<option value='' selected>请选择</option>";
        ccc.innerHTML = "<option value='' selected>全  部</option>";
        $.getJSON("${contextPath}/xmt/sub/getAllcontent",function(result){
            $.each(result, function(i, v){
                if (type === 1){
                    sec.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else if (type === 2){
                    sss.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else{
                    ccc.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }
            });
        });
    }
    function preAddSub() {
        $("#add-title").val("");
        $("#add-documents").val("");
        $("#add-type").val("");
        $("#add-territory").val("");
        $("#add-content").val("");
        getAlltype(1);
        getAllterritory(1);
        getAllcontent(1);
        $("#modal-add-body").modal("toggle");
    }

    function addSub() {
        var title = $("#add-title").val();
        var documents = $("#add-documents").val();
        var type = $("#add-type").val();
        var territory = $("#add-territory").val();
        var content = $("#add-content").val();
        if (type === "" || territory === "" || content === ""){
            alert("请将内容填写完整！");
            return;
        }
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/sub/addSubject",
            type : "post",
            data : {
                'title' : title,
                'documents' : documents,
                'type' : type,
                'territory' : territory,
                'content' : content,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-add-body").modal("toggle");
                /*getAlltype(2);
                getAllterritory(2);
                getAllcontent(2);*/
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }

    /*getAlltype(2);
    getAllterritory(2);
    getAllcontent(2);*/
    getAlltype(3);
    getAllterritory(3);
    getAllcontent(3);


    function showEdit(id) {
        getAlltype(2);
        getAllterritory(2);
        getAllcontent(2);
        $("#subId").val("");
        $("#edit-title").val("");
        $("#edit-documents").val("");
        $("#edit-type").val("");
        $("#edit-territory").val("");
        $("#edit-content").val("");
        $("#edit-isLive").val("");
        $("#modal-edit-body").modal("toggle");
        setTimeout(function () {
            $.ajax({
                dataType : "json",
                url : "${contextPath}/xmt/sub/getSubById",
                type : "post",
                data : {
                    'id' : id,
                },
                complete : function(ret) {
                    var ret = eval("(" + ret.responseText + ")");
                    console.log(ret);
                    $("#subId").val(id);
                    $("#edit-title").val(ret.title);
                    $("#edit-documents").val(ret.documents);
                    $("#edit-type").val(ret.typeId);
                    $("#edit-territory").val(ret.territoryId);
                    $("#edit-content").val(ret.contentId);
                    $("#edit-isLive").val(ret.isLive);
                    console.log(ret);
                }
            });
        }, 100);

    }

    function editSub() {
        var id = $("#subId").val();
        var title = $("#edit-title").val();
        var documents = $("#edit-documents").val();
        var type = $("#edit-type").val();
        var territory = $("#edit-territory").val();
        var content = $("#edit-content").val();
        var isLive = $("#edit-isLive").val();
        if (title.length === 0 || documents.length === 0 || type.length === 0
            || territory.length === 0 || content.length === 0){
            alert("请把信息补充完整！！");
            return;
        }
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/sub/editSub",
            type : "post",
            data : {
                'id' : id,
                'title' : title,
                'documents' : documents,
                'type' : type,
                'territory' : territory,
                'content' : content,
                'isLive' : isLive,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                if (ret.succ === "true"){
                    $("#modal-edit-body").modal("toggle");
                    jQuery("#grid-table").trigger("reloadGrid");
                    getAlltype(2);
                    getAllterritory(2);
                    getAllcontent(2);
                }
            }
        });
    }

    function predelete(id) {
        var result = confirm('是否删除？');
        if(result){
            deleteSub(id)
        }
    }

    function deleteSub(id) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/sub/deleteSub",
            type : "post",
            data : {
                'id' : id,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                jQuery("#grid-table").trigger("reloadGrid");
                getAlltype(2);
                getAllterritory(2);
                getAllcontent(2);
            }
        });
    }



    function exportExcel() {
        var keys = [], ii = 0, rows = "";
        var grid_selector = "#grid-table";
        var ids = $(grid_selector).getDataIDs(); // Get All IDs
        console.log("哈哈");
        console.log(ids);
        var row = $(grid_selector).getRowData(ids[0]); // Get First row to get the labels
        console.log("呵呵");
        console.log(row);
        for (var k in row) {
            keys[ii++] = k; // capture col names
            rows = rows + k + "\t"; // output each Column as tab delimited
        }
        rows="选题Id\t选题标题\t选题文案\t内容形式\t领域\t类型\t上线状态\t选题入库时间\t平台总播放量";
        console.log(typeof(rows));
        rows = rows + "\n"; // Output header with end of line
        for (i = 0; i < ids.length; i++) {
            row = $(grid_selector).getRowData(ids[i]); // get each row
            //var rowData = $('#gridTable').jqGrid('getRowData',rowId);
            for (j = 0; j < keys.length-1; j++){
                rows = rows + row[keys[j]].replace(/^\s*|\s*$/g,"") + "\t"; // output each Row as tab delimited
            }
            rows = rows + "\n"; // output each row with end of line
        }
        rows = rows + "\n"; // end of line at the end
        var form = "<form name='csvexportform' action='${contextPath}/sys/ssdbOrder/operateOrder?oper=subject' method='post'>";
        form = form + "<input type='hidden' name='csvBuffer' value='" + encodeURIComponent(rows) + "'>";
        form = form + "</form><script>document.csvexportform.submit();</sc" + "ript>";
        OpenWindow = window.open('', '');
        OpenWindow.document.write(form);
        OpenWindow.document.close();
    }

    function importExcel() {
        var file = document.getElementById("excelFile");
        if (file.outerHTML) {
            file.outerHTML = file.outerHTML;
        } else { // FF(包括3.5)
            file.value = "";
        }
        $(".showFileName").html("");
        $(".fileerrorTip").html("");
        $("#modal-excel-body").modal("toggle");
    }

    function _submit() {
        var files = document.getElementsByName("filename");
        if (files[0].value === "") {
            alert("上传有误，请检查是否上传文件或者检查文件类型！!");
            return false;
        }else{
            $('#formId').submit();
        }
    }

    $("#up1").on("change","input[type='file']",function(){
        var filePath=$(this).val();
        //alert(filePath);
        if(filePath.indexOf("xlsx")!==-1){
            $(".fileerrorTip").html("").hide();
            var arr=filePath.split('\\');
            var fileName=arr[arr.length-1];
            $(".showFileName").html(fileName);
        }else{
            $(".showFileName").html("");
            $(".fileerrorTip").html("上传有误！请上传后缀名为xlsx的文件").show();
            return false
        }
    });
    function preAddAcc(id , title) {
        console.log(id, title);
        $("#mod-addsub-id").val(id);
        $("#update-acc").val("");
        $("#mod-addsub-title").html(title);
        $("#modal-addsub-body").modal("toggle");
    }

    //添加账号数据
    function addSubAcc() {
        var accoundId = $("#update-acc").val();
        var subjectId = $("#mod-addsub-id").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/data/addAccSubData",
            type : "post",
            data : {
                'accoundId' : accoundId,
                'subjectId' : subjectId,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-addsub-body").modal("toggle");
                // getAllAcc();
                // getAlltype(2);
                // getAllterritory(2);
                // getAllcontent(2);
                // jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }
    function getAllAcc() {
        var sec = document.getElementById('addsub-acc');
        sec.innerHTML = "<option value='' selected>请选择</option>";
        $.getJSON("${contextPath}/xmt/sub/getAllAcc",function(result){
            $.each(result, function(i, v){
                console.log(i ,  v)
                sec.innerHTML += "<option value='" + v[2] + "'>ID:" + v[0] + "&nbsp;&nbsp;&nbsp;账号名称:" + v[1] +"</option>";
            });
        });
    }
    // getAllAcc();
    function subEdit(id,type) {
        $("#subbb").html("");
        var sec = document.getElementById('subbb');
        sec.innerHTML += "发布过该选题的账号：";
        $.getJSON("${contextPath}/xmt/sub/getSubAccs?id="+id,function(result){
            $.each(result, function(i, v){
                console.log(i ,  v);
                console.log(result);
                var asId = v[2];
                sec.innerHTML += "<p >ID："+v[0]+"&nbsp;&nbsp;&nbsp;&nbsp;账号名称："+v[1]+"&nbsp;&nbsp;&nbsp;<button class='btn-danger' onclick='deleteAcc(\""+asId+"\",\""+id+"\")'>删除</button></p>";
            });
            sec.innerHTML += "共上线 " + result.length + "个账号";
        });
        if (type !== 1){
            $("#modal-sub-body").modal("toggle");
        }
    }

    $(document).ready(function($) {
        $.fn.typeahead.Constructor.prototype.blur = function() {
            var that = this;
            setTimeout(function () { that.hide() }, 250);
        };
        $("#update-acc").typeahead({
            source: function (query, process) {
                return $.ajax({
                    url: '${contextPath}/xmt/data/selectAccSub',
                    type: 'post',
                    data: {accountName: query,
                        type: 1,},
                    success: function (result) {
                        console.log(result);
                        var arr1 = new Array();
                        for (var i = 0;i<result.length;i++){
                            var obj = new Object();
                            obj.id =  result[i][0];
                            obj.accountName = result[i][1];
                            console.log(obj.id);
                            console.log(obj.accountName);
                            arr1.push("{\"id\" :\""+obj.id+"\",\"accountName\":\""+obj.accountName+"\"}");
                        }
                        process(arr1);
                    }
                });
            },
            highlighter: function (obj) {
                var item = JSON.parse(obj);
                console.log(item);
                var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
                return (item.id+"&nbsp;"+item.accountName).replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
                    return '<strong>' + match + '</strong>'
                });
            },
            updater: function (obj) {
                var item = JSON.parse(obj);
                return item.id;
            }
        });
    });

    function deleteAcc(asId,id) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/data/deleteAcc",
            type : "post",
            data : {
                'id' : asId,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                subEdit(id,1);
            }
        });
    }


</script>