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

  public query = {
    query: "",
    year_code: null,
    already_in_city:"",
    is_high_risk:"",
    is_quarantine:"",
    enrol_agent_id:""
  };

  QUERY_URL = "epidemic/stdReportForm/page.htm";
  ADD_URL = "epidemic/stdReportForm/add.htm";
  DELETE_URL= "epidemic/stdReportForm/delete.htm";
  GET_URL= "epidemic/stdReportForm/get.htm";
  EDIT_URL= "epidemic/stdReportForm/edit.htm";
  UPLOAD_URL = "epidemic/stdReportForm/upload.htm";
  EXPORT_URL = "epidemic/stdReportForm/export.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";

  AREA_LIST_URL = "cf/cfOrganization/list.htm?org_type=4&status=1"; //考区列表

  timeList = [];
  planList = [];
  examAgentArray = [];


  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.requestArray(this.AREA_LIST_URL,{}, async (array) => {
      this.examAgentArray = array;
    });

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

    
  }

  async preGoEditEvent(entity?) {
    FrameRequest.requestArray("epidemic/stdReportForm/queryStdReportFormPlanCityByReportFormId.htm", {id:entity.id}, (array) => {
      this.planList = array;

    });

    return true;
  }


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

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

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

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

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

        if (!this.query.enrol_agent_id) {
          this.query.enrol_agent_id="";
        }
        
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download(this.EXPORT_URL, this.query, () => {
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("导出成功");
        });
      }

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

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

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

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

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

        if (!this.query.enrol_agent_id) {
          this.query.enrol_agent_id="";
        }
        
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("epidemic/stdReportForm/exportHigh.htm", this.query, () => {
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("导出成功");
        });
      }

      


}
