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

@Component({
  selector: 'app-final-notice-page',
  templateUrl: './final-notice-page.component.html',
  styleUrls: ['./final-notice-page.component.scss']
})
export class FinalNoticePageComponent extends FramePageComponent implements OnInit {
  @ViewChild('passedPager', {static: false}) passedPager: FramePager;
  @ViewChild('noPassedPager', {static: false}) noPassedPager: FramePager;
  schoolArray = [];
  batchArray = [];
  majorArray = [];
  optrArray = [];

  query = {
    school_id : '',
    batch_id : '',
    major_ids : '',
    optr_ids: ''
  };
  constructor(private modalService: BsModalService) {
    super(modalService);
  }

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

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

  queryAspectAndOptr() {
    const param = {school_id: this.query.school_id, batch_id: this.query.batch_id};
    this.AjaxService.requestArray('./cf/major/list.htm', param, (array) => {
      // debugger;
      this.majorArray = array;
    });
    this.AjaxService.requestArray('./final/review/optr/list.htm', param, (array) => {
      this.optrArray = array;
    });
  }

  selectMajor() {
    this.passedPager.pagerParams["major_ids"] = this.query.major_ids;
    this.passedPager.reload();

    this.noPassedPager.pagerParams["major_ids"] = this.query.major_ids;
    this.noPassedPager.reload();
  }

  selectOptr() {
    console.log('optrIds--->' + this.query.optr_ids + ',' + this.passedPager.pagerParams["optr_ids"]);

    this.passedPager.pagerParams["optr_ids"] = this.query.optr_ids;
    this.passedPager.reload();

    this.noPassedPager.pagerParams["optr_ids"] = this.query.optr_ids;
    this.noPassedPager.reload();
  }

  requestData() {
    // debugger;

    this.passedPager.pagerParams['batch_id'] = this.query.batch_id;
    this.passedPager.pagerParams["major_ids"] = this.query.major_ids;
    this.passedPager.pagerParams["optr_ids"] = this.query.optr_ids;
    this.passedPager.reload();

    this.noPassedPager.pagerParams["batch_id"] = this.query.batch_id;
    this.noPassedPager.pagerParams["major_ids"] = this.query.major_ids;
    this.noPassedPager.pagerParams["optr_ids"] = this.query.optr_ids;
    this.noPassedPager.reload();
  }

  sendPassedNotice() {
    const param = {
      school_id: this.query.school_id,
      batch_id: this.query.batch_id,
      major_ids: this.query.major_ids,
      optr_ids: this.query.optr_ids,
      pass_type: 'passed'
    };
    AjaxService.requestEntity('final/review/notice/send.htm',param,(entity)=>{
      MsgService.successToastr('发送成功!');
    });
  }

  sendNoPassedNotice() {
    const param = {
      school_id: this.query.school_id,
      batch_id: this.query.batch_id,
      major_ids: this.query.major_ids,
      optr_ids: this.query.optr_ids,
      pass_type: 'no_passed'
    };
    AjaxService.requestEntity('final/review/notice/send.htm',param,(entity)=>{
      MsgService.successToastr('发送成功!');
    });
  }

  exportPassed() {
    const entity = {
      school_id: this.query.school_id,
      batch_id: this.query.batch_id,
      report_name:'exportPassed',
      report_title :'准考考生名单'
    };
    this.FuncService.ajaxDownload('frame/report/download.htm', entity);
  }

  exportNoPassed() {
    const entity = {
      school_id: this.query.school_id,
      batch_id: this.query.batch_id,
      report_name:'exportNoPassed',
      report_title :'不予准考考生名单'
    };
    this.FuncService.ajaxDownload('frame/report/download.htm', entity);

  }
}
