import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { NzMessageService } from "ng-zorro-antd";
import { MainService } from "src/app/services/main.service";
import { ToolService } from "src/app/services/tool.service";

@Component({
  selector: "app-worker-statistics-detail",
  templateUrl: "./worker-statistics-detail.component.html",
  styleUrls: ["./worker-statistics-detail.component.less"],
})
export class WorkerStatisticsDetailComponent implements OnInit {
  loading1 = false; //获取选中配送人员的工单列表loading
  hosId; //当前院区id
  currentPersonId; //当前配送人员id
  startTime; //当前开始时间
  endTime; //当前结束时间
  groupId; //当前组ID
  type; //区别跳转页面类型
  deptId; //当前科室
  countRemark; //标识
  listOfData = []; //获取选中配送人员的工单列表
  pageIndex: number = 1; //页码
  listLength: number = 0; //总数据条目数
  pageSize: number = 10; //每页条数
  isFiveOrder: boolean = false; //是否执行时长五分钟以内工单
  specialClose: boolean = false; //特殊关闭工单
  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.currentPersonId = this.route.snapshot.params.workerId;
    this.startTime = this.route.snapshot.params.startTime;
    this.endTime = this.route.snapshot.params.endTime;
    this.groupId = this.route.snapshot.params.groupId;
    this.type = this.route.snapshot.params.type;
    this.deptId = this.route.snapshot.params.deptId;
    this.countRemark = this.route.snapshot.params.countRemark;
    this.getOrdersByPerson(this.hosId, this.currentPersonId);
  }

  // 是否剖执行时长五分钟以内工单
  changeIsFiveOrder(e: boolean) {
    this.isFiveOrder = e;
    this.pageIndex = 1;
    this.getOrdersByPerson(this.hosId, this.currentPersonId);
  }
  // 是否特殊关闭工单
  changeSpecialClose(e: boolean) {
    this.specialClose = e;
    this.pageIndex = 1;
    this.getOrdersByPerson(this.hosId, this.currentPersonId);
  }
  //获取选中配送人员的工单列表
  getOrdersByPerson(
    hosId: number,
    currentPersonId: number,
  ) {
    this.loading1 = true;
    let postData:any = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      startTime: this.startTime === 'null' ? undefined : this.startTime,
      endTime: this.endTime === 'null' ? undefined : this.endTime,
      hosId,
    };
    if(this.isFiveOrder){
      postData.difLong = true;
    }
    if(this.specialClose){
      postData.specialClose = 1;
    }else{
      postData.specialClose = this.route.snapshot.params.specialClose === 'null' ? undefined : this.route.snapshot.params.specialClose;
    }
    if(this.type === 'workerStatistics'){
      postData.groupId = this.groupId === 'null' ? undefined : this.groupId;
      postData.worker = currentPersonId;
    }else if(this.type === 'deptInspectionRateStatistics'){
      postData.deptId = this.deptId === 'null' ? undefined : this.deptId;
    }else if(this.type === 'deptInspectionStatistics'){
      postData.deptId = this.deptId === 'null' ? undefined : this.deptId;
      postData.countRemark = this.countRemark === 'null' ? undefined : this.countRemark;
    }
    this.mainService.postCustom("report", "userDetails", postData).subscribe(
      (result) => {
        this.loading1 = false;
        if (result.status == 200) {
          this.listOfData = result.list;
          this.listLength = result.totalNum;
        } else {
          this.message.error("获取数据失败");
        }
      },
      (err) => {
        this.loading1 = false;
        this.message.error("获取数据失败");
      }
    );
  }
  //查看积分详情
  integralDetail(obj) {
    this.router.navigateByUrl(
      `/main/${this.type}/workerStatisticsDetail/${this.currentPersonId}/${this.startTime}/${this.endTime}/${this.groupId}/${this.type}/${this.deptId}/${this.route.snapshot.params.specialClose}/orderDetail/${obj.id}/3`
    );
  }
  //关闭
  close() {
    this.router.navigateByUrl(`/main/${this.type}`);
  }
}
