import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { MainService } from '../../services/main.service';
import { ToolService } from 'src/app/services/tool.service';
@Component({
  selector: 'app-relationTrans-detail-modal',
  templateUrl: './relationTrans-detail-modal.component.html',
  styleUrls: ['./relationTrans-detail-modal.component.less']
})
export class RelationTransDetailModalComponent implements OnInit {
  // 切换科室，切换弹窗
  isAllDisplayDataChecked = false;
  listOfDisplayData: any[] = [];
  mapOfCheckedId: { [key: string]: boolean } = {};
  isIndeterminate = false;
  currentDept; //当前科室

  hosId;
  hsLoading = false;
  dataList: any = [];// 血制品列表
  pageIndex: number = 1;//表格当前页码
  pageSize: number = 100;//表格每页展示条数
  total: number = 0;//表格总数据量
  searchCriteria:any = {
    stype: 0,
    specimenDesc: 0,
  }
  @Input() show: Boolean;
  @Input() deptDisplay: string | number;
  @Input() otherBindConfigDto: any;

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

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

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.currentDept = this.tool.getCurrentUserDept();
    this.tabType = 1;
    this.getOtherTransState();
  }

  currentPageDataChange($event: any[]): void {
    this.listOfDisplayData = this.dataList; //改变页数，长度也要变
    this.refreshStatus();
  }

  refreshStatus(): void {
    this.isAllDisplayDataChecked = this.listOfDisplayData.every(
      (item) => this.mapOfCheckedId[item.id]
    );
    this.isIndeterminate =
      this.listOfDisplayData.some((item) => this.mapOfCheckedId[item.id]) &&
      !this.isAllDisplayDataChecked;
  }

  // 开通科室选择整行
  selectedListData(id) {
    this.mapOfCheckedId[id] = !this.mapOfCheckedId[id];
    this.refreshStatus();
  }

  checkAll(value: boolean): void {
    this.listOfDisplayData.forEach(
      (item) => (this.mapOfCheckedId[item.id] = value)
    );
    this.refreshStatus();
  }

  // 确认弹窗
  confirmModal() {
    console.log(this.mapOfCheckedId);
    let otherList = [];
    for (const key in this.mapOfCheckedId) {
      if (this.mapOfCheckedId[key]) {
        otherList.push({id: key});
      }
    }
    this.confirmModelHs.emit(JSON.stringify({ show: false, otherList }));//emits(向上弹射)事件
  }

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

  // 切换tab栏
  tabType: number = 1; //tab栏
  checkTab(type) {
    this.tabType = type;
  }

  // 切换标本类型下拉框
  selectStype(e){
    console.log(e);
    this.searchCriteria.specimenDesc = 0;
  }

  // 切换标本类型下拉框
  selectDesc(e){
    console.log(e);
  }

  // 分页获取数据
  getList(){
    this.getOther();
  }

  //获取状态
  other_trans_state1; // 未建单
  getOtherTransState() {
    this.mainService.getDictionary("list", "other_trans_state").subscribe((data) => {
      this.other_trans_state1 = data.find(v => v.value == 1);
      if(this.other_trans_state1){
        this.getOther(1);
      }
    });
  }

  // 获取列表数据
  getOther(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      other: {
        taskTypeId: this.otherBindConfigDto.taskTypeId,
        relationTrans: this.otherBindConfigDto.relationTrans,
        hosId: this.hosId,
        state: this.other_trans_state1,
        startDept: this.currentDept.id,
        extra1Time: '1010-01-01 01:01:01',
      }
    }
    this.hsLoading = true;
    this.mainService.getFetchDataList('simple/data','other',postData).subscribe(data => {
      this.hsLoading = false;
      this.dataList = data.list || [];
      this.total = data.totalNum || 0;
    })
  }

}



