import { Component, OnInit, ViewChild } from '@angular/core';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { AppComponent } from '../../../../..';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FrameUploaderComponent } from '../../../../../core/uploader/uploader.component';

@Component({
  selector: 'app-join-school-std-record',
  templateUrl: './join-school-std-record.component.html',
  styleUrls: ['./join-school-std-record.component.scss']
})
export class JoinSchoolStdRecordComponent extends FlexTableComponent implements OnInit {

  public query = {
    "query": "",
    "year_code": "",
    "mr_id": "",
    "status": "",
    "school_id" :""
  };

  QUERY_URL = "pl/plStdRecord/pagejunior.htm";
  DELETE_URL = "pl/plStdRecord/delete.htm";
  GET_URL = "pl/plStdRecord/get.htm";
  ADD_URL = "pl/plStdRecord/add.htm";
  EDIT_URL = "pl/plStdRecord/edit.htm";
  UPLOAD_URL = "pl/plStdRecord/juniorUpload.htm";
  DOWNLOAD_URL = "pl/plStdRecord/download.htm";
  ERROR_DOWNLOAD_URL = "pl/plStdRecord/downloadError.htm";
  YEAR_CODE_URL = "pl/plYearCode/list.htm";
  MAJOR_LIST_URL = "pl/plMajorRecord/list.htm";
  SUBMIT_URL = "pl/plStdRecord/submit.htm";
  FIND_URL = "pl/plStdRecordFile/find.htm";
  LASTEST_YEAR_CODE_URL = "pl/plStdRecordTime/getBatch.htm";
  FIND_STD_URL = "/std/stdRegInfo/findStd.htm";
  SCHOOL_LIST_URL = "pl/plMajorRecord/listSchool.htm"; // 衔接学校所在的主考学校列表

  majorList = []; //主考学校申报的专业
  filePath: any;//错误文件路径
  apply_form = 1; //专本衔接考生
  status = 1; //待主考学校审核

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  @ViewChild('uploaderFile')
  uploaderFile: FrameUploaderComponent;


  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      'ticket_no': [null, Validators.required],
      'std_name': [null, Validators.required],
      'cert_no': [null, Validators.required],
      'major_record_id': [null, Validators.required],
      'std_no': [null, null],
      'std_num': [null, Validators.required],
      'junior_grade': [null, null],
      'junior_major': [null, null]
    });
  }

  ngOnInit() {
    FrameRequest.requestEntity(this.LASTEST_YEAR_CODE_URL, {}, (entity) => {
      if(entity) {
        this.query.year_code = entity.year_code;
        this.chooseMajor();
        this.framePager.reload();
      }
    });
  }


  chooseMajor() {
    if(!this.query.year_code) {
      this.query.year_code = "";
    }
    FrameRequest.requestArray(this.MAJOR_LIST_URL, { year_code: this.query.year_code, apply_form: this.apply_form }, (array) => {
      this.majorList = array;
    });
  }
  
  //根据准考证查询考生信息
  findStdInfo(ticket_no: any) {
    FrameRequest.requestEntity(this.FIND_STD_URL, {ticket_no: ticket_no}, (entity) => {
      if(entity) {
        if(!this.item) {
          this.item = {};
        } 
        this.item.std_name =  entity.std_name;
        this.item.cert_no =   entity.cert_no;
      } else {
        AppComponent.alert({ title: "信息提示", text: "准考证填写有误!" });
        return ;
      }
    });
  }

  search(param) {
    param.query = this.query.query;
    param.status = this.query.status;
    param.mr_id = this.query.mr_id;
    if(!this.query.year_code) {
      this.query.year_code = "";
    }
    param.year_code=this.query.year_code;
    if(!this.query.school_id) {
      this.query.school_id = "";
    }
    param.school_id = this.query.school_id;
  }
 

  async preGoEditEvent(entity?) {
    if (!this.checkBatch()) return;
    return true;
  }

  //提交审核
  audit() {
    if (!this.checkBatch()) return;
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray.length === 0) {
      AppComponent.confirm("确定提交审核" + this.query.year_code + "批次的所有考生吗？", () => {
        FrameRequest.request(this.SUBMIT_URL, { year_code: this.query.year_code, status: this.status }, () => {
          AppComponent.GobalComponent.successMsg("提交成功");
          this.framePager.reload();
        });
      });
    } else {
      AppComponent.confirm("确定提交审核选中考生吗？", () => {
      let checkIds = this.framePager.getCheckedArray((item) => item.id);
      FrameRequest.request(this.SUBMIT_URL, { year_code: this.query.year_code, ids: checkIds.join(","), status: this.status }, () => {
        AppComponent.GobalComponent.successMsg("提交成功");
        this.framePager.reload();
      });
    });
    }
  }

  checkBatch() {
    if (!this.query.year_code) {
      AppComponent.alert({ title: "信息提示", text: "请选择批次!" });
      return false;
    }
    return true;
  }


  /**
  * 批量导入
  */
  upload(item?: any) {
    if (!this.checkBatch()) {
      return;
    }
    this.uploader.choooseFileAndUpload({ year_code: this.query.year_code }, ((ajax) => {
      if (ajax.errorCode === -1) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else {
        AppComponent.GobalComponent.successMsg('批量导入成功');
        this.filePath = '';
        this.hideModal();
        this.framePager.reload();
      }
    }).bind(this));
  }


  //模板下载
  download() {
    FrameRequest.download(this.DOWNLOAD_URL, { fileName: 'junior_std_record.xlsx' });
  }

  //错误数据下载
  downloadErrFile() {
    FrameRequest.download(this.ERROR_DOWNLOAD_URL, { filePath: this.filePath });
  }

  async preSaveEvent() {
    if (!this.item) {
      this.item = {};
    }
    if (!this.item.year_code) {
      this.item.year_code = this.query.year_code;
    }
    if (!this.item.apply_form) {
      this.item.apply_form = this.apply_form;
    }
    return true;
  }

  async preDeleteEvent() {
    let checkedArray = this.framePager.getCheckedArray((item) => item.status);
    for (let i = 0; i < checkedArray.length; i++) {
      if (checkedArray[i] === 2 || checkedArray[i].status === 3) {
        AppComponent.alert({ 'title': '提示', text: '待考试院审核和审批通过的记录不能删除！' });
        return false;
      }
    }
    return true
  }


  goUploadFile(editFormTemplateModal, modalcss?) {
    if(!this.query.year_code) {
      AppComponent.alert({ 'title': '提示', text: '请选择批次！' });
      return;
    }
    let css = modalcss ? modalcss : "modal-lg";
    this.bsModalWin = this.bsModalService.show(editFormTemplateModal, { class: css });
  }


  async aftSaveEvent(){
    AppComponent.alert({ 'title': '提示', text: '保存成功后，请提交到主考学校审核！' });
  }

  

}
