<template>
  <div class="device-association-record">
    <section class="library-workbench">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        inline
        size="default"
        class="library-workbench__header">
        <div class="library-header-main">
          <div class="workbench-title">
            <span class="workbench-title__icon">
              <Icon icon="ep:connection" />
            </span>
            <div>
              <h3>关联设备</h3>
            </div>
          </div>

          <el-form-item label="设备编码" prop="deviceCode">
            <el-input
              v-model="queryParams.deviceCode"
              class="library-search-input"
              clearable
              placeholder="请输入设备编码"
              @keyup.enter="handleQuery">
              <template #prefix>
                <Icon icon="ep:search" />
              </template>
            </el-input>
          </el-form-item>
        </div>

        <el-form-item class="library-actions">
          <el-button type="primary" @click="handleQuery">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <div class="library-table-wrap">
        <el-auto-resizer>
          <template #default="{ width, height }">
            <ZmTable
              :data="list"
              class="library-table"
              :loading="loading"
              :width="width"
              :height="height"
              :max-height="height"
              :column-max-width="420"
              row-key="id"
              empty-text="暂无数据"
              show-border>
              <ZmTableColumn label="序号" width="70" align="center">
                <template #default="{ $index }">
                  {{ (queryParams.pageNo - 1) * queryParams.pageSize + $index + 1 }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn
                label="成套名称"
                prop="groupName"
                min-width="180"
                align="left"
                fixed="left">
                <template #default="{ row }">
                  <span class="group-name">{{ row.groupName || '-' }}</span>
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="设备名称" prop="deviceName" min-width="180">
                <template #default="{ row }">
                  {{ row.deviceName || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="设备编码" prop="deviceCode" min-width="150">
                <template #default="{ row }">
                  {{ row.deviceCode || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="是否主设备" prop="ifMaster" min-width="120">
                <template #default="{ row }">
                  <el-tag v-if="row.ifMaster" type="success" effect="light">是</el-tag>
                  <el-tag v-else type="info" effect="light">否</el-tag>
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="创建时间" prop="createTime" min-width="160">
                <template #default="{ row }">
                  {{ formatRecordTime(row.createTime) }}
                </template>
              </ZmTableColumn>
            </ZmTable>
          </template>
        </el-auto-resizer>
      </div>

      <div class="library-pagination">
        <Pagination
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          :total="total"
          @pagination="getList" />
      </div>
    </section>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { IotDeviceApi } from '@/api/pms/device'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'
import { formatToDate } from '@/utils/dateUtil'

defineOptions({ name: 'DeviceAssociationRecord' })

const props = defineProps<{
  deviceId?: number
}>()

interface AssociationRecordRow {
  id?: number
  createTime?: Date | string | number
  deviceCode?: string
  deviceName?: string
  groupName?: string
  ifMaster?: boolean
}

const { ZmTable, ZmTableColumn } = useTableComponents<AssociationRecordRow>()
const loading = ref(false)
const list = ref<AssociationRecordRow[]>([])
const total = ref(0)
const queryFormRef = ref()

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceId: undefined as number | undefined,
  deviceCode: ''
})

const getList = async () => {
  if (!props.deviceId) return

  loading.value = true
  queryParams.deviceId = props.deviceId
  try {
    const data = await IotDeviceApi.getIotDeviceSetRelation(queryParams)
    list.value = data?.list || []
    total.value = data?.total || 0
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields()
  queryParams.deviceCode = ''
  handleQuery()
}

const formatRecordTime = (value?: Date | string | number) => {
  return value ? formatToDate(value) : '-'
}

watch(
  () => props.deviceId,
  () => {
    getList()
  },
  { immediate: true }
)
</script>

<style scoped lang="scss">
.device-association-record {
  display: grid;
  gap: 14px;
}

.library-workbench {
  padding: 14px;
  overflow: hidden;
  background: linear-gradient(180deg, #f8fbff 0, var(--el-bg-color) 96px), var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
}

.library-workbench__header {
  display: flex;
  gap: 16px;
  min-height: 56px;
  margin-bottom: 14px;
  align-items: center;
  justify-content: space-between;

  :deep(.el-form-item) {
    margin-right: 10px;
    margin-bottom: 0;
  }

  :deep(.el-form-item:last-child) {
    margin-right: 0;
  }

  :deep(.el-button .app-iconify) {
    margin-right: 4px;
  }
}

.library-header-main {
  display: flex;
  gap: 10px;
  min-width: 0;
  align-items: center;
  flex: 1;
  flex-wrap: wrap;
}

.workbench-title {
  display: flex;
  gap: 12px;
  min-width: 0;
  align-items: center;

  h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    line-height: 24px;
    color: var(--el-text-color-primary);
  }
}

.workbench-title__icon {
  display: flex;
  width: 38px;
  height: 38px;
  font-size: 18px;
  color: #3478f6;
  background: #eaf2ff;
  border-radius: 10px;
  flex: none;
  align-items: center;
  justify-content: center;
}

.library-search-input {
  width: 220px;
}

.library-actions {
  flex: none;
}

.library-table-wrap {
  height: 560px;
  min-width: 0;
}

.library-table {
  --el-table-header-bg-color: var(--el-fill-color-lighter);
  --el-table-row-hover-bg-color: #f6faff;

  overflow: hidden;
  border-radius: 8px;

  :deep(.el-table__cell) {
    padding: 9px 0;
  }
}

.group-name {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-pagination {
  display: flex;
  height: 40px;
  margin-top: 8px;
  align-items: center;
  justify-content: flex-end;
}

@media (width <= 767px) {
  .library-workbench__header {
    align-items: stretch;
    flex-direction: column;
  }

  .library-header-main {
    align-items: stretch;
    flex-direction: column;
  }

  .library-search-input {
    width: 100%;
  }
}
</style>
