/**
 * 系统管理>短信管理
 * 2018/4/20 by liufang
 */
$(function() {
	$.ajaxSetup({
	  async: false
	});
	var ERROK = 200;
	var contextPath = $("#contextPath").val()
	var table;
	var orgList = []; //项目
	var allMealnameList = [] // 所有菜品
	var mealnameList = []; // 组合
	var mealnameSlist = []; // 单品
	var specList = []; // 规格
	var foodList = []; // 主食
	var isMax = 0 //新增单品还是组合
	
	var mealManage = {
		init: function() {
			this.render();
			this.bindEvent();
		},
		render: function() {
			this.rendertable();
			this.itemAjax();
//			this.ajaxinitData();
		},
		bindEvent: function() {
			var _this = this;
			layui.use('laydate', function() {
				var laydate = layui.laydate;

				//执行一个laydate实例
				laydate.render({
					elem: '#beginTime' //提交开始时间

				});
				laydate.render({
					elem: '#endTime' //提交结束时间
				});
			});
			//重置
			$(".set-btn").on("click", function() {
				var $orgId = $("#conditionform  .single-orgid");
				var $dom = $("#conditionform input,#conditionform select")
				if($orgId.length){
					$dom.val("").removeAttr('com__id dep__id')
				}else{
					$dom.val("").removeAttr('org__id com__id dep__id')
				}
				_this.againRenderTable({type:1});
			});
			_this.search_fuzzy()
		},
		// 模糊搜索事件
		search_fuzzy: function(){
			var _this = this
			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,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,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
			})
		},
		rendertable: function() {
			var _this = this;
			layui.use('table', function() {
				table = layui.table;

				//方法级渲染
				table.render({
					elem: '#LAY_table_user',
					url: $("#contextPath").val() + "/admin/sms/info/getSmsInfoList",
					method: "post",
					skin: 'nob' //行边框风格
						,
					even: true //开启隔行背景
						,
					size: 'lg' //小尺寸的表格
						,
					cols: [
						[{
							field: 'index',
							title: '序号',
							width: '80',
							templet: '#indexTpl'
						}, {
							field: 'orgName',
							title: '项目名称',
							minWidth: 300
						}, {
							field: 'mobile',
							title: '手机号码',
							minWidth: 200
						},{
							field: 'type',
							title: '功能模块',
							minWidth: 200,
							templet: function(data) {
								//
								if (data.type == 1) {
									return '验证码模块'
								} else if (data.type == 2) {
									return '订餐模块'
								} else if (data.type == 3) {
									return '访客模块'
								} else if (data.type == 4) {
									return '空调加时模块'
								} else {
									return '其他模块'
								}
							}
						}, {
							field: 'content',
							title: '短信内容',
							minWidth: 300
						}, {
							field: 'status',
							title: '状态',
							minWidth: 200,
							templet: function(data) {
								//
								if (data.status == 1) {
									return '成功'
								} else {
									return '失败'
								}
							}
						},  {
							field: 'sendTime',
							title: '发送时间',
							minWidth: 285
						}]
					],
					id: 'testReload',
					page: true,
					response: {
						statusName: 'code' //数据状态的字段名称，默认：code
							,
						statusCode: 200 //成功的状态码，默认：0
							,
						countName: "count" //数据总数的字段名称，默认：count
							,
						dataName: "data" //数据列表的字段名称，默认：data
					},
					request: {
						pageName: 'pageNo' //页码的参数名称，默认：page
							,
						limitName: 'pageSize' //每页数据量的参数名，默认：limit
					},
					done: function(res, curr, count) {
					}
				});
				//查询
				$('#conditionform  .search-btn').on('click', function() {
					_this.againRenderTable();
				});
			});
		},
		/*重新渲染table*/
		againRenderTable: function(option) {
			var obj = $.extend(true, {}, option);
			var type =  obj.type || ''
			var $orgId = $("#conditionform  .single-orgid");
			var orgId = $("#conditionform  #orgId").attr('org__id')
			if(type == 1){
				table.reload('testReload', {
					where: {
						mobile: '',
						type: '',
						status: '',
						beginTime: '',
						endTime: '', 
						orgId: ''
					},
					page: {
						curr: 1 //重新从第 1 页开始
					}
				});
			}else{
				table.reload('testReload', {
					where: {
						mobile: $("#conditionform .mobile").val(),
						status: $("#conditionform .status").val(),
						type: $("#conditionform .type").val(),
						beginTime: $("#conditionform #beginTime").val(),
						endTime: $("#conditionform #endTime").val(),
						orgId: $("#conditionform  #orgId").attr('org__id'),
					},
					page: {
						curr: 1 //重新从第 1 页开始
					}
				});
			}
		},
		//请求项目列表
		itemAjax: function() {
			$.ajax({
				type: "post",
				url: contextPath + "/admin/templ/getOrgList",
				success: function(data) {
					if (data.code == ERROK) {
						orgList = data.data;
					} else {
						layer.msg(data.msg)
					}

				},
				error: function(error) {
					layer.msg('error')
				}
			});
		}
	};
	mealManage.init();
})