<script setup lang="ts">
import { ref } from "vue";
import { useCheckin } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Refresh from "~icons/ep/refresh";
import View from "~icons/ep/view";
import Download from "~icons/ep/download";

defineOptions({
  name: "CheckinManage"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  statistics,
  pagination,
  onSearch,
  resetForm,
  handleViewDetail,
  handleExport,
  handleSizeChange,
  handleCurrentChange
} = useCheckin();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="签到类型：" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择"
          clearable
          class="w-[180px]!"
        >
          <el-option label="理货盘点签到" value="inventory_tally" />
          <el-option label="补货确认签到" value="delivery_replenish" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择"
          clearable
          class="w-[180px]!"
        >
          <el-option label="待同步" value="pending" />
          <el-option label="已同步" value="synced" />
          <el-option label="同步失败" value="failed" />
        </el-select>
      </el-form-item>
      <el-form-item label="签到日期：">
        <el-date-picker
          v-model="form.startDate"
          type="date"
          placeholder="开始日期"
          value-format="YYYY-MM-DD"
          class="w-[150px]!"
        />
        <span class="mx-2">至</span>
        <el-date-picker
          v-model="form.endDate"
          type="date"
          placeholder="结束日期"
          value-format="YYYY-MM-DD"
          class="w-[150px]!"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="flex gap-4 mb-4">
      <el-card class="flex-1">
        <div class="text-center">
          <div class="text-2xl font-bold text-primary">{{ statistics.total }}</div>
          <div class="text-gray-500">总签到数</div>
        </div>
      </el-card>
      <el-card class="flex-1">
        <div class="text-center">
          <div class="text-2xl font-bold text-success">{{ statistics.today }}</div>
          <div class="text-gray-500">今日签到</div>
        </div>
      </el-card>
      <el-card class="flex-1">
        <div class="text-center">
          <div class="text-2xl font-bold text-warning">{{ statistics.pending }}</div>
          <div class="text-gray-500">待同步</div>
        </div>
      </el-card>
      <el-card class="flex-1">
        <div class="text-center">
          <div class="text-2xl font-bold text-info">{{ statistics.synced }}</div>
          <div class="text-gray-500">已同步</div>
        </div>
      </el-card>
    </div>

    <PureTableBar
      title="签到记录"
      :columns="columns"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(Download)"
          @click="handleExport"
        >
          导出
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="handleViewDetail(row)"
            >
              详情
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
