/**
 * 超时原因模块
 */
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 { url } from '@/utils/url';

// 超时原因
export interface Reason {
  code: string;
  name: string;
}

@Module({
  dynamic: true,
  store,
  namespaced: true,
  name: 'jobTimeoutReason',
})
class ReasonService extends VuexModule {
  // 超时原因
  reasons: Array<Reason> = [];

  // 获取超时原因
  @Action
  async getReasons({ loginName }: { loginName: User['loginName'] }): Promise<Array<Reason>> {
    const result: any = await http.post(url.appointmentreg.delay.list, {
      loginName,
    });
    if (Array.isArray(result.list)) {
      return result.list;
    } else {
      return [];
    }
  }

  // 设定超时原因列表
  @Mutation
  setDelayReasons(reasons: Array<Reason>) {
    this.reasons = reasons;
  }
}

export const reason = getModule(ReasonService);
