/**
 * 校区组织信息管理
 * 2018/9/19 by liufang
 */
$(function () {
	var layer;
	var table;
	var util;
	layui.use('layer', function () {
		layer = layui.layer;
	});
	var ERROR_OK = 200;
	var contextPath = $("#contextPath").val();
	var updateAct = $("#updateAct").val();
	var deleteAct = $("#deleteAct").val();
	var funcid = $("#funcid").val();
	var isAdmin = $("#isAdmin").val();

	var permAdd = $("#perm-add").val(); //新增权限
	var permDelete = $("#perm-delete").val(); //删除权限
	var permEdit = $("#perm-edit").val(); //编辑权限
	var permApprove = $("#perm-approve").val(); //审核权限
	var permAboutus = $("#perm-aboutus").val();//关于我们

	var tpl = {

	};
	var elementgroupList = {
		init: function () {
			this.render();
			this.bindEvent();

		},
		render: function () {
			this.rendertable();

		},
		bindEvent: function () {
			var _this = this;
			var a = $("#orgList").val();
			//重置
			$(".set-btn").on("click", function () {
				$("#conditionform input,#conditionform select").val("");
				_this.againRenderTable();
			});
			//新增
			$(".add-ele-btn").on("click", function () {
				window.location.href = contextPath + '/busi/property/propertyForm?funcid=' + funcid;
			});
			//批量导入校区信息
			$(".export-ele-btn").on("click", function () {
				window.location.href = contextPath + '/busi/property/toBatchImportOrganization?funcid=' + funcid
			})
			// 模糊搜索
			_this.search_fuzzy()
		},
		// 模糊搜索事件
		search_fuzzy: function(){
			$('body').on("input",'#orgId',function(event){
				var _this = $(this)
				cjqt.search_fuzzy.event_input(_this,'org__id','org__span',contextPath,'/admin/templ/getOrgListByName')
			});
			$('body').on("focus",'#orgId',function(event){
				var _this = $(this)
				cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',contextPath,'/admin/templ/getOrgListByName')
			});
			$('body').on('blur','#orgId',function(event) {
				var _this = $(this)
				cjqt.search_fuzzy.event_blur(_this,'org__id')
			});
			$('body').on('click','.select_wrap>.org__span', function(event) {
				event.preventDefault();
				event.stopPropagation();
				/* Act on the event */
				var _this = $(this)
				cjqt.search_fuzzy.event_click(_this,'org__id','#orgId')
			});
		},
		rendertable: function () {
			var _this = this;
			layui.use(['table', 'util'], function () {
				table = layui.table,
					util = layui.util;
				//方法级渲染
				table.render({
					elem: '#LAY_table_user',
					url: contextPath + "/busi/property/getPropertyList",
					skin: 'nob' //行边框风格
						,
					even: true //开启隔行背景
						,
					size: 'lg' //小尺寸的表格
						,
					cols: [
						[{
							title: '序号',
							templet: '#indexTpl',
							width: '8%'
						}, {
							field: 'name',
							title: '校区名称',
							width: '12%'
						}, {
							field: 'code',
							title: '编码',
							width: '10%'
						}, {
							field: 'telephone',
							title: '服务热线',
							width: '12%'
						}, {
							field: 'address',
							title: '详细地址',
							width: '15%'
						}, {
							field: 'status',
							title: '审核状态',
							width: '8%',
							templet: function (data) {
								var str = '';
								if (data.status == 1) {
									return '通过'
								} else if (data.status == 2) {
									return '不通过'
								} else if (data.status == 0) {
									return '待审核'
								}

							}
						}, {
							field: 'createTime',
							title: '创建时间',
							width: '15%',
							templet: function (data) {
								return util.toDateString(data.createTime);
							}
						}, {

							title: '操作',
							width: '25%',
							fixed: 'right',
							templet: function (data) {
								var str = '';
								/*if(permAdd==1){
									  str += '<button class="t-btn t-red-btn t-export-btn" data-id='+data.id+' >批量导入企业</button>';
									  str += '<button class="t-btn t-red-btn t-manage-btn" data-id='+data.id+' >企业机构管理</button>';
	    						  };
									if (permApprove==1){
										str += '<button class="t-btn t-red-btn t-audit-btn" data-id='+data.id+' >审核</button>'
								}*/
								if (permEdit == 1) {
									str += '<button class="t-btn t-red-btn t-edit-btn" data-id=' + data.id + ' >修改</button>';
								}
								if(permAboutus == 1){
									str += '<button class="t-btn t-red-btn t-about-btn" data-id=' + data.id + ' >便民服务</button>';
								}
								if (permDelete == 1) {
									str += '<button class="t-btn t-white-btn t-del-btn" data-id=' + data.id + ' >删除</button>';
								}
								return str;
							}
						}]
					],
					id: 'testReload',
					page: true,
					method: 'post',
					request: {
						pageName: 'pageNo' //页码的参数名称，默认：page
							,
						limitName: 'pageSize' //每页数据量的参数名，默认：limit
					},
					response: {
						statusName: 'code' //数据状态的字段名称，默认：code
							,
						statusCode: 200 //成功的状态码，默认：0
							,
						countName: "count" //数据总数的字段名称，默认：count
							,
						dataName: "data" //数据列表的字段名称，默认：data
					},
					done: function (res, curr, count) {
						//						        //如果是异步请求数据方式，res即为你接口返回的信息。
						//批量导入企业
						$(".t-export-btn").on("click", function () {
							var id = $(this).data("id");
							window.location.href = contextPath + '/busi/property/toBatchImportVirtualArch?organizationId=' + id + '&funcid=' + funcid

						});

						//企业机构管理
						$(".t-manage-btn").on("click", function () {
							var id = $(this).data("id");
							openWin('企业机构管理', 800, 700, 'virtualArchForm/' + id + '&' + funcid);

						});

						//审核
						$(".t-audit-btn").on("click", function () {
							var id = $(this).data("id");
							openWin('校区信息审核管理', 600, 600, 'propertyViewForm?organizationId=' + id)
							//						    		window.location.href= contextPath + '/busi/property/toBatchImportVirtualArch?organizationId='+id+'&funcid='+funcid

						});

						//删除
						$(".t-del-btn").on("click", function () {
							var id = $(this).data("id");
							_this.delEles(id)
							//updateProperty/${obj.id}&${funcid}
						});
						//修改
						$(".t-edit-btn").on("click", function () {
							var id = $(this).data("id");
							var funcid = $("#funcid").val();
							window.location.href = contextPath + '/busi/property/updateProperty/' + id + '&' + funcid;
						})
						// 关于我们
						$(".t-about-btn").on("click",function(){
							var id = $(this).data("id");
							var funcid = $("#funcid").val();
							window.location.href = contextPath + '/admin/aboutOrg/list';
						})
					}
				});
				//查询
				$('#conditionform  .search-btn').on('click', function () {
					_this.againRenderTable();
				});
			});
		},
		/*重新渲染table*/
		againRenderTable: function () {
			table.reload('testReload', {
				where: {
					name: $(".name").val(),
					telephone: $(".telephone").val(),
					reviewStatus: $(".reviewStatus").val(),
					address: $(".address").val()
				},
				page: {
					curr: 1 //重新从第 1 页开始
				}
			});
		},
		/**
		 * 删除
		 * @param id 元素id
		 */
		delEles: function (id) {
			var _this = this;
			layer.confirm('确定删除?', {
				btn: ['确定', '取消']
			}, function (index, layero) {
				layer.close(index); //如果设定了yes回调，需进行手工关闭

				window.location.href = contextPath + "/busi/property/deleteProperty/" + id + "&" + funcid;
			}, function (index) {

			});
		}

	};
	elementgroupList.init();
})

function openWin(name, iWidth, iHeight, url) {
	//iWidth 弹出窗口的宽度;
	//iHeight 弹出窗口的高度;
	//获得窗口的垂直位置
	var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
	//获得窗口的水平位置
	var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
	var params = 'width=' + iWidth + ',height=' + iHeight + ',top=' + iTop + ',left=' + iLeft + ',channelmode=no' + ',directories=yes' +
		',location=no' + ',menubar=no' + ',resizable=no' + ',scrollbars=yes' + ',status=yes' + ',titlebar=yes' + ',toolbar=no';
	var winObj = window.open(url, name, params);
	var loop = setInterval(function () {
		if (winObj != null && winObj.closed) {
			clearInterval(loop);
			$("#conditionform").submit();
		}
	}, 1000);
}