import { Action, getModule, Module, 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';

class OrderInInfo {
  stoAmount?: 0;
  stoNo?: string; //STO采购单号
  orderAmount?: string; //单据总数
  Allocated?: string; //已分配
  Unallocated?: string; //未分配
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'product.outbound.stoOutbound',
})
export class ReturningModule extends VuexModule {
  /**
   * 月台列表
   */
  returningTypeList: any[] = [];
  /**
   * 物料列表
   */
  materielList: any[] = [];

  //条码扫描的返回的结果
  oneMaterielDetail: any = {};
  /**
   * 订单号查询结果
   */
  orderInInfo: OrderInInfo = new OrderInInfo();
  //记账按钮的code码
  code = '';
  DNcode: any = '';
  /**
   * 查询月台列表
   */
  @MutationAction
  async queryReturningTypeList() {
    const list: [] = await http.post(url.outbound.stoOutbound.yuetaiType, {
      factoryCode: session.factoryCode,
      whCode: session.warehouseCode,
      loginName: session.loginName,
    });
    const returningTypeList = list.map((item: any) => ({
      label: item.dockName,
      value: item.dockCode,
    }));
    return { returningTypeList };
  }

  /**
   * 查询订单号
   * @param orderNo
   */
  @MutationAction
  async queryOrderInInfo(nxOutCode: string) {
    const result = await http.post(url.outbound.stoOutbound.info, {
      nxOutCode,
      deleteFlag: 2,
      finalState: 3,
      loginName: session.loginName,
    });
    let stoAmount = 0;
    result.data.records.forEach((item: any) => {
      stoAmount += item.orderAmount;
    });
    const materielList = result.data.records.map((item: any) => ({
      label: item.materialCode,
      value: item.stoItem,
      ...item,
    }));
    console.log('materielList', materielList);
    if (result.data.records[0]) {
      result.data.records[0].stoAmount = stoAmount;
    }
    const orderInInfo = result.data.records[0] || {};
    return { orderInInfo, materielList };
  }
  /**
   * 查询订单号
   * @param nxOutCode
   */
  @MutationAction
  async findProdInfoByDN(nxOutCode: string) {
    const result: any = await http.post(url.outbound.stoOutbound.findProdInfoByDN, {
      nxOutCode,
      loginName: session.loginName,
      factoryCode: session.factoryCode,
    });
    console.log('resule>>>>>>>>>>>>>>>>>>>>>>>', result);
    let stoAmount = 0;
    const DNcode = result.code;
    result.data.forEach((item: any) => {
      stoAmount += item.orderAmount;
    });
    const materielList = result.data.map((item: any) => ({
      value: item.rowItem,
      label: item.rowItem + '(' + item.materialCode + ')',
      ...item,
    }));
    console.log('materielList', materielList);
    if (result.data[0]) {
      result.data[0].stoAmount = stoAmount;
    }
    const orderInInfo = result.data[0] || {};
    return { orderInInfo, materielList, DNcode };
  }

  /**
   * 扫条码
   */
  @Action
  async tluSubmit(list: any[]) {
    const obj = {
      barcode: list[0].barCode,
      loginName: session.loginName,
    };
    let oneMaterielDetail: any = {};
    const { data }: any = await http.post(url.outbound.stoOutbound.checkScan, obj);
    console.log('shneme a ', data.records);
    oneMaterielDetail = data.records[0];
    data.records[0].materialCode = list[0].productCode;
    data.records[0].materialDesc = list[0].productDescZh;
    console.log('aaaaa', oneMaterielDetail);
    return oneMaterielDetail;
  }
  /**
   * 扫条码
   */
  @Action
  async getMaterialByCode(list: any = {}) {
    let oneMaterielDetail: any = {};
    const { data }: any = await http.post(url.outbound.stoOutbound.getMaterialByCode, list);
    oneMaterielDetail = data.records[0];
    return oneMaterielDetail;
  }
  /**
   * 确认按钮
   * @param orderNo
   */
  @MutationAction
  async confirm(list: any[]) {
    const { code, data } = await http.post(url.outbound.stoOutbound.Bookkeeping, list);
    console.log('res数据', data, code);
    // if (code == '1') {
    //   uni.showToast({ icon: 'success', title: msg });
    // } else {
    //   uni.showToast({ icon: 'none', title: msg });
    // }
    const materielList: any = [];
    return { code, materielList };
  }
  /**
   * 确认按钮
   * @param orderNo
   */
  @MutationAction
  async confirmMove(list: any) {
    const { code, data } = await http.post(url.outbound.stoOutbound.Bookkeeping, list);
    console.log('res数据', data, code);
    // if (code == '1') {
    //   uni.showToast({ icon: 'success', title: msg });
    // } else {
    //   uni.showToast({ icon: 'none', title: msg });
    // }
    const materielList: any = [];
    return { code, materielList };
  }
  /**
   * 查询详细
   * @param orderNo
   */
  @MutationAction
  async queryDetailed(order1: string) {
    const { code, data } = await http.post(url.outbound.stoOutbound.getDetailByorderNo, {
      order1,
      loginName: session.loginName,
      factoryCode: session.factoryCode,
    });
    console.log('res数据', data, code);
    const materielList = data;
    console.log('>>>>>>>', materielList);
    return { code, materielList };
  }

  //退扫接口
  @MutationAction
  async BackSweep(params: any) {
    const res: any = await http.post(url.outbound.stoOutbound.scanBarcodeDN, params);
    console.log('res>>>>>>>>>>>>>>>>>>>>>>>>.', res);
    const code = res.code;
    return { code };
  }
  //退扫查询接口
  @MutationAction
  async queryAmount(params: any) {
    const res: any = await http.post(url.outbound.stoOutbound.queryAmount, params);
    console.log('退扫>>>>>>>>>>>>>>>>>>>>>>>>.', res);
    return {};
  }
}

export default getModule(ReturningModule);
