<script setup lang="ts">
import { ref } from "vue";
import { useInventoryLogs } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Refresh from "~icons/ep/refresh";

defineOptions({
  name: "InventoryLogs"
});

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

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  deviceOptions,
  productOptions,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange
} = useInventoryLogs();
</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="deviceId">
        <el-select
          v-model="form.deviceId"
          placeholder="请选择设备"
          clearable
          filterable
          class="w-[220px]!"
        >
          <el-option
            v-for="item in deviceOptions"
            :key="item.deviceId"
            :label="item.shopName ? `${item.deviceId} — ${item.shopName}` : item.deviceId"
            :value="item.deviceId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品：" prop="productId">
        <el-select
          v-model="form.productId"
          placeholder="请选择商品"
          clearable
          filterable
          class="w-[200px]!"
        >
          <el-option
            v-for="item in productOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="变动类型：" prop="changeType">
        <el-select
          v-model="form.changeType"
          placeholder="全部"
          clearable
          class="w-[130px]!"
        >
          <el-option label="上货增加" :value="1" />
          <el-option label="销售减少" :value="2" />
          <el-option label="调整增加" :value="3" />
          <el-option label="调整减少" :value="4" />
          <el-option label="盘点调整" :value="5" />
        </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>

    <PureTableBar
      title="库存变动日志"
      :columns="columns"
      @refresh="onSearch"
    >
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :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>
    </PureTableBar>
  </div>
</template>

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