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

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

  constructor(private mainService: MainService) { }

  //获取任务类型
  getTaskType({hosId}) {
    let postData = {
      idx: 0,
      sum: 10,
      taskType: {
        simpleQuery: true,
        hosId: {
          id: hosId
        },
        associationType: {
          key:"association_types",
          value: 'drugsBag'
        }
      }
    };
    return this.mainService.getFetchDataList("simple/data", "taskType", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({id: vv.id, associationType: vv.associationType}));
      }else{
        v.list = [];
      }
      return v;
    }));
  }

  // 获取配置
  getConfig({hosId, tasktype}) {
    let postData = {
      idx: 0,
      sum: 10,
      taskTypeConfig: {
        taskTypeDTO: {
          hosId: {
            id: hosId
          },
          associationType: tasktype.associationType
        }
      }
    };
    return this.mainService.getFetchDataList("simple/data", "taskTypeConfig", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        let firstList = v.list[0];
        v.list = firstList ? firstList : {};
        if(firstList){
          v.list = {id: firstList.id, drugsBatchInfo: firstList.drugsBatchInfo};
        }else{
          v.list = {};
        }
      }else{
        v.list = {};
      }
      return v;
    }));
  }

  // 保存
  submitForm({configs, tasktype, hosId, drugsBatchInfo}) {
    let postData:any = {
      id: configs.id,
      taskType: tasktype.id,
      hosId: hosId,
      drugsBatchInfo: drugsBatchInfo[0].checked ? 1 : 0,
    };
    return this.mainService.simplePost("addData", "taskTypeConfig", postData);
  }
}
