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";

class OrderInInfo {
  checked?: boolean;
  kdOrderNo?: string;
  kw?: string;
  userDefined10?: string;
  orderStatus?: string;
}

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

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

  /**
   * 是否全选
   */
  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.location, {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      workareaCode: session.workareaCode,
    });
    const cboPlaceList = result.list.map((_: any) => ({
      label: _.locationCode,
      value: _.locationCode,
    }));
    return { cboPlaceList };
  }

  /**
   * 查询看单号
   * @param hideKdOrderNo
   */
  @MutationAction
  async queryOrderInInfo(hideKdOrderNo: string) {
    const { list }: any = await http.post(url.material.POorderin.info, {
      hideKdOrderNo,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    });
    const orderInInfo = list[0] || {};
    const orderInInfoList = list.filter(
      (_: OrderInInfo) => _.userDefined10 === "1"
    );
    const orderInInfoListEx = list.filter(
      (_: OrderInInfo) => _.userDefined10 === "0"
    );
    return { hideKdOrderNo, orderInInfo, orderInInfoList, orderInInfoListEx };
  }

  /**
   * 提交看单明细
   */
  @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.POorderin.enter, {
      hideKdOrderNo: this.hideKdOrderNo,
      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", {
      loginName: session.loginName,
      warehouseCode: "",
      factoryCode: session.factoryCode,
      locationCode: kw,
    });
    return kw;
  }
}

export default getModule(ReceiptModule);
