import { getModule, Module, Mutation, Action, MutationAction, VuexModule } from 'vuex-module-decorators';
import store from '@/store';
import http from '@/utils/request';
import { url } from '@/utils/url';
import { cloneDeep } from 'lodash/fp';
import vm from '@/main';
@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'raw.handover.aggregating',
})
export class AggregatingModule extends VuexModule {
  /**
   * 查询列表
   */
  proOrderList = [];
  param: any;
  orderOutIdListParams: any;

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

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

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

  /**
   * 查询结果辅料列表
   */
  accessoryList: 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);
  }

  /**
   * 清空查询结果列表
   */
  @Mutation
  clearProOrderResultList() {
    this.proOrderResultList = [];
  }
  /**
   * 查询列表
   */
  @Mutation
  clearProOrderList() {
    this.proOrderList = [];
  }
  /**
   * 汇总列表是否全选
   */
  get isAggregateCheckedAll() {
    return !this.aggregateList.filter((_: any) => !_.checked).length;
  }

  /**
   * 全选/取消全选汇总列表
   * @param checked
   */
  @Mutation
  checkAllAggregateList(checked: boolean) {
    this.aggregateList = this.aggregateList.map((item: any) => {
      item.checked = checked;
      if (item.checked) {
        item.hvAmount = item.totalMoAmount - item.totalHvAmount;
      } else {
        item.hvAmount = 0;
      }
      return item;
    });
  }

  /**
   * 列表提交-汇总交接
   * @param params
   */
  @Action
  async uploadAggregateList(params: any) {
    return http.post(url.sumscan.u.hzlist, params);
  }

  /**
   * 设置汇总列表项本次交接数量
   * @param params
   */
  @Mutation
  setAggregateListItemHvAmount({ index, hvAmount }: { index: number; hvAmount: number }) {
    const original = this.aggregateList[index];
    const newItem = { ...original, hvAmount };
    this.aggregateList[index].hvAmount = hvAmount;
    // this.aggregateList.splice(index, 1, newItem);
    // if (this.aggregateList[index].checked) {
    //   this.aggregateList[index].hvAmount = this.aggregateList[index].totalMoAmount - this.aggregateList[index].totalHvAmount;
    // } else {
    //   this.aggregateList[index].hvAmount = 0;
    // }
  }

  /**
   * 设置按单列表项本次交接数量
   * @param params
   */
  @Mutation
  setOrderListItemHvAmount({ index, hvAmount }: { index: number; hvAmount: number }) {
    const original = this.orderList[index];
    const newItem = { ...original, hvAmount };
    // this.orderList.splice(index, 1, newItem);
    this.orderList[index].hvAmount = hvAmount;
  }
  // setOrderListItemHvAmount() {
  //   this.orderList
  //     .filter((_: any) => _.checked)
  //     .forEach((item: any) => {
  //       item.hvAmount = item.totalMoAmount - item.totalHvAmount;
  //       // if (params.hvAmount) {
  //       //   item.currentAmount = params.hvAmount;
  //       // }
  //     });
  // }

  /**
   * 设置辅料列表项本次交接数量
   * @param params
   */
  @Mutation
  setAccessoryListItemHvAmount({ index, hvAmount }: { index: number; hvAmount: number }) {
    const original = this.accessoryList[index];
    const newItem = { ...original, hvAmount };
    this.accessoryList.splice(index, 1, newItem);
  }

  /**
   * 按单列表是否全选
   */
  get isOrderCheckedAll() {
    return !this.orderList.filter((_: any) => !_.checked).length;
  }
  /**
   * 全选/取消全选查询列表
   * @param checked
   */
  @Mutation
  checkAllProOrderList(checked: boolean) {
    this.proOrderList = this.proOrderList.map((item: any) => ({
      ...item,
      checked,
    }));
  }
  /**
   * 全选/取消按单列表
   * @param checked
   */
  @Mutation
  checkAllOrderList(checked: boolean) {
    this.orderList = this.orderList.map((item: any) => {
      item.checked = checked;
      if (item.checked) {
        item.hvAmount = item.totalMoAmount - item.totalHvAmount;
      } else {
        item.hvAmount = 0;
      }
      return item;
    });
  }

  /**
   * 查询看单号
   * @param params
   */
  @MutationAction
  async queryProOrder(params: any) {
    const { list: proOrderList }: any = await http.post(url.sumscan.query.orderno, params);
    //const proOrderList = values.map((v: string) => ({ prdOrder, amount: v, materialCode: v, materialDesc: v }));
    //const proOrderList = values;
    //console.log(">>>>>?????proOrderList",proOrderList);
    proOrderList.forEach((_: any) => (_.checked = true));
    if (!proOrderList.length) {
      // uni.showToast({
      //   icon: 'none',
      //   title: 'No Data Found！',
      // });
      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.sumscan.query.ordoutlist, params);
    return { proOrderResultList };
  }

  /**
   * 查询结果-汇总列表
   * @param params
   */
  @MutationAction
  async queryAggregateList(params: any) {
    const { list: aggregateList }: any = await http.post(url.sumscan.query.hzlist, params);
    if (!aggregateList.length) {
      // uni.showToast({
      //   icon: 'none',
      //   title: 'No Data Found！',
      // });
      vm.customToast(vm.$t('message.Pi_NoDataFound') as any);
      return { aggregateList };
    }
    return { aggregateList };
  }

  /**
   * 查询结果-按单列表
   * @param params
   */
  @MutationAction
  async queryOrderList(params: any) {
    const { queryParams, proOrderResultList } = params;
    await http.post(url.sumscan.lock.list, queryParams);
    const orderList = cloneDeep<any>(proOrderResultList);
    console.log('orderList', orderList);
    if (orderList.length == 0) {
      return { orderList };
    }
    return { orderList };
  }
  /**
   * 查询结果-辅料列表
   * @param params
   */
  @MutationAction
  async queryAccessoryList(params: any) {
    const { list: accessoryList }: any = await http.post(url.sumscan.query.fllist, params);
    if (!accessoryList.length) {
      // uni.showToast({
      //   icon: 'none',
      //   title: 'No Data Found！',
      // });
      vm.customToast(vm.$t('message.Pi_NoDataFound') as any);
    }
    return { accessoryList };
  }

  /**
   * 列表提交-按单交接
   * @param params
   */
  @Action
  async uploadOrderList(params: any) {
    await http.post(url.sumscan.u.order, params);
    uni.showToast({
      icon: 'success',
      title: 'success',
    });
  }

  /**
   * 列表提交-辅料交接
   * @param params
   */
  @Action
  async uploadAccessoryList(params: any) {
    return http.post(url.sumscan.u.fllist, params);
  }

  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    let res = await http.post('/wmspda/fg/queryByFactoryCodeAndWorkAreaCode', content);
    return res;
  }
}

export default getModule(AggregatingModule);
