<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { getSmartCabinetDetailApi, type SmartCabinetDTO } from "@/api/cabinet/smart-cabinet";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { CabinetImgMap } from "@/utils/cabinetImgMap";
import GatewayConfigModal from "@/views/cabinet/smart-cabinet/GatewayConfigModal.vue";
import ShopConfigModal from "@/views/cabinet/smart-cabinet/ShopConfigModal.vue";
import MainCabinetConfigModal from "@/views/cabinet/smart-cabinet/MainCabinetConfigModal.vue";

defineOptions({
  name: "SmartCabinetDetail"
});

const route = useRoute();
const cabinetInfo = ref<SmartCabinetDTO>({
  cabinetName: "",
  cabinetType: 0,
  templateNo: "",
  lockControlNo: 0,
  location: 0
});
const loading = ref(false);
const activeTab = ref('basic');
const cabinetId = ref<number>(0);
const gatewayConfigVisible = ref(false);
const shopConfigVisible = ref(false);
const mainCabinetConfigVisible = ref(false);

async function fetchCabinetDetail() {
  try {
    loading.value = true;
    const { data } = await getSmartCabinetDetailApi(cabinetId.value);
    cabinetInfo.value = data;
  } finally {
    loading.value = false;
  }
}

onMounted(() => {
  cabinetId.value = Number(route.query.id);
  fetchCabinetDetail();
});
</script>

<template>
  <div class="detail-container">
    <div class="flex-container">
      <el-card class="cabinet-info-card">
        <div class="cabinet-header">
          <img :src="`/img/cabinet/${CabinetImgMap[cabinetInfo.templateNo]?.img || 'default.jpg'}`"
            class="cabinet-image" />
          <div class="cabinet-name">{{ cabinetInfo.cabinetName }}</div>
        </div>

        <el-divider />

        <el-descriptions class="cabinet-details" :column="1" border>
          <el-descriptions-item label="柜体ID">{{ cabinetInfo.cabinetId }}</el-descriptions-item>
          <el-descriptions-item label="柜体类型">
            {{ cabinetInfo.cabinetType === 0 ? '主柜' : '副柜' }}
          </el-descriptions-item>
          <el-descriptions-item label="模板">
            {{ CabinetImgMap[cabinetInfo.templateNo]?.name || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="归属商店">
            {{ cabinetInfo.shopName || '-' }}
            <el-button type="success" link :icon="useRenderIcon('ep:shop')" @click="shopConfigVisible = true">
              配置
            </el-button>
          </el-descriptions-item>
          <el-descriptions-item label="MQTT网关">
            {{ cabinetInfo.mqttServerId || '-' }}
            <el-button type="warning" link :icon="useRenderIcon('ant-design:gateway-outlined')"
              @click="gatewayConfigVisible = true">
              配置
            </el-button>
          </el-descriptions-item>
          <el-descriptions-item label="归属主柜" v-if="cabinetInfo.cabinetType === 1">
            {{ cabinetInfo.mainCabinetName || '-' }}
            <el-button type="primary" link :icon="useRenderIcon('ep:setting')" @click="mainCabinetConfigVisible = true">
              配置
            </el-button>
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <el-card class="info-card">
        <div class="tab-header">
          <el-tabs type="card" v-model="activeTab">
            <el-tab-pane label="基本信息" name="basic"></el-tab-pane>
            <el-tab-pane label="单元格配置" name="cells"></el-tab-pane>
          </el-tabs>
        </div>

        <el-descriptions class="info-details" v-if="activeTab === 'basic'" :column="2" border>
          <el-descriptions-item label="主柜ID">{{ cabinetInfo.mainCabinet || '-' }}</el-descriptions-item>
          <el-descriptions-item label="主柜名称">{{ cabinetInfo.mainCabinetName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="MQTT服务器ID">{{ cabinetInfo.mqttServerId || '-' }}</el-descriptions-item>
          <el-descriptions-item label="操作员">{{ cabinetInfo.operator || '-' }}</el-descriptions-item>
        </el-descriptions>

        <div class="info-details" v-if="activeTab === 'cells'">
          <!-- 单元格配置内容 -->
        </div>
      </el-card>
      <GatewayConfigModal v-model="gatewayConfigVisible" :cabinet-id="cabinetId"
        @refresh="fetchCabinetDetail" />
      <ShopConfigModal v-model="shopConfigVisible" :cabinet-id="cabinetId" @refresh="fetchCabinetDetail" />
      <MainCabinetConfigModal v-model="mainCabinetConfigVisible" :cabinet-id="cabinetId"
        @refresh="fetchCabinetDetail" />
    </div>
  </div>
</template>

<style scoped lang="scss">
.detail-container {
  display: flex;
  flex-direction: column;
  height: 100%;

  .flex-container {
    display: flex;
    flex: 1;
    gap: 20px;
    min-height: 0;

    .cabinet-info-card {
      width: 30%;
    }

    .info-card {
      width: 70%;
    }
  }

  .cabinet-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;

    .cabinet-image {
      width: 100%;
      height: 420px;
      object-fit: contain;
      margin-bottom: 15px;
    }

    .cabinet-name {
      font-size: 20px;
      font-weight: bold;
    }
  }

  .tab-header {
    margin-bottom: 20px;
  }
}
</style>