

<template>
  <view class="container">
<!--    商家信息-->
    <view class="store-card">
      <view class="info-top">
        <view class="left">
          <image class="storeImg" :src="merchantInfo.merchantLogo" mode="aspectFit"></image>
        </view>
        <view class="center">
          <view class="name">{{merchantInfo.merchantName}}</view>
        </view>
      </view>
      <view class="desc">{{merchantInfo.merchantDescribe}}</view>
      <view class="tel" @click="makePhoneCall">
        <image class="phone mr20" src="@/static/images/phone2.png" mode="widthFix"></image>
        <text>联系电话：{{merchantInfo.merchantPhone}}</text>
        <image class="phone fr" src="@/static/images/phoneyellow.png" mode="widthFix"></image>
      </view>
    </view>
<!--    商品-->
    <up-tabs :list="tabList"
             @click="tabChange"
             lineColor="#333333"
             :itemStyle="{
               flex:1,
               height:'44px',
             }"
             :activeStyle="{
                color: '#333333',
                fontWeight: 'bold',
                transform: 'scale(1.05)'
            }"
             :inactiveStyle="{
              color: '#666666',
              transform: 'scale(1)'
        }"></up-tabs>
      <!--    商品列表-->
    <view class="index-product-wrapper" v-if="tabActive == '0'">
      <view
          class="list-box animated"
          :class="goodsList.length > 0 ? 'fadeIn on' : ''"
      >
        <view
            class="item"
            v-for="(item, index) in calculatedProducts"
            :key="index"
            @click="goDetail(item)"
        >
          <view class="pictrue">
            <image :src="item.image" mode=""></image>
          </view>
          <view class="text-info">
            <view class="title" >
              <view class="text line1">{{ item.storeName }}</view>
              <view class="weight">{{ item.weight }}g</view>
            </view>
            <view class="bottom-row">
              <!-- <text class="price">工费: {{ item.price }}/克</text> -->
              <text class="price">¥ {{ item.totalLaborCost }}</text>
              <text class="sales">
                销量：{{ Number(item.sales || 0) + Number(item.ficti || 0) }}件
              </text>
              <!-- <view class="txt">券</view> -->
            </view>
            <view class="bottom-row">
              <!-- <text class="price">工费: {{ item.price }}/克</text> -->
              <text class="sales">工费： {{ item.totalLaborCost }}</text>
              <text class="sales">
                附加费： {{ item.totalLaborCost }}
              </text>
              <!-- <view class="txt">券</view> -->
            </view>
          </view>
        </view>
      </view>
      <view class="loadingicon acea-row row-center-wrapper" v-if="goodScroll">
        <text
            class="loading iconfont icon-jiazai"
            :hidden="loading == false"
        ></text>
      </view>
      <view class="no-data" v-if="isNoDataState">
        <image
            src="https://my-go-easy-im.oss-cn-shenzhen.aliyuncs.com/goeasy-im-%E6%B0%B4%E8%B4%9D%E5%95%86%E5%9F%8E/zhanwu_20250827104005_1720_6.png"
        />
      </view>
      <view class="mores-txt flex" v-if="!goodScroll && !isNoDataState">
        <text>我是有底线的</text>
      </view>
    </view>
    <view class="index-product-wrapper" v-if="tabActive == '1'">
      <merchantCate :merchant-id="query.merchantId || merchantInfo.id" />
    </view>
  </view>
</template>
<script setup>
import {computed, ref} from 'vue';
import { onLoad, onShow, onReachBottom } from "@dcloudio/uni-app";
import { getSbmerchantInfo } from "@/api/merchant.js";
import merchantCate from "./components/merchant_cate.vue"
import { getGroomList as getGroomListApi, } from "@/api/store.js";
// 获取实时金价
import useRealGoldPrice from "@/hooks/useRealGoldPrice";
// 实时价格处理
const {
  realGoldprice, // 黄金实时销售价（基础）
  realPtprice, // 铂金实时销售价（基础）
  realAgprice, // 白银实时销售价（基础）
} = useRealGoldPrice({});

const query = ref({});
const merchantInfo = ref({});
const tabList = ref([
  {name:'推荐商品',code:'0'},
  {name:'商品分类',code:'1'}
])
const tabActive = ref('0');
const goodsList = ref([]);
const goodType = ref(1);
// Pagination
const params = ref({
  page: 1,
  limit: 10,
});
const loading = ref(false);
const goodScroll = ref(true);

const calculatedProducts = computed(() => {
  // 计算逻辑与原代码一致，但基于响应式数据 products
  return goodsList.value.map((product) => {
    // 1. 将price字符串转为数字
    const price = Number(product.price);
    // 2. 计算乘积
    const total = (price + product.sales) * realGoldprice.value;
    // 3. 向上取整到两位小数（先放大100倍取整，再缩小100倍）
    const roundedTotal = Math.ceil(total * 100) / 100;
    // 4. 格式化保留两位小数
    const formattedTotal = roundedTotal.toFixed(2);

    return {
      ...product,
      calculatedTotal: formattedTotal, // 新增计算结果字段
    };
  });
});
const isNoDataState = computed(() => {
  return goodsList.value.length === 0 && !loading.value;
});


onLoad((options) => {
  query.value = options;
  getSbmerchantInfoFn();
  getGroomList();
});
onReachBottom(() => {
  getGroomList();
});
const getSbmerchantInfoFn = () => {
  let data = {
    merchantId:query.value.merchantId
  }
  getSbmerchantInfo(data).then((res) => {
    console.log(res);
    merchantInfo.value = res.data;
  })
}
const tabChange = (item) => {
  console.log('item',item)
  tabActive.value = item.code;
}
// Product Lists
const getGroomList = async () => {
  if (!goodScroll.value) return;
  try {
    loading.value = true;
    query.value.merchantId!='' ?  params.value.merchantId = query.value.merchantId:'';
    const { data } = await getGroomListApi(goodType.value, params.value);

    goodsList.value = [...goodsList.value, ...data.productList.list] || [];
    goodScroll.value = data.productList.list.length >= params.value.limit;
    params.value.page++;
  } catch (err) {
    console.error(err);
  } finally {
    loading.value = false;
  }
};
// 在methods部分添加：
const makePhoneCall = () => {
  if (!merchantInfo.value.merchantPhone) {
    uni.showToast({
      title: '暂无联系电话',
      icon: 'none'
    });
    return;
  }

  // 显示确认弹窗
  uni.showModal({
    title: '拨打电话',
    content: `是否拨打 ${merchantInfo.value.merchantPhone}？`,
    success: (res) => {
      if (res.confirm) {
        // 调用拨打电话API
        uni.makePhoneCall({
          phoneNumber: merchantInfo.value.merchantPhone,
          success: () => {
            console.log('拨打电话成功');
          },
          fail: (err) => {
            console.log('拨打电话失败：', err);
            uni.showToast({
              title: '拨打电话失败',
              icon: 'none'
            });
          }
        });
      }
    }
  });
};
</script>
<style scoped lang="scss">
.container{
  padding: 30rpx;
  box-sizing: border-box;
}
.store-card{
  background: #ffffff;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 10rpx;

  .info-top{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #F9F7F0;
    border-radius: 16rpx;
    .left{
      width: 140rpx;
      .storeImg{
        width: 100rpx;
        height: 100rpx;
      }
    }
    .center{
      width: 70%;
      .name{
        font-size: 32rpx;
        color: #333;line-height: 60rpx;
      }

    }
  }
  .desc{
    font-size: 24rpx;
    color: #666;
    line-height: 40rpx;
    padding: 20rpx 0;
  }
  .tel{
    font-size: 28rpx;
    color:#333;
    line-height: 40rpx;
    .phone{
      width: 34rpx;
      vertical-align: middle;
    }
  }
}
.mr20{
  margin-right: 20rpx;
}
.fr{
  float: right;
}
.index-product-wrapper {
  //padding: 0 30rpx;
  margin-bottom: 110rpx;
  min-height: 700rpx;
  margin-top: 30rpx;
  //background: #fff;
  position: relative;

  &.on {
    min-height: 1500rpx;
  }

  .list-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .item {
      width: 48.99%;
      height: 490rpx;
      background-color: #ffffff;
      box-shadow: 0rpx 3rpx 13rpx 0rpx rgba(0, 0, 0, 0.13);
      border-radius: 20rpx;
      margin-bottom: 20rpx;

      overflow: hidden;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .pictrue {
        position: relative;

        image {
          width: 100%;
          height: 330rpx;
        }
      }

      .text-info {
        padding: 10rpx 20rpx 15rpx;

        .title {
          color: #222222;
          display: flex;
          align-items: center;
          justify-items: space-between;
          .tip {
            width: 61rpx;
            height: auto;
            font-size: 22rpx;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #ffffff;
            position: relative;
            margin-right: 5rpx;
            background-color: #ef4800;
            border-radius: 5rpx;
          }
          .text{
            width: 80%;
          }
          .weight{
            background-color: rgba(197, 128, 3, 0.10);
            color: #C58003;
            font-size: 24rpx;
            padding: 8rpx 16rpx;
            border-radius: 8rpx;
            float: right;
          }
        }

        .bottom-row {
          color: $theme-color;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 28rpx;
          margin: 10rpx 0 0;

          .price {
            padding-bottom: 4rpx;
            font-weight: 800;
            white-space: nowrap;
            font-size: 28rpx;
            color: #f16327;
          }

          .sales {
            color: #b4b4b4;
            font-size: 22rpx;
            white-space: nowrap;
            color: #6e6e6e;
          }
        }
      }
    }

    &.on {
      display: flex;
    }
  }
}
.no-data {
  margin: 150rpx auto 0;
  text-align: center;

  img {
    width: 65%;
    height: auto;
  }
}
.mores-txt {
  width: 100%;
  align-items: center;
  justify-content: center;
  height: 70rpx;
  color: #999;
  font-size: 24rpx;

  .iconfont {
    margin-top: 2rpx;
    font-size: 20rpx;
  }
}
</style>