<template>
  <view class="asset-page asset-theme wallet-page">
    <view class="wallet-nav">
      <view class="wallet-nav__side">
        <view class="wallet-nav__back" @click="handleBack">
          <text class="wallet-nav__back-icon">‹</text>
        </view>
      </view>
      <text class="wallet-nav__title">钱包地址</text>
      <view class="wallet-nav__side"></view>
    </view>

    <view class="asset-scroll wallet-scroll">
      <view class="wallet-panel">
        <view class="wallet-panel__head">
          <view class="wallet-panel__title">
            <view class="wallet-panel__title-icon">
              <view class="wallet-panel__title-tab"></view>
              <view class="wallet-panel__title-body"></view>
            </view>
            <text class="wallet-panel__title-text">当前钱包地址</text>
          </view>
          <text class="wallet-panel__subtitle">我在交易所的钱包地址</text>
        </view>

        <view v-if="currentWallet" class="wallet-panel__content">
          <view class="wallet-panel__address-box">{{
            currentWallet.address
          }}</view>
          <view class="wallet-panel__footer">
            <text class="wallet-panel__name">{{ currentWallet.name }}</text>
            <text
              class="wallet-panel__copy"
              @click="copy(currentWallet.address)"
              >复制地址</text
            >
          </view>
        </view>

        <view v-else class="wallet-panel__empty">
          <text class="wallet-panel__empty-title">暂未设置钱包地址</text>
          <text class="wallet-panel__empty-desc">
            新增后即可用于 BMT 提取和后续交易所对接。
          </text>
        </view>
      </view>

      <view class="wallet-actions">
        <view class="wallet-button wallet-button--primary" @click="openForm()">
          {{ currentWallet ? "修改钱包地址" : "保存" }}
        </view>
        <view
          class="wallet-button wallet-button--secondary"
          @click="handleBack"
        >
          返回
        </view>
      </view>

      <text
        v-if="currentWallet"
        class="wallet-delete"
        @click="openDeleteDialog"
      >
        删除地址
      </text>
    </view>

    <wallet-action-popup
      :visible="deleteDialogVisible"
      title="删除钱包"
      message="确认删除钱包地址"
      :address="currentWallet ? currentWallet.address : ''"
      confirm-text="确认"
      cancel-text="取消"
      @cancel="deleteDialogVisible = false"
      @confirm="remove"
    />
  </view>
</template>

<script>
import WalletActionPopup from "../../components/wallet-action-popup.vue";
import { fetchWalletDetail, deleteAssetWallet } from "../../api/assets";

export default {
  components: {
    WalletActionPopup,
  },
  data() {
    return {
      wallets: [],
      deleteDialogVisible: false,
    };
  },
  onLoad() {
    this.loadPage();
  },
  onShow() {
    this.loadPage();
  },
  computed: {
    currentWallet() {
      for (let i = 0; i < this.wallets.length; i += 1) {
        if (this.wallets[i].isDefault) {
          return this.wallets[i];
        }
      }

      return this.wallets[0] || null;
    },
  },
  methods: {
    async loadPage() {
      try {
        const result = await fetchWalletDetail();
        this.wallets = result.wallets || [];
      } catch (error) {
        uni.showToast({
          title: error.message || "钱包加载失败",
          icon: "none",
        });
      }
    },
    handleBack() {
      if (getCurrentPages().length > 1) {
        uni.navigateBack();
        return;
      }

      uni.reLaunch({
        url: "/pages/index/index",
      });
    },
    copy(address) {
      uni.setClipboardData({
        data: address,
        success() {
          uni.showToast({
            title: "已复制",
            icon: "none",
          });
        },
      });
    },
    openForm() {
      let url = "/pages/assets/wallet-form";
      if (this.currentWallet && this.currentWallet.id) {
        url += "?id=" + this.currentWallet.id;
      }
      uni.navigateTo({
        url: url,
      });
    },
    openDeleteDialog() {
      if (!this.currentWallet) {
        return;
      }

      this.deleteDialogVisible = true;
    },
    async remove() {
      if (!this.currentWallet) {
        return;
      }

      try {
        await deleteAssetWallet(this.currentWallet.id);
        this.deleteDialogVisible = false;
        uni.showToast({
          title: "删除成功",
          icon: "none",
        });
        this.loadPage();
      } catch (error) {
        uni.showToast({
          title: error.message || "删除失败",
          icon: "none",
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../styles/tokens.scss";
@import "../../styles/common.scss";

.wallet-page {
  min-height: 100vh;
  background: #191e32;
}

.wallet-nav {
  display: grid;
  grid-template-columns: 120rpx 1fr 120rpx;
  align-items: center;
  padding: calc(env(safe-area-inset-top) + 12rpx) 14rpx 12rpx;
}

.wallet-nav__side {
  display: flex;
  align-items: center;
}

.wallet-nav__back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56rpx;
  height: 56rpx;
}

.wallet-nav__back-icon {
  margin-top: -4rpx;
  font-size: 56rpx;
  line-height: 1;
  color: #ffffff;
}

.wallet-nav__title {
  text-align: center;
  font-size: 36rpx;
  font-weight: 700;
  color: #ffffff;
}

.wallet-scroll {
  min-height: calc(100vh - env(safe-area-inset-top) - 104rpx);
  display: flex;
  flex-direction: column;
  padding: 10rpx 14rpx calc(env(safe-area-inset-bottom) + 36rpx);
}

.wallet-panel {
  padding: 22rpx 20rpx 24rpx;
  border-radius: 12rpx;
  background: #242944;
  box-shadow: 0 12rpx 24rpx rgba(8, 13, 30, 0.12);
}

.wallet-panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12rpx;
}

.wallet-panel__title {
  display: flex;
  align-items: center;
  min-width: 0;
}

.wallet-panel__title-icon {
  position: relative;
  width: 34rpx;
  height: 28rpx;
  margin-right: 10rpx;
  flex-shrink: 0;
}

.wallet-panel__title-tab {
  position: absolute;
  left: 5rpx;
  top: -4rpx;
  width: 16rpx;
  height: 10rpx;
  border-radius: 6rpx 6rpx 0 0;
  background: #b48eff;
}

.wallet-panel__title-body {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 34rpx;
  height: 22rpx;
  border-radius: 6rpx;
  background: linear-gradient(135deg, #b48eff 0%, #8c6ef0 100%);
}

.wallet-panel__title-text {
  font-size: 28rpx;
  font-weight: 700;
  color: #ffffff;
}

.wallet-panel__subtitle {
  margin-left: 16rpx;
  max-width: 280rpx;
  font-size: 26rpx;
  line-height: 1.4;
  text-align: right;
  flex-shrink: 0;
  color: rgba(174, 183, 211, 0.88);
}

.wallet-panel__content,
.wallet-panel__empty {
  margin-top: 18rpx;
}

.wallet-panel__address-box {
  padding: 30rpx 24rpx;
  border-radius: 8rpx;
  background: rgba(53, 62, 94, 1);
  font-size: 26rpx;
  line-height: 1.6;
  word-break: break-all;
  color: #ffffff;
}

.wallet-panel__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 20rpx;
}

.wallet-panel__name {
  font-size: 26rpx;
  color: rgba(196, 205, 231, 0.88);
}

.wallet-panel__copy {
  font-size: 26rpx;
  font-weight: 700;
  color: #1fb7f9;
}

.wallet-panel__empty-title {
  display: block;
  font-size: 26rpx;
  font-weight: 700;
  color: #ffffff;
}

.wallet-panel__empty-desc {
  display: block;
  margin-top: 12rpx;
  font-size: 22rpx;
  line-height: 1.7;
  color: rgba(174, 183, 211, 0.82);
}

.wallet-actions {
  display: flex;
  gap: 20rpx;
  margin-top: 180rpx;
  padding: 0 10rpx;
}

.wallet-button {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  height: 92rpx;
  border-radius: 999rpx;
  font-size: 32rpx;
  font-weight: 700;
}

.wallet-button--primary {
  background: linear-gradient(135deg, #22c2ff 0%, #159de6 100%);
  box-shadow: 0 16rpx 30rpx rgba(31, 169, 243, 0.2);
  color: #ffffff;
}

.wallet-button--secondary {
  border: 1px solid rgba(154, 164, 200, 0.64);
  background: rgba(67, 77, 118, 0.92);
  color: #ffffff;
}

.wallet-delete {
  margin-top: auto;
  padding: 140rpx 0 10rpx;
  text-align: center;
  font-size: 28rpx;
  font-weight: 700;
  color: #1fb7f9;
}
</style>
