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

@Component({
  selector: 'app-final-score-register',
  templateUrl: './final-score-register.component.html',
  styleUrls: ['./final-score-register.component.scss']
})
export class FinalScoreRegisterComponent extends FramePageComponent implements OnInit {

  @ViewChild('batchPager', {static: false}) batchPager: FramePager;
  query = {
    school_id: '',
    batch_id: '',
    class_room: ''
  };
  classroomArray = [];
  schoolArray = [];
  batchArray = [];
  classVoArray = [];
  update_list = [];//修改拟录取数组
  currSchoolCode: any;
  currSchoolId: any;
  currBatchId: any;
  curStep = "batch";

  constructor(private modalService: BsModalService, private detectRef: ChangeDetectorRef) {
    super(modalService);
  }

  ngOnInit() {
    //debugger;
    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();
      }
    });
  }

  //查询导入明细
  showDetailList(v) {
    this.currSchoolCode = v.school_code;
    this.currSchoolId = v.school_id;
    this.currBatchId = v.batch_id;
    this.curStep = "detail";
    //查询考场
    this.AjaxService.requestArray('./std/final/score/classroomlist.htm', {school_id: this.currSchoolId, batch_id: this.currBatchId, is_admin: false}, (array) => {
      this.classroomArray = array;
    });
    //重新渲染 考场列表
    this.freshClassRoomList(this.currSchoolId, this.currBatchId, "");
  }

  goDetail() {
    this.curStep = "detail";
  }

  freshClassRoomList(schoolId, batchId, classRoom) {
    this.AjaxService.requestArray('./std/final/score/classroom/default/list.htm', {school_id: schoolId, batch_id: batchId, class_room: classRoom, is_admin: false}, (array) => {
      this.classVoArray = array;
    });
  }

  changeClassroom() {
    this.freshClassRoomList(this.currSchoolId, this.currBatchId, this.query.class_room);
  }

  //返回批次列表
  backBatch() {
    this.curStep = "batch";
  }

  doUploaFile() {
    debugger;
    //线程参数 和 业务参数
    let reqParam = {
      thread_name: "初始导入线程",
      error_flag: "InActive",
      callback_type: "Swal",
      school_id: this.currSchoolId,
      batch_id: this.currBatchId
    };
    //上传参数
    let uploadParam = {
      uploadTitle: '登分数据导入',
      templateFile: 'ScoreRegister' + this.currSchoolId + '.xlsx',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose: true
    };
    ModalService.showUploadWindow(this.bsService, './std/final/score/upload.htm', reqParam, uploadParam);
  }

  uploadWithThreadCallback(ajax) {
    console.log("k--->" + ajax);
    MsgService.successToastr("导入成功!");
    
  }

  queryBatchArray() {
    AjaxService.requestArray("cf/batch/list.htm", {school_id: this.query.school_id}, (array) => {
      this.batchArray = array;
      //debugger;
      if (this.batchArray.length >= 1) {
        this.query.batch_id = this.batchArray[0].batch_id;
      }
    });
    this.batchPager.reload();
  }

  delete() {
    debugger;
    if (!this.currSchoolId) {
      MsgService.errorAlert('请选择学校！');
      return;
    }
    if (!this.currBatchId) {
      MsgService.errorAlert('请选择批次！');
      return;
    }
    MsgService.confirmAlert("确定删除该批次下导入数据吗?", () => {
      this.AjaxService.request("std/final/score/upload/delete.htm", {school_id: this.currSchoolId, batch_id: this.currBatchId}, (ret) => {
        MsgService.successAlert('删除成功！');
        //重新渲染 考场列表
        this.freshClassRoomList(this.currSchoolId, this.currBatchId, "");
      });
    });
  }

  //导出复试最终表
  export() {
    FuncService.ajaxDownload('std/final/score/export.htm', this.query);
  }

  //保存考场成绩
  saveClassRoomScore(v) {
    debugger;
    /*let hasError = false;
    for (let gList of v.group_list) {
      for (let s of gList) {
        if (!s.final_score) {
          MsgService.errorAlert("考生【" + s.std_name + "】成绩不能为空!");
          hasError = true;
          break;
        }
      }
    }
    if (hasError) {
      return;
    }*/
    // MsgService.confirmAlert(`确定保存？`, () => {
      AjaxService.request('std/final/score/save.htm', {school_id: this.currSchoolId, batch_id: this.currBatchId, data_json: JSON.stringify(v)}, () => {
        MsgService.successToastr(`保存成功！`);
        this.freshClassRoomList(this.currSchoolId, this.currBatchId, this.query.class_room);
      }, {RequestWithLoading: true});
    // });
  }

  goSubmit(v) {
    debugger;
    if (v.group_list && v.group_list.length > 0 && v.group_list[0][0].score_level) {
      this.curStep = "update";
      this.AjaxService.requestArray('./std/final/score/classroom/level/list.htm', {school_id: this.currSchoolId, batch_id: this.currBatchId, class_room: v.class_room}, (array) => {
        debugger;
        this.update_list = array[0].group_list;
      });
    } else {
      MsgService.errorAlert(`保存成绩后才能提交！`);
    }

  }

  //提交考场成绩录取意见
  updateScoreRemark() {
    debugger;
    let v = {};
    v["group_list"] = this.update_list;
    // MsgService.confirmAlert(`确定保存？`, () => {
      AjaxService.request('std/final/score/remark/save.htm', {school_id: this.currSchoolCode, batch_id: this.currBatchId, data_json: JSON.stringify(v)}, () => {
        MsgService.successToastr(`保存成功！`);
      }, {RequestWithLoading: true});
    // });
  }

  checkScore(s) {
    if (/^[0-9]\d*$/.test(s.final_score) == false) {
      s.final_score = "";
    }
    if (parseInt(s.final_score, 10) > 100) {
      s.final_score = "";
    }
  }
}
