<script setup lang="ts">
import { ref } from "vue";
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 loading = ref(false);
const dataList = ref([]);
const pagination = ref({
  total: 0,
  pageSize: 10,
  currentPage: 1
});

const form = ref({
  deviceId: "",
  productId: ""
});

const columns = [
  {
    label: "设备名称",
    prop: "deviceName",
    minWidth: 120
  },
  {
    label: "商品名称",
    prop: "productName",
    minWidth: 120
  },
  {
    label: "变动类型",
    prop: "changeType",
    minWidth: 100
  },
  {
    label: "变动数量",
    prop: "quantity",
    minWidth: 100
  },
  {
    label: "变动前库存",
    prop: "beforeStock",
    minWidth: 100
  },
  {
    label: "变动后库存",
    prop: "afterStock",
    minWidth: 100
  },
  {
    label: "操作人",
    prop: "operator",
    minWidth: 100
  },
  {
    label: "变动时间",
    prop: "createTime",
    minWidth: 160
  }
];

const onSearch = () => {
  loading.value = true;
  setTimeout(() => {
    dataList.value = [];
    loading.value = false;
  }, 500);
};

const resetForm = (formEl: any) => {
  if (!formEl) return;
  formEl.resetFields();
  onSearch();
};

const handleSizeChange = (val: number) => {
  pagination.value.pageSize = val;
  onSearch();
};

const handleCurrentChange = (val: number) => {
  pagination.value.currentPage = val;
  onSearch();
};

onSearch();
</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-[180px]!"
        >
        </el-select>
      </el-form-item>
      <el-form-item label="商品：" prop="productId">
        <el-select
          v-model="form.productId"
          placeholder="请选择商品"
          clearable
          filterable
          class="w-[180px]!"
        >
        </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, dynamic_columns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamic_columns"
          :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"
        >
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

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