<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { getSmartCabinetList, type SmartCabinetDTO } from "@/api/cabinet/smart-cabinet";
import { type PaginationProps } from "@pureadmin/table";
import { CommonUtils } from "@/utils/common";
import { useRouter } from "vue-router";
import { CabinetImgMap } from "@/utils/cabinetImgMap";

import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import View from "@iconify-icons/ep/view";
import AddFill from "@iconify-icons/ri/add-circle-line";
import SmartCabinetCardFormModal from "./smart-cabinet-card-form-modal.vue";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
const { VITE_PUBLIC_IMG_PATH: IMG_PATH } = import.meta.env;

defineOptions({
  name: "SmartCabinetCard"
});

const router = useRouter();
const formRef = ref();
const modalVisible = ref(false);
const searchFormParams = ref({
  cabinetName: "",
  cabinetType: null
});

const pageLoading = ref(false);
const dataList = ref<SmartCabinetDTO[]>([]);
const pagination = ref<PaginationProps>({
  total: 0,
  pageSize: 12,
  currentPage: 1,
  background: true
});

async function onSearch() {
  pagination.value.currentPage = 1;
  getList();
}

async function getList() {
  try {
    pageLoading.value = true;
    const { data } = await getSmartCabinetList({
      ...searchFormParams.value,
      pageSize: pagination.value.pageSize,
      pageNum: pagination.value.currentPage
    });
    dataList.value = data.rows;
    pagination.value.total = data.total;
  } finally {
    pageLoading.value = false;
  }
}

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

const handleViewDetail = (row: SmartCabinetDTO) => {
  // 保存信息到标签页
  useMultiTagsStoreHook().handleTags("push", {
    path: `/cabinet/card/detail`,
    name: "smartCabinetCardDetail",
    query: { id: row.cabinetId },
    meta: {
      title: `${row.cabinetName}`,
      dynamicLevel: 3
    }
  });
  router.push({
    path: '/cabinet/card/detail',
    query: {
      id: row.cabinetId
    }
  });
};

onMounted(() => {
  getList();
});
</script>

<template>
  <div class="main">
    <div class="float-right w-full">
      <el-form ref="formRef" :inline="true" :model="searchFormParams"
        class="search-form bg-bg_color flex w-[99/100] pl-[22px] pt-[12px]">
        <el-form-item label="" prop="cabinetName">
          <el-input v-model="searchFormParams.cabinetName" placeholder="请输入柜体名称" clearable class="!w-[200px]" />
        </el-form-item>
        <el-form-item label="" prop="cabinetType">
          <el-select v-model="searchFormParams.cabinetType" placeholder="请选择柜体类型" clearable class="!w-[180px]">
            <el-option label="主柜" :value="0" />
            <el-option label="副柜" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="useRenderIcon(Search)" :loading="pageLoading" @click="onSearch">
            搜索
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
        <el-form-item class="space-item">
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="modalVisible = true"
            style="margin-right: 10px;">
            新增设备
          </el-button>
        </el-form-item>
      </el-form>

      <div class="grid-container">
        <el-row :gutter="12">
          <el-col v-for="(item, index) in dataList" :key="item.cabinetId" :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
            <el-card class="cabinet-card" :body-style="{ padding: '8px 20px' }">
              <div class="card-content">
                <img
                  :src="`${IMG_PATH}img/cabinet/${CabinetImgMap[item.templateNo]?.img || 'default.jpg'}`"
                  class="cabinet-image" />
                <el-descriptions class="cabinet-info" :column="2">
                  <el-descriptions-item class="name" :span="2">柜体名称：{{ item.cabinetName }}
                  </el-descriptions-item>
                  <!-- <div class="type">柜体类型：{{ item.cabinetType === 0 ? '主柜' : '副柜' }}</div> -->
                  <el-descriptions-item class="template">模板：{{ CabinetImgMap[item.templateNo]?.name || '-' }}
                  </el-descriptions-item>
                  <el-descriptions-item class="shop">商店：{{ item.shopName || '-' }}
                  </el-descriptions-item>
                </el-descriptions>
              </div>
              <el-divider class="divider" />
              <el-button class="detail-btn" :icon="useRenderIcon(View)" @click="handleViewDetail(item)" />
            </el-card>
          </el-col>
        </el-row>
        <div class="pagination-wrapper">
          <el-pagination background layout="prev, pager, next" :page-size="pagination.pageSize"
            :total="pagination.total" v-model:current-page="pagination.currentPage" @current-change="getList"
            @size-change="getList" />
        </div>
      </div>
    </div>
    <smart-cabinet-card-form-modal v-model:visible="modalVisible" @refresh="getList" />
  </div>
</template>

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

:deep(.el-descriptions__cell) {
  padding-bottom: 1px !important;
}

.cabinet-card {
  margin-bottom: 12px;
  min-height: 210px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .cabinet-image {
    width: 100%;
    height: 200px;
    object-fit: contain;
    border-radius: 4px;
    margin-bottom: 10px;
  }

  .card-content {
    flex: 1;
    margin: 15px 0px;

    .cabinet-info {
      text-align: left;

      .name,
      .type,
      .shop,
      .location,
      .template {
        width: 100%;
        font-size: 14px;
        color: #606266;
        margin-bottom: 6px;
        line-height: 1;
        text-align: left;
      }

      .name {
        width: 100%;
        font-weight: 500;
        color: #303133;
      }
    }
  }

  .divider {
    margin: 5px 0px;
  }

  .detail-btn {
    width: 100%;
    border: 0;
    margin-top: auto;
    padding: 8px 0;
  }
}

.grid-container {
  margin: 12px 0;
  position: relative;

  .el-row {
    margin-bottom: -20px;
  }
}

.pagination-wrapper {
  position: relative;
  background: var(--el-bg-color);
  padding: 8px 12px;
  margin-top: 20px;
  text-align: center;

  :deep(.el-pagination) {
    margin: 0;
    padding: 4px 0;
  }
}

.space-item {
  flex: 1;
  width: 100%;
}
</style>