import { Component, OnInit } from '@angular/core';
import { MyService } from '../my.service';
import { LifeCommonService } from '../../common/life-common.service';
import { ActivatedRoute,Router } from "@angular/router";

@Component({
  selector: 'ydlife-salary',
  templateUrl: './salary.component.html',
  styleUrls: ['./salary.component.scss']
})
export class SalaryComponent implements OnInit {
  practitionerId: any;
  payScaleInfos: Array<any>;
  //控制弹框
  toastDialog: boolean;
  toastInfo: any;
  tabList:Array<any>;
  selectedId:any = 0;
  nowYear:any = (new Date()).getFullYear();
  historyPart1:Array<any> = [];
  historyPart2:Array<any> = [];
  constructor(private activatedRoute: ActivatedRoute,private myService: MyService,
     public lifeCommonService: LifeCommonService,
     private router: Router) {

      }

  ngOnInit() {
    console.log(this.nowYear)
    const title = this.activatedRoute.snapshot.data[0]['title'];
    this.lifeCommonService.setTitle(title);
    this.practitionerId = JSON.parse(localStorage.getItem('lifeCustomerInfo'))['practitionerId']
    this.payScaleListQuery();
    this.tabList = [
      {name:`${this.nowYear}年`,id:0},
      {name:'历史薪资',id:1}
    ]
  }

  payScaleListQuery() {
    this.myService.payScaleListQuery({ practitionerId:this.practitionerId,isHistory: this.selectedId,isActive:1}).subscribe((res) => {
      if (res['success']) {
        if(this.selectedId == 0){
          if(this.nowYear == 2021){
            this.payScaleInfos = res['data']['payScaleInfos'].filter((item)=>{
              return (item.years == 2021 && item.month >= 5)
            });
          }else{
            this.payScaleInfos = res['data']['payScaleInfos'];
          }
        }else{
          //2021年5月之后的数据
          this.historyPart2 = res['data']['payScaleInfos'].filter((item)=>{
            return  new Date(item.monDtlPeriod).getTime() >= 1619827200000
          });
          //2021年5月之前的数据
          this.historyPart1 = res['data']['payScaleInfos'].filter((item)=>{
            return  new Date(item.monDtlPeriod).getTime() < 1619827200000
          });
        }
      } else {
        this.payScaleInfos = [];
      }
    })
  }


  selectTab(tabId){
    this.selectedId = tabId;
    this.payScaleListQuery();
  }

  //进入薪资详情
  enterDetail(years,month,agent_id,isBasic){
    this.router.navigate(['/salary_detail'],{ queryParams: { years:years,month:month,agent_id:agent_id,isBasic:isBasic} });
  }

  //下载pdf
  getPdf(monShId,url) {
    if(url){
      window.open(url)
      // location.href = url;
    }else{
      this.myService.payScalePdf({ monShId: monShId, practitionerId: Number(this.practitionerId) }).subscribe((res) => {
        if (res['success']) {
          if (res['data']['url']) {
            window.open(res['data']['url'])
            // location.href = res['data']['url'];
          } else {
            this.toastDialog = true;
            this.toastInfo = {
              status: 1,
              msg: '暂无薪资单！',
              timeout: 3000,
              align: 'center'
            };
            return;
          }
        }
      })
    }
  }
  
}

