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


@Component({
  selector: 'app-high-risk-area',
  templateUrl: './high-risk-area.component.html',
  styleUrls: ['./high-risk-area.component.scss']
})
export class HighRiskAreaComponent  extends FlexTableComponent implements OnInit {

  public query = {
    query: "",
    year_code: null
  };

  QUERY_URL = "epidemic/highRiskArea/page.htm";
  ADD_URL = "epidemic/highRiskArea/add.htm";
  DELETE_URL= "epidemic/highRiskArea/delete.htm";
  GET_URL= "epidemic/highRiskArea/get.htm";
  EDIT_URL= "epidemic/highRiskArea/edit.htm";
  UPLOAD_URL = "epidemic/highRiskArea/upload.htm";
  EXPORT_URL = "epidemic/highRiskArea/export.htm";
  DOWNLOAD_URL = "epidemic/highRiskArea/download.htm";
  ERROR_DOWNLOAD_URL = "epidemic/highRiskArea/downloadError.htm";
  EXAM_TIME_URL = "pl/plExamTime/list.htm";
  BZMBGBXZQHDM_URL = "epidemic/highRiskArea/queryBzmbGbXzqhdm.htm?parent_id=000000";
  CURR_YEAR_CODE_URL = "pl/plExamTime/getCurrentYearCode.htm";



  timeList = [];

  provinceList = [];
  cityList = [];
  countyList = [];

  filePath: any;//错误文件路径

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      year_code: [null],
      province: [null],
      city: [null],
      county: [null]
    });
   }

  ngOnInit() {

    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (array) => {
      this.timeList = array;

    });

    FrameRequest.requestEntity(this.CURR_YEAR_CODE_URL, {}, (entity) => {
      if(entity){
        this.query.year_code = parseInt(entity);
        this.framePager.reload();
      }
     
    });

    FrameRequest.requestArray("epidemic/highRiskArea/queryBzmbGbXzqhdm.htm", {parent_id:"000000"}, (array) => {
      this.provinceList = array;

    });

   

  }

  async preGoEditEvent(entity?) {
    
    
    this.cityList = [];
    this.countyList = [];

    if (entity && entity.province) {
      FrameRequest.requestArray("epidemic/highRiskArea/queryBzmbGbXzqhdm.htm", {parent_id:entity.province}, (array) => {
        this.cityList = array;

      });
    } 

    if (entity && entity.city) {
      FrameRequest.requestArray("epidemic/highRiskArea/queryBzmbGbXzqhdm.htm", {parent_id:entity.city}, (array) => {
        this.countyList = array;
      });
    } 

    return true;
  }

  changeCity(parent_id : any) {
    
    if(parent_id) {
      FrameRequest.requestArray("epidemic/highRiskArea/queryBzmbGbXzqhdm.htm", {parent_id:parent_id}, (array) => {
        this.cityList = array;
      });
     
    }
  }

  changeCounty() {
    
    if(this.item.city) {
      FrameRequest.requestArray("epidemic/highRiskArea/queryBzmbGbXzqhdm.htm", {parent_id:this.item.city}, (array) => {
        this.countyList = array;
      });
     
    }
  }

  saveData() {
   
    if(!this.item.year_code){
      AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
      return;
    }

    if(!this.item.province){
      AppComponent.alert({ 'title': '提示', 'text': '请选择省（市、自治区）' });
      return;
    }

    if(!this.item.city){
      AppComponent.alert({ 'title': '提示', 'text': '请选择市' });
      return;
    }

    if(!this.item.county){
      AppComponent.alert({ 'title': '提示', 'text': '请选择区（县）' });
      return;
    }

    let url = this.ADD_URL;
    if(this.item.id){
      url = this.EDIT_URL;
    }

   
      AppComponent.confirm("确定保存吗?",()=>{
        FrameRequest.requestMap(url,  this.item, ()=>{
          this.hideModal();
          this.framePager.reload();
          AppComponent.GobalComponent.successMsg("保存成功!");
        });
     });
     
      
      }


      export() {
        if (!this.query.year_code) {
          AppComponent.alert({ title: '提示', text: '请选择批次' });
          return;
        }
        
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download(this.EXPORT_URL, { year_code: this.query.year_code, query: this.query.query }, () => {
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("导出成功");
        });
      }


      download() {
        FrameRequest.download(this.DOWNLOAD_URL, { fileName: 'epidemic_high_risk_area.xlsx' }, () => {
          AppComponent.GobalComponent.successMsg("模板下载成功");
        });
      }

      downloadErrFile() {
        FrameRequest.download(this.ERROR_DOWNLOAD_URL, { filePath: this.filePath });
      }

      upload() {
        if (!this.query.year_code) {
          AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
          return;
        }
        this.uploader.choooseFileAndUpload({ year_code: this.query.year_code }, ((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));
      }

}
