<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html>
<!--<![endif]-->
<!-- BEGIN HEAD -->
<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">
    <link rel="stylesheet"
          href="plugins/ztree/css/metroStyle/metroStyle.css" type="text/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">

    <!-- 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]-->
    <link rel="stylesheet" href="plugins/select2/select2.min.css">
    <style>
        .modal-backdrop {
            z-index: 998;
        }
        #MassMyModal label{
            font-weight:400 !important;
        }
    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini">


<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">
        <div id="importExcelDiv"></div>

        <section class="content-header">
            <h1>
                素材管理
                <small>素材管理</small>
            </h1>

        </section>

        <input id="previewOpenId" style="display: none;">

        <div id="MassMyModal" class="modal fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
             style="z-index:999">
            <form id="MassSendForm">
                <div class="modal-dialog" style="width:50%">
                    <div class="modal-content" style="width:80%;margin:0px auto;">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">选择群发对象</h4>
                        </div>
                        <!-- dialog body -->
                        <div class="modal-body" style="width:100%;">
                            <div class="form-group form-md-line-input">
                                <label style="width:100%">群发方式</label>
                                <label><input type="radio" name="sendType" value="0" checked>全部用户</label>
                                <label><input type="radio" name="sendType" value="1">按条件</label>
                            </div>
                            <div style="display:none" id="sex">
                                <label style="clear:both;width:100%">性别</label>
                                <select class="form-control"
                                        name="sex" style="width: 100%;" tabindex="-1"
                                        aria-hidden="true">
                                    <option value="">全部</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </div>
                            <div class="form-group form-md-line-input" style="display:none" id="massSendArea">
                                <label style="clear:both;width:100%">地区</label>
                                <texterae style="-webkit-box-flex: 1;float:left;width:80%;overflow-y: scroll;height: 50px;" rows="2" type="text"
                                          class="form-control" name="area" id="area"
                                          maxlength="2000" placeholder="请点击选择按钮选择地区" disabled></texterae>
                                <input type="text" class="form-control" name="areaIds"
                                       id="areaIds" value="" maxlength="2000"
                                       disabled style="display: none">
                                <a style="width: 15%;-webkit-box-flex: 0; margin-left: 20px; margin-top: 2px;float:left"
                                   class="btn btn-primary btn-sm" id="areaselect">选择</a>
                            </div>
                            <div class="form-group form-md-line-input" style="display:none" id="massSendTag">
                                <label style="clear:both;width:100%">选择标签</label>
                                <select class="form-control select2" multiple='multiple' style="width: 100%"
                                        id="addlabel" name="tags" data-placeholder='请选择标签'>
                                </select>
                            </div>
                            <div class="form-group form-md-line-input" id="storeDiv" style="display:none">
                                <label>所属门店</label>
                                <div class="input-group">
                                    <input type="text" name="storeIds" id="storeIds"
                                           value="$!{storeIds}"
                                           class="form-control" style="display: none">
                                    <input type="text" id="storeNames"
                                           name="storeNames"value="$!{storeNames}"
                                           class="form-control" disabled>
                                    <span class="input-group-btn">
                                            <button type="button" id="storeSelect"
                                                    class="btn btn-info btn-flat">选择</button>
                                         </span>
                                </div>
                            </div>
                            <div class="box-footer" style="clear:both;">
                                <a class="btn btn-primary" id="massSend">群发</a>
                                <a id="massSendCanel" class="btn btn-default">取消</a>
                            </div>
                            <input name="newMedias" style="display:none;"/>
                        </div>
                        <!-- dialog buttons -->
                    </div>
                </div>
            </form>
        </div>


        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <form id="seachTableForm" action="#springUrl('/a/article/list')" method="get">
                                <div class="col-xs-5">
                                    #if($shiro.hasPermission("mp:article:edit"))
                                    <!-- <a href="#springUrl('/a/article/form')" class="btn btn-primary">新增</a>-->
                                    <a href="javascript:void(0)" id="syns" class="btn btn-primary">同步微信图文</a>
                                    #end
                                </div>
                            </form>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <table id="table" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <td hidden="true">Id</td>
                                    <th>微信官方MediaId</th>
                                    <th>图文内容</th>
                                    <th>最后更新时间</th>
                                    <th>记录同步时间</th>
                                    <th>操作</th>
                                </tr>
                                </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>
<script src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script src="plugins/ztree/js/jquery.ztree.all.min.js"
        type="text/javascript"></script>
<script src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script src="js/jquery.form.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="common/js/cfapp.js"></script>
<script type="text/javascript" src="common/js/seachImageText.js"></script>
<script src="plugins/select2/select2.full.min.js"></script>
<script src="common/js/dateUtils.js"></script>
<script src="js/jquery.serializejson.js"></script>
<script>
    var csrfheader = {name:'_csrf_header',value:'${_csrf.headerName}'};
    var csrftoken  = {name:'_csrf',value:'${_csrf.token}'};
</script>

<script>
    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;
    }

    var fanssList = "#springUrl('/a/managefanss/fanss/fetchdata')";

    function seachTable() {
        var sSource = "#springUrl('/a/article/listData')";
        var aoData = {
            iDisplayStart: 1,
            iDosplayLength: 10
        }
        var retrieveData = function (sSource, aoData, fnCallback) {

            $("#seachTableForm input").each(function () {
                var params = {
                    name: $(this).attr("name"),
                    value: $(this).val()
                };
                aoData.push(params);
            })

            $.ajax({
                "type": "GET",
                "url": sSource,
                "dataType": "json",
                "data": aoData, //以json格式传递
                "success": fnCallback
            });
        };

        $('#table').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",
            "aoColumns": [
                {
                    "mData": "id"
                },
                {
                    "mData": "media_id"
                }, {
                    "mData": "news_item"
                },
                {
                    "mData": "wxUpdateTime"
                },
                {
                    "mData": "createTime"
                },
                {
                    "mData": "media_id"
                }],
            "aoColumnDefs": [
                { // set default column settings
                    'visible': false,
                    'targets': [0]
                },

                {
                    "aTargets": [1],
                    "mData": "media_id",
                    "mRender": function (a, b, c, d) {
                        return a;
                        //return '<a href="#springUrl("/a/article/form?id=' + c.id + '")" data-id="' + c.id + '" data-action="view">' + a + '</a>';

                    }
                }, {
                    "aTargets": [2],
                    "mData": "news_item",
                    "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                        var HTML = "";
                        if (c.news_item) {
                            c.news_item = eval(c.news_item);
                            for (var i = 0; i < c.news_item.length; i++) {
                                HTML += "<p>" + (i + 1) + ":<a href='" + c.news_item[i].url + "' target='_blank'>" + c.news_item[i].title + "</a></p>";
                            }
                        }
                        return HTML;
                    }
                },

                {
                    "aTargets": [3],
                    "mData": "wxUpdateTime",
                    "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                        return dataUtils.dateFormat(a, "yyyy-MM-dd hh:mm:ss");
                    }
                },

                {
                    "aTargets": [4],
                    "mData": "createTime",
                    "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                        return dataUtils.dateFormat(a, "yyyy-MM-dd hh:mm:ss");
                    }
                }, {

                    "aTargets": [5],
                    "mData": "media_id",
                    "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                        var html = '<a href="javascript:void(0);" data-id="' + a + '" data-action="remove" onclick="preview(\'' + a + '\')" class="btn red">预览</a>';
                        html += '#if($shiro.hasPermission("mp:article:mass"))';//
                        html += '<a href="javascript:void(0);" data-id="' + a + '" data-action="remove" onclick="mass(\'' + a + '\')" class="btn red">群发</a>';
                        html += '#end';
                        return html;
                    }
                }
                /* ,
                 {
                 "aTargets": [3],
                 "mData": "id",
                 "mRender": function (a, b, c, d) {//a表示id对应的值，c表示当前记录行对象
                 var html = '#if($shiro.hasPermission("qy:article:edit"))';//
                 html += '<a href="#springUrl("/a/article/form?id=' + a + '")" data-id="' + a + '" data-action="view" class="btn green">修改</a>';
                 html += '<a href="javascript:void(0);" data-id="' + a + '" data-action="remove" onclick="removeData(' + a + ')" class="btn red">删除</a>';
                 html += '#end';
                 return html;
                 }
                 }*/

            ]
        });
    }


    function preview(mediaId) {
        Cfapp.f7({
            dataUrl: fanssList,
            checkType: 'single', // 多选为multi
            title: "选择粉丝",
            btnoktext: "确定",
            btncanceltext: "取消",
            columns: ['选择', '头像', '昵称'],
            isSearch: '0',
            searchColumns:["nickname"],//搜索的字段名称
            searchColumnsRemarks:['昵称'],//搜索的字段备注
            aoColumns: [{
                "mData": "openid"
            }, {
                "mData": "headimgurl"
            }, {
                "mData": "nickname"
            }],
            aoColumnDefs: [{
                "aTargets":[0],
                "mData":"openid",
                "mRender":function (a,b,c,d) {
                    return '<input type="radio" id="' + a + '" name="sel" data-value="' + a + '" data-name="' + c.nickname + '">';
                }
            },{
                "aTargets": [1],
                "mData": "img",
                "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                    return "<img src='"+a+"' style='width: 80px;height:80px;' />";
                }
            }],
            success: function () {
                OKpreview(mediaId);
            },
            cancel: function () {

            },
            storeFields: 'previewOpenId', // 储存的字段
            displayFields:''
        })
    }

    function OKpreview(mediaId){

        $.ajax({
            type: "get",
            url: "#springUrl('/a/article/preview')",
            data: {openId: $("#previewOpenId").val(), mediaId: mediaId},
            dataType: "json",
            success: function (previewData) {
                if (previewData.errorNo == 0) {
                    Cfapp.alert({
                        message: "预览文章发送成功,请在手机上查看",
                        btntext: "确定",
                        success: function () {
                            location.href = "#springUrl('/a/cfarticle/list')";
                        }
                    });
                }
            }
        })
    }

    var mediaId = "";//点击群发时记录ID
    function mass(id) {
        mediaId = id;
        $("#MassMyModal").modal('show');
    }

    jQuery(document).ready(function () {

        $("#syns").click(function () {
            var warningMsg = Cfapp.warning({
                content: '正在同步中，请稍后!',  //
                title: "提示"
            });
            $.ajax({
                "type": "POST",
                "data":{"_csrf_header":csrfheader.value,"_csrf":csrftoken.value},
                "url": "#springUrl('/a/article/syns')",
                "dataType": "json",
                "success": function (data) {
                    warningMsg.destory();
                    location.href = "#springUrl('/a/article/list')";
                }
            });

        })


        /**
         * 选择门店
         */
        $("#storeSelect").click(function(){
            var storeURL = "#springUrl('/a/store/listData?isEff=3')";
            Cfapp.f7({
                dataUrl: storeURL,
                checkType: 'multi', // 多选为multi
                title: "选择门店",
                btnoktext: "确定",
                btncanceltext: "取消",
                columns: ['选择', '门店名称'],
                isSearch: '0',
                searchColumns: ["name"],//搜索的字段名称
                searchColumnsRemarks: ['门店名称'],//搜索的字段备注
                aoColumns: [{
                    "mData": "id"
                }, {
                    "mData": "name"
                }],
                aoColumnDefs: [{
                    "aTargets": [0],
                    "mData": "id",
                    "mRender": function (a, b, c, d) {
                        return '<input type="checkbox" id="' + c.id + '" name="sel" data-value="' + c.id + '" data-name="' + c.name + '">';
                    }
                }],
                success: function () {

                },
                cancel: function () {

                },
                storeFields: 'storeIds', // 储存的字段
                displayFields: 'storeNames'
            })
        })



        seachTable();
        $('.search').click(function () {
            $('#table').dataTable().fnClearTable();

            //$("#table").dataTable().fnClearTable();
        });


        /**
         * 群发
         */
        var sendMessageMassUrl = "#springUrl('/a/newsmedia/uploadMediaArticle')";//群发
        var areaUrl = "#springUrl('/a/area/getfetchDataChild')";//按地区群发时，获取省市县接口
        var groupUrl = "#springUrl('/a/fanssgroup/fetchdata')";//获取标签接口

        $.ajax({
            "type": "get",
            "contentType": "application/json",
            "url": groupUrl,
            "dataType": "json",
            "data": {
                iDisplayStart: 0,
                iDisplayLength: 0
            },
            "success": function (data) {
                data = data.aaData;
                for (var i = 0; i < data.length; i++) {
                    $("#addlabel").append('<option value="' + data[i].groupid + '" data-attr="">' + data[i].name + '</option>')
                }
                $(".select2").select2();
            }
        });


        $('#areaselect').on('click',
                function () {
                    Cfapp.treeSelect({
                        title: '地区选择',
                        url: areaUrl,
                        displayFields: 'area',
                        storeFields: 'areaIds',
                        success: function () {
                            console.log('success');
                        },
                        cancel: function () {
                            console.log('cancel');
                        }
                    })
                });
        $("input[name='sendType']").change(function () {
            if ($(this).val() == "0") {
                $("#massSendArea").hide();
                $("#sex").hide();
                $("#massSendTag").hide();
                $("#storeDiv").hide();
            } else {
                $("#massSendArea").show();
                $("#sex").show();
                $("#massSendTag").show();
                //$("#storeDiv").show();
            }
        });


        $("#massSendCanel").click(function () {
            $("#MassMyModal").modal('hide');
        })

        $("#massSend").click(function () {//群发

            var warningMsg = Cfapp.warning({
                content: '正在群发中，请稍后!',  //
                title: "提示"
            });
            $("#MassMyModal").modal('hide');


            var data = $("#MassSendForm").serializeJSON();
            data.area = $("#area").text();
            data.mediaId = mediaId;
            data._csrf_header=csrfheader.value;
            data._csrf=csrftoken.value;
            $.ajax({
                "type": "POST",
                "url": sendMessageMassUrl,
                "dataType": "json",
                "data": data, //以json格式传递
                "success": function (data) {

                    warningMsg.destory();
                    if (data.errorNo == "0") {
                        alertHtml("群发成功");
                    } else if (data.errorNo == "2") {
                        alertHtml("未找到群发对象");
                    } else if (data.errorNo == "3") {
                        alertHtml("未找到群发对象");
                    } else if (data.errorNo == "4") {
                        alertHtml("发送对象少于2人,不可进行群发");
                    } else if(data.errorNo == "5"){
                        alertHtml("重复群发，请等待上次群发成功");
                    } else {
                        alertHtml("群发失败，系统异常");
                    }
                }
            });
        })


    });

    function alertHtml(msg) {
        Cfapp.alert({
            message: msg,
            btntext: "确定",
            success: function () {

            }
        })
    }

    Cfapp.init();
    function removeData(data) {
        Cfapp.confirm({
            message: "确定要删除吗",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    type: "POST",
                    url: "#springUrl('/a/article/delete')",
                    data: {id: data,"_csrf_header":csrfheader.value,"_csrf":csrftoken.value},
                    dataType: "json",
                    success: function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "删除成功",
                                btntext: "确定",
                                success: function () {
                                    location.href = "#springUrl('/a/article/list')";
                                }
                            });
                        }
                    },
                    error: function () {
                    }
                })
            },
            cancel: function () {
                $(".modal-backdrop").fadeOut();
            }
        });
    }


</script>

</body>
</html>
