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

class OrderInInfo {
  stoAmount?: 0;
  stoNo?: string; //STO采购单号
  orderAmount?: string; //单据总数
  Allocated?: string; //已分配
  Unallocated?: string; //未分配
}

@Module({
  namespaced: true,
  dynamic: true,
  store,
  name: "product.outbound.stoOutbound",
})
export class ReturningModule extends VuexModule {
  /**
   * 月台列表
   */
  returningTypeList: any[] = [];
  detailedList: any;

  /**
   * 物料列表
   */
  materielList: any[] = [];
  locationCodeList: any[] = [];
  LoctionList:any[] = []

  //条码扫描的返回的结果
  oneMaterielDetail: any = {};
  /**
   * 订单号查询结果
   */
  orderInInfo: OrderInInfo = new OrderInInfo();
  //记账按钮的code码
  code = "";
  /**
   * 委外出库单号查询
   * @param orderNo
   */
  @Action
  async queryOrder(form: any) {
    const result = await http.post(url.material.commission.entrant.queryOrder, {
      ...form,
      loginName: session.loginName,
      factoryCode: session.factoryCode,
    });
    const orderInInfo = result.data.records[0] || {};
    return { orderInInfo };
  }

  /**
   * 库位
   */
  //  @MutationAction
  //  async queryByFactoryCodeAndWorkAreaCode() {
  //    const list: [] = await http.post(url.material.commission.goOut.queryByFactoryCodeAndWorkAreaCode, {
  //      factoryCode: session.factoryCode,
  //      whCode: session.warehouseCode,
  //      loginName: session.loginName,
  //    });
  //    const locationCodeList = list.map((item: any) => ({
  //      label: item.locationCode,
  //      value: item.locationCode,
  //    }));
  //    return { locationCodeList };
  //  }

  /**
   * 提交
   */
  @Action
  async materialComplete(form: any) {
    const res = await http.post(
      url.material.commission.goOut.materialComplete,
      {
        factoryCode: session.factoryCode,
        loginName: session.loginName,
        ...form,
      }
    );
    console.log(res, ">>>>>res");
  }
  /**
   * 获取物料
   *
   * @returns
   * @memberof ReturningModule
   */
  @MutationAction
  async info(poNo: string) {
    const list: [] = await http.post(url.material.commission.entrant.info, {
      factoryCode: session.factoryCode,
      poNo: poNo,
      loginName: session.loginName,
    });
    const locationCodeList = list.map((item: any) => ({
      label: item.locationCode,
      value: item.locationCode,
    }));
    return { locationCodeList };
  }
  /**
   * 查询月台列表
   */
  @MutationAction
  async queryReturningTypeList() {
    const list: [] = await http.post(url.outbound.stoOutbound.yuetaiType, {
      factoryCode: session.factoryCode,
      whCode: session.warehouseCode,
      loginName: session.loginName,
    });
    const returningTypeList = list.map((item: any) => ({
      label: item.dockName,
      value: item.dockCode,
    }));
    return { returningTypeList };
  }

  /**
   * 查询订单号
   * @param orderNo
   */
  @MutationAction
  async queryOrderInInfo(stoNo: string) {
    const result = await http.post(url.outbound.stoOutbound.info, {
      stoNo,
      deleteFlag: 2,
      finalState: 3,
      loginName: session.loginName,
    });
    let stoAmount = 0;
    result.data.records.forEach((item: any) => {
      stoAmount += item.orderAmount;
    });
    const materielList = result.data.records.map((item: any) => ({
      label: item.materialCode,
      value: item.stoItem,
      ...item,
    }));
    console.log("materielList", materielList);
    if (result.data.records[0]) {
      result.data.records[0].stoAmount = stoAmount;
    }
    const orderInInfo = result.data.records[0] || {};
    return { orderInInfo, materielList };
  }

  /**
   * 扫条码
   */
  @Action
  async tluSubmit(list: any[]) {
    const obj = {
      barcode: list[0].barCode,
      loginName: session.loginName,
    };
    let oneMaterielDetail: any = {};
    const { data }: any = await http.post(
      url.outbound.stoOutbound.checkScan,
      obj
    );
    console.log("shneme a ", data.records);
    oneMaterielDetail = data.records[0];
    data.records[0].materialCode = list[0].productCode;
    data.records[0].materialDesc = list[0].productDescZh;
    console.log("aaaaa", oneMaterielDetail);
    return oneMaterielDetail;
  }
  /**
   * 确认按钮
   * @param orderNo
   */
  @MutationAction
  async confirm(list: any[]) {
    const { code, data } = await http.post(
      url.outbound.stoOutbound.Bookkeeping,
      list
    );
    console.log("res数据", data, code);
    // if (code == '1') {
    //   uni.showToast({ icon: 'success', title: msg });
    // } else {
    //   uni.showToast({ icon: 'none', title: msg });
    // }
    const materielList: any = [];
    return { code, materielList };
  }
  @MutationAction
  async queryByFactoryCodeAndWorkAreaCode(content: any) {
    let res:any = await http.post(
      "/wmspda/fg/queryByFactoryCodeAndWorkAreaCode",
      content
    );
    const LoctionList = res
    return {LoctionList};
  }

  @Action
  async queryOutsourcing(poNo: any) {
    let res = await http.post(url.outbound.stoOutbound.queryOrder, {
      poNo,
      loginName: session.loginName,
      factoryCode: session.factoryCode,
    });
    return res;
  }

  @Action
  async submitOutsourcing(upload: any) {
    let res = await http.post(url.outbound.stoOutbound.submit, upload);
    return res;
  }
  //查询明细
  @Action({ commit: "updateCheckedOrderInInfoListKw" })
  async querydetaildlist(content: any) {
    let res: any = await http.post(
      "/wmspda/material/outsourcing/into/info",
      content
    );
    console.log("明细》》》》》》》》》", res.list);
    this.detailedList = res.data;
    return res;
  }
}

export default getModule(ReturningModule);
