import {
  Action,
  getModule,
  Module,
  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";

class OrderInInfo {
  amount?: any;
  sapFactoryCode?: any;
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: "raw.handover.returning",
})
export class ReturningModule extends VuexModule {
  /**
   * 退料类型列表
   */
  // returningTypeList: any[] = [];
  /**
   * 物料列表
   */
  materielList: any[] = [];
  /**
   * 订单号查询结果
   */
  orderInInfo: OrderInInfo = new OrderInInfo();

  /**
   * 查询退料类型列表
   */
  // @MutationAction
  // async queryReturningTypeList() {
  //   const { list }: any = await http.post(url.tl.get.type, {
  //     factoryCode: session.factoryCode,
  //     loginName: session.loginName,
  //   });
  //   const returningTypeList = list.map((item: any) => ({
  //     label: item.name,
  //     value: item.code,
  //   }));
  //   return { returningTypeList };
  // }

  /**
   * 查询订单号
   * @param orderNo
   */
  @MutationAction
  async queryOrderInInfo(prdOrderFrom: string) {
    this.materielList = [];
    const { data }: any = await http.post(url.material.prdOrder.info, {
      prdOrderFrom,
      page: "1",
      rows: "50",
      loginName: session.loginName,
    });
    console.log("qqqqqqqqq", data);
    const materielList = data.map((item: any) => ({
      label: item.materialCode,
      value: item.materialCode,
      amount: item.amount,
      wkposCode: item.wkposCode,
      sendSpot: item.sendSpot,
    }));
    console.log("物料列表", data);
    console.log("物料名列表", materielList);
    const orderInInfo = data[0] || {};
    return { orderInInfo, materielList };
  }
  //根据时间查询单号列表
  @MutationAction
  async queryTimeInInfo(time: any) {
    this.materielList = [];
    const { data }: any = await http.post(url.material.prdOrder.info, {
      requireDateMin: time.requireDateMin,
      requireDateMax: time.requireDateMax,
      page: "1",
      rows: "50",
      loginName: session.loginName,
    });
    console.log("qqqqqqqqq", data);
    const materielList = data.map((item: any) => ({
      label: item.prdOrder,
      value: item.materialCode,
      amount: item.amount,
      wkposCode: item.wkposCode,
      sendSpot: item.sendSpot,
    }));
    //materielList 包含页面table想要展示的字段：sendSpot：接收人；value：物料号；label：订单号
    console.log("时间查询物料列表", data);
    console.log("时间查询物料名列表", materielList);
    const orderInInfo = data[0] || {};
    return { orderInInfo, materielList };
  }
  /**
   * 获取默认的工厂
   */
  @Action
  async queryDefaultFactoryCode() {
    const values: any = await http.post(url.auth.factory.sap, {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
    });
    return values[0];
  }

  /**
   * 退料上传
   */
  @Action
  async tluSubmit(params: any) {
    await auth.checkPassword({
      factoryCode: session.factoryCode as string,
      loginName: session.loginName as string,
      rfPwd: params.rfPwd,
    });
    await http.post(url.tl.u, {
      operatorPass: params.operator,
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      unit: "",
      ...params,
    });
    uni.showToast({
      icon: "success",
      title: "success",
    });
  }
  @Action({ commit: "updateCheckedOrderInInfoListKw" })
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    let res = await http.post(
      "/wmspda/fg/queryByFactoryCodeAndWorkAreaCode",
      content
    );
    return res;
  }
}

export default getModule(ReturningModule);
