import { Injectable } from '@angular/core';
import { MainService } from 'src/app/services/main.service';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class QuiltDepartmentBindService {

  constructor(
    private mainService: MainService,
  ) { }

  // simple增删改
  simplePost(type, model, postData) {
    return this.mainService.simplePost(type, model, postData);
  }

  // 获取被服种类列表
  getClothesTypes(postData) {
    return this.mainService.getFetchDataList("simple/data", "dictionaryTree", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({id: vv.id, name: vv.name}));
      }else{
        v.list = [];
      }
      return v;
    }));
  }

  // 获取科室列表
  getDepts(postData) {
    return this.mainService.getFetchDataList("simple/data", "department", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({id: vv.id, dept: vv.dept}));
      }else{
        v.list = [];
      }
      return v;
    }));
  }

  // 查询
  getFetchDataList(postData) {
    return this.mainService.getFetchDataList("simple/data", "clothesDept", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({
          ...vv,
          buildingListNames: vv.buildingList ? vv.buildingList.map(v => v.buildingName).toString() : '',
          deptListNames: vv.deptList ? vv.deptList.map(v => v.dept).toString() : '',
        }));
      }else{
        v.list = [];
      }
      return v;
    }));;
  }

  // 字典
  getDictionary(type) {
    return this.mainService.getDictionary("list", type);
  }
}


