var permDelete = $("#perm-delete").val(); //删除权限
var permEdit = $("#perm-edit").val(); //编辑权限
/**
 * 用户信息
 */
$(function () {
  var service = $("#contextPath").val();
  var beginTime = "";
  template.config("escape", false);
  var tpl = {
    noticeTpl: '<div class="notice-wrapper">\
		  	<div class="notice-mask"></div>\
		  	<div class="notice-close-btn"></div>\
		  	<div class="notice-content clearfloat">\
		  		<div class="notice-con-left"></div>\
		  		<div class="notice-con-right">\
		  			<div class="notice-tabs clearfloat">\
		  				<div class="tab tab-left active">微信端预览</div>\
		  				<div class="tab tab-right">app端预览</div>\
		  			</div>\
		  			// <% if(showTip){%>\
		  			// 	<div class="notice-tip" >提示：点击banner可查看跳转效果。</div>\
		  			// <%}%>\
		  		</div>\
		  	</div>\
		  </div>',
    noticeDetailTpl: '<div class="notice-wrapper notice-detail-tpl notice-detail-tpl-<%=type%>">\
			  	<div class="notice-mask"></div>\
			  	<div class="notice-close-btn"></div>\
			  	<div class="notice-content clearfloat">\
			  		<div class="notice-con-left">\
					  <div class="notice-detail-wp">\
					  	<div class="notice-detail-con">\
						  	<div class="notice-detail-top">\
						  		<div class="back-btn"></div>\
					  		</div>\
					  		<div class="notice-detail-con-wp">\
						  		<div class="notice-title-and-date">\
								  	<div class="title"><%=title%></div>\
							  		<div class="date">04-12 10:30 &nbsp;&nbsp;编辑人：学友</div>\
						  		</div>\
						  		<div class="content"><%=con%></div>\
							</div>\
					  </div>  	\
			  		</div>\
			  		</div>\
			  		<div class="notice-con-right" >\
			  			<% if(!showTab){%>\
				  			<div class="notice-tabs clearfloat">\
				  				<div class="tab tab-left <%if(type=="detail"){%> active <%}%>">微信端预览</div>\
				  				<div class="tab tab-right <%if(type=="main"){%> active <%}%>"">app端预览</div>\
				  			</div>\
			  			<%}%>\
			  			<% if(showTip){%>\
			  				<div class="notice-tip" >提示：点击banner可查看跳转效果。</div>\
			  			<%}%>\
			  		</div>\
			  	</div>\
			  </div>',
    appTpl: '<div class="notice-wrapper app-tpl">\
				  	<div class="notice-mask"></div>\
				  	<div class="notice-close-btn"></div>\
				  	<div class="notice-content clearfloat">\
				  		<div class="notice-con-left">\
						  <div class="app-wp">\
						 	<div class="app-con">\
						 		<div class="banner-wp"  style="background-image: url( <%=banner%>)"></div>\
						 	</div>\
						 </div>\
				  		</div>\
				  		<div class="notice-con-right">\
				  			<div class="notice-tabs clearfloat">\
				  				<div class="tab tab-left ">微信端预览</div>\
				  				<div class="tab tab-right active">app端预览</div>\
				  			</div>\
				  			<% if(showTip){%>\
				  				<div class="notice-tip" >提示：点击banner可查看跳转效果。</div>\
				  			<%}%>\
				  		</div>\
				  	</div>\
				  </div>',
    wxTpl: '<div class="notice-wrapper wx-tpl">\
					  	<div class="notice-mask"></div>\
					  	<div class="notice-close-btn"></div>\
					  	<div class="notice-content clearfloat">\
					  		<div class="notice-con-left">\
								  <div class="wx-wp">\
								 	<div class="wx-con">\
							 		<div class="banner-wp"  style="background-image: url( <%=banner%>)"></div>\
							 	</div>\
							 </div>\
					  		</div>\
					  		<div class="notice-con-right">\
					  			<div class="notice-tabs clearfloat">\
					  				<div class="tab tab-left active">微信端预览</div>\
					  				<div class="tab tab-right">app端预览</div>\
					  			</div>\
					  			<% if(showTip){%>\
					  				<div class="notice-tip" >提示：点击banner可查看跳转效果。</div>\
					  			<%}%>\
					  		</div>\
					  	</div>\
					  </div>',
    noticeListTpl: '<div class="notice-wrapper notice-list-tpl">\
						  	<div class="notice-mask"></div>\
						  	<div class="notice-close-btn"></div>\
						  	<div class="notice-content clearfloat">\
						  		<div class="notice-con-left">\
									  <div class="notice-list-wp">\
									 	<div class="notice-list-con">\
										<div class="notice-item-wp">\
									 		<div class="banner-wp" style="background-image: url( <%=banner%>)"></div>\
									 		<div class="banner-descript"><%=title%></div>\
									 	</div>\
									</div>\
								</div>\
						  		</div>\
						  		<div class="notice-con-right">\
						  			<div class="notice-tabs clearfloat">\
						  				<div class="tab tab-left active">微信端预览</div>\
						  				<div class="tab tab-right">app端预览</div>\
						  			</div>\
						  			<% if(showTip){%>\
						  				<div class="notice-tip" >提示：点击banner可查看跳转效果。</div>\
						  			<%}%>\
						  		</div>\
						  	</div>\
						  </div>'
  }
  // 数据获取
  var api = {
    get_user_del: function (id) {
      $.ajax({
        type: "post",
        url: service + "/notice/appNotice/deleteNotice",
        data: {
          id: id
        },
        success: function (data) {
          data = $.parseJSON( data );
          console.log(data, 222222222);
          if (data.code == '200') {
            layer.msg("删除成功")
            api.get_user_info_list();
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_user_detail: function (id) {
      $.ajax({
        type: "post",
        url: service + "/notice/appNotice/deleteNotice",
        data: {
          id: id
        },
        success: function (data) {
          console.log(data, 333333);
          if (data.code == '200') {
              layer.msg("Success")
            // api.get_user_info_list();
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_user_info_list: function (beginTime) {
      layui.use(['table', 'laydate'], function () {
        var table = layui.table;
        var laydate = layui.laydate;
        //日期
        laydate.render({
          elem: '#date1'
        });
        table.render({
          elem: '#table_user_info',
          url: service + '/notice/appNotice/getDiscoverBannerList',
          method: 'post',
          where: {
            c_beginTime: beginTime
          },
          skin: 'nob', //行边框风格
          even: true, //开启隔行背景
          size: 'lg', //小尺寸的表格
          limit: 10,
          cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          cols: [
            [{
              field: 'createTime',
              width: '20%',
              title: '时间',
            }, {
              field: 'content',
              width: '30%',
              title: '内容',
            }, {
              field: 'dept',
              width: '20%',
              title: '详情',
              templet: function (data) {
                return "<div class='operate'><span class='op_detail_btn' lay-event='detail'>查看详情</span></div>"
              }
            }, {
              field: 'manage',
              width: '20%',
              title: '操作管理',
              templet: function (data) {
                var str = "<div class='operate'>";
                if (permEdit == 1) {
                  str += "<span class='op_edit_btn' lay-event='edit'><i class='edit'></i><i class='op_hover'>编辑</i></span>";
                }
                if (permDelete == 1) {
                  str += "<span class='op_del_btn' lay-event='del'><i class='delete'></i><i class='op_hover'>删除</i></span>";
                }
                str += "</div>";
                return str;
              }
            }]
          ],
          id: 'reloadData',
          page: true,
          response: {
            statusName: 'code', //数据状态的字段名称，默认：code             
            statusCode: 200, //成功的状 态码，默认：0        
            countName: "count", //数据总数的字段名称，默认：count            
            dataName: "data" //数据列表的字段名称，默认：data
          },
          done: function (res, curr, count) {
            $(".edit,.delete").mouseover(function(){
              $(this).next().show();
            })
            $(".edit,.delete").mouseout(function(){
              $(this).next().hide();
            })
          }
        });
        table.on('tool(user)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
          if (obj.event === 'edit') {
            window.location.href = service + '/notice/banner/discoverBannerEditPage?id=' + obj.data.id;
          } else if (obj.event === 'del') {
            layer.confirm('确认删除?', function (index) {
              api.get_user_del(obj.data.id);
              obj.del();
              layer.close(index);
            });
          } else if (obj.event === 'detail') {
            console.log("detail");
            api.previewPhone(obj.data.content,obj.data.cover_url,2)
          }
          console.log(obj.data); //所在行的所有相关数据  
        });
      });
    },
    previewPhone: function (content,picture, type) { //详情
      if (type == 2) {
        var wx = {
          banner: picture,
          // title: $("#graphic .title-con").val(),
          // con: UE.getEditor('myeditor_c').getContent(),
          showTip: true
        };
        var wxDetail = {
          type: 'detail',
          // title: $("#graphic .title-con").val(),
          // con: UE.getEditor('myeditor_c').getContent(),
          showTip: true,
        }
        var app = {
          banner: picture,
          // title: $("#graphic .title-con").val(),
          // con: UE.getEditor('myeditor_c').getContent(),
          showTip: true,
        }
        var appDetail = {
          type: 'main',
          // title: $("#graphic .title-con").val(),
          // con: UE.getEditor('myeditor_c').getContent(),
          showTip: true,
        };
        //图文微信端
        $("body").append(template.render(tpl.wxTpl)(wx));

        //图文app端
        $("body").append(template.render(tpl.appTpl)(app));

        //图文微信端详情
        $("body").append(template.render(tpl.noticeDetailTpl)(wxDetail));

        //图文app端详情
        $("body").append(template.render(tpl.noticeDetailTpl)(appDetail));
        var $wx = $(".wx-tpl");
        var $wxDetail = $(".notice-detail-tpl-detail");
        var $app = $(".app-tpl");
        var $appDetail = $(".notice-detail-tpl-main");
        $wxDetail.hide();
        $app.hide();
        $appDetail.hide();
        //微信端和APP段banner跳转
        $wx.find(".banner-wp").on("click", function () {
          $wx.hide();
          $wxDetail.show();
          $app.hide();
          $appDetail.hide();
        });
        $app.find(".banner-wp").on("click", function () {
          $wx.hide();
          $wxDetail.hide();
          $app.hide();
          $appDetail.show();
        });
        //点击返回
        $wxDetail.find(".back-btn").on("click", function () {
          $wx.show();
          $wxDetail.hide();
          $app.hide();
          $appDetail.hide();
        });
        $appDetail.find(".back-btn").on("click", function () {
          $wx.hide();
          $wxDetail.hide();
          $app.show();
          $appDetail.hide();
        });
        //tab切换
        $wx.find(".tab-right").on("click", function () {
          $wx.hide();
          $wxDetail.hide();
          $app.show();
          $appDetail.hide();
        });
        $wxDetail.find(".tab-right").on("click", function () {
          $wx.hide();
          $wxDetail.hide();
          $app.show();
          $appDetail.hide();
        });
        $app.find(".tab-left").on("click", function () {
          $wx.show();
          $wxDetail.hide();
          $app.hide();
          $appDetail.hide();
        });
        $appDetail.find(".tab-left").on("click", function () {
          $wx.show();
          $wxDetail.hide();
          $app.hide();
          $appDetail.hide();
        });
      } else if (type == 3) {
        var wx = {
          banner: picture
        };
        var app = {
          banner: picture
        }
        //图文微信端
        $("body").append(template.render(tpl.wxTpl)(wx));

        //图文app端
        $("body").append(template.render(tpl.appTpl)(app));

        var $wx = $(".wx-tpl");

        var $app = $(".app-tpl");
        $app.hide();

        //tab切换
        $wx.find(".tab-right").on("click", function () {
          $wx.hide();

          $app.show();

        });
        $app.find(".tab-left").on("click", function () {
          $wx.show();

          $app.hide();

        });
      }
      $(".notice-mask,.notice-close-btn").on("click", function () {
        $(".notice-wrapper").remove();
      })
    }
  }
  api.get_user_info_list();

  // 事件处理
  $('.btn1').click(function () {
    window.location.href = service + '/notice/banner/discoverBannerEditPage';
  })
  // 查询
  $('#searchUser').click(function () {
    beginTime = $('input[name="date1"]').val();
    api.get_user_info_list(beginTime);
  })
  // 重置
  $('#resetSearch').click(function () {
    $('input[name="date1"]').val('');
  })
})