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';


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

  public query = {
  };
  @ViewChild('uploader')
  uploader: FrameUploaderComponent;


  QUERY_URL = "by/byFiles/page/query.htm";
  ADD_URL = "by/byFiles/add.htm";
  DOWNLOAD_URL = "by/byFiles/std/download.htm";
  UPLOAD_URL="by/byFiles/upload/std/form.htm";



  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
        id: [null],
        diaplma_no: [null,Validators.required],
        std_name: [null,Validators.required],
        std_sex: [null,null],
        cert_no: [null,null],
        ticket_no: [null,null],
        zy: [null,null],
        zylx: [null,null],
        bysj: [null,null],
        zkyx: [null,null],
        files_path: [null,null],
    });
   }

  ngOnInit() {
    this.item ={};
    this.item.zylx = '';
    this.item.std_sex = ''; 
  }

  queryByByDiaplmano(e){
      if(this.item && this.item.diaplma_no){
        FrameRequest.requestEntity("by/byFiles/queryByByDiaplmano.htm", {diaplma_no: this.item.diaplma_no}, (entity) => {
          if(entity && entity.diaplma_no){
            if(!this.item.std_name) {
              this.item = entity;
            }
          }
        }); 
      }
  }

  //上传电子档案
  uploadFile() {
    if(!this.item || !this.item.diaplma_no) {
      AppComponent.GobalComponent.errorMsg('请填写考生毕业证号！');
      return;
    }
    let param = {
      RequestLoading:true,
      diaplma_no: this.item.diaplma_no
    }
    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();
    });
  }

 
}
