import { ref } from 'vue';
import { getWorkLocationList } from '@/api/datamanagement/alert'

type Location = {
  value: number,
  label: string,
  children: [
    {
      value: number,
      label: string
    }
  ]
}

export function useWorkLocation() {
  const locationOptions = ref<Location[]>([]);

  const getLocationOptions = () => {
    getWorkLocationList().then((res) => {
      res.forEach((item) => {
        locationOptions.value.push({
          value: item.workshopId,
          label: item.workshopName,
          children: [
            {
              value: item.workspaceList[0].workspaceId,
              label: item.workspaceList[0].workspaceName
            }
          ]
        })
      })
    })
  };

  // 根据workshopId + workspaceId 指定表格显示 地点 label
  const getNameByWorkid = (workshopId, workspaceId, array) => {
    if (workshopId < 0 || workshopId > array.length) return '-';
    const obj = array[workshopId - 1];

    if (!obj.children || !Array.isArray(obj.children)) return '-';
    if (workspaceId < 0 || workspaceId > obj.children.length) return '-';
    const subObj = obj.children.find(subobj => subobj.value === workspaceId);

    return obj.label + ' - ' + subObj.label;
  }

  return {
    locationOptions,
    getLocationOptions,
    getNameByWorkid
  }
}
