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 { stringify } from 'query-string';

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'product.outbound.internal',
})
export class OutboundInternalModule extends VuexModule {
  /**
   * 订单类型列表
   */
  orderTypeList: any[] = [
    { label: '运单', value: 'YD' },
    { label: '倒短单', value: 'JHD' },
    { label: '车次号', value: 'PCD' },
  ];
  /**
   * 单据列表
   */
  odsProOrderOutList: any[] = [];
  /**
   * 物料信息
   */
  productInfos: any[] = [];
  /**
   * 第一条物料信息
   */
  productInfo: any = {};
  /**
   * 拣货任务
   */
  pickTaskList: any[] = [];
  /**
   * 单据总数
   */
  orderQty = null;
  /**
   * 已分配
   */
  ygcj = null;
  /**
   * 已扫描
   */
  yicj = null;

  /**
   * 提单
   */
  @MutationAction
  async outStorageLoadProduct(params: any) {
    const data = stringify({
      ...params,
      user: '1999', // TODO user.loginName
      sign: '',
      whCode: '1999C1', // TODO warehouseCode
      regionCode: '1999C11', // TODO regionCode
      areaCode: '19990009', // TODO workareaCode
      factoryCode: '1999', // TODO factoryCode
      jhtype: 'PTCK',
      orderSourceSys: 'NX',
      isCkJhRwDetail: '1',
      isSmartHouse: '2', // TODO warehouseType
    });
    const result: any = await http.post(url.wmspda.fg.outStorageLoadProduct, data);
    if (!result.productInfos) {
      uni.showToast({ icon: 'none', title: '提单服务错误:返回物料信息为空！' });
      return;
    }
    const productInfos = result.productInfos;
    if (!result.pickTaskList) {
      uni.showToast({ icon: 'none', title: '当前单号在当前库区已无可操作拣货任务！' });
      const { orderQty, ygcj, yicj, productInfos = [] } = result;
      return { orderQty, ygcj, yicj, productInfo: productInfos[0] };
    }
    const pickTaskList = result.pickTaskList;
    return { productInfos, pickTaskList };
  }

  /**
   * 按车次加载单据列表
   * @param params
   */
  @Action
  async outStorageLoadDocumentByCarBatchno(params: any) {
    const data = stringify({
      ...params,
      factoryCode: '1999', // TODO factoryCode
      whCode: '1999C1', // TODO warehouseCode
      user: '1999', // TODO user.loginName
    });
    const result: any = await http.post(url.wmspda.fg.outStorageLoadDocumentByCarBatchno, data);
    console.log(result);
  }

  /**
   * 记账
   * @param params
   */
  @Action
  async orderPost(params: any) {
    const data = stringify({
      ...params,
      user: '1999', // TODO user.loginName
      sign: '',
      whCode: '1999C1', // TODO warehouseCode
      regionCode: '1999C11', // TODO regionCode
      areaCode: '19990009', // TODO workareaCode
      factoryCode: '1999', // TODO factoryCode
      jhtype: 'PTCK',
      orderSourceSys: 'NX',
      isCkJhRwDetail: '1',
      isSmartHouse: '2', // TODO warehouseType
    });
    const result: any = await http.post(url.wmspda.fg.orderType, data);
    console.log(result);
  }
}

export default getModule(OutboundInternalModule);
