import {ChangeDetectorRef, 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';
import { ImpDataDetailComponent } from '../imp-data-detail/imp-data-detail.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-imp-data-page',
  templateUrl: './imp-data-page.component.html',
  styleUrls: ['./imp-data-page.component.scss']
})
export class ImpDataPageComponent extends FramePageComponent implements OnInit {
  @ViewChild('batchPager', {static: false}) batchPager: FramePager;
  @ViewChild('detailPager', {static: false}) detailPager: FramePager;
  @ViewChild('uploader', {static: false}) uploader: FrameUpload;
  query = {
    school_id : '',
    batch_id : ''
  };
  schoolArray = [];
  batchArray = [];
  currSchoolCode : any;
  currSchoolId : any;
  currBatchId : any;
  curStep = "batch";

  constructor(private modalService: BsModalService, private detectRef: ChangeDetectorRef) {
    super(modalService);
    this.modalComponent = ImpDataDetailComponent;
  }

  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:'ImpData' + this.currSchoolCode + '.xlsx',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    ModalService.showUploadWindow(this.bsService,'./imp/data/upload.htm', reqParam, uploadParam);    
  }

  uploadWithThreadCallback(ajax) {
    console.log("k--->" + ajax);
    /*console.log("upload callback --->" + ajax.errorCode + " , " + ajax.url);
    if (ajax.errorCode != 0 && ajax.url) {
      MsgService.errorToastr("导入报错!");
    } else {
      MsgService.successToastr("导入成功!");
    }*/
    MsgService.successToastr("导入成功!");
    this.detailPager.reload();
  }

  //增量导入
  doUploaFileIncre() {

  }

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

  requestData() {
    
  }

  //导出
  export() {

  }

  showEditModal(v) {

  }

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