<template>
  <view class='content'>

    <view class='card-view'>
      <template v-for='(item,index) in orderBean?.orderGoods' :key='index'>
        <image class='goods-image' :src='item?.images' />
        <view class='c-flex-column' style='flex: 1'>
          <text class='goods-name'>{{ item?.goodsName }}</text>
          <text class='goods-sku'>{{ item?.stockStock?.colorName }} {{ item?.stockStock?.sizeName }}</text>
        </view>

        <view class='c-flex-column'>
          <text class='goods-num'>x{{ item?.goodsNum }}</text>
          <view class='c-flex-row'>
            <text class='goods-price' style='margin-right: 10rpx;text-decoration: line-through'>¥{{ item?.originPrice }}
            </text>
            <text class='goods-price'>¥{{ item?.consumePrice }}</text>
          </view>
        </view>
      </template>
    </view>

    <view class='card-view'>
      <text style='flex: 1'>实付</text>
      <text style='color: #F32B2B'>¥{{ orderBean?.totalPrice || 0 }}</text>
    </view>

    <view class='bottom-view c-flex-row'>
      <view class='confirm-button' @click.stop='payment'>支付</view>
    </view>
  </view>

</template>

<script lang='ts' setup>
import { pay, progress } from '@/api/groupbuy';
import { OrderBean } from '@/api/groupbuy/types';
import { parseParameter, sortASCII } from '@/utils';
import { hexMD5 } from '@/utils/common/md5';
import { useUserStore } from '@/store';

const userState = useUserStore();
const { terminalInfo } = storeToRefs(userState);

const orderBean = ref<OrderBean>();

onLoad((e: any) => {
  orderBean.value = JSON.parse(decodeURIComponent(e?.orderBean));
});

const payment = () => {
  let signParams = {
    return_url: 'return_url',
    total_amount: orderBean.value?.totalPrice,
    client_sn: orderBean.value?.id,
    terminal_sn: terminalInfo.value.terminalSn,
    subject: 'subject',
    subject_img: 'subject_img',
    merchant_name: 'merchant_name',
    notify_url: 'https://www.baidu.com'
  };
  // signParams = util.sortASCII(signParams, true);
  sortASCII(signParams, true);
  //参数拼接
  // const signStr = util.pars(signParams) + '&key=' + terminalInfo.value.terminalKey;
  const signStr = parseParameter(signParams) + '&key=' + terminalInfo.value.terminalKey;
  console.log('签名字符串', signStr);
  // const sign = utilMd5.hexMD5(signStr).toUpperCase();
  const sign = hexMD5(signStr);

  console.log('签名结果', sign);

  const params = {
    'id': orderBean.value?.id,
    'orderSn': signParams.client_sn,
    'terminal_key': terminalInfo.value.terminalKey,
    'terminal_sn': terminalInfo.value.terminalSn
  };

  progress(params);
  pay({
    'orderId': orderBean.value?.id,
    'result': JSON.stringify('{payResult:xxx}')
  });
};

</script>

<style lang='scss' scoped>

.content {
  .card-view:nth-of-type(1) {
    margin-top: 30rpx;
  }
}

.card-view {
  display: flex;
  flex-direction: row;
  margin: 10rpx 30rpx;
  background: #FFFFFF;
  font-size: 35rpx;
  padding: 30rpx;

  .goods-image {
    width: 130rpx;
    height: 130rpx;
    margin-right: 15rpx;
    border-radius: 8rpx;
  }

  .goods-name {
    color: #333333;
  }

  .goods-sku {
    font-size: 30rpx;
    color: #666666;
  }

  .c-flex-column {
    justify-content: space-between;

    .goods-num {
      display: flex;
      font-size: 30rpx;
      justify-content: flex-end;
      color: #999999;
    }

    .goods-price {
      @extend .goods-num;
    }
  }
}

.bottom-view {
  background: #FFFFFF;
  padding: 20rpx 30rpx 78rpx 33rpx;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;

  .confirm-button {
    display: flex;
    flex: 1;
    font-weight: bold;
    width: 100%;
    font-size: 30rpx;
    height: 80rpx;
    align-items: center;
    justify-content: center;
    background: #F32B2B;
    color: #FFFFFF;
    border-radius: 40rpx;
  }
}
</style>
