/**
 * 配置流程说明
 * 2018/4/20 by liufang
 */
$(function () {
  var orgId = $('#orgId').attr('org__id');
  var layer;
  var contentA = '',
    contentB = ''
  layui.use('layer', function () {
    layer = layui.layer;
  });
  var appPublish = {
    init: function () {
      this.render();
      this.bindEvent();
    },
    render: function () {
      var _this = this;
      var editor = new UE.ui.Editor({
        imageActionName: 'ueAction',
        imageFieldName: "uploadfile",
        fileActionName: "ueAction",
        fileFieldName: "uploadfile",
        fileAllowFiles: [
          ".png", ".jpg", ".jpeg", ".gif", ".bmp",
          ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
          ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
          ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
          ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
        ],
        imageAllowFiles: [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
        /* 上传图片格式显示 */
        imageUrlPrefix: $("#contextPath").val() //$("#contextPath").val()
      });
      editor.render("myeditorA");
      editor.ready(function () {
        editor.setHeight(450);
        //					editor.addListener("afterUpfile",_this._afterUpfile);	
      });
      var editorb = new UE.ui.Editor({
        imageActionName: 'ueAction',
        imageFieldName: "uploadfile",
        fileActionName: "ueAction",
        fileFieldName: "uploadfile",
        fileAllowFiles: [
          ".png", ".jpg", ".jpeg", ".gif", ".bmp",
          ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
          ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
          ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
          ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
        ],
        imageAllowFiles: [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
        /* 上传图片格式显示 */
        imageUrlPrefix: $("#contextPath").val() //$("#contextPath").val()
      });
      editorb.render("myeditorB");
      editorb.ready(function () {
        editorb.setHeight(450);
        //					editor.addListener("afterUpfile",_this._afterUpfile);	
      });
      _this.ajaxSettingInfor();


    },
    bindEvent: function () {
      var _this = this;
      //切换三级菜单
      $(".third-tab").each(function (index, ele) {
        $(ele).on("click", function () {
          var name = $(this).data("name");
          $(".third-bread").html(name);
          $(ele).addClass("active").siblings().removeClass("active");
          $(".third-con").eq(index).show().siblings().hide();
        })
      })
      //确认修改
      $(".submit-btn").on("click", function () {
        var type = $(this).data("type")
        _this.ajaxSubmit(type);
      });
      //取消修改
      $(".cancel-btn").on("click", function () {
        var type = $(this).data("type");
        if (type == 7) {
          UE.getEditor('myeditorA').setContent(contentA);
          //						 UE.getEditor('myeditorA').addListener("ready", function () {
          //								// editor准备好之后才可以使用
          //								   UE.getEditor('myeditorA').setContent(contentA);
          //								});
        } else if (type == 8) {
          UE.getEditor('myeditorB').setContent(contentB);
          //						 UE.getEditor('myeditorB').addListener("ready", function () {
          //								// editor准备好之后才可以使用
          //								   UE.getEditor('myeditorB').setContent(contentB);
          //								});
        }
      })
      _this.search_fuzzy()
    },
    // 模糊搜索事件
    search_fuzzy: function(){
      console.log($("#contextPath").val())
      var default_option = {
        org_attrName:'org__id',
        com_attrName:'com__id',
        dep_attrName:'dep__id',
        org_spanName:'org__span',
        com_spanName:'com__span',
        dep_spanName:'dep__span',
        org_path:'/admin/templ/getOrgListByName',
        com_path:'/admin/templ/getVirList',
        dep_path:'/admin/templ/getDept',
        contextPath: $("#contextPath").val()
      }
      var option_1 = $.extend(true,{},default_option,{
        org_target:'.default-select#orgId',
      });
      function eve(option_,otherOption){
        var obj = $.extend(true, {}, option_, otherOption);
        var type = obj.type
        var _path, _target, _attrName, _spanName
        if(type == 1){
          _path = obj.org_path
          _target = obj.org_target
          _attrName = obj.org_attrName
          _spanName = obj.org_spanName
        }else if(type == 2){
          _path = obj.com_path
          _target = obj.com_target
          _attrName = obj.com_attrName
          _spanName = obj.com_spanName
        }else if(type == 3){
          _path = obj.dep_path
          _target = obj.dep_target
          _attrName = obj.dep_attrName
          _spanName = obj.dep_spanName
        }
        $('body').on("input",_target,function(event){
          var _this = $(this)
          var obj_input = $.extend(true,{}, obj,{"_this":_this});
          cjqt.search_fuzzy.event_input(_this,_attrName,_spanName,default_option.contextPath,_path,obj_input)
        });
        $('body').on("focus",_target,function(event){
          var _this = $(this)
          var obj_focus = $.extend(true,{},obj,{"_this":_this});
          cjqt.search_fuzzy.event_focus(_this,_attrName,_spanName,default_option.contextPath,_path,obj_focus)
        });
        $('body').on('blur',_target,function(event) {
          var _this = $(this)
          var obj_blur = $.extend(true,{},obj,{"_this":_this});
          cjqt.search_fuzzy.event_blur(_this,_attrName,obj_blur)
        });
        $('body').on('click',_target+'~.select_wrap>.'+_spanName, function(event) {
          event.preventDefault();
          event.stopPropagation();
          /* Act on the event */
          var _this = $(this)
          var obj_click = $.extend(true,{},obj,{"_this":_this});
          cjqt.search_fuzzy.event_click(_this,_attrName,_target,obj_click)
        });

      }
      // 搜索联动
      eve(option_1,{type:1})
    },
    /**
     * 获取模板配置接口
     * 
     */
    ajaxSettingInfor: function () {
      var _this = this;
      $.ajax({
        type: "post",
        url: $("#contextPath").val() + "/admin/enter/getVirModel",
        data: {
          "type": 0,
          'orgId': orgId
        },
        success: function (data) {
          //						   var data = JSON.parse(data);
          if (data.code == '200') {
            if (data.data.length > 0) {
              var list = data.data;
              for (var i = 0; i < list.length; i++) {
                if (list[i].type == 7) {
                  $("#myeditorA").data("id", list[i].id);
                  contentA = list[i].content;
                  UE.getEditor('myeditorA').addListener("ready", function () {
                    // editor准备好之后才可以使用
                    UE.getEditor('myeditorA').setContent(contentA);
                  });
                  //										   ue.setContent(list[i].content) ;
                  //										   contentA = list[i].content
                } else if (list[i].type == 8) {
                  $("#myeditorB").data("id", list[i].id);
                  var ue = UE.getEditor('myeditorB');
                  contentB = list[i].content
                  UE.getEditor('myeditorB').addListener("ready", function () {
                    // editor准备好之后才可以使用
                    UE.getEditor('myeditorB').setContent(contentB);
                  });

                }
              }
            }

          }
        },
        error: function () {}
      });
    },
    /**
     * 多校区重新渲染模板
     * 
     */
    ajaxModifiedInfor: function () {
      var _this = this;
      $.ajax({
        type: "post",
        url: $("#contextPath").val() + "/admin/enter/getVirModel",
        data: {
          "type": 0,
          'orgId': orgId
        },
        success: function (data) {
          //						   var data = JSON.parse(data);
          if (data.code == '200') {
            if (data.data.length > 0) {
              var list = data.data;
              for (var i = 0; i < list.length; i++) {
                if (list[i].type == 7) {
                  $("#myeditorA").data("id", list[i].id);
                  contentA = list[i].content;
                  // UE.delEditor('myeditorA');
                  // UE.getEditor('myeditorA').addListener("ready", function () {
                  // editor准备好之后才可以使用
                  UE.getEditor('myeditorA').setContent(contentA);
                  // });
                } else if (list[i].type == 8) {
                  $("#myeditorB").data("id", list[i].id);
                  contentB = list[i].content
                  UE.getEditor('myeditorB').setContent(contentB);
                }
              }
            } else {
              UE.getEditor('myeditorA').setContent("");

              UE.getEditor('myeditorB').setContent("");

            }

          }
        },
        error: function () {}
      });
    },
    /**
     * 提交发送
     * @param type
     */
    ajaxSubmit: function (type) {
      var ue, content, id;
      if (type == 7) {
        id = $("#myeditorA").data("id");
        ue = UE.getEditor('myeditorA');
        content = ue.getContent(); //发件主题
      } else if (type = 8) {
        id = $("#myeditorB").data("id");
        ue = UE.getEditor('myeditorB');
        content = ue.getContent(); //发件主题
      }
      if (!content) {
        layer.alert('请输入内容');
        return false;
      }
      $.ajax({
        type: "post",
        url: $("#contextPath").val() + "/admin/enter/saveVirModel",
        data: {
          "type": type,
          "content": content,
          "id": id,
          'orgId': orgId

        },
        success: function (data) {
          //						   var data = JSON.parse(data);
          if (data.code == '200') {
            //							   ue.setContent('') ;
            if (type == 7) {
              contentA = content;
              $("#myeditorA").data("id", data.data.id)
            } else {
              contentB = content;
              $("#myeditorB").data("id", data.data.id);
            }
            layer.msg('修改成功！');
          }
        },
        error: function () {}
      });
    }
  };
  appPublish.init();
  $(document).on("change", '#orgId', function () {
    var that = this 
    setTimeout(function(){
      orgId = $(that).attr('org__id');
      // $("#myeditorA").html('');
      // $("#myeditorB").html('');
      // $("#demoList").html('');
      // UE.delEditor('_editor');
      // var ue = UE.getEditor('_editor');
      // UE.delEditor('#myeditorA');
      // UE.delEditor('#myeditorB');
      // appPublish.init();
      appPublish.ajaxModifiedInfor();
    },200)
  });
})