import {Component, OnInit} from '@angular/core';
import {BsModalService} from 'ngx-bootstrap/modal';
import {FramePageComponent} from '../../../frame/core/page/frame.page';
import {LyRoomPageDetailComponent} from '../ly-room-page-detail/ly-room-page-detail.component';
import {AjaxService} from '../../../../core/service/ajax.service';
import {MsgService} from '../../../../core/service/msg.service';

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

  constructor(modalService: BsModalService) {
    super(modalService);
    this.modalComponent = LyRoomPageDetailComponent;
  }

  schoolArray = [];
  batchArray = [];
  query = {
    school_id: '',
    batch_id: ''
  };


  ngOnInit() {
    this.AjaxService.requestArray('./cf/school/list.htm', {}, (array) => {
      this.schoolArray = array;
      if (this.schoolArray.length === 1) {
        this.query.school_id = this.schoolArray[0].school_id;
        this.queryBatchArray();
      }
    });
    this.onModalClose = () => {
      this.pager.reload();
    };
  }

  queryBatchArray() {

    AjaxService.requestArray('cf/batch/list.htm', {school_id: this.query.school_id}, (array) => {
      this.batchArray = array;
      if (this.batchArray.length >= 1) {
        this.query.batch_id = this.batchArray[0].batch_id;
        this.requestData();
      }
    });
  }

  requestData() {

    // this.pager.pagerParams.batch_id = this.query.batch_id;
    this.pager.reload(this.query);
  }


  showAddModal() {
    if (this.query.school_id && this.query.batch_id)
      this.showModal(this.query)
    else MsgService.errorAlert('请选择学校和批次！');
  }
}
