import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";

import { MainService } from "../../services/main.service";
import { DateService } from "../../services/date.service";
import { MyServiceService } from "../../services/my-service.service";
import { ToolService } from "../../services/tool.service";
import { format, startOfDay, endOfDay, startOfToday } from 'date-fns';
import { NzMessageService } from "ng-zorro-antd";

@Component({
  selector: "app-inspect-statistics",
  templateUrl: "./inspect-statistics.component.html",
  styleUrls: ["./inspect-statistics.component.less"],
})
export class InspectStatisticsComponent implements OnInit {
  constructor(
		private message: NzMessageService,
    private route: ActivatedRoute,
    private router: Router,
    private mainService: MainService,
    private dateService: DateService,
    private myService: MyServiceService,
    private tool: ToolService
  ) {}

  ngOnInit() {
		const todayStart = startOfToday();
		const formattedTodayStart = format(todayStart, 'yyyy-MM-dd');
		this.startDate = formattedTodayStart+' '+'00:00:00';
		this.endDate = formattedTodayStart+' '+'23:59:59';
		this.dateRange = [this.startDate,this.endDate];
		this.hosId = this.tool.getCurrentHospital().id;
    this.coopBtns = this.tool.initCoopBtns(this.route);
		this.getList();
  }
	hosId:any = null;
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量

  dateRange: any = []; //发起时间区间 天

  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //操作提示框是否展示


  // 初始化增删改按钮
  coopBtns: any = {};
  searchData: any = {}; // 综合统计页面带过来的参数

 // 搜索
 search() {
   this.pageIndex = 1;
   this.getList();
 }
 // 重置
 reset() {
  this.pageIndex = 1;
	const todayStart = startOfToday();
	const formattedTodayStart = format(todayStart, 'yyyy-MM-dd');
	this.startDate = formattedTodayStart+' '+'00:00:00';
	this.endDate = formattedTodayStart+' '+'23:59:59';
	this.dateRange = [this.startDate,this.endDate];
  this.getList();
 }
 // 表格数据
 loading1 = false;
 getList() {
   var that = this;
   let data = {
     idx: that.pageIndex - 1,
     sum: that.pageSize,
     startTime: this.startDate,
     endTime: this.endDate,
     // hosId: this.hosId
   };

   this.loading1 = true;
   that.mainService
     .inspectionStatistics(data)
     .subscribe((res:any) => {
       this.loading1 = false;
       that.listOfData = res.list;
       that.listLength = res.totalNum;
     });
 }
 
 // 日期选择
 startDate: string; //发起时间开始
 endDate: string; //发起时间结束
 changeDate(result?): void {
   if (result.length==0) {
     this.startDate = this.endDate = null;
     return;
   }
   this.startDate = format(startOfDay(result[0]), 'yyyy-MM-dd HH:mm:ss');
   this.endDate = format(endOfDay(result[1]), 'yyyy-MM-dd HH:mm:ss');
	 this.dateRange = [this.startDate,this.endDate]
 }

// 导出
	maskFlag:any = null;
	excelExport(){
		this.maskFlag = this.message.loading("正在导出..", {
		  nzDuration: 0,
		}).messageId;
		this.mainService.exportReport("accompaniedInspection", {
			startTime: this.startDate,
			endTime: this.endDate,
		}).subscribe(
		  (data) => {
		    this.message.remove(this.maskFlag);
		    this.maskFlag = false;
		    this.message.success('导出成功');
		    var file = new Blob([data], {
		      type: "application/vnd.ms-excel",
		    });
		    var fileURL = URL.createObjectURL(file);
		    var a = document.createElement("a");
		    a.href = fileURL;
		    a.target = "_blank";
		    a.download = `陪检统计数据.xls`;
		    document.body.appendChild(a);
		    a.click();
		  },
		  (err) => {
		    this.message.remove(this.maskFlag);
		    this.maskFlag = false;
		    this.message.error('导出失败');
		  }
		);
	}
	
  //查看
  personDetail(data) {
		this.router.navigateByUrl("/main/inspectStatistics/orderDetail/" + data['0']);
  }

}
