<template>
  <view class="content safe-area-inset-bottom">
    <MessageNotification />
    <!-- 资质证明 -->
    <view class="section">
      <view class="section-title">{{ storeName || (isPlatform ? '萧康云医' : '店铺') }} - 资质证明</view>
      <view class="qualifications-list">
        <view class="qualification-item" v-for="(item, index) in qualifications" :key="index" @click="previewImage(index)">
          <view class="qualification-content">
            <text class="qualification-name">{{ item.name }}</text>
            <image :src="item.image || '/static/mine/avatar_1.png'" mode="aspectFit" class="qualification-img"></image>
          </view>
        </view>
      </view>
    </view>

    <!-- 联系方式 - 暂时隐藏 -->
    <!-- <view class="section" v-if="contactInfo">
      <view class="section-title">联系方式</view>
      <view class="info-card">
        <view class="info-item" v-if="contactInfo.phone">
          <text class="label">客服电话：</text>
          <text class="value" @click="makeCall(contactInfo.phone)">{{ contactInfo.phone }}</text>
        </view>
        <view class="info-item" v-if="contactInfo.email">
          <text class="label">邮箱：</text>
          <text class="value">{{ contactInfo.email }}</text>
        </view>
        <view class="info-item" v-if="contactInfo.address">
          <text class="label">地址：</text>
          <text class="value">{{ contactInfo.address }}</text>
        </view>
      </view>
    </view> -->

    <!-- 投诉举报方式 - 暂时隐藏 -->
    <!-- <view class="section" v-if="complaintInfo">
      <view class="section-title">投诉举报方式</view>
      <view class="info-card">
        <view class="info-item" v-if="complaintInfo.phone">
          <text class="label">投诉电话：</text>
          <text class="value" @click="makeCall(complaintInfo.phone)">{{ complaintInfo.phone }}</text>
        </view>
        <view class="info-item" v-if="complaintInfo.email">
          <text class="label">投诉邮箱：</text>
          <text class="value">{{ complaintInfo.email }}</text>
        </view>
        <view class="info-item" v-if="complaintInfo.address">
          <text class="label">投诉地址：</text>
          <text class="value">{{ complaintInfo.address }}</text>
        </view>
      </view>
    </view> -->
  </view>
</template>

<script>
import { getPlatformQualificationsApi, getPlatformInfoApi } from '../../request/api/platform'

export default {
  data() {
    return {
      storeId: '', // 店铺ID，0表示平台
      isPlatform: false, // 是否为平台资质
      storeName: '', // 店铺名称
      qualifications: [],
      contactInfo: {
        phone: '',
        email: '',
        address: ''
      },
      complaintInfo: {
        phone: '',
        email: '',
        address: ''
      }
    }
  },
  onLoad(options) {
    // 接收store_id参数，如果传入0则显示平台资质，否则显示店铺资质
    if (options.store_id !== undefined) {
      this.storeId = options.store_id;
      this.isPlatform = options.store_id === '0' || options.store_id === 0;
    } else {
      // 默认使用当前店铺ID
      this.storeId = uni.getStorageSync('store_id') || '11001';
      this.isPlatform = false;
    }
    // 动态设置页面标题
    uni.setNavigationBarTitle({
      title: this.isPlatform ? '平台资质' : '店铺资质'
    });
    this.getQualifications();
    this.getPlatformInfoData();
  },
  methods: {
    // 获取资质证明
    getQualifications() {
      getPlatformQualificationsApi({
        store_id: this.storeId,
      }).then((res) => {
        if (res.data.code == 0 || res.data.errcode == 0) {
          const result = res.data.data || res.data.result || {};
          // 适配新的返回格式（包含 store_name 和 list）
          if (result.store_name !== undefined) {
            this.storeName = result.store_name || '';
            this.qualifications = result.list || [];
          } else {
            // 兼容旧格式（直接返回数组）
            this.qualifications = Array.isArray(result) ? result : [];
          }
        }
      }).catch(() => {
        // 如果接口不存在，使用默认数据
        this.qualifications = [
          { name: '互联网药品经营许可证', image: '', type: 1 },
          { name: '营业执照', image: '', type: 2 },
          { name: '医疗器械经营许可证', image: '', type: 3 },
          { name: '食品经营许可证', image: '', type: 4 },
          { name: '药师资格证', image: '', type: 5 }
        ];
      })
    },
    // 获取平台信息
    getPlatformInfoData() {
      getPlatformInfoApi({
        store_id: this.storeId,
      }).then((res) => {
        if (res.data.code == 0 || res.data.errcode == 0) {
          const data = res.data.data || res.data.result || {};
          if (data.contact) {
            this.contactInfo = data.contact;
          }
          if (data.complaint) {
            this.complaintInfo = data.complaint;
          }
        }
      }).catch(() => {
        // 接口不存在时使用空数据
      })
    },
    // 预览资质图片，支持滑动查看所有资质
    previewImage(index) {
      // 获取所有有图片的资质
      const allImages = this.qualifications
        .filter(q => q.image)
        .map(q => q.image);
      if (allImages.length > 0) {
        const currentImage = this.qualifications[index]?.image;
        uni.previewImage({
          urls: allImages,
          current: currentImage || allImages[0]
        });
      }
    },
    // 拨打电话
    makeCall(phone) {
      if (phone) {
        uni.makePhoneCall({
          phoneNumber: phone,
          fail: (err) => {
            console.error('拨打电话失败:', err);
          }
        });
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  width: 750rpx;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
  background: #F5F5F5;
  padding: 20rpx;

  .section {
    background: #fff;
    border-radius: 24rpx;
    padding: 40rpx 32rpx;
    margin-bottom: 24rpx;
    box-shadow: 0 2rpx 16rpx rgba(0, 0, 0, 0.04);

    .section-title {
      font-size: 40rpx;
      font-weight: bold;
      color: #232323;
      margin-bottom: 32rpx;
      padding-bottom: 24rpx;
      border-bottom: 2rpx solid #F0F0F0;
      position: relative;

      &::after {
        content: '';
        position: absolute;
        left: 0;
        bottom: -2rpx;
        width: 80rpx;
        height: 2rpx;
        background: linear-gradient(90deg, #4175EE 0%, #6B9EFF 100%);
        border-radius: 2rpx;
      }
    }

    .qualifications-list {
      display: flex;
      flex-direction: column;
      gap: 24rpx;

      .qualification-item {
        background: #fff;
        border-radius: 20rpx;
        overflow: hidden;
        box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
        transition: all 0.3s ease;

        &:active {
          transform: scale(0.98);
          box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
        }

        .qualification-content {
          display: flex;
          flex-direction: column;
          padding: 32rpx;
        }

        .qualification-name {
          font-size: 36rpx;
          font-weight: bold;
          color: #232323;
          margin-bottom: 24rpx;
          line-height: 1.5;
          letter-spacing: 1rpx;
        }

        .qualification-img {
          width: 100%;
          height: 500rpx;
          border-radius: 16rpx;
          background: #F7F8FA;
        }
      }
    }

    .info-card {
      .info-item {
        display: flex;
        align-items: flex-start;
        padding: 20rpx 0;
        border-bottom: 1rpx solid #F0F0F0;

        &:last-child {
          border-bottom: none;
        }

        .label {
          font-size: 28rpx;
          color: #666;
          width: 160rpx;
          flex-shrink: 0;
        }

        .value {
          font-size: 28rpx;
          color: #232323;
          flex: 1;
          word-break: break-all;

          &:active {
            opacity: 0.7;
          }
        }
      }
    }
  }
}
</style>
