import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { MainService } from 'src/app/services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-form-management-detail',
  templateUrl: './form-management-detail.component.html',
  styleUrls: ['./form-management-detail.component.less']
})
export class FormManagementDetailComponent implements OnInit {

  constructor(
    private router: Router,
    private mainService: MainService,
    private route: ActivatedRoute,
    private tool: ToolService,
    private msg: NzMessageService,
  ) { }

  hosId; //院区
  data = [];

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

  // 返回列表
  toList(){
    this.router.navigateByUrl('/main/formManagement');
  }

  // 获取列表
  buildings:any[] = [];
  getList(){
    let maskFlag = this.msg.loading("加载中", { nzDuration: 0 }).messageId;
    this.mainService.listMsgByMain('reserveView', {
      reserveFormId: +this.route.snapshot.paramMap.get('id'),
      taskTypeId: +this.route.snapshot.paramMap.get('taskType'),
    }).subscribe((result: any) => {
      this.msg.remove(maskFlag);
      console.log(result);
      const data = result.data || {};
      console.log(data);
      if(result.state == 200){
        if(Object.keys(data).length > 0){
          // 楼栋-楼层-科室-工单 转成前端可用的 楼栋-科室-工单
          let buildings = Object.keys(data)
          .map(v => ({
            id: v,
            name: Object.values(Object.values(data[v])[0])[0][0][3],
            departments: (
              Object.values(data[v]).map(vv => Object.keys(vv).map(vvv => ({id: vvv, name: vv[vvv][0][6], orders: vv[vvv], checked: false, isDisabled: vv[vvv].every(vvvv => !(vvvv[12] == 2 || (!vvvv[8] && vvvv[12] != 2 && vvvv[12] != 6 && vvvv[12] != 7 && vvvv[12] != 11)))}))) as any
            ).flat()
          }));
          console.log(buildings)
          this.buildings = buildings;
        }else{
          this.buildings = [];
        }
      }else{
        this.buildings = [];
      }
    })
  }

  // 批量分派
  batchDispatch() {
    console.log(this.buildings);
    let arr = [];
    this.buildings.forEach(v => {
      v.departments.forEach(vv => {
        if(vv.checked){
          arr = arr.concat(vv.orders.filter(vvv => vvv[12] == 2 || (!vvv[8] && vvv[12] != 2 && vvv[12] != 6 && vvv[12] != 7 && vvv[12] != 11)));
        }
      })
    })
    console.log(arr);
    if(arr.length === 0){
      this.msg.info('请勾选科室！');
      return;
    }
    let ids_types = [];
    let stateIds = [];
    arr.forEach((item) => {
      if (item[12] == 2) {
        //普通派单
        ids_types.push(`${item[9]}_assign`);
      } else if (!item[8] && item[12] != 2 && item[12] != 6 && item[12] != 7 && item[12] != 11) {
        ids_types.push(`${item[9]}_reassign`);
      }
      stateIds.push(item[10]);
    });
    this.router.navigateByUrl(
      `/main/formManagementDetail/${this.route.snapshot.paramMap.get('id')}/${this.route.snapshot.paramMap.get('taskType')}/allotWorker/${ids_types.join("-")}/${stateIds.join("-")}/2/${this.hosId}`
    );
  }

}
