import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { MainService } from '../../services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { startOfDay, format } from 'date-fns';
import { NzMessageService } from 'ng-zorro-antd';
@Component({
  selector: 'app-add-inspect-two-modal',
  templateUrl: './add-inspect-two-modal.component.html',
  styleUrls: ['./add-inspect-two-modal.component.less']
})
export class AddInspectTwoModalComponent implements OnInit {
  // 切换科室，切换弹窗
  isAllDisplayData2Checked = false;
  listOfDisplayData2: any[] = [];
  mapOfCheckedData2Id: { [key: string]: boolean } = {};
  isIndeterminateData2 = false;
  currentDept; //当前科室

  hosId;
  hsLoading = false;
  data2List: any = [];// 列表

  @Input() deptDisplay: any;

  @Output() closeModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件
  @Output() confirmModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件

  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
  ) { }

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.currentDept = this.tool.getCurrentUserDept();
    this.getList();
  }

  currentPageData2Change($event: any[]): void {
    this.listOfDisplayData2 = this.data2List; //改变页数，长度也要变
    this.refreshData2Status();
  }

  refreshData2Status(): void {
    this.isAllDisplayData2Checked = this.listOfDisplayData2.length ? this.listOfDisplayData2.every(
      (item) => this.mapOfCheckedData2Id[item.id]
    ) : false;
    this.isIndeterminateData2 = this.listOfDisplayData2.some((item) => this.mapOfCheckedData2Id[item.id]) && !this.isAllDisplayData2Checked;
  }

  // 开通科室选择整行
  selectedListData2(id) {
    this.mapOfCheckedData2Id[id] = !this.mapOfCheckedData2Id[id];
    this.refreshData2Status();
  }

  checkAllData2(value: boolean): void {
    this.listOfDisplayData2.forEach(
      (item) => (this.mapOfCheckedData2Id[item.id] = value)
    );
    this.refreshData2Status();
  }

  // 确认弹窗
  confirmModal() {
    console.log(this[`mapOfCheckedData2Id`]);
    let list = [];
    for (const key in this[`mapOfCheckedData2Id`]) {
      if (this[`mapOfCheckedData2Id`][key]) {
        list.push({id: key});
      }
    }
    if(list.length == 0){
      this.message.warning('请选择数据');
      return;
    }
    this.confirmModelHs.emit(JSON.stringify({ show: false, list, type: 2 }));//emits(向上弹射)事件
  }

  // 关闭弹窗
  hideModal() {
    this.closeModelHs.emit(JSON.stringify({ show: false }));//emits(向上弹射)事件
  }

  getList(){
    this.getInspectCheckItem();
    this.getList2();
  }

  // 获取列表数据
  getList2() {
    let postData = {
      idx: 0,
      sum: 9999,
      dictionary: {
        key: 'inspect_check_type',
        extra5: this.activeInspectCheckItemId,
      }
    }
    this.hsLoading = true;
    this.mainService.getFetchDataList('simple/data','dictionary',postData).subscribe(data => {
      this.hsLoading = false;
      this.data2List = data.list || [];
    })
  }

  // 获取检查项目
  inspectCheckItemList:any[] = [];
  activeInspectCheckItemId;
  getInspectCheckItem() {
    this.mainService.getDictionary("list", "inspect_check_item").subscribe((data) => {
			this.inspectCheckItemList = data || [];
		});
  }

  clickInspectCheckItem(id){
    if(this.activeInspectCheckItemId == id){
      this.activeInspectCheckItemId = undefined;
    }else{
      this.activeInspectCheckItemId = id;
    }
    this.getList2();
  }

}



