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

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

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

  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);
  }


  buildLayout() {
    if (this.query.school_id && this.query.batch_id)
      AjaxService.requestEntity('std/stdLayout/build.htm', this.query, (entity) => {
        ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
      });
    else MsgService.errorAlert('请选择学校和批次！');

  }

  deleteData() {
    if (this.query.school_id && this.query.batch_id)
      MsgService.confirmAlert('确认删除编排数据？', () => {
        AjaxService.request('std/stdLayout/delete.htm', this.query, () => {
          MsgService.successToastr('删除成功！');
          this.pager.reload(this.query);
        });
      })
    else MsgService.errorAlert('请选择学校和批次！');


  }
}
