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 {CfAspectDetailComponent} from '../aspect-detail/aspect-detail.component';
import { FramePager } from '../../../../core/frame/pager/frame-pager/frame-pager';
import { SetAspectOptrComponent } from '../set-aspect-optr/set-aspect-optr.component';
import { FrameUpload } from 'src/app/core/frame/upload/frame-upload/frame-upload';
import { ModalService } from 'src/app/core/service/modal.service';

@Component({
  selector: 'app-cf-aspect-page',
  templateUrl: './aspect-page.component.html',
  styleUrls: ['./aspect-page.component.scss']
})
export class CfAspectPageComponent extends FramePageComponent implements OnInit {
  @ViewChild('uploader', {static: false}) uploader: FrameUpload;
  schoolArray = [];
  batchArray = [];
  query = {
    school_id : '',
    batch_id : ''
  };

  constructor(private modalService: BsModalService) {
    super(modalService);
    this.modalComponent = CfAspectDetailComponent;
  }


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

  requestData() {
    //debugger;
    this.pager.pagerParams["batch_id"] = this.query.batch_id;
    this.pager.reload();
  }

  setReviewOptr(v) {
    this.onModalClose = () => {
      this.pager.reload();
    };
    this.showComponentModal(SetAspectOptrComponent, { 'ResponseEntity': v });
  }

  formatOptrName(v) {
    //debugger;
    if (!v.optr_name) {
      return "设置专业评审老师";
    }
    return v.optr_name;
  }

  doUploaFile() {
    debugger;
    //线程参数 和 业务参数
    let reqParam = {
      thread_name: "初始导入线程",
      error_flag: "InActive", 
      callback_type: "Swal",
      school_id : this.query.school_id,
      batch_id : this.query.batch_id
    };
    //上传参数
    let uploadParam = {
      uploadTitle:'初始数据导入',
      templateFile:'ImpAspectOptr.xlsx',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    ModalService.showUploadWindow(this.bsService,'./cf/aspect/optr/upload.htm', reqParam, uploadParam);    
  }

  uploadWithThreadCallback(ajax) {
    console.log("导入完成....");
    MsgService.successToastr("导入成功!");
    this.pager.reload();
  }
}
