/**
 * 月台模块
 */
import { Module, VuexModule, Mutation, Action, getModule } from 'vuex-module-decorators';
import store from '@/store';
import http from '@/utils/request';
import { User } from '@/store/modules/session';
import { Area } from '@/pages/login/area/model';
import { url } from '@/utils/url';

// 月台信息
export interface Platform {
  sapCode: string;
  code: string;
  name: string;
  sapName?: string;
}

@Module({
  dynamic: true,
  store,
  namespaced: true,
  name: 'platform',
})
class PlatformService extends VuexModule {
  // 月台列表
  private platforms: Array<Platform> = [];

  // 月台列表 getter
  get getPlatforms() {
    return this.platforms;
  }

  // 获取菜单
  @Action({ rawError: true })
  async getPlatformItems({
    loginName,
    warehouseCode,
    factoryCode,
  }: {
    loginName: User['loginName'];
    warehouseCode: Area['warehouseCode'];
    factoryCode: User['factoryCode'];
  }): Promise<Array<Platform>> {
    const result: any = await http.post(url.appointmentreg.platform.list, {
      loginName,
      warehouseCode,
      factoryCode,
    });
    if (Array.isArray(result.list)) {
      return result.list;
    } else {
      return [];
    }
  }

  // 设定月台列表
  @Mutation
  setPlatforms(platforms: Array<Platform>) {
    this.platforms = platforms;
  }
}

export const platform = getModule(PlatformService);
