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;
  poNo?: string;
  kw?: string;
  userDefined10?: string;
  orderStatus?: string;
  supplyCode?: string;
  poType?: string;
  location?: string;
}

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

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

  /**
   * 是否全选
   */
  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 };
  }
  /**
   * 查询看单号
   * @param poNo
   */
  @MutationAction
  async queryOrderInInfo(poNo: string) {
    const { list, code }: any = await http.post(url.material.POorderin.info, {
      poNo,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    });
    const code1 = code;
    console.log("code1", code1);
    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;
      // item.receiptAmount = parseFloat(item.receiptAmount).toFixed(1);
    });
    localStorage.setItem("list", JSON.stringify(list) as any);
    localStorage.setItem("sendSpot", JSON.stringify(list[0].sendSpot) as any);
    return { poNo, orderInInfo, orderInInfoList, orderInInfoListEx, code1 };
  }

  /**
   * 提交看单明细
   */
  @MutationAction
  async submitOrderInEnter(list: any) {
    // 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, {
      poNo: this.poNo,
      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", {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      warehouseCode: session.warehouseCode,
      locationCode: kw,
    });
    return kw;
  }
  @Action({ commit: "updateCheckedOrderInInfoListKw" })
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    let res = await http.post(
      "/wmspda/fg/queryByFactoryCodeAndWorkAreaCode",
      content
    );
    return res;
  }
}

export default getModule(ReceiptModule);
