<template>
  <view class="tab-page mine-page">
    <scroll-view scroll-y class="mine-scroll" enable-back-to-top>
      <view class="mine-profile">
        <view class="mine-profile__avatar">
          <image
            v-if="userAvatar"
            class="mine-profile__avatar-img"
            :src="userAvatar"
            mode="aspectFill"
          />
          <up-icon v-else name="account-fill" color="#22C55E" :size="56" />
        </view>
        <view class="mine-profile__meta">
          <text class="mine-profile__name">{{ displayName }}</text>
          <text v-if="!isLoggedIn" class="mine-profile__hint">登录后查看个人资料与业务数据</text>
        </view>
      </view>

      <view v-if="!isLoggedIn" class="mine-auth-bar">
        <up-button
          type="primary"
          color="#22C55E"
          text="去登录"
          @click="goLogin"
        />
      </view>

      <view v-if="isLoggedIn" class="mine-cells">
        <view
          v-for="row in infoRows"
          :key="row.id"
          class="mine-cell"
        >
          <view class="mine-cell__icon-wrap">
            <up-icon :name="row.icon" color="#22C55E" :size="22" />
          </view>
          <text class="mine-cell__title">{{ row.title }}</text>
          <text class="mine-cell__value">{{ row.value }}</text>
        </view>
      </view>

      <view v-if="isLoggedIn" class="mine-logout-wrap">
        <up-button
          type="info"
          plain
          text="退出登录"
          @click="handleLogout"
        />
      </view>
    </scroll-view>
  </view>
</template>

<script>
import UIcon from 'uview-plus/components/u-icon/u-icon.vue'
import UButton from 'uview-plus/components/u-button/u-button.vue'
import { useUserStore } from '@/store/user'
import { getPartnerIdentityLabel } from '@/utils/partnerRole'

export default {
  components: {
    'up-icon': UIcon,
    'up-button': UButton
  },
  computed: {
    userStore() {
      return useUserStore()
    },
    isLoggedIn() {
      return this.userStore.isLoggedIn()
    },
    displayName() {
      return this.userStore.displayName() || '访客'
    },
    userAvatar() {
      return this.userStore.state.avatar
    },
    infoRows() {
      const roles = this.userStore.state.roles
      const phone = (this.userStore.state.phonenumber || '').trim()
      return [
        {
          id: 'identity',
          icon: 'account',
          title: '用户身份',
          value: getPartnerIdentityLabel(roles)
        },
        {
          id: 'entityType',
          icon: 'home',
          title: '主体类型',
          value: '--'
        },
        {
          id: 'contact',
          icon: 'phone',
          title: '联系方式',
          value: phone || '--'
        }
      ]
    }
  },
  onShow() {
    if (this.isLoggedIn && (!this.userStore.state.roles.length || !this.userStore.state.phonenumber)) {
      this.userStore.fetchUserInfo().catch(() => {})
    }
  },
  methods: {
    goLogin() {
      uni.navigateTo({ url: '/pages/login/index' })
    },
    handleLogout() {
      uni.showModal({
        title: '',
        content: '确定退出当前账号吗？',
        success: (res) => {
          if (!res.confirm) {
            return
          }
          this.userStore.logOut().then(() => {
            uni.reLaunch({ url: '/pages/login/index' })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/morandi.scss';
@import '@/styles/scroll-fill.scss';

.mine-page {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 100%;
  background: $morandi-bg-page;
  box-sizing: border-box;
  /* #ifdef H5 */
  flex: 1;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  /* #endif */
}

.mine-scroll {
  @include flex-scroll-y;
  padding: 24rpx 24rpx 40rpx;
}

.mine-profile {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 28rpx;
  min-width: 0;
  margin-bottom: 24rpx;
  padding: 36rpx 28rpx 40rpx;
  border-radius: 20rpx;
  overflow: hidden;
  box-sizing: border-box;
  background: linear-gradient(145deg, #3d9b6e 0%, #22c55e 48%, #a7f3d0 100%);
  box-shadow: 0 12rpx 36rpx rgba(61, 155, 110, 0.2);

  &::before,
  &::after {
    content: '';
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.14);
    pointer-events: none;
  }

  &::before {
    width: 200rpx;
    height: 200rpx;
    top: -72rpx;
    right: -48rpx;
  }

  &::after {
    width: 120rpx;
    height: 120rpx;
    bottom: -32rpx;
    left: -24rpx;
    background: rgba(255, 255, 255, 0.1);
  }
}

.mine-profile__avatar {
  position: relative;
  z-index: 1;
  flex-shrink: 0;
  width: 144rpx;
  height: 144rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  border: 4rpx solid rgba(255, 255, 255, 0.9);
  box-sizing: border-box;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
}

.mine-profile__avatar-img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.mine-profile__meta {
  position: relative;
  z-index: 1;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.mine-profile__name {
  font-size: 40rpx;
  font-weight: 700;
  line-height: 1.35;
  color: #ffffff;
  word-break: break-word;
  overflow-wrap: anywhere;
  text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
}

.mine-profile__hint {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.9);
  line-height: 1.45;
}

.mine-auth-bar {
  margin-bottom: 24rpx;
  min-width: 0;

  ::v-deep .u-button {
    border-radius: 999rpx !important;
    box-shadow: 0 8rpx 20rpx rgba(34, 197, 94, 0.25);
  }
}

.mine-logout-wrap {
  margin-top: 32rpx;
  min-width: 0;
  padding: 8rpx 0 0;

  ::v-deep .u-button {
    border-radius: 16rpx !important;
    border-color: $morandi-border-strong !important;
    color: $morandi-text-secondary !important;
    background: $morandi-bg-card !important;
  }
}

.mine-cells {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-radius: 20rpx;
  overflow: hidden;
  background: $morandi-bg-card;
  border: 1rpx solid $morandi-border;
  box-sizing: border-box;
  box-shadow: 0 8rpx 28rpx rgba(74, 69, 66, 0.06);
}

.mine-cell {
  display: flex;
  flex-direction: row;
  align-items: center;
  min-width: 0;
  padding: 28rpx 24rpx;
  gap: 20rpx;
  box-sizing: border-box;
  border-bottom: 1rpx solid $morandi-border-soft;
  background: $morandi-bg-card;
}

.mine-cell:last-child {
  border-bottom-width: 0;
}

.mine-cell__icon-wrap {
  flex-shrink: 0;
  width: 72rpx;
  height: 72rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #e8f5ec;
  box-sizing: border-box;
}

.mine-cell:nth-child(1) .mine-cell__icon-wrap {
  background: #e8f0e6;
}

.mine-cell:nth-child(2) .mine-cell__icon-wrap {
  background: #e4eaef;
}

.mine-cell:nth-child(3) .mine-cell__icon-wrap {
  background: #efeae4;
}

.mine-cell__title {
  flex: 1;
  min-width: 0;
  font-size: 30rpx;
  font-weight: 500;
  color: $morandi-text;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.mine-cell__value {
  flex-shrink: 1;
  max-width: 280rpx;
  font-size: 26rpx;
  color: $morandi-accent-soft;
  text-align: right;
  word-break: break-word;
  overflow-wrap: anywhere;
}
</style>
