<template>
  <view class="asset-page asset-theme wallet-form-page">
    <asset-page-shell title="钱包地址" />

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

        <view class="wallet-input">
          <input
            v-model="form.address"
            class="wallet-input__field asset-number-font"
            type="text"
            :placeholder="form.id ? '粘贴新钱包地址' : '粘贴新钱包地址'"
            placeholder-class="wallet-input__placeholder"
          />
          <!-- <text class="wallet-input__paste" @click="paste">粘贴</text> -->
        </view>

        <text class="wallet-form-panel__helper">
          钱包地址请在【海南农综】APP中获取
        </text>
      </view>

      <view class="wallet-form-actions">
        <view class="wallet-button wallet-button--primary" @click="openConfirm">
          保存
        </view>
        <view
          class="wallet-button wallet-button--secondary"
          @click="handleBack"
        >
          返回
        </view>
      </view>
    </view>

    <wallet-action-popup
      :visible="confirmVisible"
      :title="form.id ? '修改钱包' : '新增钱包'"
      :message="form.id ? '确认修改钱包新地址' : '确认保存钱包地址'"
      :address="trimmedAddress"
      description="请仔细核对钱包地址，以免造成财产损失"
      confirm-text="确认"
      cancel-text="取消"
      @cancel="confirmVisible = false"
      @confirm="submit"
    />

    <view v-if="successVisible" class="wallet-success">
      <image
        class="wallet-success__icon"
        src="https://imgs.agrimedia.cn/bm-bmt/success.png"
        mode="aspectFit"
      ></image>
      <text class="wallet-success__text">{{
        form.id ? "修改成功！" : "保存成功！"
      }}</text>
    </view>
  </view>
</template>

<script>
import AssetPageShell from "../../components/asset-page-shell.vue";
import WalletActionPopup from "../../components/wallet-action-popup.vue";
import { fetchWalletDetail, saveAssetWallet } from "../../api/assets";

export default {
  components: {
    AssetPageShell,
    WalletActionPopup,
  },
  data() {
    return {
      form: {
        id: "",
        name: "海南农综交易所",
        address: "",
      },
      confirmVisible: false,
      successVisible: false,
      submitting: false,
    };
  },
  onLoad(options) {
    this.form.id = (options && options.id) || "";
    this.loadPage(true);
  },
  computed: {
    trimmedAddress() {
      return String(this.form.address || "").trim();
    },
  },
  methods: {
    async loadPage(showLoading) {
      try {
        const result = await fetchWalletDetail(
          showLoading
            ? {
                showLoading: true,
                loadingText: "加载中",
              }
            : null,
        );
        if (!this.form.id) {
          return;
        }
        for (let i = 0; i < result.wallets.length; i += 1) {
          if (result.wallets[i].id === this.form.id) {
            this.form.name = result.wallets[i].name;
            this.form.address = result.wallets[i].address;
          }
        }
      } catch (error) {
        uni.showToast({
          title: error.message || "页面加载失败",
          icon: "none",
        });
      }
    },
    handleBack() {
      if (getCurrentPages().length > 1) {
        uni.navigateBack();
        return;
      }

      uni.reLaunch({
        url: "/pages/index/index",
      });
    },
    paste() {
      const that = this;
      uni.getClipboardData({
        success: function (result) {
          that.form.address = result.data || "";
        },
      });
    },
    openConfirm() {
      if (!this.trimmedAddress) {
        uni.showToast({
          title: "请先粘贴钱包地址",
          icon: "none",
        });
        return;
      }

      this.confirmVisible = true;
    },
    async submit() {
      if (this.submitting) {
        return;
      }

      this.submitting = true;

      try {
        await saveAssetWallet({
          id: this.form.id,
          name: this.form.name,
          address: this.trimmedAddress,
        }, {
          showLoading: true,
          loadingText: "保存中",
        });
        this.confirmVisible = false;
        this.successVisible = true;
        setTimeout(() => {
          this.successVisible = false;
          this.handleBack();
        }, 700);
      } catch (error) {
        uni.showToast({
          title: error.message || "保存失败",
          icon: "none",
        });
      } finally {
        this.submitting = false;
      }
    },
  },
};
</script>

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

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

.wallet-form-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-form-panel {
  padding: 22rpx 20rpx 24rpx;
  border-radius: 12rpx;
  background: #242944;
  box-shadow: 0 12rpx 24rpx rgba(8, 13, 30, 0.12);
}

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

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

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

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

.wallet-form-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-form-panel__title-text {
  font-size: 26rpx;
  font-weight: 700;
  color: #ffffff;
}

.wallet-form-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-input {
  display: flex;
  align-items: center;
  margin-top: 18rpx;
  padding: 0 20rpx;
  height: 94rpx;
  border-radius: 10rpx;
  background: #191e32;
}

.wallet-input__field {
  flex: 1;
  min-width: 0;
  height: 94rpx;
  font-size: 28rpx;
  color: #ffffff;
}

.wallet-input__placeholder {
  color: rgba(154, 163, 197, 0.82);
}

.wallet-input__paste {
  margin-left: 18rpx;
  font-size: 28rpx;
  font-weight: 500;
  color: #1fb7f9;
}

.wallet-form-panel__helper {
  display: block;
  margin-top: 18rpx;
  font-size: 26rpx;
  line-height: 1.6;
  color: rgba(174, 183, 211, 0.82);
}

.wallet-form-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-success {
  position: fixed;
  left: 54rpx;
  right: 54rpx;
  bottom: 250rpx;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24rpx 28rpx;
  border-radius: 20rpx;
  background: #242944;
  box-shadow: 0 24rpx 60rpx rgba(0, 0, 0, 0.26);
}

.wallet-success__icon {
  width: 44rpx;
  height: 44rpx;
  margin-right: 16rpx;
}

.wallet-success__text {
  font-size: 26rpx;
  font-weight: 700;
  color: #ffffff;
}
</style>
