<script setup lang="ts">
import { reactive, onMounted, ref, nextTick } from "vue";
import { getParkingCouponRecordPage } from "@/api/parking-coupon";
import { getStationList } from "@/api/station";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

defineOptions({ name: "AdminParkingCouponRecord" });

const queryRef = ref();

const state = reactive({
  formQuery: {
    stationId: "",
    status: "" as any
  },
  stationOptions: [] as Array<{ stationId: string; stationName: string }>,
  pageQuery: {
    pageNum: 1,
    pageSize: 10,
    total: 0
  },
  tableData: {
    height: 500,
    data: [] as Array<any>,
    loading: false,
    columns: [
      { label: "券码", prop: "code", width: 280, fixed: "left" },
      { label: "用户ID", prop: "userId", width: 100 },
      { label: "站点ID", prop: "stationId", width: 120 },
      { label: "关联订单", prop: "orderId", width: 200 },
      { label: "状态", prop: "status", width: 100 },
      { label: "发放时间", prop: "createTime", width: 180 },
      { label: "使用时间", prop: "usedTime", width: 180 },
      { label: "过期时间", prop: "expireTime", width: 180 }
    ]
  }
});

onMounted(() => {
  loadStations();
  loadData();
  nextTick(() => {
    const bodyHeight = document.body.clientHeight;
    const queryHeight = queryRef.value?.$el?.clientHeight || 0;
    state.tableData.height = bodyHeight - queryHeight - 260;
  });
});

const loadStations = () => {
  getStationList({ pageNum: 1, pageSize: 200 }).then((res: any) => {
    state.stationOptions = res?.list || res?.records || [];
  }).catch(() => {});
};

const loadData = (refresh: boolean = false) => {
  if (refresh) {
    state.pageQuery.pageNum = 1;
  }
  state.tableData.loading = true;
  const params: any = {
    pageNum: state.pageQuery.pageNum,
    pageSize: state.pageQuery.pageSize
  };
  if (state.formQuery.stationId) params.stationId = state.formQuery.stationId;
  if (state.formQuery.status !== "" && state.formQuery.status !== null && state.formQuery.status !== undefined) {
    params.status = state.formQuery.status;
  }

  getParkingCouponRecordPage(params)
    .then((res: any) => {
      state.tableData.data = res?.list || res?.records || [];
      state.pageQuery.total = res?.total || 0;
    })
    .catch(() => {
      state.tableData.data = [];
    })
    .finally(() => {
      state.tableData.loading = false;
    });
};

const handleSizeChange = (size: number) => {
  state.pageQuery.pageSize = size;
  loadData(true);
};

const handleCurrentChange = (page: number) => {
  state.pageQuery.pageNum = page;
  loadData();
};

const handleSearch = () => loadData(true);

const handleReset = () => {
  state.formQuery = { stationId: "", status: "" };
  loadData(true);
};
</script>

<template>
  <div class="page-container">
    <el-card shadow="hover">
      <template #header>
        <span class="card-header">停车券发放记录</span>
      </template>
      <el-form ref="queryRef" :model="state.formQuery" inline class="search-form">
        <el-form-item label="站点">
          <el-select
            v-model="state.formQuery.stationId"
            placeholder="选择站点"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="item in state.stationOptions"
              :key="item.stationId"
              :label="item.stationName"
              :value="item.stationId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="state.formQuery.status"
            placeholder="状态"
            clearable
            style="width: 150px"
          >
            <el-option label="未使用" :value="0" />
            <el-option label="已使用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/search-line')"
            @click="handleSearch"
          >
            查询
          </el-button>
          <el-button
            :icon="useRenderIcon('ri/refresh-line')"
            @click="handleReset"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="state.tableData.loading"
        :data="state.tableData.data"
        :height="state.tableData.height"
        border
        stripe
      >
        <template #empty>
          <el-empty description="暂无数据" />
        </template>
        <el-table-column
          v-for="col in state.tableData.columns"
          :key="col.prop"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
          :fixed="(col as any).fixed"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <template v-if="col.prop === 'status'">
              <el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
                {{ row.status === 1 ? "已使用" : "未使用" }}
              </el-tag>
            </template>
            <template v-else>
              {{ row[col.prop] || "-" }}
            </template>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="state.pageQuery.pageNum"
          v-model:page-size="state.pageQuery.pageSize"
          :total="state.pageQuery.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.page-container {
  padding: 20px;
}

.card-header { font-size: 16px; font-weight: 600; }

.search-form {
  margin-bottom: 16px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>
