/**
 * app公告发布 发送历史
 * 2018/4/20 by liufang
 */

var userType = '';
var companyArr = []; // 运营中心列表
var companyId = '';
var orgArr = []; // 项目列表

$(function(){
	// 每户时间设置
	// <div class="price-house">
	// 	每户<input type="text" class="house-text"/>元/每小时
	// </div>
	// 编辑
	// <div class="price-house">
	// 	每户<input type="text" class="house-text" value="<%=priceHouse%>"/>元/每小时
	// </div>
	var tpl = {
			"ruleTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-rule-con">\
					<div class="title-wp">\
						<span>新增规则</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-rule-wp">\
						<div class="rule-con">\
							<div class="rule-name-wp rule-item">\
								<div class="rule-tip">规则名称</div>\
								<input class="rule-name item-con" type="text" placeholder="请输入规则名称">\
							</div>\
							<div class="price-wp rule-item">\
								<div class="rule-tip">收费规则</div>\
								<div class="rule-con-box">\
									<div class="date-type-con">\
										<%if(priceType==2){%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus1" class="radio-item audit-pass" data-status="1" name="priceType" disabled/>\
													<span class="radio-inner"></span>\
												</span>		\
												<label class="radio-con radio-not-operate" for="priceStatus1">平米每小时</label>\
											</div>\
										<%} else {%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus1" class="radio-item audit-pass" data-status="1" name="priceType" <%if(priceType==1){%> checked <%}%>/>\
													<span class="radio-inner"></span>\
												</span>		\
												<label class="radio-con" for="priceStatus1">平米每小时</label>\
											</div>\
										<%}%>\
										<%if(priceType==1){%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus2" class="radio-item audit-pass" data-status="2" name="priceType" disabled/>\
													<span class="radio-inner"></span>\
												</span>\
												<label class="radio-con radio-not-operate" for="priceStatus2">层（半层）每小时</label>\
											</div>\
										<%} else {%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus2" class="radio-item audit-pass" data-status="2" name="priceType" <%if(priceType==2){%> checked <%}%>/>\
													<span class="radio-inner"></span>\
												</span>\
												<label class="radio-con" for="priceStatus2">层（半层）每小时</label>\
											</div>\
										<%}%>\
									</div>\
									<div class="priceType-content"></div>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">日期类型</div>\
								<div class="date-type-con item-con">\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="work" class="radio-item audit-pass" data-status="0" name="dataType" />\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="work">工作日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_work" class="radio-item audit-no-pass" data-status="1" name="dataType"/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_work">休息日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_pass" class="radio-item audit-no-pass" data-status="2" name="dataType"/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_pass">节假日</label>\
									</div>\
								</div>\
							</div>\
							<div class="charge-wp rule-item">\
								<div class="rule-tip">收费时段</div>\
								<div class="charge-con item-con" >\
									<input class="charge-text" type="text" id="charge" placeholder="请设置时段" readonly/>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">应用模块</div>\
								<div class="date-type-con item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										  <input type="checkbox" id="part" class="check-item enterprise-wx" name="applicationMeeting"/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="part">预定会议室</label>\
									</div>\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" class="check-item enterprise-wx" name="applicationAirAddTime"/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">空调加时</label>\
									</div>\
								</div>\
							</div>\
							<div class="remark-wp rule-item">\
								<div class="rule-tip">备注</div>\
								<textarea class="remark-con item-con" placeholder="请输入备注"></textarea>\
							</div>\
						</div>\
						<div class="dialog-btm">\
							<button class="cancel-rule-btn btn-white">取消</button>\
							<button class="sure-rule-btn btn-red">保存</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"priceTypeTpl": '<div class="priceType-content">\
				<%if(priceType==1) {%>\
					<div class="price-con">\
						<div class="price-hour">\
							每平米<input type="text" class="hour-text" value="<%=priceHour%>"/>元/每小时\
						</div>\
					</div>\
				<%} else if(priceType==2) {%>\
					<div class="price-con">\
						<div class="price-hour">\
							每&nbsp;&nbsp;&nbsp;层<input type="text" class="floor-Hour-text" value="<%=floorPriceHour%>"/>元/每小时\
						</div>\
						<div class="price-hour">\
							每半层<input type="text" class="halfFloor-Hour-text" value="<%=halfFloorPriceHour%>"/>元/每小时\
						</div>\
					</div>\
				<%}else{}%>\
			</div> ',
			"ruleEditTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-rule-con">\
					<div class="title-wp">\
						<span>编辑规则</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-rule-wp">\
						<div class="rule-con">\
							<div class="rule-name-wp rule-item">\
								<div class="rule-tip">规则名称</div>\
								<input class="rule-name item-con" type="text" placeholder="请输入规则名称" value="<%=ruleName%>">\
							</div>\
							<div class="price-wp rule-item">\
								<div class="rule-tip">收费规则</div>\
								<div class="rule-con-box">\
									<div class="date-type-con">\
										<%if(priceType==2){%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus1" class="radio-item audit-pass" data-status="1" name="priceType" disabled/>\
													<span class="radio-inner"></span>\
												</span>		\
												<label class="radio-con radio-not-operate" for="priceStatus1">平米每小时</label>\
											</div>\
										<%} else {%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus1" class="radio-item audit-pass" data-status="1" name="priceType" <%if(priceType==1){%> checked <%}%>/>\
													<span class="radio-inner"></span>\
												</span>		\
												<label class="radio-con" for="priceStatus1">平米每小时</label>\
											</div>\
										<%}%>\
										<%if(priceType==1){%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus2" class="radio-item audit-pass" data-status="2" name="priceType" disabled/>\
													<span class="radio-inner"></span>\
												</span>\
												<label class="radio-con radio-not-operate" for="priceStatus2">层（半层）每小时</label>\
											</div>\
										<%} else {%>\
											<div class="radio-wp">\
												<span class="radio-input">\
													<input type="radio" id="priceStatus2" class="radio-item audit-pass" data-status="2" name="priceType" <%if(priceType==2){%> checked <%}%>/>\
													<span class="radio-inner"></span>\
												</span>\
												<label class="radio-con" for="priceStatus2">层（半层）每小时</label>\
											</div>\
										<%}%>\
									</div>\
									<div class="priceType-content"></div>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">日期类型</div>\
								<div class="date-type-con item-con">\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="work" class="radio-item audit-pass" data-status="0" name="dataType" <%if(dataType==0){%> checked <%}%>/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="work">工作日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_work" class="radio-item audit-no-pass" data-status="1" name="dataType" <%if(dataType==1){%> checked <%}%>/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_work">休息日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_pass" class="radio-item audit-no-pass" data-status="2" name="dataType" <%if(dataType==2){%> checked <%}%>/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_pass">节假日</label>\
									</div>\
								</div>\
							</div>\
							<div class="charge-wp rule-item">\
								<div class="rule-tip">收费时段</div>\
								<div class="charge-con item-con" >\
									<input class="charge-text" type="text" id="charge" placeholder="请设置时段" value="<%=startTime%> - <%=endTime%>" readonly/>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">应用模块</div>\
								<div class="date-type-con item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										  <input type="checkbox" id="part" class="check-item enterprise-wx" name="applicationMeeting" <%if(applicationMeeting==1){%> checked <%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="part">预定会议室</label>\
									</div>\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" class="check-item enterprise-wx" name="applicationAirAddTime" <%if(applicationAirAddTime==1){%> checked <%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">空调加时</label>\
									</div>\
								</div>\
							</div>\
							<div class="remark-wp rule-item">\
								<div class="rule-tip">备注</div>\
								<textarea class="remark-con item-con" placeholder="请输入备注" value=""><%=remark%></textarea>\
							</div>\
						</div>\
						<div class="dialog-btm">\
							<button class="cancel-rule-btn btn-white">取消</button>\
							<button class="sure-rule-btn btn-red">保存</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"applyTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-apply-usSet dialog-view">\
					<div class="title-wp">\
						<span>申请/取消规则</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-apply-wp dialog-box">\
						<div class="apply-con-set" style="height: initial !important;">\
							<div class="apply-item">\
								<div class="apply-tip">提前申请时间</div>\
								<div class="item-con">\
									<input class="apply-time" type="text" placeholder="请输入" value="<%=beforeApplyTime%>">小时\
								</div>\
							</div>\
							<div class="apply-item">\
								<div class="apply-tip">提前取消时间</div>\
								<div class="item-con">\
									<input class="cancel-time" type="text" placeholder="请输入" value="<%=beforeCancelTime%>">小时\
								</div>\
							</div>\
							<div class="apply-item">\
								<div class="apply-tip">周六日期类型</div>\
								<div class="item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" name="satDataType" class="check-item enterprise-wx" <%if(satDataType==1){%>checked<%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">勾选后非节假日的周六为工作日</label>\
									</div>\
								</div>\
							</div>\
							<div class="apply-item">\
								<div class="apply-tip">用户选择房间</div>\
								<div class="item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" name="isSelectRoom" class="check-item enterprise-wx" <%if(isSelectRoom==1){%>checked<%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">勾选后用户申请加开空调可以选择只开企业部分房间</label>\
									</div>\
								</div>\
							</div>\
							<div>设置审核流程：</div>\
							<div class="div-box" style="margin-top: 10px;">\
								<div class="apply-item line-bottom" style="padding: 0 !important;">\
									<div class="apply-title line-right"><span>公司审核</span></div>\
									<div class="comAudit-content"></div>\
								</div>\
								<div class="apply-item" style="padding: 0 !important;">\
									<div class="apply-title line-right"><span>物业审核</span></div>\
									<div class="proAudit-content"></div>\
								</div>\
							</div>\
						</div>\
						<div class="dialog-btm">\
							<button class="cancel-rule-btn btn-white">取消</button>\
							<button class="sure-rule-btn btn-red">保存</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
		    "comAuditTpl": '<div class="apply-content comAudit-content">\
		    	<%for(var i=0;i<comAuditList.length;i++) {%>\
					<div style="margin-bottom: 10px;">\
					<span><%=comAuditList[i].name%></span>\
					<%if(comAuditList[i].isDisabled===1) {%>\
						<select class="item-com-select device-type" data-task-row="<%=i%>">\
							<option value="">请选择审核角色</option>\
							<%for(var j=0;j<auditList.length;j++) {%>\
								<%if(auditList[j].id==comAuditList[i].value) {%>\
								 	<option value="<%=auditList[j].id%>" selected><%=auditList[j].name%></option>\
								<%} else {%>\
									<%if(auditList[j].id!=comAuditList[0].value&&auditList[j].id!=comAuditList[1].value&&auditList[j].id!=comAuditList[2].value&&auditList[j].id!=comAuditList[3].value&&auditList[j].id!=comAuditList[4].value) {%>\
										<option value="<%=auditList[j].id%>"><%=auditList[j].name%></option>\
									<%}%>\
								<%}%>\
							<%}%>\
						</select>\
					<%} else {%>\
						<select class="item-com-select device-type" disabled="disabled">\
							<option value="">请选择审核角色</option>\
							<%for(var j=0;j<auditList.length;j++) {%>\
								<option value="<%=auditList[j].id%>"><%=auditList[j].name%></option>\
							<%}%>\
						</select>\
					<%}%>\
					</div>\
		    	<%}%>\
		    </div> ',
			"proAuditTpl": '<div class="apply-content proAudit-content">\
		    	<%for(var i=0;i<proAuditList.length;i++) {%>\
					<div style="margin-bottom: 10px;">\
					<%if(i===0) {%>\
						<span class="audit-text"><%=proAuditList[i].name%></span>\
					<%} else {%>\
						<span><%=proAuditList[i].name%></span>\
					<%}%>\
					<%if(proAuditList[i].isDisabled===1) {%>\
						<select class="item-pro-select device-type" data-task-row="<%=i%>">\
						       	<option value="">请选择审核角色</option>\
							<%for(var j=0;j<auditList.length;j++) {%>\
								 <%if(auditList[j].ID==proAuditList[i].value) {%>\
									<option value="<%=auditList[j].ID%>" selected><%=auditList[j].NAME%></option>\
								 <%} else {%>\
								 	<%if(auditList[j].ID!=proAuditList[0].value&&auditList[j].ID!=proAuditList[1].value&&auditList[j].ID!=proAuditList[2].value&&auditList[j].ID!=proAuditList[3].value&&auditList[j].ID!=proAuditList[4].value) {%>\
										<option value="<%=auditList[j].ID%>"><%=auditList[j].NAME%></option>\
									<%}%>\
								 <%}%>\
							<%}%>\
						</select>\
					<%} else {%>\
						<select class="item-pro-select device-type" disabled="disabled">\
						       	<option value="">请选择审核角色</option>\
							<%for(var j=0;j<auditList.length;j++) {%>\
								<option value="<%=auditList[j].ID%>"><%=auditList[j].NAME%></option>\
							<%}%>\
						</select>\
					<%}%>\
					</div>\
		    	<%}%>\
		    </div> ',
			"detailTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-detail-con">\
					<div class="title-wp">\
						<span>详情</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-detail-wp">\
						<div class="detail-con">\
							<div class="detail-item">\
								<div class="detail-tip">规则名称:</div>\
								<div class="item-con"><%=ruleName%></div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">单价:</div>\
								<div class="item-con">每户<%=priceHouse%>元/每小时,每平米<%=priceHour%>元/每小时</div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">日期类型:</div>\
								<div class="item-con">\
									<%if(dataType==0){%>\
										工作日\
									<%}else if(dataType==1){%>\
										休息日\
									<%}else if(dataType==2){%>	\
										节假日\
									<%}%>\
								</div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">收费时段:</div>\
								<div class="item-con"><%=startTime%> - <%=endTime%></div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">应用模块:</div>\
								<div class="item-con">\
									<%if(applicationAirAddTime==1){%>\
										预定会议室&nbsp;\
									<%}%>\
									<%if(applicationMeeting==1){%>\
										空调加时\
									<%}%>\
								</div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">备注:</div>\
								<div class="item-con"><%=remark%></div>\
							</div>\
						</div>\
					</div>\
				</div>\
			</div>	',
	}
	template.config("escape", false);
	var contextPath =  $("#contextPath").val();
	var table;
	var orgId;
	var comAuditList;
	var proAuditList;
	var comDataList;
	var proDataList;
	var priceType;

	var appSend = {
			init: function(){
				userType = $("#userType").val();
				this.getProjectList();
			},
			render: function(){
				this.rendertable();
//				$("body").append(template.render(tpl.applyTpl))
			},
			bindEvent: function(){
				var _this = this;
				layui.use('laydate', function(){
					  var laydate = layui.laydate;

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

					  });
					  laydate.render({
						    elem: '#endTime' //提交结束时间
					  });

					});
				//重置
				$(".set-btn").on("click",function(){
					$("#conditionform select").val("");
					let contextPath = window.location.hostname === 'localhost' ? '//wx.palmnest.com/super_cloud' : "/super_cloud"
					if (userType == 1 || userType == 2|| userType == 6) {
						$('#comdId').val(companyArr[0].label).attr('comd__id', companyArr[0].value).attr('meetCode', companyArr[0].meetCode);
						companyId = $("#conditionform #comdId").attr('comd__id');
						_this.setOrgData(contextPath);
					} else {
						companyId = localStorage.getItem("companyId")
						orgId = localStorage.getItem('orgId');
						_this.setOrgData(contextPath);
					}
					 // _this.againRenderTable();
				});
				//新增规则
				$(".add-rule").on("click",function(){
					var laydate = layui.laydate;
					$("body").append(template.render(tpl.ruleTpl)({
						priceType: priceType
					}));
					_this.upPriceTypeUI({
						priceType: priceType,
						priceHour: '',
						floorPriceHour: '',
						halfFloorPriceHour: ''
					});

					//关闭弹窗
					$(".close-btn,.dialog-wp .cancel-rule-btn ").on("click",function(){
						$(".dialog-wp").remove();
					});
					laydate.render({
					    elem: '#charge'
					    ,type: 'time'
					    ,range: true
					    ,format: 'HH:mm'
				    });
					//点击保存绑定事件
					$(".sure-rule-btn").on("click",function(){
						_this.addRuleEvent();
					});
					// 点击按面积
					$("#priceStatus1").on('click', function () {
						priceType = Number($(this).attr("data-status"));
						_this.upPriceTypeUI({
							priceType: priceType,
							priceHour: '',
							floorPriceHour: '',
							halfFloorPriceHour: ''
						});
					});
					// 点击按层/半层
					$("#priceStatus2").on('click', function () {
						priceType = Number($(this).attr("data-status"));
						_this.upPriceTypeUI({
							priceType: priceType,
							priceHour: '',
							floorPriceHour: '',
							halfFloorPriceHour: ''
						});
					});
				});
				//申请/取消规则
				$(".apply-or-cancel-rule").on("click",function(){
					comAuditList = [];
					proAuditList = [];

					comDataList = [
						{name: '一审', value: '', isDisabled: 1},
						{name: '二审', value: '', isDisabled: 0},
						{name: '三审', value: '', isDisabled: 0},
						{name: '四审', value: '', isDisabled: 0},
						{name: '五审', value: '', isDisabled: 0}
					];
					proDataList = [
						{name: '一审', value: '', isDisabled: 1},
						{name: '二审', value: '', isDisabled: 0},
						{name: '三审', value: '', isDisabled: 0},
						{name: '四审', value: '', isDisabled: 0},
						{name: '五审', value: '', isDisabled: 0}
					];

					_this.getRule(function(data){
						$("body").append(template.render(tpl.applyTpl)(data));
						//关闭弹窗
						$(".close-btn,.dialog-wp .cancel-rule-btn ").on("click",function(){
							$(".dialog-wp").remove();
						});
						$(".sure-rule-btn").on("click",function(){
							_this.saveRuleConfig();
						});
						_this.getPermByCheckRole(function(comData) {
							if (comData) {
								comAuditList = comData;
							}
							_this.getRoleByOrgAndSysCode(function (proData) {
								if (proData) {
									proAuditList = proData;
								}
								_this.getGoodsConfig(function (auditList) {
									if (auditList) {
										if (auditList['airVirList'].length>0) {
											for (let i=0;i<auditList['airVirList'].length;i++) {
												comDataList[i].value = auditList['airVirList'][i].roleId;
												comDataList[i].isDisabled = 1;
												if (i<comDataList.length-1) {
													comDataList[i+1].isDisabled = 1;
												}
											}
										}
										if (auditList['airProList'].length>0) {
											for (let i=0;i<auditList['airProList'].length;i++) {
												proDataList[i].value = auditList['airProList'][i].roleId;
												proDataList[i].isDisabled = 1;
												if (i<proDataList.length-1) {
													proDataList[i+1].isDisabled = 1;
												}
											}
										}
									}
									_this.upDataComAuditUI();
									_this.upDataProAuditUI();
								});
							})
						});
					})
				})

				let contextPath = window.location.hostname === 'localhost' ? '//wx.palmnest.com/super_cloud' : "/super_cloud"
				if (userType == 1 || userType == 2|| userType == 6) {
					_this.getCompanyList({}, contextPath)
					$('body').on("focus", '#comdId', function (event) {
						_this.setOrgCompanyData($('#comdId'), 'comd__id', 'comd__span' , companyArr);
					});
					$('body').on('blur', '#comdId', function (event) {
						var _that = $(this)
						cjqt.search_fuzzy.event_blur(_that, 'comd__id')
					});
					$('body').on('click', '.select_wrap>.comd__span', function (event) {
						event.preventDefault();
						event.stopPropagation();
						/* Act on the event */
						var _that = $(this)
						cjqt.search_fuzzy.event_click(_that, 'comd__id', '#comdId')
						companyId = $("#conditionform #comdId").attr('comd__id')
						_this.setOrgData(contextPath);
					});
					$('body').on("input",'#comdId',function(event){ // input内容变化执行
						_this.setFuzzyFilter($('#comdId').val(), companyArr, backData => {
							_this.setOrgCompanyData($('#comdId'), 'comd__id', 'comd__span' , backData);
						})
					});
				} else {
					companyId = localStorage.getItem("companyId")
					orgId = localStorage.getItem('orgId');
					_this.setOrgData(contextPath);
				}
			},
			setOrgData: function(contextPath) {
				var _this = this;
				_this.getOrgList({
					pageNo: 1,
					pageSize: 9999,
					"conditions[companyId]": companyId
				}, contextPath)
				$('body').on("focus", '#orgId', function (event) {
					_this.setOrgCompanyData($('#orgId'), 'org__id', 'org__span' , orgArr);
				});
				$('body').on('blur', '#orgId', function (event) {
					var _that = $(this)
					cjqt.search_fuzzy.event_blur(_that, 'org__id')
				});
				$('body').on('click', '.select_wrap>.org__span', function (event) {
					event.preventDefault();
					event.stopPropagation();
					/* Act on the event */
					var _that = $(this)
					orgId = $("#conditionform #orgId").attr('org__id')
					cjqt.search_fuzzy.event_click(_that, 'org__id', '#orgId')
				});
				$('body').on("input",'#orgId',function(event){
					_this.setFuzzyFilter($('#orgId').val(), orgArr, backData => {
						_this.setOrgCompanyData($('#orgId'), 'org__id', 'org__span' , backData);
					})
				});
			},
			// 获取运营中心数据
			getCompanyList: function (obj, contextPath) {
				var _this = this;
				companyArr = [];
				$.ajax({
					url: contextPath + '/api/organisation/company/getselectlist',
					type: "POST",
					dataType: "json",
					data: obj,
					success: function (data) {
						if (data.code == 200 && data.data) {
							companyArr = data.data;
							_this.setInputData($('#comdId'), 'comd__id', 'comd__span' , companyArr);
							companyId = $("#conditionform #comdId").attr('comd__id')
							_this.setOrgData(contextPath);
						} else {
							layer.msg(data.msg);
						}
					}
				});
			},
			// 获取项目数据
			getOrgList(obj, contextPath) {
				var _this = this;
				orgArr = [];
				$.ajax({
					url: contextPath + '/api/organisation/organization/getselectlist',
					type: "POST",
					dataType: "json",
					data: obj,
					success: function (data) {
						if (data.code == 200 && data.data) {
							orgArr = data.data;
							_this.setInputData($('#orgId'), 'org__id', 'org__span', orgArr);
						} else {
							layer.msg(data.msg);
						}
					}
				});
			},
			setInputData: function(_this, attrName, liClassName, dataArr) {
				// 先清空
				_this.attr(attrName, '')
				var html = ''
				if (dataArr.length) {
					_this.val(dataArr[0].label).attr(attrName, dataArr[0].value).attr('meetCode', dataArr[0].meetCode);
					if (attrName == 'org__id') {
						$('#conditionform  .search-btn').click();
					}
					$.each(dataArr, function (index, el) {
						var meetCode = el.meetCode || ''
						html += '<span class="' + liClassName + '" id="' + el.value + '" meetCode="' + meetCode + '">'
						html += el.label
						html += '</span>'
					});
					_this.parent().find('.select_wrap').empty().append(html);
				} else {
					_this.val('').attr(attrName, '').attr('meetCode', '');
					_this.parent().find('.select_wrap').empty().hide()
				}
			},
			setOrgCompanyData: function(_this, attrName, liClassName, dataArr) {
				var html = ''
				if (dataArr.length) {
					_this.parent().find('.select_wrap').show()
					$.each(dataArr, function (index, el) {
						var meetCode = el.meetCode || ''
						html += '<span class="' + liClassName + '" id="' + el.value + '" meetCode="' + meetCode + '">'
						html += el.label
						html += '</span>'
					});
					_this.parent().find('.select_wrap').empty().append(html)
				} else {
					_this.parent().find('.select_wrap').empty().hide()
				}
			},
			setFuzzyFilter: function (strLabel, dataArr, backData) {
				const fuzzyArr = dataArr; // 内容省略
				const objArr = [];
				fuzzyArr.forEach(item => {
					if (item.label.includes(strLabel)) {
						objArr.push(item);
					}
				});
				backData(objArr);
			},
			// 渲染收费规则切换内容
			upPriceTypeUI: function(data) {
				$(".priceType-content").replaceWith(template.render(tpl.priceTypeTpl)(data));
			},
			// 渲染公司审核中审核角色UI
			upDataComAuditUI: function() {
				var _this = this;
				$(".comAudit-content").replaceWith(template.render(tpl.comAuditTpl)({comAuditList:comDataList,auditList:comAuditList}));
				$(".item-com-select").change(function(){ // 公司审核
					var indexRow = Number($(this).attr("data-task-row"));
					var selected=$(this).children('option:selected').val();
					if (comDataList[indexRow].value !== selected) {
						comDataList[indexRow].value = selected;
						if (comDataList[indexRow].value === '') {
							for (var i=indexRow+1;i<comDataList.length;i++) {
								comDataList[i].value = '';
								comDataList[i].isDisabled = 0;
							}
						} else {
							if (indexRow<comDataList.length-1) {
								comDataList[indexRow+1].isDisabled = 1;
							}
						}
						_this.upDataComAuditUI();
					}
				});
			},
			// 渲染物业审核中审核角色UI
			upDataProAuditUI:function() {
				var _this = this;
				console.log('proDataList', proDataList);
				console.log('proAuditList', proAuditList);
				$(".proAudit-content").replaceWith(template.render(tpl.proAuditTpl)({proAuditList:proDataList,auditList:proAuditList}));
				$(".item-pro-select").change(function() { // 物业审核
					var indexRow = Number($(this).attr("data-task-row"));
					var selected=$(this).children('option:selected').val();
					console.log('indexRow', indexRow);
					console.log('selected', selected);
					if (proDataList[indexRow].value !== selected) {
						proDataList[indexRow].value = selected;
						if (proDataList[indexRow].value === '') {
							for (var i=indexRow+1;i<proDataList.length;i++) {
								proDataList[i].value = '';
								proDataList[i].isDisabled = 0;
							}
						} else {
							if (indexRow<proDataList.length-1) {
								proDataList[indexRow+1].isDisabled = 1;
							}
						}
						_this.upDataProAuditUI();
					}
				});
			},
			rendertable: function(){
				var _this = this;
				layui.use('table', function(){
					   table = layui.table;

					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user'
				    	 ,skin: 'nob' //行边框风格
			    		  ,even: true //开启隔行背景
			    		  ,size: 'lg' //小尺寸的表格
					    ,cols: [[
					      {
//				    		   field:'id',
//				    		   title: 'ID',
//				    		   minWidth:60
//				    	  },{
				    		  field:'ruleName',
				    		  title: '规则名称',
				    		  minWidth:200
			    		  },{
			    			  field:'priceDisplay',
			    			  title: '单价',
			    			  minWidth:300
	    				  },{
		    				  field:'chargeTimeDisplay',
		    				  title: '收费时段',
		    				  minWidth:200
	    				  },{
		    				  field:'applicationAirAddTime',
		    				  title: '应用模块',
		    				  minWidth:150,
		    				  templet: function(data){
		    					var str = '';
		    					if(data.applicationMeeting==1){
		    						str += '预定会议室 &nbsp'
		    					};
		    					if(data.applicationAirAddTime==1){
		    						str += '空调加时'
		    					};
		    					return str;
	    					  }
	    				  },{
		    				  field:'isEnabled',
		    				  title: '状态',
		    				  minWidth:80,
		    				  templet: function(data){
			    					if(data.isEnabled==1){
			    						return '启用'
			    					}else if(data.isEnabled==0){
			    						return '停用'
			    					}
		    					  }
	    				  },{
		    				  field:'createTime',
		    				  title: '创建时间',
		    				  minWidth:180
	    				  },{
	    					  field:'approval_user',
	    					  title: '查看详情',
	    					  minWidth:120,
	    					  templet: function(data){
		    						 return "<span data-id="+data.id+" class='check-blue-btn check-detail'>详情<span>"
	    					  }
	  					  },{
							  field:'id',
							  title: '操作',
							  fixed: "right",
							  minWidth: 180,
							  templet: function(data){
								  var str = '';
    							  str+= "<span data-id="+data.id+" class='check-blue-btn edit-btn'>编辑</span>";
    							  if(data.isEnabled==1){
    								  str+= "<span data-id="+data.id+" data-status="+data.isEnabled+" class='red disable-btn'>停用</span>"
    							  }else{
    								  str+= "<span data-id="+data.id+" data-status="+data.isEnabled+" class='red enable-btn'>启用</span>"
    							  }

    							  str+= "<span data-id="+data.id+" data-status="+data.isEnabled+" class='red del-btn'>删除</span>"
	    						 return str;
	    					  }
						  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						    method: 'post',
						    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){
						        //如果是异步请求数据方式，res即为你接口返回的信息。

						        //详情
						        $(".check-detail").on("click",function(){
						        	var id = $(this).data("id");

						        	//显示详情弹窗
						        	_this.showDetailDialog(id)
						        });
						        //编辑
						        $(".edit-btn").on("click",function(){
						        	var id = $(this).data("id");
						        	_this.showEditDialog(id);
						        })
						        //启用
						        $(".enable-btn").on("click",function(){
						        	var id = $(this).data("id");

						        	_this.showOperate(id,'enable')
						        })
						        //停用
						        $(".disable-btn").on("click",function(){
						        	var id = $(this).data("id");

						        	_this.showOperate(id,'disable')
						        })
						        //删除

						        $(".del-btn").on("click",function(){
						        	var id = $(this).data("id");

						        	_this.delAjax(id)
						        })
						      }
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  orgId = $("#conditionform #orgId").attr('org__id');
						  if (!orgId&&userType!=1&&userType!=2) {
							  layer.msg('请选择项目')
							  return;
						  }
						  _this.getAirRuleByOrgId();
						  _this.againRenderTable();
					  });
					});
			},
			/*重新渲染table*/
			againRenderTable: function(){
				table.reload('testReload', {
					url: contextPath + "/rule/list",
					where: {
						orgId: orgId,
						isEnabled: $("#conditionform .isEnabled").val(),
						dataType: $("#conditionform .dataType").val()
					} ,
					  page: {
					    curr: 1 //重新从第 1 页开始
					  }
				});
			},
			//调用项目列表接口，如果是1个的话就显示审核
			getProjectList:function(){
				this.render();
				this.bindEvent();
			},
			/**
			 * 根据项目Id获得空调加时规则
			 */
			getAirRuleByOrgId: function(){
				priceType = 0;
				$.ajax({
					type: "post",
					url: contextPath + "/air/getRuleByOrgId",
					data:{
						orgId: orgId
					},
					success: function(data){
						if(data.code == 200){
							if (data.data.priceType) {
								priceType = data.data.priceType;
							}
						}
					},
					error: function(error){}
				});
			},
			/**
			 * 启用或者停用
			 * @param id
			 * @param type 操作内型 enable启用 disable禁用
			 */
			showOperate: function(id,type){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: contextPath+ "/rule/enableOrDisable",
					   data:{
						   id: id,
						   operationType: type
					   },
					   success: function(data){
						   if(data.code == 200){
							   _this.againRenderTable();
							   if(type=='enable'){
								   layer.msg("启用成功")
							   }else{
								   layer.msg("停用成功")
							   }
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(error){
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 删除
			 * @param id
			 */
			delAjax: function(id){
				var _this = this;
				layer.confirm('确定删除?', {
					  btn: ['确定', '取消']
					}, function(index, layero){
						 layer.close(index); //如果设定了yes回调，需进行手工关闭
						 $.ajax({
							   type: "post",
							   url: contextPath+ "/rule/del",
							   data:{
								   id: id
							   },
							   success: function(data){
								   if(data.code == 200){
									   _this.againRenderTable();
									   _this.getAirRuleByOrgId();
									   layer.msg("删除成功！")
								   }else{
									   layer.msg(data.msg)
								   }
							   },
							   error: function(error){
								   layer.msg('error')
							   }
					   });
					}, function(index){

					});
			},
			/**
			 * 编辑或者新增规则弹窗绑定事件
			 * @param id
			 */
			addRuleEvent: function(id){
				var _this = this;
				var result = {
						id: !!id ? id: '',
						ruleName: '',
						priceType: '', //收费规则（1. 按面积 2. 按层/半层）
						priceHouse: '',//每户/元
						priceHour: '',//每平米/元
						floorPriceHour: '',//每层/元
						halfFloorPriceHour: '',//每半层/元
						dataType: '',//日期类型(0工作日\1休息日2节假日
						startTime:'',//收费开始时间
						endTime: '',//收费结束时间
						applicationMeeting: '',//应用模块(预定会议室 0 1
						applicationAirAddTime: '',//应用模块(空调加时 0 1
						remark:''//备注
				}
				//规则名称
			    var ruleName = $(".rule-name").val();
			    if(!ruleName){
					layer.msg('请输入规则名称！');
					return false;
				};
				result['ruleName'] = ruleName;

				if (priceType != 1&&priceType != 2) {
					layer.msg('请选择收费规则！');
					return false;
				}
				result['priceType'] = priceType;

				//每户/元
				var priceHouse = $(".house-text").val();
				//if(!priceHouse){
				//	layer.msg('请输入每户多少元/每小时！');
				//	return false;
				//};
				result['priceHouse'] = 0;

				if (priceType == 1) {
					//每平米/元
					var priceHour = $(".hour-text").val();
					if(!priceHour){
						layer.msg('请输入每户多少元/每小时！');
						return false;
					};
					result['priceHour'] = priceHour;

					result['floorPriceHour'] = '';
					result['halfFloorPriceHour'] = '';
				}

				if (priceType == 2) {

					var floorHour = $(".floor-Hour-text").val();
					if(!floorHour){
						layer.msg('请输入每层多少元/每小时！');
						return false;
					};
					result['floorPriceHour'] = floorHour;

					var halfFloorHour = $(".halfFloor-Hour-text").val();
					if(!halfFloorHour){
						layer.msg('请输入每半层多少元/每小时！');
						return false;
					};
					result['halfFloorPriceHour'] = halfFloorHour;

					result['priceHour'] = '';
				}

				//日期类型
				var dataType = $(".radio-wp .radio-item[name='dataType']:checked").data("status");
				if(dataType != 0 && dataType != 1 && dataType != 2 ){
					layer.msg('请选择日期类型！');
					return false;
				}
				result['dataType'] = dataType;

				//收费时间
				var time = $(".charge-text").val();
				if(!time ){
					layer.msg('请选择收费时段！');
					return false;
				}
				result['startTime'] = time.substring(0,5);
				result['endTime'] = time.substring(time.length-5);

				//应用模块
				var applicationMeeting = $(".checkbox-wp .check-item[name='applicationMeeting']").prop('checked') ? 1 : 0;
				var applicationAirAddTime= $(".checkbox-wp .check-item[name='applicationAirAddTime']").prop('checked') ? 1 : 0;
				if(!applicationMeeting && !applicationAirAddTime){
					layer.msg('请选择应用模块！');
					return false;
				}
				result['applicationMeeting'] = applicationMeeting;
				result['applicationAirAddTime'] = applicationAirAddTime;

			   //备注
				var remark = $(".remark-con").val();
				result['remark'] = remark;

				_this.ruleAjax(result);

			},
			/**
			 * 新增或者编辑规则请求
			 * @param data
			 */
			ruleAjax: function(data){
				var _this = this;
				 var tip = !!data.id ? '编辑成功!' : '新增成功!';
				 $.ajax({
					   type: "post",
					   url: contextPath+ "/rule/addOrSaveRole",
					   data:data,
					   success: function(data){
						   if(data.code == 200){
							   _this.againRenderTable();
							   $(".dialog-wp").remove();
							   layer.msg(tip)
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(error){
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 显示编辑弹窗
			 * @param id
			 */
			showEditDialog: function(id){
				var _this = this;
				_this.ajaxDetail(id,function(data){
					var laydate = layui.laydate;
					$("body").append(template.render(tpl.ruleEditTpl)(data));
					_this.upPriceTypeUI({
						priceType: priceType?priceType:1,
						priceHour: data.priceHour,
						floorPriceHour: data.floorPriceHour,
						halfFloorPriceHour: data.halfFloorPriceHour
					});
					//关闭弹窗
					$(".close-btn,.dialog-wp .cancel-rule-btn ").on("click",function(){
						$(".dialog-wp").remove();
					});
					laydate.render({
					    elem: '#charge'
					    ,type: 'time'
					    ,range: true
					    ,format: 'HH:mm',

				    });
					//点击保存绑定事件
					$(".sure-rule-btn").on("click",function(){
						_this.addRuleEvent(id);
					})
				})
			},
			/**
			 * 请求详情
			 * @param id
			 * @param callback
			 */
			ajaxDetail: function(id,callback){
				var _this = this;

				 $.ajax({
					   type: "post",
					   url: contextPath+ "/rule/getDetail",
					   data:{
						   id: id
					   },
					   success: function(data){
						   if(data.code == 200){
							  callback(data.data);
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(error){
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 显示详情弹窗
			 * @param id
			 */
			showDetailDialog: function(id){
				var _this = this;
				_this.ajaxDetail(id,function(data){
					$("body").append(template.render(tpl.detailTpl)(data));
					//关闭弹窗
					$(".close-btn").on("click",function(){
						$(".dialog-wp").remove();
					});
				})
			},
			//获取规则
			getRule: function(callback){
				var _this = this;
				 $.ajax({
					   type: "post",
					   url: contextPath+ "/projectConfig/getProjectConfig",
					   success: function(data){
						   if(data.code == 200){

							  callback(data.data);
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(error){
						   layer.msg('error')
					   }
			   });
			},
		    // 获取公司审核角色
			getPermByCheckRole: function(callback) {
				var _this = this;
				$.ajax({
					type: "post",
					url: contextPath + "/admin/virPerm/getPermByCheckRole",
					data: {
						orgId: orgId,
						pageType: '2'
					},
					success: function(data){
						if(data.code == 200){
							callback(data.data);
						}else{
							layer.msg(data.msg);
							callback();
						}
					},
					error: function(error){
						layer.msg('error')
					}
				});
			},
			// 获取物业审核角色
			getRoleByOrgAndSysCode: function(callback) {
				var _this = this;
				$.ajax({
					type: "post",
					url: contextPath + "/admin/role/getRoleByOrgAndSysCode",
					data: {
						orgId: orgId,
						pageType: '2'
					},
					success: function(data){
						if(data.code == 200){
							callback(data.data);
						}else{
							layer.msg(data.msg);
							callback();
						}
					},
					error: function(error){
						layer.msg('error')
					}
				});
			},
			// 获取审核流程配置获取
			getGoodsConfig: function(callback) {
				var _this = this;
				$.ajax({
					type: "post",
					url: contextPath + "/admin/virPerm/getGoodsConfig",
					data: {
						orgId: orgId
					},
					success: function(data){
						if(data.code == 200){
							callback(data.data);
						}else{
							layer.msg(data.msg);
							callback();
						}
					},
					error: function(error){
						layer.msg('error')
					}
				});
			},
			//点击保存 申请/取消规则
			saveRuleConfig: function(){
				var _this = this;
				var result = {
						beforeApplyTime: '',//提前申请时间
						beforeCancelTime: '',//提前取消时间
						satDataType: '',//周六日期类型(勾选后非节假日的周六为工作日 0否 1是
					    isSelectRoom: ''//用户申请加开空调可以选择只开企业部分房间
				};
				//提前申请时间
				var beforeApplyTime = $(".apply-time").val();
				if(!beforeApplyTime ){
					layer.msg('请输入提前申请时间！');
					return false;
				}
				result['beforeApplyTime'] = beforeApplyTime;

				var beforeCancelTime = $(".cancel-time").val();
				if(!beforeCancelTime ){
					layer.msg('请输入提前取消时间！');
					return false;
				}
				if (proDataList[0].value==='') {
					layer.msg('请选择物业审核一审人员！');
					return false;
				}
				result['beforeCancelTime'] = beforeCancelTime;

				result['satDataType'] = $(".checkbox-wp .check-item[name='satDataType']").prop('checked') ? 1 : 0;

				result['isSelectRoom'] = $(".checkbox-wp .check-item[name='isSelectRoom']").prop('checked') ? 1 : 0;

				 _this.getSaveProjectConfig(result);
			},
			// 基础配置提交保存
			getSaveProjectConfig: function (result) {
				var _this = this;
				$.ajax({
					type: "post",
					url: contextPath+ "/projectConfig/saveProjectConfig",
					data: result,
					success: function(data){
						if(data.code == 200){
							_this.getSaveOrUpdateAirConfig()
						}else{
							layer.msg(data.msg)
						}
					},
					error: function(error){
						layer.msg('error')
					}
				});
			},
			// 新增或者修改空调审核配置
			getSaveOrUpdateAirConfig: function () {

				var strVirList = '';
				var stProList = '';

				for (let i=0;i<comDataList.length;i++) {
					if (comDataList[i].value !== '') {
						if (strVirList === '') {
							strVirList = comDataList[i].value;
						} else {
							strVirList = strVirList + ';' + comDataList[i].value;
						}
					}
				}

				for (let i=0;i<proDataList.length;i++) {
					if (proDataList[i].value !== '') {
						if (stProList === '') {
							stProList = proDataList[i].value;
						} else {
							stProList = stProList + ';' + proDataList[i].value;
						}
					}
				}

				$.ajax({
					type: "post",
					url: contextPath+ "/admin/virPerm/saveOrUpdateAirConfig",
					data: {
						orgId: orgId,
						virList: strVirList,
						proList: stProList
					},
					success: function(data){
						if(data.code == 200){
							$(".dialog-wp").remove();
							layer.msg("申请/取消规则成功！");
						}else{
							layer.msg(data.msg)
						}
					},
					error: function(error){
						layer.msg('error')
					}
				});
			}
	};
	appSend.init();
})
