<template>
  <!-- 合约 -->
  <div class="contract">
    <div class="asset-total">
      <div class="asset-title pf400 fs16 fc1F2937">合约总资产(USDT)</div>
      <div class="asset-number pf600 fs32 fc1F2937">1,125,158.00</div>
      <div class="asset-approximately pf400 fs16 fcDF384C">≈35,458.00</div>
      <div class="asset-revenue">
        <div class="asset-left">
          <div class="text pf400 fs12 fc6A7187">合约收益(USDT)</div>
          <div class="number pf400 fs16 fc061237">5,678.00</div>
        </div>
        <div class="asset-right">
          <div class="text pf400 fs12 fc6A7187">未实现盈亏(USDT)</div>
          <div class="number pf400 fs16 fc061237">5,678.00</div>
        </div>
      </div>
    </div>
    <div class="submit pf600 fs14 fcFFFFFF">交易</div>
    <div class="page-container">
      <div class="tabs-header">
        <div class="tabs-left">
          <div
            v-for="(tab, index) in tabs"
            :key="index"
            class="tab-item"
            :class="{ active: currentTab === index }"
            @click="currentTab = index">
            {{ tab }}
            <div class="active-line" v-if="currentTab === index"></div>
          </div>
        </div>
        <div class="tabs-right">
          <span
            class="history-icon"
            style="align-content: center; display: flex; gap: 5px; align-items: center">
            <img src="../../../assets/icon/bitcoin/shizhong.svg" alt="" /> 全部</span
          >
        </div>
      </div>
      <position v-if="currentTab == 0"></position>
      <div v-if="currentTab == 1" class="order-list">
        <div v-for="item in orders" :key="item.id" class="order-card">
          <div class="card-top">
            <div class="coin-info">
              <div class="coin-icon">₿</div>
              <div class="coin-details">
                <div class="coin-name">
                  {{ item.symbol }}
                  <span class="contract-tag">{{ item.contractType }}</span>
                </div>
                <div class="tags-row">
                  <span class="tag" :class="item.side === 'buy' ? 'tag-buy' : 'tag-sell'">
                    {{ item.side === "buy" ? "买入" : "卖出" }}
                  </span>
                  <span class="tag tag-gray">逐仓 {{ item.leverage }}</span>
                  <span class="tag tag-gray">{{ item.orderType }}</span>
                </div>
              </div>
            </div>
            <button
              v-if="item.canCancel"
              class="btn-cancel"
              @click="handleCancel(item.id)">
              撤单
            </button>
            <div v-else class="badge-completed">已完成</div>
          </div>
          <div class="divider"></div>
          <div class="card-body-grid">
            <div class="grid-item align-left">
              <div class="label">数量</div>
              <div class="value">{{ item.amount }}</div>
            </div>
            <div class="grid-item align-center">
              <div class="label">价格</div>
              <div class="value">{{ item.price }}</div>
            </div>
            <div class="grid-item align-right">
              <div class="label">状态</div>
              <div class="value bold-text">{{ item.statusText }}</div>
            </div>
            <div class="grid-item align-left">
              <div class="label">触发价格</div>
              <div class="value">{{ item.triggerPrice }}</div>
            </div>
            <div class="grid-item align-center">
              <div class="label">下单时间</div>
              <div class="value">{{ item.time }}</div>
            </div>
            <div class="grid-item align-right">
              <div class="label">状态</div>
              <div class="value bold-text">{{ item.stateText }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
  import { defineAsyncComponent, ref } from "vue";

  const position = defineAsyncComponent(() =>
    import("../../bitcoin/components/position.vue")
  );

  // 1. 模拟数据：为了还原图片，我们创建两个不同的订单对象
  const orders = ref([
    {
      id: 1,
      symbol: "BTC/USDT",
      contractType: "永续",
      side: "buy", // 'buy' | 'sell'
      leverage: "20X",
      orderType: "市价",
      amount: "0.215 USDT",
      price: "0.215 USDT",
      statusText: "止损",
      triggerPrice: "标记价格>20",
      time: "2025-11-04, 16:30",
      stateText: "未触发",
      canCancel: true, // 显示红色撤单按钮
    },
    {
      id: 2,
      symbol: "BTC/USDT",
      contractType: "永续",
      side: "sell",
      leverage: "20X",
      orderType: "市价",
      amount: "0.215 USDT",
      price: "0.215 USDT",
      statusText: "止盈",
      triggerPrice: "标记价格>20",
      time: "2025-11-04, 16:30",
      stateText: "未触发",
      canCancel: false, // 显示灰色已完成/不可操作按钮
    },
  ]);

  // 当前选中的 Tab，默认选中 index 1 (当前委托)
  const currentTab = ref(0);
  const tabs = ["持有仓位(2)", "当前委托(2)"];

  // 按钮操作
  const handleCancel = (id) => {
    console.log("撤单 ID:", id);
    // 这里写撤单逻辑
  };
</script>
<style lang="less" scoped>
  .contract {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    width: 100%;

    .asset-total {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      margin-top: 16px;
      width: 343px;

      .asset-title {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;
        height: 22px;

        .eye-close {
          margin-left: 5px;
          width: 16px;
          height: 16px;
        }
      }

      .asset-number {
        margin-top: 3px;
        height: 44px;
        line-height: 44px;
      }

      .asset-approximately {
        margin-top: 1px;
        height: 22px;
        line-height: 22px;
      }

      .asset-revenue {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        margin-top: 6px;
        height: 44px;

        .asset-left,
        .asset-right {
          height: 100%;

          .text {
            height: 20px;
            line-height: 20px;
            letter-spacing: 0.3px;
          }

          .number {
            margin-top: 3px;
            height: 24px;
            line-height: 24px;
          }
        }
      }
    }

    .submit {
      margin-top: 16px;
      width: 311px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 100px;
      background: #df384c;
      letter-spacing: 0.2px;
    }

    /* 基础设置 */
    .page-container {
      width: 100%;
      //min-height: 100vh;
      //background-color: #FAFAFA; /* 浅灰背景 */
      font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue",
        Arial, sans-serif;
    }

    /* --- Tabs 样式 --- */
    .tabs-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 16px;
      background-color: #fff;
      height: 44px;
      position: sticky;
      top: 0;
      z-index: 10;
    }

    .tabs-left {
      display: flex;
      gap: 24px;
    }

    .tab-item {
      font-size: 15px;
      color: #999;
      position: relative;
      cursor: pointer;
      padding: 10px 0;
    }

    .tab-item.active {
      color: #333;
      font-weight: 600;
      font-size: 16px;
    }

    .active-line {
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 20px;
      height: 3px;
      background-color: #121212; /* 选中时的蓝色 */
      border-radius: 2px;
    }

    .tabs-right {
      color: #999;
      font-size: 14px;
      align-content: center;
    }

    /* --- 列表卡片样式 --- */
    .order-list {
      padding: 15px;
    }

    .order-card {
      background-color: #fff;
      border-radius: 8px;
      padding: 16px;
      margin-bottom: 12px;
      //box-shadow: 0 2px 8px rgba(0,0,0,0.02); /* 轻微阴影 */
      border: 1px solid #f0f0f0;
    }

    /* 卡片头部 */
    .card-top {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      margin-bottom: 12px;
    }

    .coin-info {
      display: flex;
      gap: 10px;
    }

    .coin-icon {
      width: 36px;
      height: 36px;
      background-color: #f7931a; /* BTC 橙色 */
      color: white;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: bold;
      font-size: 20px;
    }

    .coin-name {
      font-size: 16px;
      font-weight: 600;
      color: #333;
      margin-bottom: 4px;
    }

    .contract-tag {
      font-size: 12px;
      font-weight: normal;
      color: #333;
    }

    .tags-row {
      display: flex;
      gap: 6px;
    }

    /* 标签体系 */
    .tag {
      font-size: 11px;
      padding: 1px 4px;
      border-radius: 2px;
      color: #fff;
    }

    .tag-buy {
      background-color: #2ebd85; /* 涨/买入绿 */
    }

    .tag-sell {
      background-color: #df294a; /* 跌/卖出红 */
    }

    .tag-gray {
      background-color: #f0f0f0;
      color: #999;
    }

    /* 按钮体系 */
    .btn-cancel {
      background-color: #df294a;
      color: white;
      border: none;
      padding: 6px 16px;
      border-radius: 4px;
      font-size: 13px;
      cursor: pointer;
    }

    .badge-completed {
      background-color: #aaa;
      color: white;
      padding: 6px 12px;
      border-radius: 4px;
      font-size: 13px;
    }

    .divider {
      height: 1px;
      background-color: #f5f5f5;
      margin-bottom: 12px;
    }

    /* --- 数据网格 Grid --- */
    .card-body-grid {
      display: grid;
      /* 定义三列，左侧和中间自动，右侧靠边 */
      grid-template-columns: 1fr 1fr 1fr;
      row-gap: 12px; /* 行间距 */
    }

    .grid-item {
      display: flex;
      flex-direction: column;
      gap: 4px;
    }

    /* 对齐方式 */
    .align-left {
      align-items: flex-start;
    }
    .align-center {
      align-items: center;
    }
    .align-right {
      align-items: flex-end;
    }

    .label {
      font-size: 12px;
      color: #999; /* 灰色标签 */
    }

    .value {
      font-size: 13px;
      color: #333;
      font-family: Helvetica, Arial, sans-serif; /* 数字字体优化 */
    }

    .bold-text {
      font-weight: 500;
    }
  }
</style>
