import { Action, getModule, Module, Mutation, VuexModule, MutationAction } from 'vuex-module-decorators';
import { Area } from '@/pages/login/area/model';
//import { Lang } from '@/i18n';
import request from '@/utils/request';
import { url } from '@/utils/url';
//import { stringify } from 'query-string';
import store from '@/store';
import { page } from '@/utils/page';
//import { server } from '@/pages/login/server/model';
export interface User {
  access_token?: string;
  userId?: string;
  loginName?: string;
  userName?: string;
  password?: string;
  poolNameList?: Array<Area>;
  createBy?: string;
  menuList?: Array<Menu>;
  nickName?: string;
  roles?: Array<Menu>;
}
export interface FactoryCode {
  factoryCode?: string;
  poolName?: string;
}
export interface Menu {
  menuName?: string;
  path?: string;
  perms?: string;
}

@Module({
  dynamic: true,
  store,
  namespaced: true,
  name: 'session',
})
class SessionService extends VuexModule {
  /**
   * 当前登录用户
   */
  user: User | null = null;
  factory: FactoryCode | null = null;
  factoryCode: string;
  //factoryCode: factoryCode | null = null;
  Version = '';
  url = '';
  returnCode: any = '';

  /**
   * 当前工作区
   */
  //area: Area | null = null;

  get isUserLoggedIn(): boolean {
    return this.user !== null;
  }
  /**
   * 当前工作区
   */
  get password(): any {
    if (this.user === null) {
      return null;
    } else {
      return this.user.password;
    }
  }
  /**
   * 当前用户ID
   */
  get userId(): any {
    if (this.user === null) {
      return null;
    } else {
      return this.user.userId;
    }
  }
  get userName(): any {
    if (this.user === null) {
      return null;
    } else {
      return this.user.userName;
    }
  }
  get nickName(): any {
    if (this.user === null) {
      return null;
    } else {
      return this.user.nickName;
    }
  }
  get roles(): any {
    if (this.user === null) {
      return null;
    } else {
      return this.user.roles;
    }
  }
  /**
   * 当前选择语言
   */
  // get lang(): any {
  //   if (this.user === null) {
  //     return null;
  //   } else {
  //     return this.user.lang;
  //   }
  // }
  /**
   * 当前登录名
   */
  get loginName(): any {
    if (this.user === null) {
      return null;
    } else {
      return this.user.loginName;
    }
  }

  get FactoryCode(): any {
    if (this.factory === null) {
      return null;
    } else {
      return this.factory.factoryCode;
    }
  }
  get PoolName(): any {
    if (this.factory === null) {
      return null;
    } else {
      return this.factory.poolName;
    }
  }

  /**
   * 当前工厂代码
   */
  // get factoryCode(): any {
  //   if (this.user === null) {
  //     return null;
  //   } else {
  //     return this.factoryCode.factoryCode;
  //   }
  // }
  /**
   * 当前区域代码
   */
  // get regionCode(): string | null {
  //   if (this.area === null) {
  //     return null;
  //   } else {
  //     return this.area.regionCode;
  //   }
  // }
  /**
   * 当前工作区代码
   */
  // get workareaCode(): string | null {
  //   if (this.area === null) {
  //     return null;
  //   } else {
  //     return this.area.workareaCode;
  //   }
  // }

  /**
   * 当前仓库代码
   */
  // get warehouseCode(): string | null {
  //   if (this.area === null) {
  //     return null;
  //   } else {
  //     return this.area.warehouseCode;
  //   }
  // }

  // 用户 getter
  get getUser(): User {
    if (this.user === null) {
      throw new Error('当前无登录用户');
    } else {
      console.log(this.user);
      return this.user;
    }
  }
  get getFactory(): FactoryCode {
    if (this.factory === null) {
      throw new Error('当前无选择工厂');
    } else {
      return this.factory;
    }
  }
  //获取菜单
  get getMenus(): any {
    if (this.user === null) {
      throw new Error('当前无登录用户');
    } else {
      return this.user.menuList;
    }
  }

  // 登录
  @Action({ rawError: true })
  async login({ username, password }: { username: string; password: string }) {
    const requestData = {
      username: username,
      password: password,
    };
    return request.post(url.wmspda.system.login, requestData);
  }
  // 获取用户信息
  @Action({ rawError: true })
  async getInfo({ username }: { username: string }) {
    const requestData = {
      userName: username,
    };
    console.log(request.post(url.wmspda.system.getInfo, requestData), this.user);
    return request.post(url.wmspda.system.getInfo, requestData);
  }
  // get parkName(): any {
  //   if (this.requestData === null) {
  //     return null;
  //   } else {
  //     return this.requestData.parkName;
  //   }
  // }
  // 登出
  @Mutation
  logout(): void {
    // logout
    this.user = null;
    uni.removeStorageSync('userinfo');
    //this.area = null;
    uni.reLaunch({ url: page.login.login });
  }

  // 保存用户信息
  @Mutation
  setUser(user: User): void {
    console.log(user);
    this.user = user;
  }
  @Mutation
  setFactory(factory: FactoryCode): void {
    this.factory = factory;
  }

  // 保存工作区信息
  // @Mutation
  // setArea(area: Area): void {
  //   this.area = area;
  // }
  //查询版本号
  @MutationAction
  async QueryVersion(serverAddress: string) {
    //const a = server.serverAddress;
    //const a = ServeUrl.server.address;
    console.log('serverAddress', serverAddress);
    const res: any = await request.get(serverAddress + '/sgNginxDownload/Debug/setup.json');
    const Version = res.version;
    const url = res.url + '/' + res.file;
    console.log('url..........', url);
    return { Version, url };
  }
  //前置检测
  @MutationAction
  async Predetection(params: any) {
    const ServeUrl = JSON.parse(uni.getStorageSync('__GWMS_APP_STATE_DATA__'));
    await uni.request({
      url: ServeUrl.server.address + params.url,
      method: 'POST',
      data: params,
      success: (res: any) => {
        this.returnCode = res.data.code;
        console.log('returnCode', this.returnCode);
      },
    });
    return {};
  }
  //直接下载
  // @MutationAction
  // async xiazai() {
  //   let url: any = await request.get(this.url);
  //   return { url };
  // }
}

export const session = getModule(SessionService);
