import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { FrameUploaderComponent } from '../../../../../core/uploader/uploader.component';
import { AppComponent } from '../../../../../app.component';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FrameFunction } from '../../../../../shared/frame/frame_function';
import { ByFilesPdfViewerModal } from '../../by-files-pdf-viewer/by-files-pdf-viewer-modal';


@Component({
  selector: 'app-by-roster-collect',
  templateUrl: './by-roster-collect.component.html',
  styleUrls: ['./by-roster-collect.component.scss']
})
export class ByRosterCollectComponent  extends FlexTableComponent implements OnInit {

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  @ViewChild('byFilesPdfModal') byFilesPdfModal: ByFilesPdfViewerModal;

  public query = {
   
    bysj:"",
    tag:""

  };
 

  batchArr = [];
  tagArr = [];


  ADD_URL = "by/byRoster/add.htm";
  EDIT_URL = "by/byRoster/edit.htm";

  UPLOAD_URL="by/byRoster/upload.htm";

  QUERY_URL = "by/byRoster/page.htm";
  GET_URL= "by/byRoster/get.htm";
  DOWNLOAD_URL = "by/byRoster/download.htm";

  BATCH_URL ="by/byRoster/bysj/list.htm";

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
        id: [null],
        bysj: [null,Validators.required],
        tag: [null,Validators.required],
        byzy: [null,null],
        files_path: [null,null],
        dir: [null,null],
    });
   }

   ngOnInit() {
    this.requestBatch();
  }

  requestBatch() {
    FrameRequest.requestArray(this.BATCH_URL, {}, (array)=> {
      this.batchArr = array;
    });
  }

  getTag($event) {
    this.tagArr = [];
    FrameRequest.requestArray('by/byRoster/tag/list.htm', {bysj: $event}, (array)=> {
      this.tagArr = array;
    });
  }

  //检索考生档案
  search(param) {
    
   
    if(!this.query.bysj) {
      this.query.bysj = "";
    }

    if(!this.query.tag) {
      this.query.tag = "";
    }

    param.bysj = this.query.bysj;
    param.tag = this.query.tag;


  }

  //上传电子档案
  uploadFile() {
    if(!this.item.dir) {
      AppComponent.GobalComponent.errorMsg('请填写电子名册存放目录！');
      return;
    }

    if(!this.item.tag) {
      AppComponent.GobalComponent.errorMsg('请填写袋号！');
      return;
    }

    let param = {
      RequestLoading:true,
      dir: this.item.dir,
      tag: this.item.tag
    }
    this.uploader.choooseFileAndUpload(param, ((ret) => {
      if (ret.errorCode == 0) {
        this.item.root_path = ret.map.root_path;
        this.item.files_path = ret.map.files_path;
        AppComponent.GobalComponent.successMsg("上传成功!");
      } else {
        AppComponent.GobalComponent.errorMsg(ret.errorMsg);
      }
    }));
  }


  //下载电子档案
  downloadFile(file_path) {
    FrameRequest.download(this.DOWNLOAD_URL,  { files_path: file_path });
  }

  //保存档案采集
  saveFiles() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if(!this.item.files_path) {
      AppComponent.GobalComponent.errorMsg('请上传电子档案！');
      return;
    }
    FrameRequest.requestEntity(this.ADD_URL, this.item, async () => {
      AppComponent.GobalComponent.successMsg('名册添加成功');
      this.aftSaveEvent();
      this.hideModal();
      this.framePager.reload();
    });
  }

  preShow(id){
    FrameRequest.requestEntity("by/byRoster/byRosterIsExists.htm", {id: id,is_preView:true}, (entity) => {
      if (!entity) {
        this.byFilesPdfModal.ids= id;
        this.byFilesPdfModal.PDF_URL= 'by/byRoster/download.htm';
        this.byFilesPdfModal.show();
      }else{
        AppComponent.alert({title:"信息提示",text:entity});
      }
    });

  }

 
}
