/**
 * 模板配置
 * 2018-4-17
 */
$(function() {
	var orgId = $("#orgId").val();
	console.log(orgId,555);
	var templateSetting = {
		judge: function (id) {
			$.ajax({
				type: "post",
				url: $("#contextPath").val() + "/admin/templ/checkHasMobileAccount",
				data: {
					"orgId": id,
				},
				success: function (data) {
					if (data.code == '200') {
						if(data.data == "true"){
							$(".message-mask").css("display","none")
						}else{
							$(".message-mask").css("display","block");
							return false
						}
					} else {
						layer.msg(data.msg)
					}
				},
				error: function (error) {
					layer.msg('error')
				}
			});
		},
		init: function(){
			this.render();
			this.bindEvent();
		},
		render: function(){
			
		},
		bindEvent: function(){
			var _this = this;
			var $thirdTab = $(".third-tab");
			var $thirdTabCon = $(".third-tab-con");
			var $thirdMenuName = $(".third-menu-name");
			//三级菜单切换
			$thirdTab.on("click",function(){
				var $index = $(this).index();
				var name = $(this).data("name");
				$(this).addClass("active").siblings().removeClass("active");
				$thirdTabCon.eq($index).show().siblings().hide();
				$thirdMenuName.html(name);
			});
			//textarea初始化
			 $(".textarea-con").each(function(index,ele) {
				 if(!$(ele).val()){
					 $(ele).append("[keyword1]的[keyword2]先生/女士您好!\n贵公司本月的账单已出，账单金额总计[keyword3],请您确认无误后尽快安排财务付款，谢谢!");
				 }		 	
			 });
			 $(".textarea-wp").each(function(index,ele){
				 if($(ele).find(".textarea-con").length==0){
					 $(ele).append('<textarea  class="textarea-con"  spellcheck="false" data-id="">[keyword1]的[keyword2]先生/女士您好!\n贵公司本月的账单已出，账单金额总计[keyword3],请您确认无误后尽快安排财务付款，谢谢!</textarea>');
				 }
			 })
			 _this.saveTemplate();
		},
		/*
		 * 保存模板信息
		 * 
		 */
		saveTemplate: function(){
			var _this = this;
			
			$(".save-btn").on("click",function(){
				var $text = $(this).closest(".third-tab-con").find(".textarea-con");
				var modelType = $(this).data("modeltype");//账单类型：0出账日账单 1逾期账单
				var modelWay = $(this).data("modelway");//推送方式：0短信1邮件2微信
				var modelContent = $text.val();//模板内容
				var pushModleId = $text.data("id");//模板ID，新增为空
				
				var orgId=$("#orgId").val();
				if(!modelContent){
					alert('模板内容不能为空！');
					return false;
				}else{
					$.ajax({
						   type: "post",
						   url: $("#contextPath").val()+ "/admin/pushModel/addAndUpdatePushModel", 
						   data: {
							   modelType: modelType,
							   modelWay: modelWay,
							   modelContent: modelContent,
							   pushModleId: pushModleId,
							   orgId:orgId
						   },
						   success: function(data){
							   var result = JSON.parse(data);
							   console.log(result.data.id)
							   if(result.result == 'success'){
								   if(!pushModleId){
									   $text.data("id",result.data.id);
								   }
								   alert('保存成功')
							   }else if(result.result == 'fail'){
								   alert(result.message);
							   }
						   },
						   error: function(){
							   
						   }
					   });
				}
				
			})
			   
		}
		
	};
	templateSetting.judge(orgId);
	templateSetting.init();
	
});
