import { Component, OnInit, ViewChild } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { FormBuilder, Validators, FormGroup, FormControl } from "@angular/forms";

import { MainService } from "../../services/main.service";
import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
import { ToolService } from "../../services/tool.service";
import { forkJoin, Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";
import { NzMessageService } from "ng-zorro-antd";
import { format, getYear } from 'date-fns';

@Component({
  selector: "app-task-inventory",
  templateUrl: "./task-inventory.component.html",
  styleUrls: ["./task-inventory.component.less"],
})
export class TaskInventoryComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  constructor(
    private message: NzMessageService,
    private fb: FormBuilder,
    private route: ActivatedRoute,
    private router: Router,
    private mainService: MainService,
    private tool: ToolService
  ) {}

  listOfData: any[] = []; //表格数据

  modal: boolean = false; //新增/编辑模态框
  add: boolean; //true:新增；false:编辑
  validateForm: FormGroup; //新增/编辑表单
  coopId: number; //表格中执行操作的id
  dept: any; //所属科室
  drugCode; //编号
  name; //名称
  userType: any; //用户类型
  hosId: any; //院区（搜索）
  userGroup; //所属组
  userGroup1; //所属组(搜索)
  userTypes: Array<any>; //所有用户类型
  alldepart: any = []; //所有所属科室
  alldepart1: any = []; //所有所属科室(搜索)
  hospitals1: any = []; //院区列表(搜索)
	drugTypeData: any = []; //药品类型
  allUserGroup: Array<any>; //所有用户组
  allUserGroup1: Array<any>; //用户组(搜索)
  allUserRole: Array<any>; //所有角色
  pageIndex: number = 1; //页码
  listLength: number = 10; //总条数
  pageSize: number = 10; //每页条数

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

  btnLoading: boolean = false; //提交按钮loading状态
  wxRequired = false; //新增或编辑用户的时候，微信号是否必填
  searchTaskSubject = new Subject(); //防抖
	searchParentDeptSubject = new Subject();
	specimenName:any = null;
	containPosition:any = null;
	placeList:any = [];
	systemList:any = [];
	apparatusList:any = [];
	specimenNameList:any = [];
	addressLoading:any = false;
	month:any = [];
	
  ngOnInit() {
		for(let i=1; i<13; i++){
			this.month.push({
				name:i + '月',
				value: i
			})
		}
		this.year = this.yearTime;
		console.log(2222,this.year)
    this.coopBtns = this.tool.initCoopBtns(this.route);
		this.hosId = this.tool.getCurrentHospital().id;
		this.getList();
  }
	
	yearTime:any = format(new Date(), 'yyyy');
	onChange(result: Date): void {
		this.yearTime = format(result, 'yyyy');
		console.log('onChange: ', this.yearTime);
	}
	
	// 选择月份
	selectIndex:any = 0;
	itemData:any = 1;
	itemClick(data, index){
		this.selectIndex = index;
		this.itemData = data.value;
		this.getList();
	}
	
  // 初始化增删改按钮
  coopBtns: any = {};
  // 表格数据
  loading1 = false;
	state:any;
	priority:any;
	monthTask:any;
	executor:any;
	year:any;
  getList() {
		let num = this.itemData<10 ? '0'+ this.itemData : this.itemData
    let data = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      monthTask: {
				operationType: 'taskDetails',
        yearMonth: this.yearTime + '-' + num
      },
    };
    this.loading1 = true;
    this.mainService
      .getFetchDataList("data", "monthTask", data)
      .subscribe((data) => {
        this.loading1 = false;
        this.listOfData = data.list;
        this.listLength = data.totalNum;
      });
  }

	// 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
	showPromptModal(con, success, promptInfo?) {
	  this.promptModalShow = false;
	  this.promptContent = con;
	  this.ifSuccess = success;
	  this.promptInfo = promptInfo;
	  setTimeout(() => {
	    this.promptModalShow = true;
	  }, 100);
	  this.getList();
	}
}

