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";
import { format } from "date-fns";

@Component({
  selector: "app-inspect-active-order-list",
  templateUrl: "./inspect-active-order-list.component.html",
  styleUrls: ["./inspect-active-order-list.component.less"],
})
export class InspectActiveOrderListComponent implements OnInit {
  loading1 = false; //获取选中配送人员的工单列表loading
  hosId; //当前院区id
  currentDeptId; //当前科室id
  workerId; //当前配送人员id
  startTime; //当前开始时间
  endTime; //当前结束时间
  date; //选中的日期
  listOfData = []; //获取选中配送人员的工单列表
  pageIndex: number = 1; //页码
  listLength: number = 0; //总数据条目数
  pageSize: number = 10; //每页条数
  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.currentDeptId = this.route.snapshot.params.deptId;
    this.startTime = this.route.snapshot.params.startTime;
    this.endTime = this.route.snapshot.params.endTime;
    this.date = this.route.snapshot.params.date;
    this.workerId = this.route.snapshot.params.workerId;
    this.getListByWorker(this.hosId, this.currentDeptId);
  }
  //获取选中工单列表
  getListByWorker(hosId: number, currentDeptId: number) {
    this.loading1 = true;
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      workOrder: {
        taskType: { associationType: { id: 260 } },
        abnormityType: 0,
        timeOut: 0,
        startTime1: format(this.date - 0, "yyyy-MM-dd") + " 00:00:00",
        endTime1: format(this.date - 0, "yyyy-MM-dd") + " 23:59:59",
        hosId,
        createDept: currentDeptId,
        worker: {
          id: this.workerId,
        },
      },
    };
    this.mainService.getFetchDataList("data", "workOrder", postData).subscribe(
      (result) => {
        this.loading1 = false;
        if (result.status == 200) {
          this.listOfData = result.list;
          this.listOfData.forEach((v) => {
            switch (v.sourceId) {
              case 2:
                v.sourceType = "科室派单";
                break;
              case 3:
                v.sourceType = "系统自动建单";
                break;
              case 4:
                v.sourceType = "主动建单";
                break;
              default:
                v.sourceType = "";
            }
          });
          this.listLength = result.totalNum;
        } else {
          this.message.error("获取数据失败");
        }
      },
      (err) => {
        this.loading1 = false;
        this.message.error("获取数据失败");
      }
    );
  }
  //查看积分详情
  integralDetail(obj) {
    this.router.navigateByUrl(
      `/main/inspectActive/inspectActiveDetail/${this.currentDeptId}/${this.startTime}/${this.endTime}/inspectActiveOrderList/${this.currentDeptId}/${this.startTime}/${this.endTime}/${this.date}/${this.workerId}/orderDetail/${obj.id}/3`
    );
  }
  //关闭
  close() {
    this.router.navigateByUrl(
      `/main/inspectActive/inspectActiveDetail/${this.currentDeptId}/${this.startTime}/${this.endTime}`
    );
  }
}
