<style scoped lang="scss">
.system-container {

  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}

.page-pager {
  background-color: var(--el-color-white);
  height: 24px;
}
</style>
<template>
  <div class="system-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <el-form
          :model="state.formQuery"
          ref="queryRef"
          size="default" label-width="0px" class="mt5 mb5">
        <ext-select
            v-model="state.formQuery.stationId"
            placeholder="站点"
            url="washStation/list"
            url-method="post"
            label-key="stationName"
            value-key="stationId"
            data-key="list"
            clearable
            class="wd200 ml10"/>

        <el-select
            v-model="state.formQuery.status"
            placeholder="状态"
            clearable
            class="wd150 ml10">
          <el-option label="未使用" :value="0"/>
          <el-option label="已使用" :value="1"/>
        </el-select>

        <el-button class="ml10" plain size="default" type="success" @click="loadData(true)">
          <SvgIcon name="ele-Search"/>
          查询
        </el-button>
      </el-form>

      <el-table
          border
          stripe="stripe"
          :height="state.tableData.height"
          :data="state.tableData.data"
          v-loading="state.tableData.loading">
        <template #empty>
          <el-empty></el-empty>
        </template>
        <el-table-column
            v-for="field in state.columns"
            :key="field.prop"
            :label="field.label"
            :column-key="field.prop"
            :width="field.width"
            :min-width="field.minWidth"
            :fixed="field.fixed"
            :show-overflow-tooltip="!field.fixed&&field.width>150">

          <template #default="{row}">
            <template v-if="field.prop === 'status'">
              <el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
                {{ row.status === 1 ? '已使用' : '未使用' }}
              </el-tag>
            </template>
            <template v-else-if="['createTime','usedTime','expireTime'].includes(field.prop)">
              {{ row[field.prop] ? u.fmt.fmtDateTime(row[field.prop]) : '-' }}
            </template>
            <template v-else>
              <div>{{ row[field.prop] || '-' }}</div>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <ext-page class="page-pager" v-model:value="state.pageQuery" @change="loadData(false)"/>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="adminParkingCouponRecord">
import {reactive, onMounted, ref, nextTick} from 'vue';
import {$get} from "/@/utils/request";
import u from '/@/utils/u'

import ExtPage from '/@/components/form/ExtPage.vue'
import ExtSelect from "/@/components/form/ExtSelect.vue";

const queryRef = ref();

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

onMounted(() => {
  loadData();

  nextTick(() => {
    let bodyHeight = document.body.clientHeight;
    let queryHeight = queryRef.value.$el.clientHeight;
    state.tableData.height = bodyHeight - queryHeight - 320
  })
});

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;
  }

  $get(`/parking-coupon-record/page`, params).then((res: any) => {
    let {list, total} = res;
    state.tableData.data = list || [];
    state.pageQuery.total = total || 0;
    state.tableData.loading = false;
  }).catch(e => {
    console.error(e)
    state.tableData.loading = false;
  })
};
</script>
