<template>
  <div class="detail-app-container">
    <div class="header-section">
      <el-button size="small" icon="el-icon-back" @click="goBack">返回列表</el-button>
      <span class="page-title">订单详情</span>
    </div>

    <div v-loading="loading" class="content-section">

      <div class="section-title">基本信息</div>
      <table class="detail-table">
        <tbody>
        <tr>
          <td class="label">订单号</td>
          <td class="value"><span class="text-bold">{{ orderInfo.ordernum || '--' }}</span></td>
          <td class="label">订单状态</td>
          <td class="value">
            <el-tag :type="getStatusTagType(orderInfo.status)" size="small">
              {{ getStatusLabel(orderInfo.status) }}
            </el-tag>
          </td>
        </tr>
        <tr>
          <td class="label">期号</td>
          <td class="value"><span class="text-bold">{{ orderInfo.issue || '--' }}</span></td>
          <td class="label">彩种类型</td>
          <td class="value">{{ getGameTypeLabel(orderInfo.type) }}</td>
        </tr>
        <tr>
          <td class="label">会员ID</td>
          <td class="value">{{ orderInfo.member_id || '--' }}</td>
          <td class="label">会员账号</td>
          <td class="value">{{ orderInfo.first_name || '--' }}</td>
        </tr>
        <tr>
          <td class="label">投注时间</td>
          <td class="value">{{ orderInfo.created_at || '--' }}</td>
          <td class="label">更新时间</td>
          <td class="value">{{ orderInfo.updated_at || '--' }}</td>
        </tr>
        </tbody>
      </table>

      <div class="section-title">投注与结算信息</div>
      <table class="detail-table">
        <tbody>
        <tr>
          <td class="label">玩法名称</td>
          <td class="value">{{ orderInfo.gameplay || '--' }}</td>
          <td class="label">赔率</td>
          <td class="value"><span class="odds-text">{{ orderInfo.odds || '--' }}</span></td>
        </tr>
        <tr>
          <td class="label">下注金额</td>
          <td class="value">￥{{ orderInfo.amount || '0.00' }}</td>
          <td class="label">中奖金额</td>
          <td class="value">
              <span :class="Number(orderInfo.win_amount) > 0 ? 'win-text' : ''">
                ￥{{ orderInfo.win_amount || '0.00' }}
              </span>
          </td>
        </tr>
        <tr>
          <td class="label">单笔盈亏</td>
          <td class="value" colspan="3">
              <span :class="getProfitClass(orderInfo.profit_and_loss)" class="text-bold">
                {{ Number(orderInfo.profit_and_loss) > 0 ? '+' : '' }}{{ orderInfo.profit_and_loss || '0.00' }}
              </span>
          </td>
        </tr>
        </tbody>
      </table>

      <div class="section-title">开奖结果</div>
      <table class="detail-table">
        <tbody>
        <tr>
          <td class="label" style="width: 15%;">开奖号码</td>
          <td class="value" colspan="3" style="width: 85%;">
            <div v-if="orderInfo.open_code" class="table-balls">
              <template v-if="[6, 7].includes(orderInfo.type) && getK3Dice(orderInfo.open_code).length === 3">
                <div
                  v-for="(num, idx) in getK3Dice(orderInfo.open_code)"
                  :key="idx"
                  :class="['dice-box', 'dice-' + num]"
                >
                  <span v-for="dot in Number(num)" :key="dot" :class="['dice-dot', 'dot-' + dot]"></span>
                </div>
              </template>
              <template v-else>
                <span
                  v-for="(num, idx) in orderInfo.open_code.split(',')"
                  :key="idx"
                  :class="getBallColorClass(num, orderInfo.type)"
                  class="ball-mini"
                >{{ num }}</span>
              </template>
            </div>
            <span v-else>--</span>
          </td>
        </tr>
        <tr>
          <td class="label">开奖结果</td>
          <td class="value" colspan="3">
            <div v-if="orderInfo.open_code_other" class="other-code-container">
                <span
                  v-for="(item, idx) in orderInfo.open_code_other.split(',')"
                  :key="'other-' + idx"
                  class="other-tag"
                >{{ item }}</span>
            </div>
            <span v-else>--</span>
          </td>
        </tr>
        </tbody>
      </table>

      <template v-if="orderInfo.remark">
        <div class="section-title">其他信息</div>
        <table class="detail-table">
          <tbody>
          <tr>
            <td class="label" style="width: 15%;">备注说明</td>
            <td class="value" colspan="3" style="width: 85%;">
              <span class="remark-text">{{ orderInfo.remark }}</span>
            </td>
          </tr>
          </tbody>
        </table>
      </template>

    </div>
  </div>
</template>

<script>
import { getjisuGameOrderInfo } from '@/api/data.js'

export default {
  name: 'JisuGameOrderDetail',
  data() {
    return {
      loading: false,
      orderId: null,
      orderInfo: {}
    }
  },
  created() {
    this.orderId = this.$route.query.id
    if (this.orderId) {
      this.getDetail()
    } else {
      this.$message.error('未获取到有效的订单ID')
      this.goBack()
    }
  },
  methods: {
    // 获取订单详情
    async getDetail() {
      this.loading = true
      try {
        const res = await getjisuGameOrderInfo({ id: this.orderId })
        if (res && res.code === 0) {
          this.orderInfo = res.data || {}
        } else if (res && res.data) {
          this.orderInfo = res.data.data || res.data || {}
        }
      } catch (error) {
        console.error('获取订单详情失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 返回上一页
    goBack() {
      this.$router.back()
    },

    // 动态获取开奖号码球样式 Class
    getBallColorClass(numStr, type) {
      if (!numStr) return '';
      if ([1, 3, 5].includes(type)) {
        // 1,3,5 类型 1-10 对应不同专属颜色
        return `color-${numStr}`;
      } else if ([2, 4].includes(type)) {
        // 2,4 类型 固定蓝色
        return 'wave-blue';
      }
      return '';
    },

    // 盈亏样式高亮
    getProfitClass(val) {
      const num = Number(val);
      if (num > 0) return 'profit-win'; // 盈利变绿
      if (num < 0) return 'profit-loss'; // 亏损变红
      return '';
    },

    // 解析快3开奖号码
    getK3Dice(openCode) {
      if (!openCode) return [];
      const str = String(openCode).replace(/,/g, '');
      if (str.length === 3) {
        return str.split('');
      }
      return [];
    },

    // 游戏彩种映射
    getGameTypeLabel(type) {
      const typeMap = {
        1: '极速赛车',
        2: '极速时时彩',
        3: '极速飞艇',
        4: 'SG时时彩',
        5: 'SG飞艇',
        6: '极速快3',
        7: 'SG快3'
      }
      return typeMap[type] || type || '--'
    },

    // 订单状态文案映射
    getStatusLabel(status) {
      const statusMap = {
        0: '待开奖',
        1: '未中奖',
        2: '已中奖',
        3: '已退款'
      }
      return statusMap[status] !== undefined ? statusMap[status] : '未知'
    },

    // 订单状态标签颜色映射
    getStatusTagType(status) {
      const typeMap = {
        0: 'warning', // 待开奖
        1: 'info',    // 未中奖
        2: 'success', // 已中奖
        3: ''         // 已退款
      }
      return typeMap[status] !== undefined ? typeMap[status] : 'info'
    }
  }
}
</script>

<style scoped>
/* ========== 页面基础布局 ========== */
.detail-app-container {
  background: #fff;
  padding: 20px;
  margin: 20px;
  box-sizing: border-box;
  border-radius: 6px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  min-height: calc(100vh - 100px);
}

.header-section {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 15px;
  margin-bottom: 25px;
}

.page-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-left: 15px;
}

.content-section {
  max-width: 1100px;
}

/* ========== 原生表格布局美化 ========== */
.section-title {
  font-size: 15px;
  font-weight: bold;
  color: #303133;
  margin: 25px 0 12px 0;
  padding-left: 10px;
  border-left: 4px solid #409EFF;
  line-height: 1.2;
}

.detail-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.detail-table td {
  border: 1px solid #ebeef5;
  padding: 12px 15px;
  line-height: 1.5;
}

.detail-table .label {
  background-color: #f5f7fa;
  width: 15%;
  color: #606266;
  text-align: right;
}

.detail-table .value {
  width: 35%;
  color: #303133;
  background-color: #fff;
}

/* ========== 文本与色彩高亮 ========== */
.text-bold {
  font-weight: bold;
}

.odds-text {
  color: #FF7600;
  font-weight: bold;
}

.win-text {
  color: #67C23A;
  font-weight: bold;
}

.profit-win {
  color: #67C23A;
}

.profit-loss {
  color: #F56C6C;
}

.remark-text {
  color: #E6A23C;
}

/* ========== 开奖号码球样式 ========== */
.table-balls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.ball-mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-weight: 600;
  font-size: 13px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 2、4 类型的固定蓝色 */
.wave-blue.ball-mini {
  background: linear-gradient(135deg, #42a5f5, #1e88e5);
  border-color: #1e88e5;
  color: #fff;
}

/* 1、3、5 类型的十种专属颜色 */
.color-1.ball-mini { background: #E6DE00; border-color: #E6DE00; color: #fff; text-shadow: 0 1px 1px rgba(0,0,0,0.3); }
.color-2.ball-mini { background: #0092FF; border-color: #0092FF; color: #fff; }
.color-3.ball-mini { background: #4D4D4D; border-color: #4D4D4D; color: #fff; }
.color-4.ball-mini { background: #FF7600; border-color: #FF7600; color: #fff; }
.color-5.ball-mini { background: #17E2E5; border-color: #17E2E5; color: #fff; }
.color-6.ball-mini { background: #5234FF; border-color: #5234FF; color: #fff; }
.color-7.ball-mini { background: #BFBFBF; border-color: #BFBFBF; color: #fff; }
.color-8.ball-mini { background: #FF2600; border-color: #FF2600; color: #fff; }
.color-9.ball-mini { background: #780B00; border-color: #780B00; color: #fff; }
.color-10.ball-mini { background: #07BF00; border-color: #07BF00; color: #fff; }

/* ========== 快3 骰子样式 ========== */
.dice-box {
  position: relative;
  width: 28px;
  height: 28px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  box-shadow: inset 0 0 2px rgba(0,0,0,0.1);
}
.dice-dot {
  position: absolute;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.dice-1 .dice-dot, .dice-4 .dice-dot {
  background-color: #F56C6C;
}
.dice-2 .dice-dot, .dice-3 .dice-dot, .dice-5 .dice-dot, .dice-6 .dice-dot {
  background-color: #303133;
}

.dice-1 .dot-1 { top: 50%; left: 50%; width: 10px; height: 10px; }
.dice-2 .dot-1 { top: 25%; left: 25%; width: 6px; height: 6px; }
.dice-2 .dot-2 { top: 75%; left: 75%; width: 6px; height: 6px; }
.dice-3 .dot-1 { top: 20%; left: 20%; width: 6px; height: 6px; }
.dice-3 .dot-2 { top: 50%; left: 50%; width: 6px; height: 6px; }
.dice-3 .dot-3 { top: 80%; left: 80%; width: 6px; height: 6px; }
.dice-4 .dot-1 { top: 25%; left: 25%; width: 6px; height: 6px; }
.dice-4 .dot-2 { top: 25%; left: 75%; width: 6px; height: 6px; }
.dice-4 .dot-3 { top: 75%; left: 25%; width: 6px; height: 6px; }
.dice-4 .dot-4 { top: 75%; left: 75%; width: 6px; height: 6px; }
.dice-5 .dot-1 { top: 20%; left: 20%; width: 6px; height: 6px; }
.dice-5 .dot-2 { top: 20%; left: 80%; width: 6px; height: 6px; }
.dice-5 .dot-3 { top: 50%; left: 50%; width: 6px; height: 6px; }
.dice-5 .dot-4 { top: 80%; left: 20%; width: 6px; height: 6px; }
.dice-5 .dot-5 { top: 80%; left: 80%; width: 6px; height: 6px; }
.dice-6 .dot-1 { top: 20%; left: 30%; width: 5px; height: 5px; }
.dice-6 .dot-2 { top: 50%; left: 30%; width: 5px; height: 5px; }
.dice-6 .dot-3 { top: 80%; left: 30%; width: 5px; height: 5px; }
.dice-6 .dot-4 { top: 20%; left: 70%; width: 5px; height: 5px; }
.dice-6 .dot-5 { top: 50%; left: 70%; width: 5px; height: 5px; }
.dice-6 .dot-6 { top: 80%; left: 70%; width: 5px; height: 5px; }

/* ========== 补充特征标签样式 ========== */
.other-code-container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.other-tag {
  display: inline-block;
  padding: 2px 8px;
  background-color: #f4f4f5;
  border: 1px solid #e9e9eb;
  border-radius: 4px;
  font-size: 12px;
  color: #909399;
}
</style>
