import { Component, OnInit, OnDestroy } from "@angular/core";
import { ToolService } from 'src/app/services/tool.service';
import { ActivatedRoute, Router } from '@angular/router';
import { TabService } from './services/tab.service';
import { NzMessageService } from "ng-zorro-antd/message";
import { SimpleReuseStrategy } from 'src/app/SimpleReuseStrategy';
@Component({
  selector: "app-new-statistics",
  templateUrl: "./new-statistics.component.html",
  styleUrls: ["./new-statistics.component.less"],
})
export class NewStatisticsComponent implements OnInit, OnDestroy {
  constructor(
    public tool: ToolService,
    public route: ActivatedRoute,
		public msg: NzMessageService,
    public router: Router,
    public tabService: TabService,
  ) {}

  menuList: any[] = [];
  ngOnInit() {
    this.getMenuList();
    this.initHospitalAndDuty(this.tool.getCurrentHospital());
    console.log('this.menuList:', this.menuList)
  }

  ngOnDestroy(){
		sessionStorage.removeItem('maintenanceData')
    this.tabService.deleteAllRouteSnapshot();
  }

  getMenuList(){
    this.menuList = this.tool.getMenuAutoType();
  }

  // 全院查询-1|院区查询-2|责任部门查询-3|部门垂直查询-4
  hospital;
  duty;
  queryType;//查询范围
	parent:any = 0;
  // 初始化院区、部门显示
  initHospitalAndDuty(hospitalOrDuty){
    let sessionData = JSON.parse(sessionStorage.getItem('newStatistics'));
    console.log(sessionData);
    if(sessionData){
      this.hospital = sessionData.hospital;
      this.duty = sessionData.duty;
      this.queryType = sessionData.queryType;
			this.parent = sessionData.parent;
      return;
    }
    let type = 'hospital';//默认是院区
    if(hospitalOrDuty.type){
      // 当前是责任部门
      type = 'duty';
    }

    if(type === 'duty'){
      // 当前的所属责任部门
      this.hospital = hospitalOrDuty.parent;
      this.duty = hospitalOrDuty;
    }else{
      // 当前的所属院区
      this.hospital = hospitalOrDuty;
      this.duty = undefined;
    }
    console.log('this.hospital:', this.hospital)
    console.log('this.duty:', this.duty)

    // 查询范围单选框
    if(this.hospital){
      if(this.duty){
        if(this.duty.parentDeptId){
          this.queryType = 3;
        }else{
          this.queryType = 4;
        }
      }else{
        this.queryType = 2;
      }
    }else{
      this.queryType = 1;
    }

    this.sessionSave();
  }

  // 缓存数据
  sessionSave(){
    sessionStorage.setItem('newStatistics', JSON.stringify({queryType: this.queryType, hospitalId: this.hospital ? this.hospital.id : undefined, dutyId: this.duty ? this.duty.id : undefined, hospital: this.hospital, duty: this.duty, parent: this.parent}));
  }

  // 查询范围
  queryRangeFlag = false;
  queryRangeClick(){
    this.queryRangeFlag = true;
  }
  submitQueryRange({queryType, hospital, duty, parent}) {
    console.log('queryType:', queryType)
    console.log('hospital:', hospital)
    console.log('duty:', duty)
    this.queryType = queryType;
    this.hospital = hospital;
    this.duty = duty;
		this.parent = parent;
    this.queryRangeFlag = false;

    this.sessionSave();

    let url = location.href.split('/#')[1];
    console.log(url);

    this.router.navigateByUrl('/redirect', {
      skipLocationChange: true
    }).then(() => {
      this.router.navigate([url], { replaceUrl: true });
    });
  }
  cancelQueryRange() {
    this.queryRangeFlag = false;
  }

  // 返回
  goBack(){
		let roleMenus = JSON.parse(localStorage.getItem("user")).menu;
		let sy = false;
		let gglb = false;
		let gzgd = false;
		sessionStorage.removeItem('maintenanceData')
		roleMenus.forEach((e) => {
			if (e.link == "home") {
			  sy = true;
			}else if (e.link == "orderManagement") {
		    gglb = true;
		  }else if (e.link == "incidentManagement") {
		    gzgd = true;
		  }
		});
		
		if(!gglb && !gzgd && !sy){
			this.msg.error("暂无权限进入系统！", {
			  nzDuration: 2000,
			});
			return
		}
		if(sy){
			this.router.navigateByUrl("main");
			return
		}else if(gglb){
			this.router.navigateByUrl("/main/orderManagement");
			return
		}else if(gzgd){
			this.router.navigateByUrl("/main/incidentManagement");
			return
		}
		if(gglb && gzgd){
			this.router.navigateByUrl("/main/orderManagement");
		}
		if(!gglb && !gzgd && sy){
			this.router.navigateByUrl("main");
		}
    // this.router.navigate(['/main/home']);
  }
}
