import {
  mockIp,
  mockReqId
} from '../../utils/mock';
import {
  getGoodsFreight
} from '~/services/order/getGoodsFreight';

export const transformGoodsDataToConfirmData = (goodsDataList) => {
  const list = [];

  goodsDataList.forEach((goodsData) => {
    list.push({
      storeId: goodsData.storeId,
      storeName: goodsData?.storeName,
      spuId: goodsData.spuId,
      skuId: goodsData.skuId,
      goodsName: goodsData.title,
      image: goodsData.thumb,
      reminderStock: 119,
      quantity: goodsData.quantity,
      payPrice: goodsData.price,
      totalSkuPrice: goodsData.price,
      discountSettlePrice: goodsData.price,
      realSettlePrice: goodsData.price,
      settlePrice: goodsData.price,
      oriPrice: goodsData.originPrice,
      tagPrice: null,
      tagText: null,
      skuSpecLst: goodsData.specInfo,
      promotionIds: null,
      weight: 0.0,
      unit: 'KG',
      volume: null,
      masterGoodsType: 0,
      viceGoodsType: 0,
      roomId: goodsData.roomId,
      egoodsName: null,
    });
  });

  return list;
};

/** 生成结算数据 */
export async function genSettleDetail(params) {
  const {
    userAddressReq,
    couponList,
    storeInfoList,
    goodsRequestList,
    totalDeliveryFee
  } = params;

  console.log(totalDeliveryFee, '参数是什么');

  const resp = {
    data: {
      settleType: 0,
      userAddress: null,
      totalGoodsCount: 0,
      packageCount: 0,
      totalAmount: '289997',
      totalPayAmount: '',
      totalDiscountAmount: '110000',
      totalPromotionAmount: '0',
      totalCouponAmount: '0',
      totalSalePrice: '289997',
      totalGoodsAmount: '289997',
      totalDeliveryFee: totalDeliveryFee,
      invoiceRequest: null,
      skuImages: null,
      deliveryFeeList: null,
      storeGoodsList: [
        // {
        //   storeId: '1000',
        //   storeName: '云Mall深圳旗舰店',
        //   remark: null,
        //   goodsCount: 1,
        //   deliveryFee: '0',
        //   deliveryWords: null,
        //   storeTotalAmount: '0',
        //   storeTotalPayAmount: '179997',
        //   storeTotalDiscountAmount: '110000',
        //   storeTotalCouponAmount: '0',
        //   skuDetailVos: [],
        //   couponList: [{
        //     couponId: 11,
        //     storeId: '1000',
        //   }, ],
        // },
      ],
      inValidGoodsList: null,
      outOfStockGoodsList: null,
      limitGoodsList: null,
      abnormalDeliveryGoodsList: null,
      invoiceSupport: 1,
    },
    code: 'Success',
    msg: null,
    requestId: mockReqId(),
    clientIp: mockIp(),
    rt: 244,
    success: true,
  };

  // 判断是否携带优惠券数据
  const discountPrice = [];
  console.log(couponList, '计算一下');
  if (couponList && couponList.length > 0) {
    couponList.forEach((coupon) => {
      if (coupon.status === 'default') {
        discountPrice.push({
          shopGuid: coupon.shopGuid,
          type: coupon.type,
          value: coupon.value,
        });
      }
    });
  }

  const storeGoodsList = []
  // 将商品塞进对应的店铺
  storeInfoList.forEach((store) => {
    const storeGoods = {
      storeId: "",
      storeName: "",
      goodsCount: 0,
      deliveryFee: 0,
      storeTotalPayAmount: 0,
      storeTotalAmount: 0,
      storeTotalDiscountAmount: 0,
      storeTotalCouponAmount: 0,
      couponList: [{

      }, ],
    }
    const storeGoodsRequestList = []
    storeGoods.storeId = store.storeId
    storeGoods.storeName = store.storeName

    goodsRequestList.forEach(item => {
      if (item.storeId === store.storeId) {
        storeGoods.goodsCount += item.quantity
        storeGoodsRequestList.push(item)

        // 计算店铺运费
        if (userAddressReq) {
          let data = {
            GoodsRequestList: storeGoodsRequestList,
            CustomerAddressGuid: userAddressReq.customerAddressGuid
          }
          getGoodsFreight(data).then((res) => {
            if (res.code === 0) {
              resp.data.abnormalDeliveryGoodsList = storeGoodsRequestList
            }
            storeGoods.deliveryFee = res.data
          })
        }

        // 计算优惠券价格
        let storeTotalCouponAmount = 0
        if (discountPrice && discountPrice.length > 0) {
          discountPrice.forEach(e => {
            if (e.shopGuid === item.storeId) {
              if (e.type === 1) {
                storeTotalCouponAmount = e.value;
              }
              if (e.type === 2) {
                storeTotalCouponAmount = (Number(storeGoods.storeTotalAmount) * e.value) / 10;
              }
            }
          });
        }
        storeGoods.storeTotalCouponAmount = storeTotalCouponAmount;
        storeGoods.storeTotalAmount += item.quantity * item.price;
        storeGoods.storeTotalPayAmount += item.quantity * item.price + storeGoods.deliveryFee - storeTotalCouponAmount;

      }
    });

    const list = transformGoodsDataToConfirmData(storeGoodsRequestList);
    storeGoods.skuDetailVos = list
    storeGoodsList.push(storeGoods)
  })
  console.log(storeGoodsList, '看看成果');

  const list = transformGoodsDataToConfirmData(goodsRequestList);

  // 获取购物车传递的商品数据
  // resp.data.storeGoodsList[0].skuDetailVos = list;
  resp.data.storeGoodsList = storeGoodsList;


  // 模拟计算场景

  // 计算总价
  const totalPrice = list.reduce((pre, cur) => {
    return pre + cur.quantity * Number(cur.settlePrice);
  }, 0);

  // 计算折扣
  const totalDiscountPrice =
    discountPrice.length > 0 ?
    discountPrice.reduce((pre, cur) => {
      if (cur.type === 1) {
        return pre + cur.value;
      }
      if (cur.type === 2) {
        return pre + (Number(totalPrice) * cur.value) / 10;
      }

      return pre + cur;
    }, 0) :
    0;

  resp.data.totalSalePrice = totalPrice;

  resp.data.totalCouponAmount = totalDiscountPrice;

  resp.data.totalPayAmount =
    totalPrice - totalDiscountPrice - Number(resp.data.totalPromotionAmount);

  if (totalDeliveryFee) {
    resp.data.totalPayAmount += resp.data.totalDeliveryFee
  }

  list.forEach(item => {
    resp.data.totalGoodsCount += item.quantity
  });

  if (userAddressReq) {
    resp.data.settleType = 1;
    resp.data.userAddress = userAddressReq;
  }
  return resp;
}