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 { auth } from "@/store/modules/auth";
import { session } from "@/store/modules/session";

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: "raw.handover.feeding",
})
export class FeedingModule extends VuexModule {
  /**
   * 补料明细列表
   */
  blDetailList: any[] = [];
  detailedList: any;

  /**
   * 库位列表
   */
  locationList = [];

  /**
   * 补料明细列表是否有选择项
   */
  get hasBlDetailChecked() {
    return this.checkedBlDetailList.length > 0;
  }

  /**
   * 获取选中的补料明细列表
   */
  get checkedBlDetailList() {
    return this.blDetailList.filter((_: any) => _.checked);
  }

  /**
   * 补料明细列表中是否有本次补料数量
   */
  get hasBlDetailFillAmount() {
    return this.blDetailList.findIndex((_: any) => _.fillAmount) > -1;
  }

  /**
   * 获取补料明细上传列表数据
   */
  get blDetailUploadList() {
    return this.blDetailList.map((item: any) => {
      if (!item.checked) {
        item.fillAmount = 0;
      }
      return item;
    });
  }

  /**
   * 清理状态数据
   */
  @Mutation
  clearState() {
    this.blDetailList = [];
  }

  /**
   * 确认选中补料明细列表
   * @param params
   */
  @Mutation
  confirmCheckedBlDetailList(params: { locationCode: string; amount: number }) {
    this.blDetailList
      .filter((_: any) => _.checked)
      .forEach((item: any) => {
        if (params.locationCode) {
          item.wkposCodeP = params.locationCode;
        }
        if (params.amount) {
          item.fillAmount = params.amount;
        }
      });
  }

  /**
   * 上传补料明细列表
   */
  @Action({ commit: "clearState" })
  async saveBlDetailUpload(params: any) {
    await auth.checkPassword({
      factoryCode: session.factoryCode as string,
      loginName: session.loginName as string,
      rfPwd: params.rfPwd,
    });
    await http.post(url.bl.u, {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      ...params,
    });
    uni.showToast({
      icon: "success",
      title: "success",
    });
  }

  /**
   * 解锁补料明细列表数据
   */
  @Action
  async unlockBlDetailList() {
    try {
      const sapSupplyIdList = this.blDetailList.map((_) => _.sapSupplyId);
      const params = {
        factoryCode: session.factoryCode,
        loginName: session.loginName,
        sapSupplyIdList,
      };
      await http.post(url.bl.unlock, params, {
        custom: { hideError: true },
      } as any);
    } catch {
      //
    }
  }

  /**
   * 查询补料明细列表
   */
  @MutationAction
  async queryBlDetailList(params: any) {
    const { list: blDetailList } = await http.post(url.bl.query, {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      ...params,
    });
    return { blDetailList };
  }
  //查询明细
  @Action({ commit: "updateCheckedOrderInInfoListKw" })
  async querydetaildlist(content: any) {
    let res: any = await http.post("/wmspda/tl/getBlListSn", content);
    console.log("明细》》》》》》》》》", res.list);
    this.detailedList = res.list;
    return res;
  }
  /**
   * 查询库位列表
   */
  @MutationAction
  async queryLocationList() {
    const result: any = await http.post(url.auth.query.location, {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      workareaCode: session.workareaCode,
    });
    const locationList = result.list.map((_: any) => ({
      label: _.locationCode,
      value: _.locationCode,
    }));
    return { locationList };
  }

  @Action({ commit: "updateCheckedOrderInInfoListKw" })
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    let res = await http.post(
      "/wmspda/fg/queryByFactoryCodeAndWorkAreaCode",
      content
    );
    return res;
  }
}

export default getModule(FeedingModule);
