<template>
  <view class="page-bg">
    <!-- 顶部标签切换 -->
<!--    <view class="tab-bar">-->
<!--      <view-->
<!--        v-for="tab in tabs"-->
<!--        :key="tab.value"-->
<!--        class="tab-item"-->
<!--        :class="{ active: activeTab === tab.value }"-->
<!--        @click="activeTab = tab.value"-->
<!--      >{{ tab.label }}</view>-->
<!--    </view>-->

    <!-- 步骤引导条 -->
    <view class="steps-bar">
      <view v-for="(step, idx) in steps" :key="idx" class="step-item">
        <view class="step-icon-wrap">
          <image :src="step.icon" class="step-icon" mode="aspectFit" />
        </view>
        <text class="step-label">{{ step.label }}</text>
        <view v-if="idx < steps.length - 1" class="step-line">···</view>
      </view>
    </view>

    <!-- 功能说明标签 -->
    <view class="feature-tags">
      <view v-for="tag in featureTags" :key="tag" class="feature-tag">{{ tag }}</view>
    </view>

    <!-- 我的分享 / 分享给我的 切换 -->
    <view class="sub-tabs">
      <view
        v-for="sub in subTabs"
        :key="sub.value"
        class="sub-tab"
        :class="{ 'sub-tab-active': activeSubTab === sub.value }"
        @click="activeSubTab = sub.value"
      >{{ sub.label }}</view>
    </view>

    <!-- 设备列表 -->
    <view class="device-list">
      <!-- 我的分享列表 -->
      <view v-if="activeSubTab === 'mine'">
        <view
          v-for="device in mySharedDevices"
          :key="device.id"
          class="device-item"
          @click="goToDetail(device)"
        >
          <image :src="agriImg" class="device-img" mode="aspectFit" />
          <view class="device-info">
            <text class="device-name">{{ device.agriName }}</text>
            <text class="device-sub">已分享：{{ device.sharedCount }}次</text>
          </view>
          <view class="arrow">›</view>
        </view>
      </view>

      <!-- 分享给我的列表  仅显示已接受的-->
      <view v-if="activeSubTab === 'tome'">
        <view
          v-for="device in toMeDevices"
          :key="device.id"
          class="device-item"
        >
          <image :src="agriImg" class="device-img" mode="aspectFit" />
          <view class="device-info">
            <text class="device-name">{{ device.agriName }}</text>
<!--            <text class="device-sub">{{ device.channels }}个通道 | {{ device.serial }}</text>-->
          </view>
          <button class="quit-btn" @click="quitShare(device)">退出分享</button>
        </view>
        <view v-if="!toMeDevices.length" class="empty-tip">暂无分享给我的设备</view>
      </view>
    </view>

    <!-- 底部发起分享按钮 -->
    <view class="bottom-bar">
      <button class="share-btn" @click="goToShareTarget">发起分享</button>
    </view>
  </view>
</template>

<script>
import {listAgri} from "@/api/system/assets/agri";
import store from "@/store";

export default {
  data() {
    return {
      activeTab: 'device',
      tabs: [
        { label: '分享设备', value: 'device' },
        { label: '分享团队', value: 'team' }
      ],
      steps: [
        { icon: 'https://img.xiaoces.com/photos/share/share.png', label: '发起分享' },
        { icon: 'https://img.xiaoces.com/photos/share/user2.png', label: '添加分享对象' },
        { icon: 'https://img.xiaoces.com/photos/share/access.png', label: '选择视频和权限' },
        { icon: 'https://img.xiaoces.com/photos/share/share__.png', label: '接受分享' }
      ],
      featureTags: ['功能按需分享', '最多分享 5 人', '仅视频设备'],
      activeSubTab: 'mine',
      subTabs: [
        { label: '我的分享', value: 'mine' },
        { label: '分享给我的', value: 'tome' }
      ],
      // 我的分享设备列表（实际对接API）
      mySharedDevices: [],
      // 分享给我的设备列表
      toMeDevices: [],
      agriImg: 'https://img.xiaoces.com/photos/share/agri.png'
    }
  },
  onShow() {
    this.getShareAgriList()
  },
  methods: {
    goToDetail(device) {
      uni.navigateTo({
        url: `/pages/home/invite/myShareDetail/index?id=${device.id}`
      })
    },
    goToShareTarget() {
      uni.navigateTo({ url: '/pages/home/invite/shareTarget/index' })
    },
    quitShare(device) {
      uni.showModal({
        title: '提示',
        content: `确定退出【${device.name}】的分享？`,
        success: (res) => {
          if (res.confirm) {
            this.$modal.msgSuccess('已退出分享')
          }
        }
      })
    },
    getShareAgriList() {
      // 获取已启用的设备列表
      listAgri({inviteBy: store.getters.id, status:1}).then(response => {
        if (response.code === 200) {
          // 求可分享列表
          var mySharedDevices
              = response.rows.filter(item => item.userId === store.getters.id);
          // 求已分享列表
          var sharedDevices
              = response.rows.filter(item => item.userId !== store.getters.id);
          mySharedDevices.forEach(shareDevice => {
             shareDevice.sharedCount = sharedDevices.filter(device => device.imei===shareDevice.imei
                && device.inviteBy ===  store.getters.id).length
          });
          this.mySharedDevices = mySharedDevices;

          // 分享给我的设备
          this.toMeDevices = mySharedDevices.filter(item => item.inviteBy && item.userId !== item.inviteBy );
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
page {
  background: radial-gradient(circle at top, #e8f0ff 0%, #f5f7fa 60%, #ffffff 100%);
}

.page-bg {
  min-height: 100vh;
  padding-bottom: 120rpx;
}

/* 顶部Tab */
.tab-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 24rpx 30rpx 0;
  background: #e0e5ef;
  border-radius: 40rpx;
  padding: 6rpx;

  .tab-item {
    flex: 1;
    text-align: center;
    padding: 14rpx 0;
    border-radius: 34rpx;
    font-size: 28rpx;
    color: #666;
    transition: all 0.2s;

    &.active {
      background: #1a1a1a;
      color: #fff;
      font-weight: bold;
    }
  }
}

/* 步骤条 */
.steps-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 50rpx 30rpx 20rpx;

  .step-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
    position: relative;
  }

  .step-icon-wrap {
    width: 72rpx;
    height: 72rpx;
    border-radius: 50%;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.08);
  }

  .step-icon {
    width: 40rpx;
    height: 40rpx;
  }

  .step-label {
    font-size: 22rpx;
    color: #666;
    margin-top: 8rpx;
    text-align: center;
  }

  .step-line {
    position: absolute;
    top: 30rpx;
    right: -20rpx;
    font-size: 24rpx;
    color: #4a90e2;
    letter-spacing: 2rpx;
  }
}

/* 功能标签 */
.feature-tags {
  display: flex;
  justify-content: center;
  margin: 40rpx 30rpx;
  gap: 20rpx;

  .feature-tag {
    padding: 8rpx 40rpx;
    border-radius: 8rpx;
    font-size: 22rpx;

    &:first-child {
      background: rgba(8, 115, 255, 0.14);
      color: rgb(45, 86, 216);
    }
    &:nth-child(2) {
      background: #fff3e0;
      color: #f5a623;
    }
    &:nth-child(3) {
      background: rgba(253, 60, 60, 0.13);
      color: #fd3c3c;
    }
  }
}

/* 二级Tab */
.sub-tabs {
  display: flex;
  margin: 30rpx 30rpx;

  .sub-tab {
    padding: 16rpx 30rpx;
    font-size: 28rpx;
    color: #999;
    position: relative;

    &-active {
      color: #1a1a1a;
      font-weight: bold;

      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 40rpx;
        height: 4rpx;
        background: #1a1a1a;
        border-radius: 2rpx;
      }
    }
  }
}

/* 设备列表 */
.device-list {
  margin: 16rpx 25rpx;
  background: #fff;
  border-radius: 16rpx;
  overflow: hidden;

  .device-item {
    display: flex;
    align-items: center;
    padding: 34rpx 28rpx;
    border-bottom: 1rpx solid #f2f2f2;

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

    .device-img {
      width: 90rpx;
      height: 90rpx;
      margin-right: 20rpx;
      flex-shrink: 0;
    }

    .device-info {
      flex: 1;
      overflow: hidden;

      .device-name {
        font-size: 28rpx;
        color: #333;
        display: block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .device-sub {
        font-size: 24rpx;
        color: #999;
        display: block;
        margin-top: 6rpx;
      }
    }

    .arrow {
      color: #ccc;
      font-size: 36rpx;
      margin-left: 10rpx;
    }

    .quit-btn {
      font-size: 24rpx;
      color: #555;
      background: #fff;
      border: 2rpx solid #ddd;
      border-radius: 30rpx;
      padding: 10rpx 28rpx;
      line-height: 1.4;
    }
  }
}

.empty-tip {
  text-align: center;
  color: #bbb;
  font-size: 26rpx;
  padding: 60rpx 0;
}

/* 底部按钮 */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 30rpx;
  background: #edf1f7;

  .share-btn {
    background: #3d7ff5;
    color: #fff;
    border-radius: 50rpx;
    font-size: 32rpx;
    height: 90rpx;
    line-height: 90rpx;
    text-align: center;
    border: none;
  }
}
</style>
