create.js 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
define(['jquery', 'bootstrap', 'frontend', 'form', 'template', 'swiper', 'layer'], function($, undefined, Frontend,
	Form, Template, Swiper, Layer) {
	var Controller = {
		index: function() {
			var student_nos =   [];
			var studentId = '';
			// console.log(true)
			var first_swiper = new Swiper('#first_swiper', {
				loop: true, // 循环模式选项
				// 如果需要分页器
				autoplay: true,
				pagination: {
					el: '.swiper-pagination',
				}
			});
			$(".add").click(function() {
				layer.open({
					type: 1,
					btn: ['保存'],
					btnAlign: 'c',
					yes: function(index, layero) {
						if(studentId == '') {
							layer.msg('请添加成员');
							return false;
						}
						//即将添加
						var num = $.grep(student_nos,function (elem) {
							return elem === studentId;
						}).length;
						if(num > 1) {
							layer.msg('该成员已添加');
							return false;
						}
						// 保存填写的用户信息
						if (!$('.tHead').length) {
							setTHead()
						}
						$(".myadd").before(getPeopleItem(getPeopleData()));
						studentId = '';
						layer.close(index);
					},
					title: ['添加成员', 'font-size:1rem;'],
					area: ['60rem', '40rem'],
					scrollbar: false,
					content: $('#modal').html() //这里content是一个DOM,注意:最好该元素要存放在body最外层,否则可能被其它的相对元素所影响
				});
			});
			$(document).on("click",".joiner .btn-delone",function(){
				var that = this;
				var del_index = parseInt($(that).parent().parent().children().eq(0).text());
				var del_studentNo = $(this).parent("td").siblings("td:eq(2)").text();
				console.log(del_studentNo);
				Layer.confirm("确定删除?",{},function(index){
					$(that).parents('tr').remove();
					// $(that).parent().parent().parent().find(".tbody").each(function(i,e){
					// 	if(i === del_index){
					// 		$(".joiner").find(".tbody").eq(del_index).remove();
					// 	}else if(i> del_index){
					// 		var new_val = parseInt($(e).find("td").eq(0).text());
					// 		$(e).find("td").eq(0).text(new_val-1);
					// 	}
					// });
					student_nos = $.grep(student_nos, function(value) {
						return value != del_studentNo;
					});
					$("#member").val(student_nos);
					Layer.close(index);
				});


			});
			$(document).on('submit',"form[name='add_double']",function(){
				var data = $(this).serialize();
				$.ajax({
					type:'post',
					url: $(this).attr('action'),
					data:data,
					dataType:'json',
					success:function(res){
						if(res.code ==0){
							Layer.alert(res.msg,{time:3000},function(index){
								Layer.close(index);
								if(res.url != ''){
									location.href = res.url;
								}
							});
						}else{
							Layer.msg(res.msg,{time:3000},function(index){
								Layer.close(index);
								if(res.url != ''){
									location.href = res.url;
								}
							});
						}
					}
				});
			});
			

			/**
			 * 给表格添加表头
			 */
			function setTHead() {
				$('.joiner tbody').prepend(
					'<tr class="tHead">' +
					'<td>序号</td>' +
					'<td>姓名</td>' +
					'<td>学号</td>' +
					'<td>手机号</td>' +
					'<td>学院</td>' +
					'<td>专业</td>' +
					'<td>年级</td>' +
					'<td>邮箱</td>' +
					'<td>操作</td>' +
					'</tr>');
			}

			// var studentInfo = Config.studentInfo || [];
			$(document).on("blur",'#student_no',function(){
				var student_no = $(this).val();
				if($.inArray(student_no,student_nos) != -1) {
					Layer.msg('成员已经存在');
					return false;
				}
				$.ajax({
					type:'post',
					url:'create/get_student',
					data:{"student_no":student_no},
					dataType:'json',
					success:function (res) {
						if(parseInt(res.code) ===0){
							Layer.msg(res.msg);
						}else{
							var data =res.data;
							student_nos.push(student_no);
							studentId = student_no;
							$("#member").val(student_nos);
							var number = $(".joiner").find(".tbody").length ;
							$("#number").val(number);
							$("#name").val(data.nickname);
							$("#mobile").val(data.mobile);
							$("#collage").val(data.college_text);
							$("#major").val(data.specialty_text);
							$("#grade").val(data.clazz_text);
							$("#email").val(data.email);
						}

					}
				});
			});

			/**
			 * @param {Object} peopleData 填写的用户数据
			 */
			function getPeopleItem(peopleData) {
				return Template.render($('#tpl-people').html(), peopleData);
			}
			/**
			 * 获取填写用户数据
			 */
			function getPeopleData() {
				var result = {}
				$('#popup-userInfo  input').each(function () {
					result[this.dataset.name] =  this.value;
				});
				return result;
			}
		},
		my_double:function(){

			$(document).on("click",".double_choose ul li",function(){
				location.href = "?type="+$(this).data("type");
			});

			$(document).on("click",".sx_operation #del",function(){
				var id = $(this).data("id");
				Layer.confirm("确定撤销此申请?",function (index) {
					Layer.close(index);
					$.ajax({
						type:'post',
						url:'create/del_apply',
						data:{id:id},
						dataType:'json',
						success:function(res){
							Layer.alert(res.msg);
							setTimeout(function(){
								location.reload();
							},3000);
						}
					});
				});
			});

			$(document).on("click",".sx_operation #change_apply",function(){
				var id = $(this).data("id");
				Layer.open({
					type:1,
					title:'变更申请信息',
					area: ['50%','50%'],
					content:$("#modal2"),
					success:function(){
						$.ajax({
							type:'post',
							url:'create/get_applyinfo',
							data:{id:id},
							dataType:'json',
							success:function(res){
								var data = res.data;
								$("#c-id").val(data.id);
								$("#c-mobile").val(data.mobile);
								$("#c-proposer_details").val(data.proposer_details);
								update_apply();
							}
						});
					},
				});
			});

			var update_apply = function(){
				$(document).on("click","#modal2 .btn-update",function() {
					var id               = $("#c-id").val();
					var mobile           = $("#c-mobile").val();
					var proposer_details = $("#c-proposer_details").val();
					$.ajax({
						type:'post',
						url:'create/edit_apply',
						data:{id:id,mobile:mobile,proposer_details:proposer_details},
						dataType:'json',
						success:function(res){
							Layer.msg(res.msg);
							if(res.code ==1){
								setTimeout(function(){
									location.reload();
								},3000);

							}
						}
					});
				});
			}

			$(document).on("click","#establish_content .member_declare",function(){
				var double_id = $(this).data("id");
				 Layer.open({
				 	title:'审核成员',
					type:2,
				 	area:['80%','80%'],
				 	content:'/index/create/get_memberlist?double_id='+double_id
				 });
			});
			$(document).on("click","#establish_content .teacher_declare",function(){
				var double_id = $(this).data("id");
				Layer.open({
					title:'指导老师',
					type:2,
					area:['80%','80%'],
					content:'/index/create/get_teacherlist?double_id='+double_id
				});
			});
			$(document).on("click",".btn-finish",function(){
				var id = $(this).data("id");
				$.ajax({
					type:'post',
					url:'create/finish_double',
					data:{id:id},
					dataType:'json',
					success:function(res){
						Layer.alert(res.msg);
						if(res.code ==1){
							setTimeout(function(){
								location.reload();
							},3000);

						}
					}
				});
			});
			$(document).on("click",".btn-del_apply",function(){
				var id = $(this).data("id");
				$.ajax({
					type:'post',
					url:'create/del_apply_my',
					data:{id:id},
					dataType:'json',
					success:function(res){
						Layer.alert(res.msg);
						if(res.code ==1){
							setTimeout(function(){
								location.reload();
							},3000);
						}
					}
				});
			});
			$(document).on("click",".sx_operation .btn-signup",function(){
					var project_type = $(this).data("project_type");
					var id = $(this).data("id");
					location.href = '/index/doublesign/index?project_type_id='+project_type +'&id='+id;
			});
		},
		get_memberlist:function(){
			$(document).on("click",".layui-table .btn-pass",function(){
				var id = $(this).data("id");
				$.ajax({
					type:'post',
					url:'create/pass_member',
					data:{id:id},
					dataType:'json',
					success:function(res){
						Layer.alert(res.msg);
					}
				});
			});
		},
		get_teacherlist:function(){
			$(document).on("click",".layui-table .btn-teacher-pass",function(){
				var id = $(this).data("id");
				$.ajax({
					type:'post',
					url:'create/pass_member',
					data:{id:id},
					dataType:'json',
					success:function(res){
						Layer.alert(res.msg);
					}
				});
			});
		},
		detail:function(){
			$(document).on("click",".layui-table .btn-pass",function(){
				var id = $(this).data("id");
				$.ajax({
					type:'post',
					url:'create/pass_member',
					data:{id:id},
					dataType:'json',
					success:function(res){
						Layer.alert(res.msg);
					}
				});
			});
			$(document).on("click",".button-div .btn-finish",function(){
				var id = $(this).data("id");
				$.ajax({
					type:'post',
					url:'create/finish_double',
					data:{id:id},
					dataType:'json',
					success:function(res){
						Layer.alert(res.msg);
						if(res.code ==1){
							setTimeout(function(){
								location.reload();
							},3000);

						}
					}
				});
			});
			$(document).on("click",".button-div .btn-edit",function(){
				var id = $(this).data("id");
				location.href = "/index/create/edit?id="+ id;
			});
		},
		edit:function(){

			var student_nos =   [];
			var studentId = '';
			var self_studentNo = Config.self_studentNo;
			student_nos = Config.student_nos;
			console.log( Config.student_nos);
			$(".add").click(function() {
				layer.open({
					type: 1,
					btn: ['保存'],
					btnAlign: 'c',
					yes: function(index, layero) {
						// 保存填写的用户信息
						if (!$('.tHead').length) {
							setTHead()
						}
						$(".myadd").before(getPeopleItem(getPeopleData()))
						layer.close(index);
					},
					title: ['添加成员', 'font-size:1rem;'],
					area: ['60rem', '40rem'],
					scrollbar: false,
					content: $('#modal').html() //这里content是一个DOM,注意:最好该元素要存放在body最外层,否则可能被其它的相对元素所影响
				});
			});

			$(document).on("blur",'#student_no',function(){
				var student_no = $(this).val();
				if($.inArray(student_no,student_nos) != -1) {
					Layer.msg('成员已经存在');
					return false;
				}
				if(student_nos.indexOf(student_no) > -1){

					Layer.msg("此学生已是项目成员");
					return false;

				}else{
					$.ajax({
						type:'post',
						url:'create/get_student',
						data:{"student_no":student_no},
						dataType:'json',
						success:function (res) {
							if(parseInt(res.code) ===0){
								Layer.msg(res.msg);
							}else{
								student_nos.push(student_no);
								studentId = student_nos;
								$("#member").val(student_nos);
								var data =res.data;
								var number_text = $(".joiner").find(".tbody").last().children().first().text();
								var number = parseInt(number_text)+1;
								$("#number").val(number);
								$("#name").val(data.nickname);
								$("#mobile").val(data.mobile);
								$("#collage").val(data.college_text);
								$("#major").val(data.specialty_text);
								$("#grade").val(data.clazz_text);
								$("#email").val(data.email);
							}

						}
					});
				}
			});

			/**
			 * @param {Object} peopleData 填写的用户数据
			 */
			function getPeopleItem(peopleData) {
				return Template.render($('#tpl-people').html(), peopleData);
			}
			/**
			 * 获取填写用户数据
			 */
			function getPeopleData() {
				var result = {}
				$('#popup-userInfo  input').each(function () {
					result[this.dataset.name] =  this.value;
				});
				return result;
			}
			$(document).on("click",".joiner .btn-delone",function(){
				var that = this;
				var del_index = parseInt($(that).parent().parent().children().eq(0).text());
				var del_studentNo = $(this).parent("td").siblings("td:eq(2)").text();
				console.log(del_studentNo);
				Layer.confirm("确定删除?",{},function(index){
					if(del_studentNo == self_studentNo) {
						layer.msg('不能删除自己');
						return false;
					}
					$(that).parents('tr').remove();
					student_nos = $.grep(student_nos, function(value) {
						return value != del_studentNo;
					});
					$("#member").val(student_nos);
					Layer.close(index);
				});
			});
			$(document).on('submit',"form[name='add_double']",function(){
				var data = $(this).serialize();
				$.ajax({
					type:'post',
					url: $(this).attr('action'),
					data:data,
					dataType:'json',
					success:function(res){
						if(res.code ==0){
							Layer.alert(res.msg,{time:3000},function(index){
								Layer.close(index);
								if(res.url != ''){
									location.href = res.url;
								}
							});
						}else{
							Layer.msg(res.msg,{time:3000},function(index){
								Layer.close(index);
								if(res.url != ''){
									location.href = res.url;
								}
							});
						}
					}
				});
			});
		}
	}
	return Controller;
});