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-files-maintain',
  templateUrl: './by-files-maintain.component.html',
  styleUrls: ['./by-files-maintain.component.scss']
})
export class ByFilesMaintainComponent  extends FlexTableComponent implements OnInit {

  public query = {
  };


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

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  @ViewChild('byFilesPdfModal') byFilesPdfModal: ByFilesPdfViewerModal;

  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() {
  }

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


  async preSaveEvent(){
    if(this.item.cert_no && !FrameFunction.isIdCard(this.item.cert_no)) {
      AppComponent.GobalComponent.errorMsg('身份证不符合规范！');
      return false;
    }
    return true;
  }



  //上传电子档案
  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);
      }
    }));
  }


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

    
  }

}
