$(document).on('turbolinks:load', function(){ if ($('body.admins-competition-settings-index-page').length > 0) { var dateOptions = { autoclose: true, language: 'zh-CN', format: 'yyyy-mm-dd', startDate: '2017-04-01' }; var timeOptions = { autoclose: 1, language: 'zh-CN', format: 'yyyy-mm-dd hh:ii', minuteStep: 30 }; var defineDateRangeSelect = function (element) { var options = $.extend({inputs: $(element).find('.start-date, .end-date')}, dateOptions); $(element).datepicker(options); $(element).find('.start-date').datepicker().on('changeDate', function (e) { $(element).find('.end-date').datepicker('setStartDate', e.date); }); }; $(".competition-start-end-date .start-date").datetimepicker(timeOptions); $(".competition-start-end-date .end-date").datetimepicker(timeOptions); $(".nav-setting-form .enroll_end_time").datetimepicker(timeOptions); $(".stage-update-form .section-start-time").datetimepicker(timeOptions); $(".stage-update-form .section-end-time").datetimepicker(timeOptions); defineDateRangeSelect('.teaching-mode-date'); // defineTimeRangeSelect('.competition-start-end-date'); var $basicForm = $('form.basic-setting-form'); $basicForm.validate({ errorElement: 'span', errorClass: 'danger text-danger', rules: { name: "required", subTitle: "required", startTime: "required", endTime: "required", mode: "required", identifier: "required" } }); // 保存按钮 $basicForm.on('click', ".submit-btn", function () { $basicForm.find('.submit-btn').attr('disabled', 'disabled'); $basicForm.find('.error').html(''); var valid = $basicForm.valid(); if ($("input[name='mode']:checked").val() == 2) { var $courseId = $("input[name='course_id']"); if ($courseId.val() === undefined || $courseId.val().length === 0) { $courseId.addClass('danger text-danger'); valid = false; } else { $courseId.removeClass('danger text-danger'); } } else if ($("input[name='mode']:checked").val() == 3) { var $techStartTime = $("input[name='teach_start_time']"); var $techEndTime = $("input[name='teach_end_time']"); if ($techStartTime.val() === undefined || $techStartTime.val().length === 0) { $techStartTime.addClass('danger text-danger'); valid = false; } else { $techStartTime.removeClass('danger text-danger'); } if ($techEndTime.val() === undefined || $techEndTime.val().length === 0) { $techEndTime.addClass('danger text-danger'); valid = false; } else { $techEndTime.removeClass('danger text-danger'); } } else { $("input[name='course_id']").removeClass('danger text-danger'); $("input[name='teach_start_time']").removeClass('danger text-danger'); $("input[name='teach_end_time']").removeClass('danger text-danger'); } if (!valid) return; $.ajax({ method: 'POST', dataType: 'json', url: $basicForm.attr('action'), data: new FormData($basicForm[0]), processData: false, contentType: false, success: function (data) { $.notify({message: '保存成功'}); // window.location.reload(); }, error: function (res) { var data = res.responseJSON; $basicForm.find('.error').html(data.message); }, complete: function () { $basicForm.find('.submit-btn').attr('disabled', false); } }); }); var selectOptions = { theme: 'bootstrap4', placeholder: '请输入要添加的单位名称', multiple: true, minimumInputLength: 1, ajax: { delay: 500, url: '/api/schools/for_option.json', dataType: 'json', data: function(params){ return { keyword: params.term }; }, processResults: function(data){ return { results: data.schools } } }, templateResult: function (item) { if(!item.id || item.id === '') return item.text; return item.name || item.text; }, templateSelection: function(item){ return item.name || item.text; } }; $('.sponsor-select').select2(selectOptions); $('.allow-school-select').select2(selectOptions); // 排行榜 //链接 $(".nav-setting-form").on("click",".add_linkBtn",function () { var length=$(".nav-setting-form").find(".linkFormItem").length + 1; var html='
\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + '
\n' + ' +\n' + ' ×\n' + '
'; $(this).parents(".linkFormItem").after(html); }); $(".nav-setting-form").on("click", ".del_linkBtn", function () { $(this).parents(".linkFormItem").remove(); }); //有关报名要求 $(".addRequireBtn").on("click",function () { var length=$("#requireForm").find(".requireForm_item").length + 1; var html='
\n' + '
  
\n' + '
\n' + ' \n' + '
\n' + ' ~\n' + '
\n' + ' \n' + '
\n' + ' \n' + '
\n' + ' \n' + '
\n' + '
\n' + ' \n' + '
\n' + '
\n' + ' \n' + ' \n' + ' \n' + ' \n' + '
\n' + '
'; $("#requireForm").append(html); }); $("#requireForm").on("click",".delRequrieBtn",function () { $(this).parents(".requireForm_item").remove(); }); $('.competition-staff-settings').on('click', '.mutiple-limited-radio', function(){ var radio = $(this); if (radio.is(':checked')) { radio.parent().parent().siblings().find('.mutiple-limited-radio').attr('checked', false) } else { radio.parent().parent().siblings().find('.mutiple-limited-radio').attr('checked', true) } }); var $navForm = $('form.nav-setting-form'); $navForm.on('click', ".submit-btn", function () { $navForm.find('.submit-btn').attr('disabled', 'disabled'); $navForm.find('.error').html(''); var valid = $navForm.valid(); if (!valid) return; $.ajax({ method: 'POST', dataType: 'json', url: $navForm.attr('action'), data: new FormData($navForm[0]), processData: false, contentType: false, success: function (data) { $.notify({message: '保存成功'}); // window.location.reload(); }, error: function (res) { var data = res.responseJSON; $navForm.find('.error').html(data.message); }, complete: function () { $navForm.find('.submit-btn').attr('disabled', false); } }); }); // 排行榜设置 //删除小阶段 $("#large_panel").on("click",".small_panel_item_del",function () { var list = $(this).parents(".small_panel"); $(this).parents(".small_panel_item").remove(); for(var i=0;i < $(list).find(".subName").length;i++){ console.log(i); $(list).find(".subName").eq(i).html("第"+parseInt(i+1)+"阶段"); } }); // $('form.stage-update-form').validate({ // errorElement: 'span', // errorClass: 'danger text-danger', // rules: { // stage_name: "required", // "stage[][start_time]": "required", // "stage[][end_time]": "required", // "stage[][mission_count]": { // required: true, // min: 1 // }, // "stage[][entry]": { // required: true, // min: 1 // }, // score_rate: { // required: true, // range: [0, 100] // } // }, // messages: { // "stage[][mission_count]": { // min: ">=1" // }, // "stage[][entry]": { // min: ">=1" // }, // } // }); $('.competition-chart-setting').on('click', ".update-stage", function () { var updateForm = $(this).parents("form"); $(this).attr('disabled', 'disabled'); updateForm.find('.error').html(''); // var valid = updateForm.valid(); var valid = true; var $stageName = updateForm.find('input[name="stage_name"]'); if($stageName.val() === undefined || $stageName.val().length === 0){ $stageName.addClass('danger text-danger'); valid = false; } else { $stageName.removeClass('danger text-danger'); } var $scoreRate = updateForm.find('input[name="score_rate"]'); if($scoreRate.val() === undefined || $scoreRate.val().length === 0){ $scoreRate.addClass('danger text-danger'); valid = false; } else if (parseInt($scoreRate.val()) > 100 || parseInt($scoreRate.val()) < 0) { $scoreRate.addClass('danger text-danger'); $scoreRate.after('0-100之间的数值'); valid = false; } else { $scoreRate.removeClass('danger text-danger'); $scoreRate.siblings().remove(); } updateForm.find('input[name="stage[][start_time]"]').each(function(_, e){ var $ele = $(e); if($ele.val() === undefined || $ele.val().length === 0){ $ele.addClass('danger text-danger'); valid = false; } else { $ele.removeClass('danger text-danger'); } }); updateForm.find('input[name="stage[][end_time]"]').each(function(_, e){ var $ele = $(e); if($ele.val() === undefined || $ele.val().length === 0){ $ele.addClass('danger text-danger'); valid = false; } else { $ele.removeClass('danger text-danger'); } }); updateForm.find('input[name="stage[][mission_count]"]').each(function(i, e){ var $ele = $(e); var $entry = updateForm.find('input[name="stage[][entry]"]').eq(i); if($ele.val() === undefined || $ele.val().length === 0){ $ele.addClass('danger text-danger'); valid = false; } else if (parseInt($ele.val()) < 1) { $ele.addClass('danger text-danger'); $ele.after('大于等于1'); valid = false; } else if (parseInt($ele.val()) > parseInt($entry.val())) { $ele.addClass('danger text-danger'); $ele.after('不能大于总任务数'); valid = false; } else { $ele.removeClass('danger text-danger'); $ele.siblings().remove(); } }); updateForm.find('input[name="stage[][entry]"]').each(function(_, e){ var $ele = $(e); if($ele.val() === undefined || $ele.val().length === 0){ $ele.addClass('danger text-danger'); valid = false; } else if (parseInt($ele.val()) < 1) { $ele.addClass('danger text-danger'); $ele.after('大于等于1'); valid = false; } else { $ele.removeClass('danger text-danger'); $ele.siblings().remove(); } }); updateForm.find('input[name="stage[][identifiers][]"]').each(function(_, e){ var $ele = $(e); if($ele.val() === undefined || $ele.val().length === 0){ $ele.addClass('danger text-danger'); valid = false; } else { $ele.removeClass('danger text-danger'); } }); if (!valid) return; updateForm.find('input[name="stage[][mission_count]"]').each(function(_, e){ var $missionCount = $(e); var $entryCount = $(e).parents("div.row").find('input[name="stage[][mission_count]"]'); if(parseInt($missionCount.val()) > parseInt($entryCount.val()) ){ $missionCount.addClass('danger text-danger'); $missionCount.after('不能大于总任务数'); valid = false; } else { $missionCount.removeClass('danger text-danger'); $missionCount.siblings().remove(); } }); $.ajax({ method: 'POST', dataType: 'json', url: updateForm.attr('action'), data: new FormData(updateForm[0]), processData: false, contentType: false, success: function (data) { $.notify({message: '保存成功'}); window.location.reload(); }, error: function (res) { var data = res.responseJSON; $navForm.find('.error').html(data.message); }, complete: function () { $navForm.find('.submit-btn').attr('disabled', false); } }); }); $(".competition-chart-stages").on("click", ".add-new-tab", function () { if($(".new-stage-form").length > 0){ alert("请先保存上一个tab"); } else { var count = parseInt($("#large_panel").find(".large_panel_part").length)+1; var html = '
' + '
\n' + ' tab标题\n' + '
\n' + ' \n' + '
\n' + ' 总排行榜占比:\n' + '
\n' + ' \n' + '
%\n' + '
\n' + ' 新增子阶段\n' + '
\n' + ' 删除\n' + ' 保存\n' + '
\n' + '
\n' + '
\n' + ' 第1阶段\n' + '
\n' + '
\n' + ' 有效时间:\n' + '
\n' + ' \n' + '
\n' + ' ~\n' + '
\n' + ' \n' + '
\n' + ' 总任务数:\n' + '
\n' + ' \n' + '
\n' + ' 成绩来源:\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + ' 任务1\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + ' 任务2\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + ' 任务3\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + ' \n' + ' 删除\n' + ' \n' + '
\n' + '
'; $("#large_panel").append(html); $(".stage-update-form .section-start-time").datetimepicker(timeOptions); $(".stage-update-form .section-end-time").datetimepicker(timeOptions); } }); //新增子阶段 $(".competition-chart-stages").on("click", ".add-task-sub", function () { var index = $(this).parents(".large_panel_part").attr("attr_line"); var count= 0; console.log("sdfsf"); console.log($("#small_panel_"+index).find(".small_panel_item").length > 0); if($("#small_panel_"+index).find(".small_panel_item").length > 0){ count = parseInt($("#small_panel_"+index).find(".small_panel_item").last().attr("count")) + 1; console.log($("#small_panel_"+index).find(".small_panel_item").last().attr("count")); }else{ count = 1; } var showCount=parseInt($("#small_panel_"+index).find(".small_panel_item").length) + 1; var html='
\n' + ' 第'+showCount+'阶段\n' + '
\n' + '
\n' + ' 有效时间:\n' + '
\n' + ' \n' + '
\n' + ' ~\n' + '
\n' + ' \n' + '
\n' + ' 总任务数:\n' + '
\n' + ' \n' + '
\n' + ' 成绩来源:\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n'+ '
\n' + ' 任务1\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + ' 任务2\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + ' 任务3\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + ' \n' + ' 删除\n' + ' \n' + '
'; $("#small_panel_"+index).append(html); $(".stage-update-form .section-start-time").datetimepicker(timeOptions); $(".stage-update-form .section-end-time").datetimepicker(timeOptions); }); } }); //添加主办方或者开放范围 function addSponsor(item){ var html=''; $(item).parents(".sponsorPanel").append(html); } //删除 function del_sponsor(item){ $(item).parents(".sponsor_label").remove(); } // 小阶段修改总任务数 function change_total(item) { var count=parseInt($(item).val()); var index = $(item).parents(".small_panel_item").attr("attr_line"); var indexLarge = $(item).parents(".large_panel_part").attr("attr_line"); console.log(indexLarge); console.log(index); var divCount=parseInt($("#task_Input_"+index).find(".task_Input_div").length); var html = ""; if(count > divCount){ for(var i=0;i < count-divCount ;i++){ html+='
任务'+(divCount+i+1)+'\n' + '
\n' + '\n' + '
\n' + '
'; } $("#task_Input_"+index).append(html); }else{ var delCount = divCount - count ; console.log(divCount); console.log(count); var _max=parseInt($("#task_Input_"+index).find(".task_Input_div:last").index()); console.log(_max); var _get= _max - delCount; console.log(_get); if(count == 0){ $("#task_Input_"+index).empty(); }else{ $("#task_Input_"+index).find(".task_Input_div:gt("+_get+")").remove(); } } } //删除tab function Del_tab(item) { $(item).parents(".large_panel_part").remove(); } //新增tab function addNewTab(competition_id) { if($(".new-stage-form").length > 0){ alert("请先保存上一个tab"); } else { var count = parseInt($("#large_panel").find(".large_panel_part").length)+1; var html = '
' + '
\n' + ' tab标题\n' + '
\n' + ' \n' + '
\n' + ' 总排行榜占比:\n' + '
\n' + ' \n' + '
%\n' + '
\n' + ' 新增子阶段\n' + '
\n' + ' 删除\n' + ' 保存\n' + '
\n' + '
\n' + '
\n' + ' 第1阶段\n' + '
\n' + '
\n' + ' 有效时间:\n' + '
\n' + ' \n' + '
\n' + ' ~\n' + '
\n' + ' \n' + '
\n' + ' 总任务数:\n' + '
\n' + ' \n' + '
\n' + ' 成绩来源:\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + ' 任务1\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + ' 任务2\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + ' 任务3\n' + '
\n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + ' \n' + ' 删除\n' + ' \n' + '
\n' + '
'; $("#large_panel").append(html); } }