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

import Delete from "~icons/ep/delete";
import EditPen from "~icons/ep/edit-pen";
import Refresh from "~icons/ep/refresh";
import AddFill from "~icons/ri/add-circle-line";
import View from "~icons/ep/view";
import RefreshRight from "~icons/ep/refresh-right";

defineOptions({
  name: "NewProductApply"
});

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

const {
  form,
  loading,
  columns,
  dataList,
  statistics,
  pagination,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleRefreshStatus,
  handleSizeChange,
  handleCurrentChange
} = useNewProductApply(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="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          clearable
          class="w-[180px]!"
        >
          <el-option label="待提交" :value="0" />
          <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.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-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-success">{{ statistics.approved }}</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-danger">{{ statistics.rejected }}</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(AddFill)"
          @click="openDialog()"
        >
          新增申请
        </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
              v-if="row.status === 1"
              class="reset-margin"
              link
              type="success"
              :size="size"
              :icon="useRenderIcon(RefreshRight)"
              @click="handleRefreshStatus(row)"
            >
              刷新
            </el-button>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="openDialog('查看', row)"
            >
              查看
            </el-button>
            <el-button
              v-if="row.status === 0"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('编辑', row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              v-if="row.status === 0"
              title="是否确认删除?"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </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>

<style lang="scss">
.new-apply-form {
  max-height: 560px;
  overflow-y: auto;
  padding-right: 8px;

  .form-section-title {
    font-size: 14px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 14px;
    padding-left: 8px;
    border-left: 3px solid #409eff;
  }

  .image-upload-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
  }

  .image-upload-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;

    .upload-card {
      width: 100px;
      height: 100px;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      background: #fafafa;
      transition: border-color .2s;

      &:hover {
        border-color: #409eff;
      }
    }

    .upload-card-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
    }

    .upload-card-remove {
      position: absolute;
      top: -6px;
      right: -6px;
      width: 18px;
      height: 18px;
      background: #f56c6c;
      color: #fff;
      border-radius: 50%;
      font-size: 14px;
      line-height: 18px;
      text-align: center;
      cursor: pointer;
      display: none;
    }

    .upload-card:hover .upload-card-remove {
      display: block;
    }

    .upload-card-loading {
      color: #909399;
      font-size: 12px;
    }

    .upload-label {
      font-size: 12px;
      color: #909399;
      text-align: center;
      white-space: nowrap;
    }
  }
}
</style>
