<template>
  <el-dialog
    v-model="visible"
    title="选择组态"
    width="980px"
    class="webtopo-select-dialog"
    destroy-on-close
    :close-on-click-modal="false"
    @open="loadList">
    <div class="webtopo-select-toolbar">
      <el-input
        v-model="query.projectName"
        clearable
        class="!w-260px"
        placeholder="请输入组态名称"
        @keydown.enter.prevent="handleQuery()" />
      <el-button type="primary" @click="handleQuery()">
        <Icon icon="ep:search" class="mr-5px" /> 搜索
      </el-button>
      <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" />重置</el-button>
    </div>

    <el-scrollbar v-loading="loading" height="520px" view-class="webtopo-select-list">
      <el-empty v-if="!loading && !list.length" description="暂无组态项目" />
      <div v-else class="webtopo-select-grid">
        <button
          v-for="item in list"
          :key="item.id"
          type="button"
          :class="['webtopo-select-card', modelValue?.id === item.id ? 'is-selected' : '']"
          @click="onSelect(item)">
          <div class="webtopo-select-thumb">
            <el-image v-if="item.thumbnail" :src="item.thumbnail" fit="cover" class="w-full h-full">
              <template #error>
                <div class="webtopo-select-thumb-empty">
                  <Icon icon="ep:picture" />
                  缩略图加载失败
                </div>
              </template>
            </el-image>
            <div v-else class="webtopo-select-thumb-empty">
              <Icon icon="ep:picture" />
              暂无缩略图
            </div>
            <div class="webtopo-select-device">
              <Icon icon="ep:cpu" />
              {{ getLinkedDeviceCount(item) }} 台设备
            </div>
          </div>
          <div class="webtopo-select-content">
            <div class="webtopo-select-title" :title="item.projectName">
              {{ item.projectName || '-' }}
            </div>
            <div class="webtopo-select-time">
              <Icon icon="ep:clock" />
              {{ formatCreateTime(item.createTime) }}
            </div>
            <div class="webtopo-select-remark" :title="item.remark">
              {{ item.remark || '暂无备注' }}
            </div>
          </div>
        </button>
      </div>
    </el-scrollbar>

    <div class="webtopo-select-footer">
      <el-pagination
        v-show="total > 0"
        v-model:current-page="query.pageNo"
        v-model:page-size="query.pageSize"
        background
        :page-sizes="[12, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import {
  WebtopoProjectApi,
  type WebtopoProjectPageReqVO,
  type WebtopoProjectVO
} from '@/api/pms/maotu'
import type { IDoneJsonEventWebtopoProject } from '@/components/mt-edit/store/types'
import { useUserStore } from '@/store/modules/user'
import { useDebounceFn } from '@vueuse/core'
import dayjs from 'dayjs'

type WebtopoProjectSelectDialogProps = {
  modelValue?: IDoneJsonEventWebtopoProject
}

const props = defineProps<WebtopoProjectSelectDialogProps>()
const emit = defineEmits(['update:modelValue', 'select'])
const visible = defineModel<boolean>('visible', { default: false })

const deptId = useUserStore().getUser.deptId
const loading = ref(false)
const list = ref<WebtopoProjectVO[]>([])
const total = ref(0)
const initQuery: WebtopoProjectPageReqVO = {
  pageNo: 1,
  pageSize: 12,
  deptId,
  projectName: ''
}
const query = ref<WebtopoProjectPageReqVO>({ ...initQuery })
const modelValue = computed(() => props.modelValue)

const loadList = useDebounceFn(async () => {
  loading.value = true
  try {
    const data = await WebtopoProjectApi.getWebtopoProjectPage(query.value)
    if (Array.isArray(data)) {
      list.value = data
      total.value = data.length
      return
    }

    list.value = data?.list || []
    total.value = data?.total || 0
  } finally {
    loading.value = false
  }
}, 200)

const getLinkedDeviceIds = (row: WebtopoProjectVO) => {
  return row.linkedDeviceIds || row.linkedDevices?.map((item) => item.id) || []
}

const getLinkedDeviceCount = (row: WebtopoProjectVO) => {
  return getLinkedDeviceIds(row).length
}

const formatCreateTime = (value?: number | string) => {
  if (!value) return '-'
  const time = typeof value === 'number' && value.toString().length === 10 ? value * 1000 : value
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

const handleSizeChange = (val: number) => {
  query.value.pageSize = val
  handleQuery()
}

const handleCurrentChange = (val: number) => {
  query.value.pageNo = val
  loadList()
}

const handleQuery = (setPage = true) => {
  if (setPage) {
    query.value.pageNo = 1
  }
  loadList()
}

const resetQuery = () => {
  query.value = { ...initQuery }
  handleQuery()
}

const onSelect = (item: WebtopoProjectVO) => {
  const targetProject = {
    id: item.id,
    projectName: item.projectName
  }
  emit('update:modelValue', targetProject)
  emit('select', targetProject)
  visible.value = false
}
</script>

<style scoped>
.webtopo-select-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.webtopo-select-list {
  min-height: 360px;
  padding: 2px 4px 8px;
}

.webtopo-select-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.webtopo-select-card {
  padding: 0;
  overflow: hidden;
  text-align: left;
  cursor: pointer;
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
  transition:
    border-color 0.2s,
    box-shadow 0.2s,
    transform 0.2s;
}

.webtopo-select-card:hover,
.webtopo-select-card.is-selected {
  border-color: var(--el-color-primary);
  transform: translateY(-2px);
  box-shadow: 0 10px 24px rgb(15 23 42 / 12%);
}

.webtopo-select-thumb {
  position: relative;
  height: 146px;
  overflow: hidden;
  background: var(--el-fill-color-light);
}

.webtopo-select-thumb-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  gap: 8px;
  font-size: 12px;
  color: var(--el-text-color-secondary);
  background: linear-gradient(135deg, var(--el-fill-color-blank), var(--el-fill-color-light));
}

.webtopo-select-thumb-empty .iconify {
  font-size: 30px;
  opacity: 0.65;
}

.webtopo-select-device {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  font-size: 12px;
  color: #fff;
  background: rgb(0 0 0 / 45%);
  border-radius: 999px;
  backdrop-filter: blur(8px);
}

.webtopo-select-content {
  padding: 14px;
}

.webtopo-select-title {
  overflow: hidden;
  font-size: 15px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.webtopo-select-time {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--el-text-color-secondary);
}

.webtopo-select-remark {
  display: -webkit-box;
  height: 40px;
  margin-top: 8px;
  overflow: hidden;
  font-size: 12px;
  line-height: 20px;
  color: var(--el-text-color-placeholder);
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.webtopo-select-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}
</style>
