<script setup lang="ts">
import { ref } from "vue";
import { useSync } 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 Sync from "~icons/ep/refresh-right";

defineOptions({
  name: "DataSync"
});

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

const {
  form,
  loading,
  syncing,
  columns,
  dataList,
  statistics,
  pagination,
  onSearch,
  resetForm,
  handleSyncDevices,
  handleSyncProducts,
  handleViewLogs,
  handleSizeChange,
  handleCurrentChange
} = useSync();
</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="syncType">
        <el-select
          v-model="form.syncType"
          placeholder="请选择"
          clearable
          class="w-[180px]!"
        >
          <el-option label="设备同步" value="device" />
          <el-option label="商品同步" value="product" />
        </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="1" />
          <el-option label="成功" :value="2" />
          <el-option label="失败" :value="3" />
        </el-select>
      </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.totalSync }}</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.todaySync }}</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.successRate }}%</div>
          <div class="text-gray-500">成功率</div>
        </div>
      </el-card>
      <el-card class="flex-1">
        <div class="text-center">
          <div class="text-sm font-bold text-info">{{ statistics.lastSyncTime || '-' }}</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(Sync)"
          :loading="syncing"
          @click="handleSyncDevices"
        >
          同步设备
        </el-button>
        <el-button
          type="primary"
          :icon="useRenderIcon(Sync)"
          :loading="syncing"
          @click="handleSyncProducts"
        >
          同步商品
        </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="handleViewLogs(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>
