import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePager } from '../../../../../core/frame/pager/frame-pager/frame-pager';
import {FrameUpload} from "../../../../../core/frame/upload/frame-upload/frame-upload";
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { JudgeRoomPageDetailComponent } from '../judge-room-page-detail/judge-room-page-detail.component';

@Component({
  selector: 'app-judge-room-page',
  templateUrl: './judge-room-page.component.html',
  styleUrls: ['./judge-room-page.component.scss']
})
export class JudgeRoomPageComponent extends FramePageComponent implements OnInit {
  
  
  FrameUpload = FrameUpload;
  @ViewChild('groupPager', { static: false }) groupPager: FramePager;
  @ViewChild('judgePager', { static: false }) judgePager: FramePager;

  step = 'main';
  schoolArray = [];
  batchArray = [];
  param = {
    school_id: '',
    batch_id :'',
    ly_group_id: ''
  }

  constructor(private modalService: BsModalService, private cdr : ChangeDetectorRef) {
    super(modalService);
    this.modalComponent = JudgeRoomPageDetailComponent;
   }

  ngOnInit(): void {
    this.AjaxService.requestArray('cf/school/list.htm',{}, (array) => {
      this.schoolArray = array;
      if(this.schoolArray.length == 1) {
        this.param.school_id = this.schoolArray[0].school_id;
        this.requestBatch(this.schoolArray[0].school_id);
      }
    });
  }

  requestBatch(school_id) {
    this.AjaxService.requestArray('cf/school/batch/list.htm', {
      school_id : this.param.school_id
    }, (array) => {
      this.batchArray = array;
      this.cdr.detectChanges();
      if(this.batchArray.length == 1) {
        this.param.batch_id = this.batchArray[0].batch_id;
        this.groupPager.reload({school_id : school_id, batch_id: this.batchArray[0].batch_id});
      } else if(this.batchArray.length > 1) {
        this.groupPager.reload({school_id : school_id});
      }
    });
  }

  requestGroup() {
    this.groupPager.reload({school_id: this.param.school_id, batch_id: this.param.batch_id});
  }

  requestJudgeBatch(school_id) {
    this.AjaxService.requestArray('cf/school/batch/list.htm', {
      school_id : this.param.school_id
    }, (array) => {
      this.batchArray = array;
      this.cdr.detectChanges();
      if(this.batchArray.length == 1) {
        this.judgePager.reload({school_id : school_id, batch_id: this.batchArray[0].batch_id, ly_group_id: this.param.ly_group_id});
      } else if(this.batchArray.length > 1) {
        this.judgePager.reload({school_id : school_id, ly_group_id: this.param.ly_group_id});
      }
    });
  }

  requestJudge() {
    this.param.ly_group_id = null;
    this.judgePager.reload({school_id : this.param.school_id, batch_id: this.param.batch_id, ly_group_id: this.param.ly_group_id});
  }

  goStep(step, ly_group_id?) {
    this.step = step;
    this.param.ly_group_id = ly_group_id;
  }

  export() {
    this.FuncService.ajaxDownload('ex/judge/room/group/export.htm', {school_id: this.param.school_id, batch_id: this.param.batch_id});
  }

  uploadCallback(ajax) {
    this.MsgService.successAlert('导入成功!');
  }

  showAddEditJudge(v?) {
    this.onModalClose = () => {
      this.judgePager.reload();
    };
    if (v) {
      this.showComponentModal(JudgeRoomPageDetailComponent, { 'ResponseEntity': v, 'ResponseArray' : this.schoolArray });
    } else {
      this.showComponentModal(JudgeRoomPageDetailComponent, {'ResponseArray' : this.schoolArray});
    }
  }
}
