<%
this.builtInCdnUrl = 'https://cdn.jsdelivr.net/npm/syzoj-public-cdn@1.1.0';

this.lib = uri => { return builtInCdnUrl + '/cdnjs/' + uri; }
this.selfLib = uri => { return builtInCdnUrl + '/self/' + uri; }

this.seasonStatus = function (st, ed) {
  let now = web_util.getCurrentDate();
  if (now - ed > 0) return -1;
  else if (now - st >= 0) return 0;
  else return 1;
}

this.contestLen = function (str) {
  return str.split('|').length;
}
%>
<script>

function getRanking(ranking_id, contest_index) {
	$('#rankingModal').modal('show');
  $('#rankingLoader').removeClass('disabled');
  $('#rankingLoader').addClass('active');
	let contests = JSON.parse(localStorage.getItem(`season${getSeasonId()}_contests`));
  $('#rankingTitle').text(contests[contest_index].title + '的排行榜');
  $('#rankingContent').empty();
  fetch(`/sport/season/${getSeasonId()}/ranking/${ranking_id}?dataOnly=true`)
	.then(response => response.json())
	.then(data => {
    console.log("Start loading ranking")
		
    
		let arr = data.data.split('\n').filter(x => x).map(x => x.split(' ').filter(x => x));
		let rk = 0;
		for (let i = 0; i < arr.length; ++i) {
			if (rk == 0 || arr[i][2] != arr[i - 1][2]) ++rk;
			let str = '';
			str += '<tr><td>';
			if (rk == 1)
				str += `<div class="ui yellow ribbon label">${rk}</div></td>`;
			else if (rk == 2)
				str += `<div class="ui ribbon label">${rk}</div></td>`;
			else if (rk == 3)
				str += `<div class="ui brown ribbon label" style="background-color: #C47222 !important;">${rk}</div></td>`;
			else
				str += `${rk}</td>`;
			for (let j = 0; j < arr[i].length; ++j) {
				if (j == 0) arr[i][j] = transClass(arr[i][j]);
				str += `<td>${arr[i][j]}</td>`;
			}
			str += '</tr>';
			$('#rankingContent').append(str);
      $('#rankingLoader').addClass('disabled');
      $('#rankingLoader').removeClass('active');
		}
	})
}
function transClass(str) {
  try {
    let gradeId = parseInt(str[0]);
    let ans = '';
    switch (gradeId) {
      case 1: ans += '高一'; break;
      case 2: ans += '高二'; break;
      case 3: ans += '高三'; break;
      default: throw new Error('年段配置错误');
    }
    str[0] = 0;
    let classId = parseInt(str.slice(1));
    ans += '（' + classId + '）班';
    return ans;
  } catch (e) {
    // swal({
    //   title: "错误",
    //   icon: "error",
    //   text: e.message
    // });
    console.log(e.message);
    return '&&&';
  }
}

let getGradeName = (grade) => {
  switch (grade) {
    case 0:
      return '教工';
    case 1:
      return '高一';
    case 2:
      return '高二';
    case 3:
      return '高三';
    default:
      return undefined;
  }
};

let getTypeName = (type) => {
  switch (type) {
    case 'stu_male':
      return '男子组';
    case 'stu_female':
      return '女子组';
    case 'stu_union':
      return '集体组';
    case 'teach_1':
      return '甲组';
    case 'teach_2':
      return '乙组';
    case 'teach_3':
      return '丙组';
    case 'teach_union':
      return '集体组';
    default:
      return undefined;
  }
};

let getGradeColor = (grade) => {
  switch (grade) {
    case 0:
      return 'purple';
    case 1:
      return 'red';
    case 2:
      return 'orange';
    case 3:
      return 'blue';
    default:
      return undefined;
  }
};

let getTypeColor = (type) => {
  switch (type) {
    case 'stu_male':
      return 'purple';
    case 'stu_female':
      return 'pink';
    case 'stu_union':
      return 'teal';
    case 'teach_1':
      return 'blue';
    case 'teach_2':
      return 'cyan';
    case 'teach_3':
      return 'red';
    case 'teach_union':
      return 'orange';
    default:
      return undefined;
  }
};
</script>