<template>
  <div class="digital-order-component">

    <div class="filter-body">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item :label="t('注单状态')">
          <el-select v-model="queryParams.lottery_status" :placeholder="t('全部')" style="width: 140px;" clearable @change="handleQuery">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item :label="t('日期区间')">
          <el-date-picker
              v-model="dateRange"
              type="daterange"
              :start-placeholder="t('开始日期')"
              :end-placeholder="t('结束日期')"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              @change="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon class="btn-icon"><Search /></el-icon>{{ t('立即查询') }}
          </el-button>
          <el-button @click="handleReset">
            <el-icon class="btn-icon"><RefreshRight /></el-icon>{{ t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
        :data="tableData"
        style="width: 100%;"
        height="100%"
        :empty-text="t('暂无投注记录')"
    >
      <el-table-column :label="t('注单号')" min-width="210">
        <template #default="scope">
          <div class="order-no-box">
            <span class="issue-no">{{ scope.row.ordernum }}</span>
            <el-icon class="copy-icon" @click="copyText(scope.row.ordernum)"><DocumentCopy /></el-icon>
          </div>
        </template>
      </el-table-column>

      <el-table-column :label="t('彩种/期号')" min-width="140">
        <template #default="scope">
          <span class="type-name">{{ scope.row.game }}</span>
          <span class="type-sub"> - 第{{ scope.row.issue }}期</span>
        </template>
      </el-table-column>

      <el-table-column :label="t('投注内容')" min-width="160">
        <template #default="scope">
          <span class="type-name">[{{ scope.row.gameplay }}]</span>
          <span class="bet-keywords ml-2">{{ scope.row.number }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="t('赔率')" min-width="100" align="right">
        <template #default="scope"><span class="odds-val">@{{ scope.row.odds }}</span></template>
      </el-table-column>

      <el-table-column :label="t('投注本金')" min-width="120" align="right">
        <template #default="scope">
          <span class="val-text">{{ parseFloat(scope.row.amount || 0).toFixed(2) }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="t('盈亏')" min-width="120" align="right">
        <template #default="scope">
          <span v-if="scope.row.lottery_status === 0" class="val-text">--</span>
          <span v-else class="profit-val" :class="getProfitClass(scope.row)">
            {{ getProfitText(scope.row) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column :label="t('状态')" width="100" align="center">
        <template #default="scope">
          <el-tag :type="getStatusTag(scope.row).type" effect="dark">
            {{ getStatusTag(scope.row).text }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="created_at" :label="t('投注时间')" min-width="170" align="right" fixed="right" />

<!--      <el-table-column :label="t('操作')" width="90" align="center" fixed="right">-->
<!--        <template #default="scope"><el-button link type="primary" @click="handleGameDetail(scope.row)">{{ t('详情') }}</el-button></template>-->
<!--      </el-table-column>-->
    </el-table>

    <div class="pagination-wrapper" v-if="total > 0">
      <el-pagination
          v-model:current-page="queryParams.page"
          v-model:page-size="queryParams.limit"
          :page-sizes="[10, 20, 50]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { ElMessage } from 'element-plus';
import { Search, RefreshRight, DocumentCopy } from '@element-plus/icons-vue';

// 引入获取购彩记录的真实接口
import { getDigitalOrderList } from '@/request/api.js';
import router from "@/router/index.js";

const { t } = useI18n();

const tableData = ref([]);
const total = ref(0);
const dateRange = ref(null);

const queryParams = reactive({
  page: 1,
  limit: 20,
  lottery_status: '',
  start_time: '',
  end_time: ''
});

const statusOptions = computed(() => [
  { label: t('全部'), value: '' },
  { label: t('未开奖'), value: 0 },
  { label: t('已中奖'), value: 1 },
  { label: t('未中奖'), value: 2 },
  { label: t('合局'), value: 3 }
]);

onMounted(() => {
  fetchData();
});

const fetchData = async () => {
  if (dateRange.value && dateRange.value.length === 2) {
    queryParams.start_time = dateRange.value[0];
    queryParams.end_time = dateRange.value[1];
  } else {
    queryParams.start_time = '';
    queryParams.end_time = '';
  }

  try {
    const res = await getDigitalOrderList(queryParams);
    if (res.code === 1 && res.data) {
      tableData.value = res.data.list || [];
      total.value = res.data.count || res.data.list.length || 0;
    } else {
      tableData.value = [];
      total.value = 0;
    }
  } catch (err) {
    tableData.value = [];
    total.value = 0;
  }
};

const handleQuery = () => {
  queryParams.page = 1;
  fetchData();
};

const handleReset = () => {
  queryParams.lottery_status = '';
  dateRange.value = null;
  queryParams.start_time = '';
  queryParams.end_time = '';
  handleQuery();
};

const handleSizeChange = (val) => {
  queryParams.limit = val;
  fetchData();
};

const handleCurrentChange = (val) => {
  queryParams.page = val;
  fetchData();
};

// --- 表格渲染辅助方法 ---
const getStatusTag = (row) => {
  const status = Number(row.lottery_status);
  if (status === 0) return { text: t('待开奖'), type: 'warning' };
  if (status === 1) return { text: t('未中奖'), type: 'danger' };
  if (status === 2) return { text: t('已中奖'), type: 'success' };
  if (status === 3) return { text: t('合局'), type: 'info' };
  if (status === 3) return { text: t('购物车'), type: 'info' };
  return { text: '-', type: 'info' };
};

const getProfitClass = (row) => {
  const status = Number(row.lottery_status);
  if (status === 1) return 'text-danger';
  if (status === 2) return 'text-success';
  return 'text-gray';
};

const getProfitText = (row) => {
  // const status = Number(row.lottery_status);
  // if (status === 1) return `+${parseFloat(row.win_amount || 0).toFixed(2)}`;
  // if (status === 2) return `-${parseFloat(row.amount || 0).toFixed(2)}`;
  // if (status === 3) return '0.00';
  // return '--';
   return row.profit_and_loss || '--';
};

const copyText = async (text) => {
  try {
    await navigator.clipboard.writeText(text);
    ElMessage.success(t('复制成功'));
  } catch (err) {
    const input = document.createElement('input');
    input.value = text;
    document.body.appendChild(input);
    input.select();
    document.execCommand('copy');
    document.body.removeChild(input);
    ElMessage.success(t('复制成功'));
  }
};

const handleGameDetail = (row) => {
  router.push({name: 'DigitalOrderDetail',query: { id: row.id }});
}
</script>

<style scoped lang="scss">
$main-color: #f8b932;

.digital-order-component {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  overflow: hidden;

  /* === 筛选表单 深度复刻外层布局 === */
  .filter-body {
    margin-bottom: 8px;

    .search-form { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 0; }

    :deep(.el-form-item) { margin-right: 6px; margin-bottom: 0; display: flex; align-items: center; }
    //:deep(.el-form-item__label) { line-height: 26px !important; height: 26px !important; padding-right: 4px; font-size: 12px; margin-bottom: 0; color: var(--el-text-color-regular); }
    //
    //:deep(.el-select .el-select__wrapper) { min-height: 26px !important; height: 26px !important; padding: 0 6px; font-size: 12px; }
    //:deep(.el-date-editor) { height: 26px; padding: 0 6px; max-width: 210px; width: 100%; }
    //:deep(.el-range-separator) { line-height: 24px; font-size: 12px; padding: 0 2px; }
    //:deep(.el-range-input) { font-size: 12px; }
    //:deep(.el-input__icon) { line-height: 26px; }

    //.el-button { height: 26px; padding: 4px 8px; font-size: 12px; border-radius: 4px; margin-left: 0; margin-right: 4px; }
    //.btn-icon { margin-right: 2px; font-size: 12px; }
  }

  /* === 业务文字与颜色样式同步 === */
  .order-no-box {
    display: flex; align-items: center; gap: 4px;
    .copy-icon { cursor: pointer; color: var(--el-text-color-secondary); font-size: 12px; &:hover { color: $main-color; } }
  }

  .issue-no {  color: var(--el-text-color-regular); font-size: 12px; }
  .bet-keywords { font-weight: bold; color: $main-color; font-size: 12px; }
  .type-name { font-weight: bold; color: var(--el-text-color-primary); font-size: 12px; }
  .type-sub { color: var(--el-text-color-regular); font-size: 12px; }
  .odds-val { font-weight: bold; color: $main-color; font-size: 13px; }
  .val-text { font-size: 13px; font-weight: bold; color: var(--el-text-color-primary);  }
  .profit-val { font-size: 13px; font-weight: bold;  }

  .ml-2 { margin-left: 4px; }

  //:deep(.el-tag) { height: 18px; padding: 0 4px; line-height: 16px; font-size: 10px; border-radius: 2px; }

  .text-success { color: var(--el-color-success) !important; }
  .text-danger { color: var(--el-color-danger) !important; }
  .text-gray { color: var(--el-text-color-placeholder) !important; }

  /* === 分页器 === */
  .pagination-wrapper {
    margin-top: 8px; display: flex; justify-content: flex-end; flex-shrink: 0;

    //:deep(.el-pagination) {
    //  --el-pagination-button-height: 24px;
    //  --el-pagination-font-size: 12px;
    //  --el-pagination-button-width: 24px;
    //  display: flex; align-items: center; flex-wrap: wrap; justify-content: center; gap: 4px;
    //
    //  .el-pagination__total { margin-right: 4px; font-size: 12px; height: 24px; line-height: 24px; }
    //  .el-pagination__sizes {
    //    margin-right: 4px;
    //    .el-select .el-select__wrapper { min-height: 24px !important; height: 24px !important; padding: 0 6px; font-size: 12px; line-height: 24px; }
    //  }
    //  .btn-prev, .btn-next { min-width: 24px; height: 24px; padding: 0 4px; border-radius: 4px; }
    //  .el-pager li { min-width: 24px; height: 24px; line-height: 24px; font-size: 12px; margin: 0 2px; border-radius: 4px; }
    //
    //  .el-pagination__jump {
    //    margin-left: 4px; font-size: 12px; height: 24px; line-height: 24px;
    //    .el-input { width: 36px; height: 24px; .el-input__wrapper { height: 24px; padding: 0 2px; .el-input__inner { font-size: 12px; text-align: center; } } }
    //  }
    //}
  }
}
</style>
