import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { ModalService } from 'src/app/core/service/modal.service';
import { MsgService } from 'src/app/core/service/msg.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { EnrolDataPageComponent } from '../../enrol-data/enrol-data-page/enrol-data-page.component';

@Component({
  selector: 'app-base-data-page',
  templateUrl: './base-data-page.component.html',
  styleUrls: ['./base-data-page.component.scss']
})
export class BaseDataPageComponent extends FramePageComponent implements OnInit {

    schoolArray = [];
    batchArr = [];
    layout_status = "";
    query = {
      school_id : '',
      batch_id : ''
    };

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

  ngOnInit(): void {
    this.AjaxService.requestArray('./cf/school/list.htm', {}, (array) => {
      this.schoolArray = array;
    });
  }

  changeSchool() {
    this.AjaxService.requestArray("./cf/batch/list.htm", {school_id : this.query.school_id}, (array)=>{
      debugger;
      this.batchArr = array;
    });    
  }

  requestData() {
    this.pager.reload({school_id : this.query.school_id, batch_id : this.query.batch_id});
    this.freshLyStatus();
  }

  //刷新编排状态
  freshLyStatus() {
    this.AjaxService.request("imp/layout/status.htm", {school_id: this.query.school_id, batch_id: this.query.batch_id}, (ret)=>{
      this.layout_status = ret.entity;
    });
  }

  showImpEnrols(v) {
    this.showComponentModal(EnrolDataPageComponent, { 'ResponseEntity': v });
  }

  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:'ImpData.xls',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    this.onModalClose =() => {
      this.freshLyStatus();
    }    
    ModalService.showUploadWindow(this.bsService,'./imp/base/upload.htm', reqParam, uploadParam);    
  }

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

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

  //一键编排
  layout() {
    if (!this.query.school_id) {
      MsgService.errorToastr('请选择学校！');
      return;
    }
    if (!this.query.batch_id) {
      MsgService.errorToastr('请选择批次！');
      return;
    }
    this.onModalClose =() => {
      this.freshLyStatus();
    }
    MsgService.confirmAlert('确定执行编排吗？', () => {
      this.AjaxService.request('imp/base/layout.htm', {school_id: this.query.school_id, batch_id: this.query.batch_id}, (ajax) => {
        ModalService.showThreadWindow(this.bsService, ajax.entity, {threadTitle: '请求已提交，正在编排!'});
      });
    });
  }

  //重置编排
  resetLayout() {
    MsgService.confirmAlert('谨慎!确定重置吗？', () => {
      this.AjaxService.request('imp/base/reset.htm', {school_id: this.query.school_id, batch_id: this.query.batch_id}, (ajax) => {
        MsgService.successToastr('已重置！');
        this.freshLyStatus();
      });
    });
  }

  //增量上传
  doUploaFileIncre() {
    if(!this.query.batch_id) {
      MsgService.errorToastr('请选择批次！');
      return;
    }
    //线程参数 和 业务参数
    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:'ImpData.xls',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    ModalService.showUploadWindow(this.bsService,'./imp/base/upload/incre.htm', reqParam, uploadParam);    
  }

  //增量编排
  layoutIncre() {
    if (!this.query.school_id) {
      MsgService.errorToastr('请选择学校！');
      return;
    }
    if (!this.query.batch_id) {
      MsgService.errorToastr('请选择批次！');
      return;
    }
    this.onModalClose =() => {
      debugger;
      this.freshLyStatus();
    }
    MsgService.confirmAlert('确定执行编排吗？', () => {
      this.AjaxService.request('imp/base/layout/incre.htm', {school_id: this.query.school_id, batch_id: this.query.batch_id}, (ajax) => {
        ModalService.showThreadWindow(this.bsService, ajax.entity, {threadTitle: '请求已提交，正在编排!'});
      });
    });
  }

  //清除测试数据,恢复到编排后初始状态
  clearTestData() {
    MsgService.confirmAlert('谨慎!确定清除测试数据吗？', () => {
      this.AjaxService.request('imp/base/exam/data/clear.htm', {school_id: this.query.school_id, batch_id: this.query.batch_id}, (ajax) => {
        MsgService.successToastr('已重置！');
        this.freshLyStatus();
      });
    });
  }

}
