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

defineOptions({
  name: "MasterLibrary"
});

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

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  deviceAdaptOptions,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleAddToMerchant
} = useMasterLibrary(tableRef);
</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="productName">
        <el-input
          v-model="form.productName"
          placeholder="请输入商品名称"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="条码：" prop="barcode">
        <el-input
          v-model="form.barcode"
          placeholder="请输入条码"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="商品编码：" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入商品编码"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="适用设备：" prop="deviceAdapt">
        <el-select
          v-model="form.deviceAdapt"
          placeholder="请选择适用设备"
          clearable
          class="w-[180px]!"
        >
          <el-option
            v-for="item in deviceAdaptOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </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 #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="handleAddToMerchant"
        >
          添加到商家商品库
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="code"
          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"
          @selection-change="handleSelectionChange"
        >
        </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>
