import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FrameService } from '../../../../../core/service/frame.service';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { LayoutStdSubjectDetailComponent } from '../layout-std-subject-detail/layout-std-subject-detail.component';

@Component({
  selector: 'app-layout-std-subject-page',
  templateUrl: './layout-std-subject-page.component.html',
  styleUrls: ['./layout-std-subject-page.component.scss']
})
export class LayoutStdSubjectPageComponent extends FramePageComponent implements OnInit {

  agentArray = [];
  dateArray = [];
  roomArray = [];
  checkin: any;


  constructor(private modalService: BsModalService) { 
    super(modalService);
  }

  ngOnInit(): void {
    this.checkin = {};
    this.agentArray = [];
    this.dateArray = [];
    this.roomArray = [];
    // 获取所有考点
    AjaxService.requestArray('exam/checkin/init/agent.htm', {}, (array) => {
      this.agentArray = array;
      if (this.agentArray.length === 1) {
        this.checkin.agent_id = this.agentArray[0].ly_agent_id;
        this.checkin.ly_agent_id = this.agentArray[0].ly_agent_id;
        this.requestDateArray();
      }
    });
  }

  // 获取考试日期
  requestDateArray() {
    AjaxService.requestArray('exam/checkin/init/date.htm', this.checkin, (array) => {
      if (FrameService.FrameOptr.optr_id !== 0) {
        for (const d of array) {
          if (d.ly_room_id === 0) {
            const s = new Array();
            s.push(d);
            this.dateArray = s;
            break;
          }
        }
        if (this.dateArray === undefined || this.dateArray.length == 0) {
          this.dateArray = array;
        }
      } else {
        this.dateArray = array;
      }
      if (this.dateArray.length === 1) {
        this.checkin.exam_date = this.dateArray[0].exam_date;
        this.requestRoom();
      }
    });
  }

  requestRoom() {
    AjaxService.requestArray('exam/interview/score/room/list.htm', this.checkin, (array) => {
      this.roomArray = array;
      for(const room of this.roomArray) {
        room.room_name = room.group_name + '第【' + room.room_seq + '】考场';
      }
    });
  }

  requestData() {
    this.pager.reload(this.checkin);
  }

  showStdSubject(ap) {
    this.showComponentModal(LayoutStdSubjectDetailComponent, {
      'ResponseEntity': ap,
      'arrayUrl': 'ly/std/subject/list.htm',
      'arrayParam': {
        group_id: ap.ly_group_id,
        std_id: ap.std_id
      }
    });
  }

}
