import { Component, OnInit, ViewChild } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../frame/core/detail/frame.detail';
import { AjaxService } from '../../../../core/service/ajax.service';
import { FuncService } from '../../../../core/service/func.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FrameUploadSilence } from '../../../../core/frame/upload/frame-upload-silence/frame-upload-silence';
import { FrameService } from '../../../../core/service/frame.service';

//declare let laydate;

@Component({
  selector: 'app-std-detail',
  templateUrl: './std-detail.component.html',
  styleUrls: ['./std-detail.component.scss']
})
export class StdDetailComponent extends FrameDetailComponent implements OnInit {
  @ViewChild('uploader', {static: false}) uploader: FrameUploadSilence;

  mSchoolArray = [];
  majorArray = [];
  examAreaArray = [];
  areaArray = [];
  cityArray = [];
                
  std_image_url = "";

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'cert_type': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(12)])],
      'cert_no': [null, Validators.compose([Validators.minLength(5), Validators.maxLength(18)])],
      'std_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(64)])],
      'std_eng_name': [null],
      'std_sex': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(10)])],
      'political': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(10)])],
      'std_birth': [null, Validators.compose([Validators.minLength(8), Validators.maxLength(10)])],
      'register': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'nation': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'std_source': [null, Validators.compose([Validators.required])],
      'education': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'major_level': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'm_school_id': [null, Validators.compose([Validators.min(1), Validators.max(9999999999)])],
      'major_id': [null, Validators.compose([Validators.min(1), Validators.max(9999999999)])],
      'exam_area': [null,Validators.compose([Validators.required])],
      'occupation': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(128)])],
      'health': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'area_code': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'city_code': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(15)])],
      'std_mobile': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(11)])],
      'std_post': [null, Validators.compose([Validators.minLength(5), Validators.maxLength(15)])],
      'email': [null, Validators.compose([Validators.minLength(5), Validators.maxLength(32)])],
      'address': [null, Validators.compose([Validators.minLength(5), Validators.maxLength(128)])],
      'std_image': [null, Validators.compose([Validators.minLength(1), Validators.maxLength(128)])],
      'apply_type': [null],
      'ticket_no': [null]
    });
    //this.getDate();
  }

  ngOnInit() {
    AjaxService.requestArray("conf/org/list.htm",{dept_type : "master"},(array)=>{
      this.mSchoolArray = array;
    });
    //查询专业
    AjaxService.requestArray("conf/major/list.htm",{},(array)=>{
      this.majorArray = array;
    });
    //查询考区
    AjaxService.requestArray("conf/org/list.htm",{dept_type : "examArea"},(array)=>{
      this.examAreaArray = array;
    });
    //查询地（市）
    AjaxService.requestArray("conf/area/list.htm",{},(array)=>{
      this.areaArray = array;
    });
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {

    } else if (this.detailMode == 'edit') {
     
      if (this.currentEntity.std_image && this.currentEntity.std_image.indexOf('gxzk/default') >= 0) {
        this.std_image_url = 'https://file.gxeea.cn:8811/remote/file/' + this.currentEntity.std_image;
      } else {
        this.std_image_url = FrameService.getParamValue('EnrolMaterialWebPath') + this.currentEntity.std_image;
      }
    }
  }

  //初始化时间控件
  /*getDate() {
    let _this = this;
    setTimeout(() => {
      laydate.render({
        elem:'#std_birth',
        type:'date',
        theme:'#0c6acf',
        format: 'yyyy-MM-dd',
        trigger: 'click',
        done:(value, date, std_birth)=>{
          _this.currentEntity.std_birth = value;
        }
      });
    }, 500);
  }*/

  download(contract_id) {
    FuncService.ajaxDownload('conf/contract/contract/download.htm', {contract_id: contract_id});
  }

  freshCity(area_code) {
    //查询地（市）
    AjaxService.requestArray("conf/area/city/fresh.htm",{area_code: this.currentEntity.area_code},(array)=>{
      this.cityArray = array;
    });
  }

  // 选择文件后
  chooseFile(item) {
    debugger;
    this.currentEntity.std_image = item.name;
    MsgService.showLoading();
    this.uploader.uploadFile(this.currentEntity);
  }

  afterFileUpload(data) {
    debugger;
    console.log("afterFileUpload---" + data.entity);
    this.currentEntity.std_image = data.entity;
    MsgService.successToastr("上传成功!");
  }

}
