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 httpjson from "@/utils/location";

class OrderInInfo {
  checked?: boolean;
  dnNo?: string;
  kw?: string;
  userDefined10?: string;
  orderStatus?: string;
  carNo?: string;
  bookingTime?: string;
  supplyCode?: string;
  poType?: string;
  location?: string;
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: "raw.ingoods.dnReceipt",
})
export class ReceiptModule extends VuexModule {
  // 是否有
  isState = true;
  sendSpot = "";
  /**
   * sap库存地点列表
   */
  cboPlaceList = [];
  /**
   * 隐藏看单号
   */
  dnNo = "";
  /**
   * 看单号查询结果
   */
  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 }: 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("sendSpot", JSON.stringify(list[0].sendSpot) as any);
    return { dnNo, orderInInfo, orderInInfoList, orderInInfoListEx };
  }

  /**
   * 查询单号
   * 查询盘点单号
   * @param pddNo
   */
  @Action
  async rawMaterialInventoryCheckRaw(pddNo: string) {
    const { msg }: any = await http.post(
      url.material.rawMaterialInventory.checkRaw,
      {
        pddNo,
        factoryCode: session.factoryCode,
        loginName: session.loginName,
      }
    );
    uni.showToast({
      icon: "none",
      title: msg,
    });
  }

  /**
   * 扫描物料号检查
   *
   * @param {string} productCode
   * @param {string} pddNo
   * @memberof ReceiptModule
   */
  @Action
  async checkRawProductCode(form: any) {
    const { code, msg }: any = await http.post(
      url.material.rawMaterialInventory.productCode,
      {
        productCode: form.productCode,
        pddNo: form.pddNo,
        factoryCode: session.factoryCode,
        loginName: session.loginName,
      }
    );
    uni.showToast({
      icon: "none",
      title: msg,
    });
    console.log("code:::", code, msg);
    const isState = !!+code;
    return { isState };
  }
  /**
   * 新增单号、物料信息
   *
   * @param {string} productCode
   * @param {string} pddNo
   * @memberof ReceiptModule
   */
  @Action
  async checkRawCreateInfo(form: any) {
    const { msg }: any = await http.post(
      url.material.rawMaterialInventory.createInfo,
      {
        ...form,
        factoryCode: session.factoryCode,
        loginName: session.loginName,
      }
    );
    uni.showToast({
      icon: "none",
      title: msg,
    });
  }

  /**
   * 物料盘点确定
   *
   * @param {string} productCode
   * @param {string} pddNo
   * @memberof ReceiptModule
   */
  @Action
  async checkRawProductComplete(form: any) {
    const { msg }: any = await http.post(
      url.material.rawMaterialInventory.productComplete,
      {
        ...form,
        factoryCode: session.factoryCode,
        loginName: session.loginName,
      }
    );
    uni.showToast({
      icon: "none",
      title: msg,
    });
  }

  /**
   * 物料盘点完成
   *
   * @param {string} productCode
   * @param {string} pddNo
   * @memberof ReceiptModule
   */
  @Action
  async checkRawOrderComplete(pddNo?: string | null) {
    const { msg }: any = await http.post(
      url.material.rawMaterialInventory.orderComplete,
      {
        pddNo,
        factoryCode: session.factoryCode,
        loginName: session.loginName,
      }
    );
    uni.showToast({
      icon: "none",
      title: msg,
    });
  }

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

export default getModule(ReceiptModule);
