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 LocationDetail from './model.location';
class OrderInInfo {
  checked?: boolean;
  dnNo?: string;
  kw?: string;
  userDefined10?: string;
  orderStatus?: string;
  carNo?: string;
  bookingTime?: string;
  supplyCode?: string;
  poType?: string;
  location?: string;
  SubmitCode?: string;
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: 'raw.ingoods.dnReceipt',
})
export class ReceiptModule extends VuexModule {
  sendSpot = '';
  /**
   * sap库存地点列表
   */
  cboPlaceList = [];
  /**
   * 隐藏看单号
   */
  SubmitCode:any = ''
  dnNo = '';
  code = '';
  DNdetailedList: any;
  ImportDNReceivingList: any = [];
  /**
   * 看单号查询结果
   */
  orderInInfo: OrderInInfo = new OrderInInfo();
  /**
   * 看单明细
   */
  orderInInfoList: OrderInInfo[] = [];
  /**
   * 看单明细未通过明细
   */
  orderInInfoListEx: OrderInInfo[] = [];

  /**
   * 库位列表
   */
  returningTypeList: any[] = [];

  /**
   * 是否已查询看单号
   */
  get hasDnInfo() {
    return !!this.orderInInfo.dnNo;
  }

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

  /**
   * 是否有选择项
   */
  get hasChecked() {
    return this.orderInInfoList.filter((_) => _.checked).length > 0;
  }

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

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

  /**
   * 看单明细全选/全不选
   */
  @Mutation
  checkAllOrderInInfoList(checked: boolean) {
    this.orderInInfoList = this.orderInInfoList.map((item) => {
      item.checked = checked;
      return item;
    });
  }

  /**
   * 查询库位列表
   */
  @MutationAction
  async queryLocation() {
    const result: any = await http.post(url.auth.query.location1, {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      userDefined3: 1,
    });
    console.log('result', result);
    const cboPlaceList = result.data.records.map((_: any) => ({
      label: _.sendSpot,
      value: _.sendSpot,
    }));
    return { cboPlaceList };
  }

  /**
   * 查询单号
   * 查询DN单号
   * @param dnNo
   */
  @MutationAction
  async queryOrderInInfo(dnNo: string) {
    const { list, code }: any = await http.post(url.material.DNorderin.info, {
      dnNo,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    });
    const orderInInfo = list[0] || {};
    const orderInInfoList = list.filter((_: OrderInInfo) => _.userDefined10 === '1');
    const orderInInfoListEx = list.filter((_: OrderInInfo) => _.userDefined10 === '0');
    // orderInInfoList.forEach((item: any) => {
    //   item.receiptAmount = item.requestAmount;
    // });
    localStorage.setItem('list', JSON.stringify(list) as any);
    localStorage.setItem('sendSpot', JSON.stringify(list[0].sendSpot) as any);
    return { dnNo, orderInInfo, orderInInfoList, orderInInfoListEx, code };
  }
  //海外DN 根据单号查询物料
  @MutationAction
  async ImportQueryOrderInfo(dnNo: string) {
    const { list, code }: any = await http.post(url.material.DNorderin.ImportInfo, {
      dnNo,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    });
    const orderInInfo = list[0] || {};
    const orderInInfoList = list.filter((_: OrderInInfo) => _.userDefined10 === '1');
    const orderInInfoListEx = list.filter((_: OrderInInfo) => _.userDefined10 === '0');
    // orderInInfoList.forEach((item: any) => {
    //   item.receiptAmount = item.requestAmount;
    // });
    localStorage.setItem('list', JSON.stringify(list) as any);
    localStorage.setItem('sendSpot', JSON.stringify(list[0].sendSpot) as any);
    return { dnNo, orderInInfo, orderInInfoList, orderInInfoListEx, code };
  }
  /**
   * 提交看单明细
   */
  @MutationAction
  async submitOrderInEnter(list: any) {
    // const list = (this.state as any).orderInInfoList.map((item: any) => {
    //   // item.orderStatus = item.checked ? "2" : "1";
    //   return item;
    // });
    const result: any = await http.post(url.material.DNorderin.enter, {
      dnNo: this.dnNo,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      list,
    });
    uni.showToast({
      icon: 'none',
      title: result.msg,
    });
    const SubmitCode = result.code
    const orderInInfoList = result.list.sort((a: any, b: any) => Number(a.accountingStatus) - Number(b.accountingStatus));
    return { orderInInfoList ,SubmitCode};
  }
  //海外DN提交
  @MutationAction
  async submitImportOrder(list: any) {
    // const list = (this.state as any).orderInInfoList.map((item: any) => {
    //   // item.orderStatus = item.checked ? "2" : "1";
    //   return item;
    // });
    const result: any = await http.post(url.material.DNorderin.ImportEnter, {
      dnNo: this.dnNo,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      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', {
      warehouseCode: session.warehouseCode,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      locationCode: kw,
    });
    return kw;
  }
  /**
   * 库位查询
   *
   * @param {*} content
   * @returns
   * @memberof ReceiptModule
   */
  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    const res = await http.post<LocationDetail>('wmspda/fg/queryByFactoryCodeAndWorkAreaCode', content);
    return res;
  }
  //查询DN/海外DN收货明细
  @Action({ commit: 'updateCheckedOrderInInfoListKw' })
  async querydetaildlist(content: any) {
    const res: any = await http.post('/wmspda/material/getDnRecSn', content);
    console.log('明细》》》》》》》》》', res.list);
    this.DNdetailedList = res.list;
    return res;
  }
}

export default getModule(ReceiptModule);
