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

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

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

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

  // 列表-增
  add({title, type, taskTypeIds, deptIds, hosId}) {
    let data = {
      title,
      taskTypeIds: taskTypeIds.toString(),
      deptIds: deptIds.toString(),
      type,
      hosId,
    };
    return this.mainService.simplePost("addData", "receiveOrderRule", data);
  }

  // 列表-改
  update({title, type, taskTypeIds, deptIds, coopData}) {
    let data = {
      title,
      taskTypeIds: taskTypeIds.toString(),
      deptIds: deptIds.toString(),
      type,
    };
    data = {...coopData, ...data};
    return this.mainService.simplePost("addData", "receiveOrderRule", data);
  }

  // 查询任务类型
  queryTasktype({hosId, keywords}){
    let postData = {
      taskType: {
        simpleQuery: true,
        hosId: {id: hosId},
        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;
    }));
  }

  // 查询科室
  queryDept({hosId, keywords}){
    let postData = {
      department: {
        simpleQuery: true,
        hospital: {id: hosId},
        dept: keywords,
      },
      idx: 0,
      sum: 9999,
    };
    return this.mainService.getFetchDataList("data", "department", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({id: vv.id, name: vv.dept}));
      }else{
        v.list = [];
      }
      return v;
    }));
  }

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

  // 打印二维码
  print(ids) {
    return this.mainService.coopWorkerOrder("generateReceiveCodes", ids)
  }
}


