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-imp-data-score-page',
  templateUrl: './imp-data-score-page.component.html',
  styleUrls: ['./imp-data-score-page.component.scss']
})
export class ImpDataScorePageComponent extends FramePageComponent implements OnInit {

  @ViewChild('batchPager', {static: false}) batchPager: FramePager;
  @ViewChild('detailPager', {static: false}) detailPager: FramePager;
  query = {
    school_id: '',
    batch_id: ''
  };
  schoolArray = [];
  batchArray = [];
  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";
    //重新渲染 detailPager
    if (!this.detailPager) {
      this.detectRef.detectChanges();
    }
    this.detailPager.reload();
    console.log(this.currSchoolId + "," + this.currBatchId);
  }

  //返回批次列表
  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: 'ImpScore.xlsx',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose: true
    };
    ModalService.showUploadWindow(this.bsService, './imp/impDataScore/upload.htm', reqParam, uploadParam);
  }

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



  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.errorToastr('请选择学校！');
      return;
    }
    if (!this.currBatchId) {
      MsgService.errorToastr('请选择批次！');
      return;
    }
    MsgService.confirmAlert("确定删除该批次下导入数据吗?", () => {
      this.AjaxService.request("imp/impDataScore/delete.htm", {school_id: this.currSchoolId, batch_id: this.currBatchId}, (ret) => {
        MsgService.successAlert('删除成功！');
        this.detailPager.reload();
      });
    });
  }

  export() {
    FuncService.ajaxDownload('imp/impDataScore/export.htm', this.query);
  }

  doUploaCheckFile() {
    debugger;
    //线程参数 和 业务参数
    let reqParam = {
      thread_name: "初始导入线程",
      error_flag: "InActive",
      callback_type: "Swal",
      school_id: this.currSchoolId,
      batch_id: this.currBatchId
    };
    //上传参数
    let uploadParam = {
      uploadTitle: '复核成绩数据导入',
      templateFile: 'ImpScoreCheck.xlsx',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose: true
    };
    ModalService.showUploadWindow(this.bsService, './imp/impDataScore/check/upload.htm', reqParam, uploadParam);
  }
}
