import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { AppComponent } from '../../../../app.component';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FrameUploaderComponent } from '../../../../core/uploader/uploader.component';

@Component({
  selector: 'app-group-enrol-manage',
  templateUrl: './group-enrol-manage.component.html',
  styleUrls: ['./group-enrol-manage.component.scss']
})
export class GroupEnrolManageComponent extends FlexTableComponent implements OnInit {

  query = {
    query: '',
    fee_status: ''
  }
  QUERY_URL = "std/stdEnrol/page.htm";
  PAY_URL = "std/stdEnrol/setPay.htm";
  NO_PAY_URL = "std/stdEnrol/setUnPay.htm";
  DELETE_URL = "std/stdEnrol/delete.htm";
  UPLOAD_URL = "std/stdEnrol/upload.htm";
  DOWNLOAD_URL = "std/stdEnrol/download.htm";
  ERROR_DOWNLOAD_URL = "std/stdEnrol/downloadError.htm";
  ENROL_CONFIRM_URL = "std/stdEnrol/printEnrolConfirm.htm";
  EXPORT_URL = "std/stdEnrol/export.htm";
  IN_TIME_URL = "cf/cfOperateTime/getLatest.htm";


  no_pay = 0;//未缴费
  have_pay = 1;//已缴费
  filePath: any;//错误文件路径
  inTimeFlag = true;
  timeArr = []; //报考起止时间

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  constructor(public bsModalService?: BsModalService) {
    super();
  }

  ngOnInit() {
    FrameRequest.requestArray(this.IN_TIME_URL, {oper_type : 2}, (array) => {
      if(array && array.length > 0) {
        this.inTimeFlag = true;
        this.timeArr = array[0];
      } else {
        this.inTimeFlag = false;
      }
    });
  }

  fee(flag: string) {
    let ids = this.framePager.getCheckedArray(item => item.id);
    if (!ids || ids.length === 0) {
      AppComponent.alert({ title: '提示', text: '请至少选择一个考生' });
      return;
    }
    if (!this.payjudge(flag)) return;
    if (flag === 'pay') {
      AppComponent.confirm("确定缴费吗？", () => {
        FrameRequest.request(this.PAY_URL, { ids: ids }, () => {
          AppComponent.GobalComponent.successMsg("缴费成功");
          this.framePager.reload();
        });
      });

    } else {
      AppComponent.confirm("确定标记未缴费吗？", () => {
        FrameRequest.request(this.NO_PAY_URL, { ids: ids }, () => {
          AppComponent.GobalComponent.successMsg("设置未缴费成功");
          this.framePager.reload();
        });
      });
    }
  }

  async preDeleteEvent() {
    let feeStatus = this.framePager.getCheckedArray(item => item.fee_status);
    let result = feeStatus.findIndex(item => item === this.have_pay);
    if (result !== -1) {
      AppComponent.alert({ title: '提示', text: '已缴费的考生不能删除' });
      return false;
    }
    return true;
  }

  payjudge(flag: string) {
    let feeStatus = this.framePager.getCheckedArray(item => item.fee_status);
    if (flag === 'pay') {
      let result = feeStatus.findIndex(item => item === this.have_pay);
      if (result !== -1) {
        AppComponent.alert({ title: '提示', text: '请选择未缴费的考生' });
        return false;
      } else {
        return true;
      }
    } else {
      let result = feeStatus.findIndex(item => item === this.no_pay);
      if (result !== -1) {
        AppComponent.alert({ title: '提示', text: '请选择已缴费的考生' });
        return false;
      } else {
        return true;
      }
    }
  }

  downloadTemplate() {
    FrameRequest.download(this.DOWNLOAD_URL, {fileName: 'group_std_enrol.xlsx'});
  }

  /**
 * 批量报考
 */
  upload(item?: any) {
    this.uploader.choooseFileAndUpload({}, ((ajax) => {
      if (ajax.errorCode === -1) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else if(ajax.errorCode === -2) {
        AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
      } else {
        AppComponent.GobalComponent.successMsg('批量报考成功');
        this.filePath = '';
        this.hideModal();
        this.framePager.reload();
      }
    }).bind(this));
  }

  //错误数据下载
  downloadErrFile() {
    FrameRequest.download(this.ERROR_DOWNLOAD_URL, { filePath: this.filePath });
  }

 //校对单
 print() {
   let checkedArray = this.framePager.getCheckedArray();
  let ticketArray = this.framePager.getCheckedArray((item) => item.ticket_no);
  if(ticketArray.length > 1) {
    for(let i=0;i<ticketArray.length;i++) {
      for(let j=i+1;j<ticketArray.length;j++) {
        if(ticketArray[i] !== ticketArray[j]) {
          AppComponent.alert({title:"信息提示",text:"请选择一个考生!"})
          return;
        }
      }
    }
  } else if(ticketArray.length == 0) {
    AppComponent.alert({title:"信息提示",text:"请选择一个考生!"})
    return;
  }
    FrameRequest.download(this.ENROL_CONFIRM_URL, {
      ticket_no : checkedArray[0].ticket_no,
      year_code: checkedArray[0].year_code
    });
  }


  //导出
  export() {
    FrameRequest.download(this.EXPORT_URL, {}, () => {
      AppComponent.GobalComponent.successMsg("导出成功");
    });
  }

}
