import {
  Action,
  getModule,
  Module,
  Mutation,
  MutationAction,
  VuexModule,
} from 'vuex-module-decorators';
import store from '@/store';
import http from '@/utils/request';
import { url } from '@/utils/url';
import vm from '@/main';
export interface SummaryItem {
  checked: any;
  materialCode: string;
  amount: number;
  totalMoAmount: number;
  totalHvAmount: number;
  materialDesc: string;
  unit: string;
  wkposCode: string;
  hvAmount: number;
  prdMaterialDesc: string;
  prdMaterialCode: string;
  sapFactoryCode: string;
  prdOrder: string;
  sendSpot: string;
  oriSendSpot: string;
  mrpCode: string;
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'raw.handover.virtual',
})
export class VirtualModule extends VuexModule {
  /**
   * sap 工厂代码
   */
  sapFactoryCode = '';

  /**
   * 查询列表
   */
  proOrderList = [];

  /**
   * 查询结果列表
   */
  proOrderResultList = [];

  /**
   * 汇总列表
   */
  summaryList: SummaryItem[] = [];

  /**
   * 是否全选
   */
  get isCheckedAll() {
    return !this.proOrderList.filter((_: any) => !_.checked).length;
  }

  /**
   * 是否有选择项
   */
  get hasChecked() {
    return this.checkedProOrderList.length > 0;
  }

  /**
   * 获取选中的查询列表
   */
  get checkedProOrderList() {
    return this.proOrderList.filter((_: any) => _.checked);
  }

  /**
   * 设置 sap 工厂代码
   */
  @Mutation
  setSapFactoryCode(params: { sapFactoryCode: string }) {
    this.sapFactoryCode = params.sapFactoryCode;
  }

  /**
   * 清空查询结果列表
   */
  @Mutation
  clearProOrderResultList() {
    this.proOrderResultList = [];
  }
  /**
   * 查询列表
   */
  @Mutation
  clearProOrderList() {
    this.proOrderList = [];
  }
  /**
   * 更新汇总列表项
   */
  @Mutation
  updateSummaryItem({ index, newItem }: { index: number; newItem: SummaryItem }) {
    this.summaryList.splice(index, 1, newItem);
  }

  /**
   * 查询看单号
   * @param params
   */
  @MutationAction
  async queryProOrder(params: any) {
    const { list: proOrderList }: any = await http.post(url.virtual.query.orderno, params);
    //const proOrderList = list.map((v: any) => ({ prdOrder: v.prdOrder, amount: v.amount, materialCode: v.materialCode, materialDesc: v.materialDesc }));
    proOrderList.forEach((_: any) => (_.checked = true));
    if (!proOrderList.length) {
      vm.customToast(vm.$t('message.Pi_NoDataFound') as any);
    }
    return { proOrderList };
  }

  /**
   * 查询结果列表
   * @param params
   */
  @MutationAction
  async queryProOrderResult(params: any) {
    const { list: proOrderResultList }: any = await http.post(url.virtual.query.ordoutlist, params);
    return { proOrderResultList };
  }

  /**
   * 汇总-查询列表
   * @param params
   */
  @MutationAction
  async querySummaryList(params: any) {
    const { list: summaryList }: any = await http.post(url.virtual.query.hzlist, params);
    return { summaryList };
  }

  /**
   * 汇总-检查数量
   */
  @Action
  async checkAmount(params: {
    sapFactoryCode: string;
    loginName: string;
    materialCode: string;
    unit: string;
    mrpCode: string;
    factoryCode: string;
    sendSpot: string;
    amount: string;
  }) {
    return await http.post(url.virtual.check.amount, params);
  }

  /**
   * 汇总-上传结果
   */
  @Action
  async uploadSummaryList(params: {
    sapFactoryCode: string;
    operatorPass: string;
    factoryCode: string;
    loginName: string;
    dataList: any[];
    includeOrderOutIdList: string[];
  }) {
    return await http.post(url.virtual.u.hzlist, params);
  }
}

export default getModule(VirtualModule);
