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

@Injectable({
  providedIn: 'root'
})
export class OtherAutoService {
  constructor(
    private mainService: MainService,
  ) { }

  // 列表-查
  query({pageIndex, pageSize, hosId}) {
    let data = {
      idx: pageIndex - 1,
      sum: pageSize,
      otherCreateOrderRule: {
        hosId,
      },
    };
    return this.mainService.getFetchDataList("simple/data", "otherCreateOrderRule", data);
  }

  // 列表-删
  delete({id}) {
    return this.mainService.simplePost("rmvData", "otherCreateOrderRule", [id]);
  }

  // 列表-增
  add({title, classesId, taskTypeIds, startCreated, autoCreate, banDispatch, hosId}) {
    let data = {
      title,
      classesId,
      taskTypeIds: taskTypeIds.toString(),
      startCreated,
      autoCreate,
      banDispatch,
      hosId,
    };

    return this.mainService.simplePost("addData", "otherCreateOrderRule", data);
  }

  // 列表-改
  update({title, classesId, taskTypeIds, startCreated, autoCreate, banDispatch, coopData}) {
    let data = {
      title,
      classesId,
      taskTypeIds: taskTypeIds.toString(),
      startCreated,
      autoCreate,
      banDispatch,
    };
    data = {...coopData, ...data};
    return this.mainService.simplePost("addData", "otherCreateOrderRule", data);
  }

  // 查询任务类型-其他临床服务
  queryOtherTasktype({hosId, keywords}){
    let postData = {
      taskType: {
        simpleQuery: true,
        hosId: {id: hosId},
        associationType: {
          key: 'association_types',
          value : 'other',
        },
        taskName: keywords,
      },
      idx: 0,
      sum: 9999,
    };
    return this.mainService.getFetchDataList("configuration", "taskType", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({id: vv.id, name: vv.taskName}));
      }else{
        v.list = [];
      }
      return v;
    }));
  }

  // 查询班次
  querySchedule({hosId, keywords}){
    let postData = {
      scheduleClass: {
        hospital: {
          id: hosId
        },
        name: keywords,
      },
      idx: 0,
      sum: 9999,
    };
    return this.mainService.getFetchDataList("configuration", "scheduleClass", 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;
    }));
  }
}

