<script setup>
    import {defineAsyncComponent, ref, nextTick} from 'vue';
    //状态组件
    const assetlessState = defineAsyncComponent(() => import("./assetlessState.vue"));
    //持有仓位组件
    const position = defineAsyncComponent(() => import("./position.vue"));
    import { useRouter } from 'vue-router'; // 【新增】 引入路由

    const router = useRouter(); // 【新增】 实例化路由



    // 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 // 显示灰色已完成/不可操作按钮
      },
    ]);
    const containerRef = ref(null)
    // 当前选中的 Tab，默认选中 index 1 (当前委托)
    const currentTab = ref(0);
    const tabs = ['持有仓位(2)', '当前委托(2)'];
    const TabActive = (index) => {
        currentTab.value = index
    }

    // 按钮操作
    const handleCancel = (id) => {
      console.log('撤单 ID:', id);
      // 这里写撤单逻辑
    };

    //平仓平空
    const showModal3 = ref(false);

</script>

<template>
  <div class="page-container1">

    <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="TabActive(index)"
        >
          {{ tab }}
          <div class="active-line" v-if="currentTab === index"></div>
        </div>
      </div>

      <div class="tabs-right">
        <span @click="router.push('/historyIndex')" 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>
    <div v-if="currentTab===0">
      <position ></position>
    </div>

    <div >
<!--      <assetlessState v-if="isassetlessState"></assetlessState>-->
    </div>
    <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>
</template>

<style lang="less" scoped>
/* 基础设置 */
.page-container1 {
  width: 100%;
  /* 1. 必须限制高度 (例如占满屏幕) */
  //height: 100vh;
  /* 或者 height: calc(100vh - 头部高度); */

  /* 2. 必须开启溢出滚动 */
  //overflow-y: auto;
  //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 2fr 1fr;
  row-gap: 12px; /* 行间距 */
}

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

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

.align-center {
  align-items: center;
  .value:nth-child(2){
    margin-top: 2px;
  }
}

.align-right {
  align-items: flex-end;
}

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

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

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