/**
 *
 * by 南山  维修订单管理
 */
$(function(){
	var layer;
	var orgList = [];
	var tableEditData=[]
	layui.use('layer', function(){
		   layer = layui.layer;
	});
	var contextPath = $("#contextPath").val();
	var tpl = {
			"personList" :' <% for(var j=0;j<userList.length;j++){%>\
				<div class="person-item">\
				<div class="radio-wp" >\
					<span class="radio-input">\
						<input type="radio" id="outdate-<%=j%>"  class="radio-item" name="date" data-id="<%=userList[j].id%>"/>\
						<span class="radio-inner"></span>\
					</span>		\
					<label class="radio-con" for="outdate-<%=j%>"><%=userList[j].name%></label>\
				</div>\
			</div>\
		<%}%>',
			"choosePersonTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-choose-con">\
					<div class="title-wp">\
						<span>选择工程人员</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-choose-wp">\
						<div class="choose-person-wp clearfloat">\
							<div class="choose-person-left">\
								<h2 class="title">维修类型</h2>\
								<ul class="repair-list">\
									<% for(var i=0;i<typeList.length;i++){%>\
										<li class="repair-item <%if(i==0){%>active<%}%>" data-id="<%=typeList[i].id%>"><%=typeList[i].itemname%></li>\
									<%}%>\
								</ul>\
							</div>\
							<div class="choose-person-right">\
								<h3 class="title">工程人员</h3>\
								<div class="person-list clearfloat">\
									<% for(var j=0;j<userList.length;j++){%>\
										<div class="person-item">\
											<div class="radio-wp" >\
												<span class="radio-input">\
													<input type="radio" id="outdate-<%=j%>"  class="radio-item" name="date" data-id="<%=userList[j].id%>"/>\
													<span class="radio-inner"></span>\
												</span>		\
												<label class="radio-con" for="outdate-<%=j%>"><%=userList[j].name%></label>\
											</div>\
										</div>\
									<%}%>\
								</div>\
							</div>\
						</div>\
						<div class="choose-person-btns">\
							<button class="btn-red choose-person-sure-btn">确认</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"visitTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-visit-con">\
					<div class="title-wp">\
						<span>回访编辑</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-visit-wp">\
						<div class="visit-wp">\
							<span class="tips">回访星级：</span>\
							<div class="star-wp">\
								<span class="star"></span>\
								<span class="star"></span>\
								<span class="star"></span>\
								<span class="star"></span>\
								<span class="star"></span>\
							</div>\
						</div>\
						<div class="visit-wp visit-record-wp">\
							<span class="tips">回访记录：</span>\
							<textarea class="visit-record" data-id="<%=id%>"></textarea>\
						</div> \
						<div class="visit-btns-wp">\
							<button class="btn-red visit-btn">确认</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"detailTpl": '<div class="dialog-wp-detail">\
				<div class="mask"></div>\
				<div class="dialog dialog-detail-con">\
					<div class="title-wp">\
						<span>大类详情：\
							<%=data.itemName %>\
					  </span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-detail-wp">\
						<div class="detail-top clearfloat">\
							<div class="detail-top-item clearfloat">\
								<span class="tips">项目名称：</span>\
								<span class="top-item-value"><%=data.orgName %></span>\
							</div>\
							<div class="detail-top-item clearfloat">\
							<span class="tips">大类编号：</span>\
							<span class="top-item-value"><%=data.itemCode%></span>\
							</div>\
							<div class="detail-top-item clearfloat">\
								<span class="tips">大类名称：</span>\
								<span class="top-item-value"><%=data.itemName%></span>\
							</div>\
							<div class="detail-top-item clearfloat">\
								<span class="tips">大类排序：</span>\
								<span class="top-item-value"><%=data.displayOrder%></span>\
							</div>\
							<div class="detail-top-item clearfloat">\
								<span class="tips">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小类：</span>\
								<div class="item-list-box" style="width: 382px !important;height: auto !important;">\
					                <div class="item-cell-title1 cell-line-right cell-line-bottom">小类排序</div>\
									<div class="item-cell-title2 cell-line-bottom">小类名称</div>\
									<div class="item-cell-content-box" style="min-height: 40px;max-height: calc(100% - 40px) !important;height: auto !important;">\
									<%for(var i=0;i<data.list.length;i++){%>\
										<div class="cell-box">\
											<div class="cell-content1 cell-line-right cell-line-bottom">\
											 	<span><%=data.list[i].displayOrder%></span>\
											</div>\
											<div class="cell-content2 cell-line-bottom">\
												<span><%=data.list[i].itemName%></span>\
											</div>\
										</div>\
									<%}%>\
									</div>\
					            </div>\
							</div>\
						</div>\
					</div>\
				</div>\
			</div>	',

		    "addList": '<div class="item-cell-content-box cell-line-bottom content-view">\
				<%for(var i=0;i<minList.length;i++){%>\
					<div class="cell-box">\
						<div class="cell-content1 cell-line-right cell-line-bottom">\
							<input class="cell-item-num" type="number" placeholder="请输入排序" data-task-row="<%=i%>" value="<%=minList[i].displayOrder%>"/>\
						</div>\
						<div class="cell-content2 cell-line-right cell-line-bottom">\
							<input class="cell-item-name" type="text" placeholder="请输入小类名称" data-task-row="<%=i%>" value="<%=minList[i].itemName%>"/>\
						</div>\
						<div class="cell-content3 cell-line-bottom">\
							<button class="cell-del-btn" type="button" data-task-row="<%=i%>">删除</button>\
						</div>\
					</div>\
				<%}%>\
			</div>  ',

			"addTpl": '<div class="dialog-wp addTplMethod">\
				<div class="mask"></div>\
				<div class="dialog dialog-wx-con">\
					<div class="title-wp">\
						<span>新增维修类型</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-wx-wp">\
						<div class="dialog-wx-wrapper">\
							<div class="d-wx-top" style="border-bottom: 0 !important;">\
								<% if(list!=null&&list.length>1){ %>\
								<div class="d-wx-wp d-wx-num-wp">\
									<span class="tips">项目</span>\
									<input class="orgId e-item-text d-item-wp addmethod" name="orgId" id="orgId" placeholder="请选择项目" autocomplete="off"/>\
					    			<span class="select_wrap _hide"></span>\
								</div>\
								<%}else if(list!=null&&list.length==1){%>\
									<input type="hidden" class="orgId e-item-text addmethod" id="orgId" value="<%=list[0].id %>" org__id="<%=list[0].id %>"/>\
								<%}%>\
								<div class="d-wx-wp d-wx-name-wp">\
								<span><div style="color:red;display: inline-block;">*</div>大类编号</span>\
								<input class="d-wx-code add-item-code" placeholder="请输入大类编号"/>\
								</div>\
								<div class="d-wx-wp d-wx-name-wp">\
									<span><div style="color:red;display: inline-block;">*</div>大类名称</span>\
									<input class="d-wx-name add-item-name" placeholder="请输入大类名称"/>\
								</div>\
								<div class="d-wx-wp d-wx-name-wp">\
									<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;排序</span>\
									<input class="d-wx-name add-item-num" type="number" placeholder="请输入排序"/>\
									<div class="text-note">仅支持输入正整数，数字越小排名越前</div>\
								</div>\
								<div  style="width: 600px;">\
					            <div class="item-tip">\
					                <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小类</span>\
					                <div class="item-list-box">\
					                	<div class="item-cell-title1 cell-line-right cell-line-bottom">小类排序</div>\
										<div class="item-cell-title2 cell-line-right cell-line-bottom">小类名称</div>\
										<div class="item-cell-title3 cell-line-bottom">操作</div>\
										<div class="content-view"></div>\
										<div class="cell-add-btn">\
											<button>+ 继续添加</button>\
										</div>\
					                </div>\
					            </div>\
					           </div>\
					     	</div>\
						</div>\
						<div class="d-wx-btns">\
							<button class="cancel-add-btn btn-white">取消</button>\
							<button class="sure-add-btn btn-red">确定</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"updateTpl":  '<div class="dialog-wp updateTplMethod">\
				<div class="mask"></div>\
				<div class="dialog dialog-wx-con">\
					<div class="title-wp">\
						<span>编辑维修类型 : <%=data.itemName %></span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-wx-wp">\
						<div class="dialog-wx-wrapper">\
							<div class="d-wx-top" style="border-bottom: 0 !important;">\
								<% if(list!=null&&list.length>1){ %>\
								<div class="d-wx-wp d-wx-num-wp">\
									<span class="tips">项目</span>\
									<input class="orgId e-item-text d-item-wp addmethod" name="orgId" id="orgId" placeholder="请选择项目" autocomplete="off"/>\
					    			<span class="select_wrap _hide"></span>\
								</div>\
								<%}else if(list!=null&&list.length==1){%>\
									<input type="hidden" class="orgId e-item-text addmethod" id="orgId" value="<%=list[0].id %>" org__id="<%=list[0].id %>"/>\
								<%}%>\
								<div class="d-wx-wp d-wx-name-wp">\
								<span><div style="color:red;display: inline-block;">*</div>大类编号</span>\
								<input class="d-wx-name update-item-code" placeholder="请输入大类编号" value="<%=data.itemCode%>"/>\
								</div>\
								<div class="d-wx-wp d-wx-name-wp">\
									<span><div style="color:red;display: inline-block;">*</div>大类名称</span>\
									<input class="d-wx-name update-item-name" placeholder="请输入大类名称" value="<%=data.itemName%>"/>\
								</div>\
								<div class="d-wx-wp d-wx-name-wp">\
									<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;排序</span>\
									<input class="d-wx-name add-item-num" type="number" placeholder="请输入排序"/>\
									<div class="text-note">仅支持输入正整数，数字越小排名越前</div>\
								</div>\
								<div  style="width: 600px;">\
									<div class="item-tip">\
										<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小类</span>\
										<div class="item-list-box">\
										<div class="item-cell-title1 cell-line-right cell-line-bottom">小类排序</div>\
										<div class="item-cell-title2 cell-line-right cell-line-bottom">小类名称</div>\
										<div class="item-cell-title3 cell-line-bottom">操作</div>\
										<div class="content-view"></div>\
										<div class="cell-add-btn">\
											<button>+ 继续添加</button>\
										</div>\
									</div>\
								</div>\
					           </div>\
					     	</div>\
							</div>\
						<div class="d-wx-btns">\
							<button class="cancel-edit-btn btn-white">取消</button>\
	                        <button class="sure-add-btn btn-red">确定</button>\
						</div>\
					</div>\
				</div>\
			</div>	',

	}
	//编辑的备注信息
//	<div class="d-wx-wp d-wx-id-wp">\
//	<span>大类备注</span>\
//	<input class="d-wx-id" placeholder="请输入大类备注" value="<%=data.wxTemplInfo.name%>"/>\
//</div>\
	var launchRepair = {
			init: function(){
				this.render();
				this.bindEvent();
				this.getOrgList();
			},
			render: function(){
				var _this = this;
				_this.renderTable();
			},
			bindEvent: function(){
				var _this = this;

				//查询
				$("#conditionform input[type='text']").on("keydown",function(event){
					if(event.keyCode == 13){
						$("#conditionform .search-btn").click();
					}
				});
				layui.use('laydate', function(){
					  var laydate = layui.laydate;

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

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

						  })
					});
				//重置
				$(".set-btn").on("click",function(){
					 $("#conditionform .repair-groupname").val(""),
					 $("#conditionform .repair-itemname").val(""),
					 $("#conditionform .repair-groupcode").val(""),
					_this.againRenderTable({type:1});
				});
				//添加模板
				$(".add-temp-btn").on("click",function(){
					var tableData = [{displayOrder:'',itemName:''}];
					//显示 弹窗
					$("body").append(template.render(tpl.addTpl)({list:orgList}));
					$(".content-view").replaceWith(template.render(tpl.addList)({minList:tableData}));
					_this.updateCellView(tableData);
					// 添加数据
					$(".cell-add-btn button").on("click",function() {
						tableData.push({displayOrder:'',itemName:''});
						$(".content-view").replaceWith(template.render(tpl.addList)({minList:tableData}));
						_this.updateCellView(tableData);
					});
					//关闭弹窗
					$(".close-btn,.cancel-add-btn").on("click",function(){
						$(".dialog-wp").remove();
					});
					//确认添加
					$(".addTplMethod .sure-add-btn").on("click",function(){
						console.log('tableData', tableData);
						_this.ajaxAddTemp(tableData);
					});

				});
				//选择工程人员
				$(".choose-person-btn").on("click",function(){
					_this.ajaxRepairType(function(data){
						$("body").append(template.render(tpl.choosePersonTpl)(data));
						$(".close-btn").on("click",function(){
							$(".dialog-wp").remove();
						});
						$(".repair-item").on("click",function(){
							var  repairTypeId = $(this).data("id")
							$(this).addClass("active").siblings().removeClass("active");
							_this.ajaxRepairTypeUser(repairTypeId);
						});
						//确定选中工程人员
						$(".choose-person-sure-btn").on("click",function(){
							var length = $(".person-item .radio-item:checked").length;
							if(!length){
								layer.msg("请选择工程人员")
							}else{
								$("#conditionform .choose-person-btn").data("id",$(".person-item .radio-item:checked").data("id"));
								$(this).closest(".dialog-wp").remove();
							}

						})
					})

				});
				// 模糊搜索
				_this.search_fuzzy()
				_this.getProjectList()
			},
		 	// 更新界面UI
			updateCellView:function(data) {
				var _this = this;
				var tableData = data;
				//排序输入框监听
				$(".cell-item-num").on('input propertychange', function() {
					var indexRow = $(this).attr("data-task-row");
					var content = $(this).attr("value");

					if (indexRow<tableData.length) {
						var dict = tableData[indexRow];
						dict.displayOrder = content;
						tableData.splice(indexRow,1,dict);
					} else {
						console.log('数据越界')
					}
				});
				//小类名称监听
				$(".cell-item-name").on('input propertychange', function() {
					var indexRow = $(this).attr("data-task-row");
					var content = $(this).attr("value");

					if (indexRow<tableData.length) {
						var dict = tableData[indexRow];
						dict.itemName = content;
						tableData.splice(indexRow,1,dict);
					} else {
						console.log('数据越界')
					}
				});
				//删除按钮
				$(".cell-del-btn").on("click",function() {
					var indexRow = $(this).attr("data-task-row");
					tableData.splice(indexRow, 1);
					$(".content-view").replaceWith(template.render(tpl.addList)({minList:tableData}));
					_this.updateCellView(tableData);
				});
			},
			renderEditDialog:function(id,detailList,orgid){
				console.log("detailList",detailList)
				var _this = this;
				_this.tableEditData=[]
				if(detailList.list&&detailList.list.length>0){
					_this.tableEditData=detailList.list
				}else{
					_this.tableEditData=[{ itemId:'', itemCode: '', itemName:'',displayOrder:''}]
				}
				//显示 弹窗
				$("body").append(template.render(tpl.updateTpl)({list:orgList,data:detailList}));
				$(".content-view").replaceWith(template.render(tpl.addList)({minList:_this.tableEditData}));
				_this.updateCellView(_this.tableEditData);
				// 添加数据
				$(".cell-add-btn button").on("click",function() {
					_this.tableEditData.push({ itemId:'', itemCode: '', itemName:'',displayOrder:''});
					$(".content-view").replaceWith(template.render(tpl.addList)({minList:_this.tableEditData}));
					_this.updateCellView(_this.tableEditData);
				});

				//关闭弹窗
				$(".updateTplMethod .close-btn, .cancel-edit-btn").on("click",function(){
					$(".dialog-wp").remove();
				});
				//
				$(".updateTplMethod .sure-add-btn").on("click",function(){
					//修改的时候
					_this.ajaxUpdateTemp(_this.tableEditData,detailList.itemId,orgid);
				});
			},
			getOrgList: function(){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/admin/templ/getOrgList",
					   success: function(data){
						   if(data.code == '200'){
							  orgList = data.data;
							  }
						   $(".updateTplMethod #orgId").on("change",function(){
						  		var that = this
						  		setTimeout(function(){
						  		var id = $(that).attr('org__id');
						  		//重新获取

//                           		_this.getMaterListByOrgId(id);
//         						   _this.getAddressListByOrgId(id)
						  		},200)

						  })
					   },
			   });
			},
			//修改。
			ajaxUpdateTemp:function(tabledata,itemId,orgid){
				var _this = this;
				var tableArr = [];
				console.log("表格数据",tabledata)
				if(!$(".update-item-name").val()){
					layer.msg('请填写大类名称')
					return
				}
				if(!$(".update-item-code").val()){
					layer.msg('请填写大类编号')
					return
				}
				if(tabledata&&tabledata.length>0){
					tableArr = tabledata;
					var isNull = false;
					tabledata.every(itme => {
							if(itme.itemName == ''||itme.itemName == null) {
							isNull = true;
							return false;
						} else {
							return true;
						}
				    })
					if(isNull){
						layer.msg('请填写小类名称')
						return
					}
					if (_this.isTableRepeat(tableArr)) {
						layer.msg('小类名称不能重复');
						return;
					}
				}

				var data ={
					"orgId": orgid,
					'itemName': $(".update-item-name").val(),
					'itemId': itemId,
					'displayOrder': $(".add-item-num").val(),
					'itemCode':$(".update-item-code").val(),
					'list':tableArr,
				};

				$.ajax({
				   type: "post",
				   dataType : 'json',
				   contentType: "application/json;charset=UTF-8",
				   url: $("#contextPath").val()+ "/repairType/save",
				   data: JSON.stringify(data),
				   success: function(data){
					   if(data.code == '200'){
						   $(".dialog-wp").remove();
						   layer.msg('编辑数据成功！')
						  _this.againRenderTable();

					   }else{
						   layer.msg(data.msg)
					   }
				   },
				   error: function(error){
					   layer.msg('error')
				   }
				   });

			},
			isTableRepeat:function(arr) {
				var arrName = [];
				arr.forEach(itme => {
					arrName.push(itme['itemName'])
				});
				var nary = arrName.sort();
				for(var i=0;i<arrName.length;i++){
					if (nary[i]==nary[i+1]){
						return true
					}
				}
				return false
			},
			//新增、
			ajaxAddTemp:function(tabledata){
			   var _this = this;
			   var tableArr = [];
				if(!$(".add-item-name").val()){
					layer.msg('请填写大类名称')
					return
				}
				if(!$(".add-item-code").val()){
					layer.msg('请填写大类编号')
					return
				}

				if(tabledata&&tabledata.length>0){
					tableArr = tabledata;
					var isNull = false;
					tabledata.every(itme => {
						if(itme.itemName == ''||itme.itemName == null) {
						  	isNull = true;
							return false;
						} else {
							return true;
						}
					})
					if(isNull){
						layer.msg('请填写小类名称')
						return
					}
					if (_this.isTableRepeat(tableArr)) {
						layer.msg('小类名称不能重复');
						return;
					}
				}

				var data = {
					"orgId": $(".addTplMethod #orgId").attr('org__id'),
					'itemName': $(".add-item-name").val(),
					'displayOrder': $(".add-item-num").val(),
					'itemCode':$(".add-item-code").val(),
					'list':tableArr,
				};
			$.ajax({
			   type: "post",
			   dataType : 'json',
			   contentType: "application/json;charset=UTF-8",
			   url: $("#contextPath").val()+ "/repairType/save",
			   data:JSON.stringify(data) ,
			   success: function(data){
				   if(data.code == '200'){
						$(".dialog-wp").remove();
						layer.msg('新增数据成功！')
						var $orgId = $("#conditionform  .single-orgid");
						!$orgId.length?$("#conditionform #orgId").removeAttr('org__id'): ''
						_this.againRenderTable();
				   }else{
						layer.msg(data.msg)
				   }
			   },
			   error: function(error){
				   layer.msg('error')
			   }
			   });

			},
			renderTable: function(){
				var _this = this;
				layui.use('table', function(){
					  table = layui.table;

					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user',
					    method: 'post'  ,skin: 'nob' //行边框风格
					    		  ,even: true //开启隔行背景
					    		  ,size: 'lg' //小尺寸的表格
					    ,url: $("#contextPath").val()+ "/repairType/list"
					    ,cols: [[
						  {

							  title: '序号',
							  templet: '#indexTpl',
							  width: 80
						  } ,{
				    		   field:'orgName',
				    		   title: '项目',
				    		   Width:120
				    	  }, {
				    		  field:'itemCode',
				    		  title: '大类编号',
				    		  Width: 120
			    		  },{
					    	  field:'orderno',
							  title: '排序',
							  Width: 80
						  },{
				    		  field:'itemName',
				    		  title: '大类名称',
				    		  Width: 120
			    		  },{
//		    				  field:'childList',
		    				  title: '包含小类名称',

	    				     templet: function(data){
	    					   var str = [];
	    					  if(data.childList&&data.childList.length>0){
	    						  data.childList.forEach(value =>{
	    							  str+=value.itemName+'{'+value.itemId+'}'+','
	    						  })
	    					   }
	    						  return str;
		  					  }
		  				  },
							{
		  					  title: '操作',
		  					  width: 250,
		  					  templet: function(data){
//		  						  console.log(data.orgId)
		  						 var str = '';
		  						 str += "<button data-id="+data.itemId+" data-orgid= "+data.orgId+" class='t-btn t-red-btn t-edit-btn edit-blue-btn'>编辑<button>"
		  						 str += "<button data-id="+data.itemId+" data-orgid= "+data.orgId+" class='t-btn t-red-btn t-edit-btn check-blue-btn'>查看详情<button>"
		  						 str += "<button data-id="+data.itemId+" data-orgid= "+data.orgId+" class='t-btn t-red-btn t-edit-btn t-del-btn'>删除<button>"
	    						  return str;
		  					  }
						  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						    where: {
								orgId: $("#conditionform  #orgId").attr('org__id'),
							},
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
						    done: function(res, curr, count){
						        //如果是异步请求数据方式，res即为你接口返回的信息。
						    	//编辑
						        $(".edit-blue-btn").on("click",function(){
//						        	console.log("11111111",$(this).data("id"),$(this).data("orgid"));
						        	var id = $(this).data("id")
						        	var orgId = $(this).data("orgid")
						        	_this.ajaxDetailEdit(id,orgId);
						        });
						        $(".check-blue-btn").on("click",function(){
						        	var id = $(this).data("id")
						        	var orgId = $(this).data("orgid")
						        	//查看订单详情
						        	_this.ajaxDetail(id,orgId);
						        });
						        $(".t-del-btn").on("click", function() {
									var id = $(this).data("id");
									var orgId = $(this).data("orgid")
									console.log("id",id)
									console.log("orgId",orgId)
									_this.ajaxDelTemp(id , orgId, function() {
										_this.againRenderTable();
									})
								});

						      }
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  _this.againRenderTable();
					  });
					});

			},
			ajaxDelTemp: function(id, orgId, callback) {
				var str = "确认删除当前信息吗?";
				layer.confirm(str, {
					btn: ['确定', '取消']
				}, function(index, layero) {
					layer.close(index); //如果设定了yes回调，需进行手工关闭
					$.ajax({
						type: "post",
						url: contextPath + "/repairType/delete",
						data: {
							"itemId": id,
							"orgId": orgId
						},
						success: function(data) {
							if (data.code == 200) {
								callback();
							} else {
								layer.msg(data.msg)
							}
						},
						error: function(error) {
							layer.msg('error')
						}
					});
				}, function(index) {

				});
			},
			/*重新渲染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: { //设定异步数据接口的额外参数，任意设
							  orgId:  $orgId.length?orgId:'',
							 groupName: $(".repair-groupname").val(),
							 itemName: $(".repair-itemname").val(),
							 groupCode: $(".repair-groupcode").val(),
						  }
						  ,page: {
						    curr: 1 //重新从第 1 页开始
						  }
					});
				}else{
					table.reload('testReload', {
						  where: { //设定异步数据接口的额外参数，任意设
							  orgId: orgId,
							  groupName: $(".repair-groupname").val(),
							  itemName: $(".repair-itemname").val(),
							  groupCode: $(".repair-groupcode").val(),
						  }
						  ,page: {
						    curr: 1 //重新从第 1 页开始
						  }
					});
				}

			},
			// 模糊搜索事件
			getProjectList: function(){
				var __this = this;
				$('body').on("input",'#orgId',function(event){
					var _this = $(this)
					cjqt.search_fuzzy.event_input(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
				});
				$('body').on("focus",'#orgId',function(event){
					var _this = $(this)
					cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
				});
				$('body').on('blur','#orgId',function(event) {
					var _this = $(this)
					cjqt.search_fuzzy.event_blur(_this,'org__id')
				});
				$('body').on('click','#orgId~.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')
				});

				$('body').on("input",'.d-item-wp',function(event){
					var _this = $(this)
					cjqt.search_fuzzy.event_input(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
				});
				$('body').on("focus",'.d-item-wp',function(event){

					var _this = $(this)
					cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
				});
				$('body').on('blur','.d-item-wp',function(event) {
					var _this = $(this)
					cjqt.search_fuzzy.event_blur(_this,'org__id')
				});
				$('body').on('click','.d-item-wp~.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','.d-item-wp',{},__this.changeEvent,__this)
				});
			},
			// 模糊搜索事件
			search_fuzzy: function(){
				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',
					com_target:'.default-select#companyId',
					dep_target:'.default-select#departmentId',
				});
				var option_2 = $.extend(true,{},default_option,{
					org_target:'.item-select.organ-id',
				});

				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})
				eve(option_1,{type:2})
				eve(option_1,{type:3})
				// 自动充值
				eve(option_2,{type:1})
			},
			/*显示回访弹窗*/
			showVisitDialog: function($dom){
//				$("body").append(template.render(tpl.visitTpl));
//				 $(".close-btn").on("click",function(){
//					  $(this).closest(".dialog-wp").remove();
//				  });
				//评星
				$dom.find(".star-wp .star").on("click",function(){
					var index = $(this).index();

					$dom.find(".star-wp .star").each(function(ind,ele){
						$(ele).removeClass("active");
						if(index>=ind){
							$(ele).addClass("active");
						}
					})
				})
			},
			ajaxDetailEdit:function(id,orgid){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/repairType/getDetail",
					   data: {
						   id: id,
						   orgId:orgid,
					   },
					   success: function(data){
						   if(data.code == '200'){
							   const list=data.data
							   _this.renderEditDialog(id,list,orgid)
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(){
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 获取详情
			 * @param id
			 */
			ajaxDetail: function(id,orgid){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/repairType/getDetail",
					   data: {
						   id: id,
						   orgId:orgid,
					   },
					   success: function(data){
						   if(data.code == '200'){
							  $("body").append(template.render(tpl.detailTpl)(data));
							  $(".close-btn").on("click",function(){
								  $(this).closest(".dialog-wp-detail").remove();
							  });
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(){
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 确定回访编辑
			 * @param id
			 * @param $dom
			 * @param callback
			 */
			ajaxaddOrUpdateVisit: function(id,$dom,callback){
				var _this = this;
//				visit-record
				//评价星级
				var star = $(".visit-wp .star.active").length;
				if(!star){
					layer.msg("请选择回访星级");
					return false;
				};
				//描述
				var description = $(".visit-record").val();
				if(!description){
					layer.msg("请输入回访记录");
					return false;
				};
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/repairBill/addOrUpdateVisit",
					   data: {
						   star: star,
						   billId: id,
						   description: description
					   },
					   success: function(data){

						   if(data.code == '200'){
							  $dom.remove();
							  callback(star,description);
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(){
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 获取维修类型
			 * callback
			 */
			ajaxRepairType: function(callback){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/repairBill/getRepairType",
					   success: function(data){

						   if(data.code == '200'){
							   var repairTypeId = data.data.length >0 && data.data[0].id;
							   //获取维修类型对应人员
							  _this.ajaxRepairTypeUser(repairTypeId,callback,data.data)
						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(){
						   layer.msg('error')
					   }
			   });
			},
			/*
			 * 获取维修类型对应人员
			 * @param repairTypeId 维修类型id
			 * @param callback
			 * @param typeList 维修类型列表
			 */
			ajaxRepairTypeUser: function(repairTypeId,callback,typeList){
				var $orgId = $("#conditionform  .single-orgid");
				var orgId = $("#conditionform  #orgId").attr('org__id');

				var params = {
					repairTypeId: repairTypeId
				};
				if(!$orgId.length && orgId){
					params["orgId"] = orgId;
				}else if($orgId.length){
					params["orgId"] = $orgId.val();
				};
				console.log(params);
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/repairBill/getRepairTypeUser",
					   data: params,
					   success: function(data){

						   if(data.code == '200'){
							   if(callback){
								   var list =  {
										   typeList: typeList,
										   userList : data.data
								   }
								   callback(list)
							   }else{
								   $(".dialog-wp .person-list").html(template.render(tpl.personList)({userList: data.data}));
							   }

						   }else{
							   layer.msg(data.msg)
						   }
					   },
					   error: function(){
						   layer.msg('error')
					   }
			   });
			}
	};
	launchRepair.init();
})
