science_competition.js 9.4 KB
define(['jquery', 'bootstrap', 'frontend', 'form', 'template', 'swiper', 'layer','layui','upload',], function($, undefined, Frontend, Form, Template, Swiper, Layer,layui,Upload) {
	var Controller = {
		index: function() {

			var first_swiper = new Swiper ('#first_swiper', {
				loop: true, // 循环模式选项
				// 如果需要分页器
				autoplay:true,
				pagination: {
					el: '.swiper-pagination',
				}
			});
		},
		detail:function () {
			$("#apply").click(function(){
				layer.open({
					type: 1,
					title:['请选择分类','font-size:1rem;'],
					area:['50rem','28rem'],
					scrollbar:false,
					content: $('#modal2') //这里content是一个DOM,注意:最好该元素要存放在body最外层,否则可能被其它的相对元素所影响
				});
			});
		},
		enter:function () {
			var _Form = Form;
			var vm =	new Vue({
				el:"#app",
				data() {
					return {
						activeName: '0',
						formSet:[],
						formData:[],
						loading:true,
						collageAll:{},
						match_type:Config.match_type,
						cdn_url:Config.cdn_url,
						id:Config.id || 0,
						match_id:Config.match_id || '',
						matchData:Config.matchData,
						file_flg:false,
						form:{},
						match_type_id:Config.match_type || '',
						user_id:Config.user_id || '',
						rules:{},
						formLength:1,
						f_last:false,
						f_first:false,
						teachersFormVisible:false,
						membersFormVisible:false,
						tmp_teachers:{},
						tmp_members:{},
						//项目领域数据
						territory:Config.territory || {},
					};

				},
				computed: {
					rulesList: function() {
						var formset = this.formSet;
						var formsetsVal = formset[this.activeName];
						if(formsetsVal) {
							var name = formsetsVal.stage_type;
							var rules = this.rules;
							if(name in rules) {
								return rules[name];
							}else{
								return [];
							}
						}
					}
				},
				mounted(){
					var match_type_id = this.match_type_id ? this.match_type_id : this.getDefaultType();
					this.match_type_id = parseInt(match_type_id);
					this.getData();
				},
				methods: {

					getDefaultType() {
						var initTypeId = '';
						$.each(this.matchData.types,function (i,it) {
							if(initTypeId == '') {
								initTypeId = it.id;
							}
						});
						return initTypeId;
					},

					prev(key) {

						//
						//
						key = key.toString();
						var arr = Object.keys(this.formSet);
						var index = arr.indexOf(key);
						var $prev = arr[index - 1];
						var start = arr[0];

						if($prev == start) {
							this.f_first = true;
						}
						this.activeName = $prev != 0 ? parseInt($prev) : '0';
						this.f_last = false;
						this.$nextTick(()=>{
							this.$refs.form.clearValidate();
						});
					},
					next(form,key) {
						this.$refs[form].validate((valid) => {
							if (valid) {
								var k_index = parseInt(key);
								var indexLength = this.formSet.length -1;
								k_index++
								if(k_index == indexLength) {
									this.f_last = true;
								}
								this.activeName = k_index;
								this.f_first = false;
								this.$nextTick(()=>{
									this.$refs.form.clearValidate();
								});
							}else {
								layer.msg('信息不完整',{icon: 5});
								return false;
							}
						});

					},
					getCollage(){
						var _this = this;
						Fast.api.ajax({
							url:'science_competition/getCollage',
							data:{user_id:_this.user_id}
						},function (ret) {
							_this.collageAll = ret;
							return false;
						});
					},

					changeType (val) {
						_this = this;
						this.match_type_id  = val;
						this.formSet = [];
						setTimeout(function () {
							_this.getData();
						},50);
					},
					getData(){
						var _this = this;
						Fast.api.ajax({
							url:'science_competition/getFormData',
							data:{match_id:_this.match_id ,match_type_id:_this.match_type_id,id:_this.id}
						},function (ret) {
							//初始化
							_this.formSet = ret.formSets;
							_this.form = ret.detail;
							_this.rules = ret.rules;
							_this.formLength = Object.keys(_this.formSet).length;
							console.log(_this.formSet)
							console.log(_this.form)
							_this.f_first = true;
							$.each(_this.formSet,function (k,v) {
								v.stage_title=v.stage_title+'>>';
								var stage = Object(v.stage_type);
								_this.formData.push(stage);

							});
							_this.loading = false;
							_this.getCollage();

							//上传绑定事件
							_this.$nextTick(function () {
								var _this = this;
								$.each(this.formSet,function(i,val) {
									$.each(val.form_data,function(k,v){
										if(v.type == 'file') {
											var uploadName = v.name;
											var uploadKey = v.file_key;
											$("#"+uploadName).data("upload-success", function (res) {
												$("#"+uploadName).parent().siblings('.file_box').show();
												_this.$set(_this.form[val.stage_type],uploadName,res.file.url);
												_this.$set(_this.form[val.stage_type],uploadKey,res.file.name);
											});
										}
									});
									Form.api.bindevent($("#form"));

								});
							});
							return false;
						});
					},
					del_file(key,name,stage_type) {
						var _this = this;
						$("#"+name).parent().siblings('.file_box').hide();
						_this.$set(_this.form[stage_type],name,'');
						_this.$set(_this.form[stage_type],key,'');
					},
					getMembers(e){
						var value = e.target.value;
						var _this = this;
						Fast.api.ajax({
							url:"signup_info/getMemberInfo",
							data:{members_student_no:value}
						},function (ret) {
							_this.tmp_members = ret;
							return false;
						},function(){
							_this.tmp_members = {};
							layer.msg('查询不到该学生,请重新输入');
							return false;
						});
					},
					getTeachers(e){
						var _this = this;
						var value = e.target.value;
						Fast.api.ajax({
							url:"signup_info/getTeacher",
							data:{job_num:value}
						},function (ret) {
							_this.tmp_teachers = ret;
							return false;
						}, function(ret) {
							_this.tmp_teachers = {};
							layer.msg('查询不到该教师,请重新输入');
							return false;
						});
					},
					exit_teacher() {
						var _this = this
						_this.tmp_teachers = {};
						_this.teachersFormVisible = false;
					},
					exit_members() {
						var _this = this
						_this.tmp_members = {};
						_this.membersFormVisible = false;
					},
					formTSubmit:function () {
						var _this = this;
						if($.isEmptyObject(_this.tmp_teachers)) {
							layer.msg('请添加指导教师');
							return false;
						}
						var exist = false;
						if(!$.isEmptyObject(_this.form.teachers.teachers)) {
							$.each(_this.form.teachers.teachers,function(i,v){
								if(v.id == _this.tmp_teachers.id) {
									layer.msg('该教师已选择');
									exist = true;
								}
							});
						}
						if(exist === true) {
							_this.tmp_teachers = {};
							_this.teachersFormVisible = false;
							return false;
						}
						console.log('教师信息',_this.tmp_teachers);
						_this.form.teachers.teachers.push(_this.tmp_teachers);
						_this.tmp_teachers = {};
						_this.teachersFormVisible = false;
					},
					teacherRemove:function(row) {
						var _this = this;
						$.each(_this.form.teachers.teachers , function (k,v) {
							if (v.id == row.id){
								_this.form.teachers.teachers.splice(k,1);
								return false;
							}
						});
						if($.isEmptyObject(_this.form.teachers.teachers)) {
							_this.form.teachers.teachers = [];
						}
					},
					formSubmit:function () {
						var _this = this;
						if($.isEmptyObject(_this.tmp_members)) {
							layer.msg('请添加成员');
							return false;
						}
						var exist = false;
						if(!$.isEmptyObject(_this.form.members.members)) {
							$.each(_this.form.members.members,function(i,v){
								if(v.id == _this.tmp_members.id) {
									layer.msg('该成员已选择');
									exist = true;
								}
							});
						}
						console.log(_this.form.members.members);
						if(exist === true) {
							_this.tmp_members = {};
							_this.membersFormVisible = false;
							return false;
						}
						_this.form.members.members.push(_this.tmp_members);
						_this.tmp_members = {};
						_this.membersFormVisible = false;
					},
					membersRemove(row){
						var _this = this;
						$.each(_this.form.members.members , function (k,v) {
							if (v.id == row.id){
								_this.form.members.members.splice(k,1);
								return false;
							}
						});
						if($.isEmptyObject(_this.form.members.members)) {
							_this.form.members.members = [];
						}
					},
					submit(form) {
						this.$refs[form].validate((valid) => {
							if (valid) {
								$.ajax({
									type:'post',
									url:'/index/science_competition/putData',
									data:{form:this.form,matchGroupId:this.matchData.match_group_id,match_id:this.matchData.match_id,match_type_id:this.match_type_id,item_id:this.id},
									dataType:'json',
									success:function(res){
										if(res.code != 1) {
											layer.msg(res.msg, {icon: 5});
										}else{
											layer.msg(res.msg, {icon: 1});
											setTimeout(function () {
												window.location.href = res.url;
											},800);
										}

									},
									error:function(error){
										layer.msg('网络失败,请重试', {icon: 5});
									}
								});
							}
						});
					},
					goIndex(id) {
						window.location.href = "/index/science_competition/detail/id/"+id;
					},
				}
			});
		}
	}
	return Controller;
});