import { Component, OnInit, ViewChild } from '@angular/core';
import { FrameUploaderComponent } from '../../../../core/uploader/uploader.component';
import { AppComponent } from '../../../..';
import { FrameRequest } from '../../../../shared/frame/frame-request';

@Component({
  selector: 'app-nation-elect-turn',
  templateUrl: './nation-elect-turn.component.html',
  styleUrls: ['./nation-elect-turn.component.scss']
})
export class NationElectTurnComponent implements OnInit {

  TURN_IN_UPLOAD_URL = "by/ByCoutryTurn/turnin.htm"; // 外省转入导入
  TURN_OUT_UPLOAD_URL = "by/ByCoutryTurn/outBack.htm"; //转至外省（给过来的）回执导入
  TURN_OUT_DOWNLOAD_URL = "by/ByCoutryTurn/outExport.htm"; //转至外省数据导出
  TURN_IN_DOWNLOAD_URL = "by/ByCoutryTurn/backExport.htm"; //外省转入回执导出

  batch1: any; //外省转入-导入批次码
  batch2: any; //外省转入-回执文件导出批次码
  batch3: any; //转至外省-转出导出批次码
  batch4: any; //转至外省-回执导入批次码
  province: any; //转至省份
  filePath: any;

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  @ViewChild('backUploader')
  backUploader: FrameUploaderComponent;


  constructor() { }

  ngOnInit() {
  }

  //外省转入文件导入
  upload() {
    if (!this.batch1) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择转考批次！' });
      return;
    }
    AppComponent.GobalComponent.setLoading(true);
    this.uploader.choooseFileAndUpload({ time_config_id: this.batch1 }, ((ajax) => {
      AppComponent.GobalComponent.setLoading(false);
      if (ajax.errorCode === -1) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else if (ajax.errorCode === -2) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
      }else {
        AppComponent.GobalComponent.successMsg('数据导入成功');
      }
    }).bind(this));
  }

  exportRurninResult() {
    if (!this.batch1) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择转考批次！' });
      return;
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("by/ByCoutryTurn/exportRurninResult.htm", {
      time_config_id: this.batch1
    }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("外省转入导入结果导出成功");
    });
  }

  //外省回执导入
  backUpload() {
    if (!this.batch4) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择转考批次！' });
      return;
    }
    AppComponent.GobalComponent.setLoading(true);
    this.backUploader.choooseFileAndUpload({ time_config_id: this.batch4 }, ((ajax) => {
      AppComponent.GobalComponent.setLoading(false);
      if (ajax.errorCode === -1) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      }else if (ajax.errorCode === -2) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
      } else {
        AppComponent.GobalComponent.successMsg('数据导入成功');
      }
    }).bind(this));
  }

  //转至外省数据导出
  turnoutExport() {
    if(!this.checkNull(this.batch3)) {
      return;
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.TURN_OUT_DOWNLOAD_URL, { time_config_id: this.batch3,province_code: this.province}, ()=>{
         AppComponent.GobalComponent.setLoading(false);
         AppComponent.GobalComponent.successMsg("导出成功!");
     });	

  }

  //外省转入回执导出
  turninExport() {
    if(!this.checkNull(this.batch2)) {
      return;
    }

    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.TURN_IN_DOWNLOAD_URL, {time_config_id: this.batch2}, ()=>{
         AppComponent.GobalComponent.setLoading(false);
         AppComponent.GobalComponent.successMsg("导出成功!");
     });	
    
  }


  //空值判断
  checkNull(batch: any) {
    if(!batch) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择转考批次！' });
      return false;
    }
    return true;
  }
}
