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-initial-notice-page',
  templateUrl: './initial-notice-page.component.html',
  styleUrls: ['./initial-notice-page.component.scss']
})
export class InitialNoticePageComponent extends FramePageComponent implements OnInit {
  @ViewChild('approvPager', {static: false}) approvPager: FramePager;
  @ViewChild('rejectPager', {static: false}) rejectPager: FramePager;
  schoolArray = [];
  batchArray = [];
  majorArray = [];
  optrArray = [];
  curStep = "main";
  stdMain : any;//考生主记录

  query = {
    school_id : '',
    batch_id : '',
    major_ids : '',
    optr_ids: '',
    main_id : '',
    main_status : ''
  };
  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() {
    let 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('./review/initial/optr/list.htm', param, (array) => {
      this.optrArray = array;
    });
  }

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

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

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

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

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

  requestData() {
    //debugger;

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

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

  sendApprovNotice() {
    let 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,
      detail_type: "approv",
      initial_remark: ""
    };
    MsgService.confirmAlert('确定发微信通知吗！', () => {
      MsgService.showLoading();
      AjaxService.requestEntity("review/initial/main/approve/batch.htm",param,(entity)=>{
        MsgService.hideLoading();
        MsgService.successToastr("发送成功!");
      });   
    });
  }

  sendRejectNotice() {
    let 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,
      detail_type: "reject",
      initial_remark: "材料不符要求，请在12月12日12:00前完成材料修正及重传工作，逾期不予受理。"
    };
    MsgService.confirmAlert('确定发微信通知吗！', () => {
      MsgService.showLoading();
      AjaxService.requestEntity("review/initial/main/reject/batch.htm",param,(entity)=>{
        MsgService.hideLoading();
        MsgService.successToastr("发送成功!");
      });
    });
  }

  specialList = [];
  goStdDetail(v) {
    debugger;
    this.curStep = "std";
    this.stdMain = v;
    this.query.main_id = v.apply_id;
    this.query.school_id = v.school_id;
    this.query.batch_id = v.batch_id;
    this.query.main_status = v.status;
    this.stdMain.showSpecialColor = false;
    //查询限制、特殊处理考生列表
    AjaxService.requestArray("std/specila/list.htm",{school_id: this.query.school_id},(array)=>{
      this.specialList = array;
      for (const s of this.specialList) {
        if (s.exam_id == this.stdMain.exam_id && s.std_name == this.stdMain.std_name) {
          this.stdMain.showSpecialColor = true;
          break;
        }
      }
    }); 
  }

  goBack() {
    this.curStep = "main";
  }
}
