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

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


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

  schoolArray = [];
  batchArray = [];
  query = {
    school_id: '',
    batch_id: '',
    is_practice:  '',
    subject_type:  ''
  };
  countMap={
    practice:0,nopractice:0,equedu:0
  };
  @ViewChild('group', {static: false}) group: FrameAjax;

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

  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.loadData();
    };

  }

  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.loadData();
      }
    });
  }


  makeGroup() {
    if (this.getCheckedArray().length < 1) {
      MsgService.errorToastr('分组操作至少需要1个科目！');
      return;
    }

    this.showModal({ResponseEntity:this.query,ResponseArray:this.getCheckedArray()});
  }


  loadData() {
    this.group.reload()
    this.ajax.reload()
    AjaxService.requestMap('ly/layout/count.htm', this.query, (map) => {
      this.countMap=map;
    });
  }


  buildLayout() {
    if (this.query.school_id && this.query.batch_id)
      AjaxService.requestEntity('ly/layout/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('ly/layout/delete.htm', this.query, () => {
          MsgService.successToastr('删除成功！');
          this.loadData();
        });
      })
    else MsgService.errorAlert('请选择学校和批次！');


  }

  deleteGroup(v) {

      MsgService.confirmAlert('确定解散该编排分组设置吗？', () => {
        AjaxService.request('ly/lyGroup/delete.htm', v, () => {
          MsgService.successToastr('编排分组设置已解散！');
          this.loadData();
        });
      })



  }

  public checkedMasterToggle(event){
    this.setCheckedAll(event.currentTarget.checked);
  }

  public setCheckedAll(checkStatus){
    this.ajax.getArray().forEach((item) => {
      item.isChecked = checkStatus;
    });
  }

  // 获取checked数组
  public getCheckedArray(func?){
    const checkedArray = [];
    this.ajax.getArray().forEach((item) => {
      if(item.isChecked === true)
        if(func){
          checkedArray.push(func.call(this,item));
        }else{
          checkedArray.push(item);
        }
    });
    return checkedArray;
  }
}
