import request from '@/utils/request';
import { BaseResponse } from './type';

export interface ResourceListItem {
  id?: number;
  resourceType?: number;
  locationType?: number;
  name?: string; //名称
  address?: string; //地址
  longitude?: string; // 经度
  latitude?: string; //纬度
  manageUnit?: string; //管理单位
  carType?: string;
  num?: number; // 数量
  carryGoods?: null; //携带物资（应急时）
  contactName?: string; //联系人
  contactPhone?: string; //联系电话
  updateTime?: string;
}

export interface GetResourceListParams {
  name?: string;
  manageUnit?: string; // 管理单位
  carType?: null;
  resourceType: 1 | 2 | 3; // 资源类型。1-仓库。2-队伍.3-车辆
}

export interface ResourceListResponse extends BaseResponse {
  rows?: ResourceListItem[];
  total?: number;
  pageSize?: number;
  pageNum?: number;
}

export const getResourceList = (params: GetResourceListParams) =>
  request<ResourceListResponse>('GET', {
    url: '/zhdd/resource/list',
    params: params,
  });

export const deleteResourceItem = (id: number) =>
  request<BaseResponse>('DELETE', {
    url: `/zhdd/resource/${id}`,
  });

export interface ResourceItemDetail extends ResourceListItem {
  resourceDetailList?: {
    createBy?: string;
    createTime?: string;
    delFlag?: boolean;
    id?: number;
    model?: string;
    name?: string;
    num?: number;
    resourceId?: number;
    size?: string;
    unit?: string;
    availableNum?:string;
  }[];
}
export interface ResourceItemDetailResponse extends BaseResponse {
  data: ResourceItemDetail;
}

export const getResourceItem = (id: number) =>
  request<ResourceItemDetailResponse>('GET', {
    url: `/zhdd/resource/${id}`,
  });

export const saveResourceItem = (data: ResourceItemDetail) =>
  request<BaseResponse>('POST', {
    url: `/zhdd/resource`,
    data,
  });

export const putResourceItem = (data: ResourceItemDetail) =>
  request<BaseResponse>('PUT', {
    url: `/zhdd/resource`,
    data,
  });

export interface ResourcesItem {
  id?: string;
  resourceType?: number;
  name?: string;
  address?: string;
  longitude?: string;
  latitude?: string;
  manageUnit?: string;
  carType?: null;
  num?: number;
  carryGoods?: null;
  contactName?: string;
  contactPhone?: string;
  resourceDetailList?: null;
}

export interface AllResourcesResponse {
  data: {
    应急队伍: ResourcesItem[];
    应急车队: ResourcesItem[];
    应急仓库: ResourcesItem[];
  };
}

export interface SingleDeviceItem {
  userId?: string;
  userName?: string;
  userType?: string;
  deviceCode?: string;
  gpsX?: string;
  gpsY?: string;
  height?: string;
  angle?: string;
  speed?: string;
  startCount?: number;
  dataSource?: number;
  time?: string;
  extendInfo?: null;
  deviceExtendInfo?: null;
  channelId?: string;
  isOnline?: boolean;
}

export interface AllResourcesResponse {
  data: {
    应急队伍: ResourcesItem[];
    应急车队: ResourcesItem[];
    应急仓库: ResourcesItem[];
  };
}

export interface SingleDeviceResponse {
  data: {
    [key: string]: SingleDeviceItem;
  };
}

/** 获取所有应急资源信息 */
export const getAllResources = () => {
  return request<AllResourcesResponse>('GET', {
    url: `/zhdd/resource/location`,
  });
};

export const getAllsingleDevice = () => {
  return request<SingleDeviceResponse>('GET', {
    url: `/zhdd/singleDevice/list`,
  });
};

export const getAllvideoDevice = () => {
  return request<SingleDeviceResponse>('GET', {
    url: `zhdd/dh/deviceList`,
  });
};

export interface WarehouseResponse extends BaseResponse {
  data: ResourcesItem[];
}

/** 通过物资查询应急仓库 */
export const searchWarehouseByGoods = (name?: string) => {
  return request<WarehouseResponse>('GET', {
    url: `/zhdd/resourceDetail/searchWarehouseByGoods?name=${name}`,
  });
};
