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';
// import { page } from '@/utils/page';

class OrderInInfo {
  productDescZh?: string;
  qty?: string;
  custCode?: string;
  custCodeName?: string;
  productCode?: string;
  locCode?: string;
  barcode?: string;
  // actualQty?: string;
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'product.warehouse.wholeTransfer',
})
export class wholeTransfer extends VuexModule {
  /**
   * 库位地点列表
   */
  WlList = [];
  userDefined: any = '';
  detailedList: any;
  code = '';
  /**
   * 物料型号类型
   */
  // modelType = [
  //   { label: '内销', value: '0' },
  //   { label: '外销', value: '1' },
  //   { label: '样机', value: '2' },
  //   { label: '半成品', value: '4' },
  // ];
  /**
   * 隐藏看单号
   */
  dnNo = '';
  /**
   * 看单号查询结果
   */
  orderInInfo: OrderInInfo = new OrderInInfo();

  // /**
  //  * 更新看单明细库位
  //  * @param kw 库位
  //  */
  // @Mutation
  // updateOrderInInfoListKw(kw: string) {
  //   this.orderInInfoList = this.orderInInfoList.map((item) => {
  //     item.kw = kw;
  //     return item;
  //   });
  // }

  /**
   * 查询库位列表
   */
  @MutationAction
  async queryLocation() {
    const result: any = await http.get(url.warehouse.wholeTransfer.list, {
      params: {
        factoryCode: session.factoryCode,
        loginName: session.loginName,
      },
    });
    console.log('库位数据', result);
    const WlList = result.data.map((_: any) => ({
      label: _.locationCode,
      value: _.locationCode,
    }));
    return { WlList };
  }

  /**
   * 查询单号
   * 查询DN单号
   * @param dnNo
   */
  @MutationAction
  async getProductCode(barcode: string) {
    const res: any = await http.post(url.warehouse.wholeTransfer.content, {
      barcode,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    });
    const code = res.code;
    const orderInInfo = res.data.records[0];
    if (orderInInfo.custCode == '0') {
      orderInInfo.custCodeName = vm.$t('message.Warehouse_NX') as any;
    } else if (orderInInfo.custCode == '1') {
      orderInInfo.custCodeName = vm.$t('message.Warehouse_WX') as any;
    } else if (orderInInfo.custCode == '2') {
      orderInInfo.custCodeName = vm.$t('message.Warehouse_YJ') as any;
    } else if (orderInInfo.custCode == '4') {
      orderInInfo.custCodeName = vm.$t('message.Warehouse_BCP') as any;
    }
    return { orderInInfo, code };
  }
  //清除数据
  @MutationAction
  async empty() {
    const orderInInfo: any = [];
    // const userDefined = '';
    const WlList = [];
    const code = '';
    return { orderInInfo, WlList, code };
  }
  @MutationAction
  async onTakeoutConfirm(list: any) {
    const records: any = await http.post(url.warehouse.rowTransfer.commit, list);
    const code = records.code;
    return { code };
  }
  //生成移库码
  @MutationAction
  async getCpMoveBatchNo(loginName: any) {
    let res: any = await http.post(url.warehouse.rowTransfer.getCode, loginName);
    let userDefined: any = res.data;
    return { userDefined };
  }
  //查询明细
  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async querydetaildlist(content: any) {
    let res: any = await http.post('/wmspda/fg/queryListByMoveCode', content);
    console.log('明细》》》》》》》》》', res.list);
    this.detailedList = res.data;
    return res;
  }
  // /**
  //  * 提交看单明细
  //  */
  // @MutationAction
  // async submitOrderInEnter() {
  //   const list = (this.state as any).orderInInfoList.map((item: any) => {
  //     item.orderStatus = item.checked ? '2' : '1';
  //     item.requestAmount = item.checked ? item.y : '0';
  //     return item;
  //   });
  //   const result: any = await http.post(url.material.DNorderin.enter, {
  //     dnNo: this.dnNo,
  //     factoryCode: '1999',
  //     loginName: '1999',
  //     list,
  //   });
  //   uni.showToast({
  //     icon: 'none',
  //     title: result.msg,
  //   });
  //   const orderInInfoList = result.list.sort(
  //     (a: any, b: any) => Number(a.accountingStatus) - Number(b.accountingStatus),
  //   );
  //   return { orderInInfoList };
  // }

  /**
   * 修改库位
   */
  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async changeOrderInLocation(kw: string) {
    await http.post('/wmspda/material/orderin/enter', {
      loginName: session.loginName,
      // loginName: '1999',
      warehouseCode: '',
      factoryCode: session.factoryCode,
      // factoryCode: '1999',
      locationCode: kw,
    });
    return kw;
  }
}

export default getModule(wholeTransfer);
