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 { session } from '@/store/modules/session';
import vm from '@/main';
@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'raw.handover.picking',
})
export class PickingModule extends VuexModule {
  /**
   * 查询列表
   */
  proOrderList: any[] = [];
  detailedList: any;
  mrpCodeRegion: '';
  sapFactoryCode: '';
  formParams: '';
  code = '';
  orderOutIdList: any;
  NEWparams: any;

  /**
   * 查询结果列表（按单列表）
   */
  proOrderResultList: any[] = [];

  /**
   * 查询结果汇总列表
   */
  aggregateList: any[] = [];

  /**
   * 库存地点列表
   */
  locationList = [];

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

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

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

  /**
   * 汇总列表是否全选
   */
  get isAggregateCheckedAll() {
    return !this.aggregateList.filter((_: any) => !_.checked).length;
  }

  /**
   * 汇总列表是否有选择项
   */
  get hasAggregateChecked() {
    return this.checkedAggregateList.length > 0;
  }

  /**
   * 获取选中的汇总列表
   */
  get checkedAggregateList() {
    return this.aggregateList.filter((_: any) => _.checked);
  }

  /**
   * 汇总列表中是否有本次拣配数量
   */
  get hasAggregateCurrentAmount() {
    return this.aggregateList.findIndex((_: any) => _.currentAmount) > -1;
  }

  /**
   * 获取汇总上传列表数据
   */
  get aggregateUploadList() {
    return this.aggregateList.map((item: any) => {
      if (!item.checked) {
        item.currentAmount = 0;
      }
      return item;
    });
  }

  /**
   * 结果列表是否全选
   */
  get isProOrderResultCheckedAll() {
    return !this.proOrderResultList.filter((_: any) => !_.checked).length;
  }

  /**
   * 结果列表是否有选择项
   */
  get hasProOrderResultChecked() {
    return this.checkedProOrderResultList.length > 0;
  }

  /**
   * 获取选中的结果列表
   */
  get checkedProOrderResultList() {
    return this.proOrderResultList.filter((_: any) => _.checked);
  }

  /**
   * 结果列表中是否有本次拣配数量
   */
  get hasProOrderResultCurrentAmount() {
    return this.proOrderResultList.findIndex((_: any) => _.currentAmount) > -1;
  }

  /**
   * 获取按单上传列表数据
   */
  get proOrderResultUploadList() {
    return this.proOrderResultList.map((item: any) => {
      if (!item.checked) {
        item.currentAmount = 0;
      }
      return item;
    });
  }

  /**
   * 清理状态数据
   */
  @Mutation
  clearState() {
    this.proOrderList = [];
    this.proOrderResultList = [];
    this.aggregateList = [];
  }
  /**
   * 查询列表
   */
  @Mutation
  clearProOrderList() {
    this.proOrderList = [];
  }

  /**
   * 全选/取消全选查询列表
   * @param checked
   */
  @Mutation
  checkAllProOrderList(checked: boolean) {
    this.proOrderList = this.proOrderList.map((item: any) => ({
      ...item,
      checked,
    }));
  }

  /**
   * 全选/取消全选汇总列表
   * @param checked
   */
  @Mutation
  checkAllAggregateList(checked: boolean) {
    this.aggregateList = this.aggregateList.map((item: any) => ({
      ...item,
      checked,
    }));
  }

  /**
   * 确认选中汇总列表
   * @param params
   */
  @Mutation
  confirmCheckedAggregateList(params: { locationCode: string; amount: number }) {
    this.aggregateList
      .filter((_: any) => _.checked)
      .forEach((item: any) => {
        if (params.locationCode) {
          item.currentWkposCode = params.locationCode;
        }
        if (params.amount) {
          item.currentAmount = params.amount;
        }
      });
  }

  /**
   * 全选/取消全选按单列表
   * @param checked
   */
  @Mutation
  checkAllProOrderResultList(checked: boolean) {
    this.proOrderResultList = this.proOrderResultList.map((item: any) => ({
      ...item,
      checked,
    }));
  }

  /**
   * 确认选中按单列表
   * @param params
   */
  @Mutation
  confirmCheckedProOrderResultList(params: { amount: number }) {
    this.proOrderResultList
      .filter((_: any) => _.checked)
      .forEach((item: any) => {
        // if (params.locationCode) {
        //   item.currentWkposCode = params.locationCode;
        // }
        if (params.amount) {
          item.currentAmount = params.amount;
        }
      });
  }

  /**
   * 查询看单号
   * @param params
   */
  @MutationAction
  async queryProOrder(params: any) {
    const { list: proOrderList }: any = await http.post(url.sortscan.query.prdorder, params);
    if (!proOrderList.length) {
      // uni.showToast({
      //   icon: 'none',
      //   title: 'No Data Found！',
      // });
      vm.customToast(vm.$t('message.Pi_NoDataFound') as any);
    }
    return { proOrderList };
  }
  @MutationAction
  async queryOrder() {
    const { list: proOrderList }: any = await http.post(url.sortscan.query.prdorder, this.formParams);
    if (!proOrderList.length) {
      // uni.showToast({
      //   icon: 'none',
      //   title: 'No Data Found！',
      // });
      vm.customToast(vm.$t('message.Pi_NoDataFound') as any);
    }
    return { proOrderList };
  }
  /**
   * 查询库存
   * @param params
   */
  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async queryStockList(params: any) {
    const res: any = await http.post('/material/queryOdsRawStorageNews', params);
    return res;
  }

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

  /**
   * 查询结果-汇总列表
   * @param params
   */
  @MutationAction
  async queryAggregateList(params: any) {
    const { list: aggregateList }: any = await http.post(url.sortscan.query.aggregateList, params);
    if (!aggregateList.length) {
      // uni.showToast({
      //   icon: 'none',
      //   title: 'No Data Found！',
      // });
      vm.customToast(vm.$t('message.Pi_NoDataFound') as any);
    }
    aggregateList.forEach((_: any) => (_.loginName = params.loginName));
    return { aggregateList };
  }
  /**
   * 查询库存地点列表 这里后期可能会改成url.auth.locations list相应得改成values 也不用item了 类型也改成string
   */
  @MutationAction
  async queryLocationList() {
    const params = {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    };
    const { values }: any = await http.post(url.auth.locations, params);
    const locationList = values.map((value: string) => ({
      label: value,
      value,
    }));
    return { locationList };
  }

  /**
   * 上传汇总列表
   */
  // @Action({ commit: 'clearState' })
  @MutationAction
  async saveAggregateUpload(params: any) {
    const res: any = await http.post(url.sortscan.save.aggregate, params);
    console.log('res', res);
    const code = res.code;
    return { code };
  }

  /**
   * 上传按单列表
   */
  // @Action({ commit: 'clearState' })
  @MutationAction
  async saveProOrderResultUpload(dataList: any[]) {
    const res: any = await http.post(url.sortscan.save.order, dataList);
    const code = res.code;
    console.log('code', code);
    return { code };
  }

  /**
   * 解锁数据
   * @param list
   */
  @Action
  async unlockProOrderResult(list: any[]) {
    try {
      await http.post(url.sortscan.unlock.list, list, {
        custom: { hideError: true },
      } as any);
    } catch {
      //
    }
  }

  /**
   * 锁定数据
   * @param list
   */
  @Action
  async lockProOrderResult(list: any[]) {
    await http.post(url.sortscan.lock.list, list);
  }

  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    let res = await http.post('/wmspda/fg/queryByFactoryCodeAndWorkAreaCode', content);
    return res;
  }
  //查询拣配明细
  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async querydetaildlist(content: any) {
    let res: any = await http.post('/wmspda/sortscan/getJhListSn', content);
    this.detailedList = res.list;
    console.log(this.detailedList);
    return res;
  }
}

export default getModule(PickingModule);
