import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { MainService } from '../../services/main.service';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';
import { ToolService } from '../../services/tool.service';

@Component({
  selector: 'app-replication-scheme',
  templateUrl: './replication-scheme.component.html',
  styleUrls: ['./replication-scheme.component.less']
})
export class ReplicationSchemeComponent implements OnInit {
  @ViewChild('osComponentRef1', { read: OverlayScrollbarsComponent, static: false })
  osComponentRef1: OverlayScrollbarsComponent;
  @ViewChild('osComponentRef2', { read: OverlayScrollbarsComponent, static: false })
  osComponentRef2: OverlayScrollbarsComponent;
  @ViewChild('osComponentRef3', { read: OverlayScrollbarsComponent, static: false })
  osComponentRef3: OverlayScrollbarsComponent;
  @ViewChild('osComponentRef4', { read: OverlayScrollbarsComponent, static: false })
  osComponentRef4: OverlayScrollbarsComponent;
  @Input() show: Boolean;
  @Input() currentTaskType: any;//当前任务类型，班次，绑定方式的数据

  @Output() closeModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件
  @Output() reloadModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件
  loading1 = false;//工作分配方案列表loading
  schemeNameList = [];//工作分配方案列表
  schemeNameId;//工作分配方案id(选中的)
  schemeName;//工作分配方案名称(选中的)
  loading2 = false;//任务类型列表loading
  typeList = [];//任务类型列表
  typeNameId;//任务类型id(选中的)
  typeName;//任务类型名称(选中的)
  classesList = [];//任务类型列表
  classesNameId;//任务类型id(选中的)
  classesName;//任务类型名称(选中的)
  currentHospital;//当前院区

  targetResult = [];//复制到的方案集合
  targetResultFlag = false;//必选
  isLoading = false;
  constructor(private tool: ToolService, private mainService: MainService) { }

  ngOnInit() {
    this.currentHospital = this.tool.getCurrentHospital();
    this.getSchemeNameList();
  }
  // 获取工作分配方案列表
  getSchemeNameList() {
    this.loading1 = true;
    let data = {
      idx: 0,
      sum: 999,
      workScheme: {
        "hosId": this.currentHospital.id
      }
    }
    this.mainService.getFetchDataList('configuration', 'workScheme', data).subscribe(data => {
      this.loading1 = false;
      if (data.status == 200) {
        this.schemeNameList = data.list;
      }
    })
  }
  // 分配方案选中
  schemeNameClick(o) {
    console.log(o)
    this.classesList = [];
    this.typeNameId = null;
    this.schemeNameId = o.id;
    this.schemeName = o.name;
    this.getTypeList(o.id);
  }
  // 获取任务类型
  getTypeList(id) {
    this.loading2 = true;
    let data = {
      idx: 0,
      sum: 999,
      taskType: {
        schemeId: id,
        hosIds: this.currentHospital.id + ''
      }
    }
    this.mainService.getFetchDataList('configuration', 'taskType', data).subscribe(data => {
      this.loading2 = false;
      this.typeList = data.list;
      if (this.currentTaskType['bindId'] == 4) {//科室绑定分组，需要过滤,只能选择相同班次
        this.typeList = this.typeList.filter(item => {//筛选有该班次的任务类型
          let arr = [];
          item.classesIds.forEach(item1 => {
            arr.push(item1.id);
          })
          return arr.includes(this.currentTaskType['classesId']);
        });
        this.typeList.forEach(item => {//筛选有该班次的任务类型
          item.classesIds = item.classesIds.filter(item1 => item1.id == this.currentTaskType['classesId']);
        });
        //过滤当前任务类型
        if (this.schemeNameId == this.currentTaskType['queryParamsId']) {
          this.typeList = this.typeList.filter(item => item.id != this.currentTaskType['taskTypeId']);
        }
      } else if (this.currentTaskType['bindId'] == 3 && this.schemeNameId == this.currentTaskType['queryParamsId']) {//科室绑定人员，去除当前班次
        this.typeList.forEach(item => {
          if (item.id == this.currentTaskType['taskTypeId']) {
            let index = item.classesIds.findIndex(item1 => item1.id == this.currentTaskType['classesId']);
            item.classesIds.splice(index, 1);
          }
        })
      }
    })
  }
  // 任务类型选中
  typeNameClick(o) {
    console.log(o)
    this.classesNameId = null;
    this.typeNameId = o.id;
    this.typeName = o.taskName;
    this.getClassesList(o.id);
  }
  // 获取班次
  getClassesList(id) {
    let obj = this.typeList.find(item => item.id == id);
    this.classesList = obj.classesIds;
    this.classesList.forEach(item => {
      item.flag = false;
    });
    this.classesList.forEach(item => {
      this.targetResult.forEach(item1 => {
        if (this.schemeNameId == item1.schemeId && item.id == item1.classId && item1.schemeId == this.schemeNameId && item1.taskTypeId == this.typeNameId) {
          item.flag = true;
        }
      });
    });
  }
  // 班次选中
  classesNameClick(o) {
    console.log(o);
    let obj = this.targetResult.find(item => item.schemeId == this.schemeNameId && item.classId == o.id && item.schemeId == this.schemeNameId && item.taskTypeId == this.typeNameId);
    if (obj) {
      return;
    }
    this.classesList.find(item => item.id == o.id).flag = true;//选中项高亮
    this.classesNameId = o.id;
    this.classesName = o.name;
    this.targetResult.push({
      "schemeId": this.schemeNameId,
      "taskTypeId": this.typeNameId,
      "classId": this.classesNameId,
      "schemeName": this.schemeName,//x
      "typeName": this.typeName,//x
      "classesName": this.classesName,//x
      "bindingType": this.currentTaskType['bindId'] == 3 ? 1 : 2
    });
    this.targetResultFlag = false;
    console.log(this.targetResult);
  }
  // 删除复制到的方案
  deleteThis(o) {
    console.log(o);
    let objs = this.targetResult.filter(item => !(item.schemeId == o.schemeId && item.classId == o.classId && item.schemeId == o.schemeId && item.taskTypeId == o.taskTypeId));
    this.targetResult = objs;
    this.getClassesList(this.typeNameId);
  }
  // 关闭弹窗
  hideModal() {
    this.targetResultFlag = false;
    this.targetResult = [];
    this.schemeNameId = null;//工作分配方案id(选中的)
    this.typeList = [];//任务类型列表
    this.typeNameId = null;//任务类型id(选中的)
    this.classesList = [];//任务类型列表
    this.classesNameId = null;//任务类型id(选中的)
    this.closeModelHs.emit(JSON.stringify({ show: false }));//emits(向上弹射)事件
  }
  // 确定
  ok() {
    if (this.targetResult.length === 0) {
      this.targetResultFlag = true;
      return;
    }
    let target = this.targetResult.map(item => {
      return {
        "schemeId": item.schemeId,
        "taskTypeId": item.taskTypeId,
        "classId": item.classId,
        "bindingType": item.bindingType
      }
    });
    let dataObj = {
      "source": {
        "schemeId": this.currentTaskType['queryParamsId'],
        "taskTypeId": this.currentTaskType['taskTypeId'],
        "classId": this.currentTaskType['classesId'],
        "bindingType": this.currentTaskType['bindId'] == 3 ? 1 : 2
      },
      "target": target
    }
    console.log(dataObj);
    this.isLoading = true;
    this.mainService.coopConfig('allocatePropertyCopy', dataObj).subscribe(data => {
      this.isLoading = false;
      this.hideModal();
      if (data.status == 200) {
        this.showPromptModal('复制', true, '')
        this.reloadModelHs.emit(JSON.stringify({ show: true }));//emits(向上弹射)事件
      } else {
        this.showPromptModal('复制', false, data.msg)
      }
    })
  }

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


