<template>
  <div class="device-inspect-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:checked" />
            </span>
            <div>
              <h3>巡检记录</h3>
            </div>
          </div>

          <el-form-item label="工单名称" prop="inspectOrderTitle">
            <el-input
              v-model="queryParams.inspectOrderTitle"
              class="library-search-input"
              clearable
              placeholder="请输入工单名称"
              @keyup.enter="handleQuery">
              <template #prefix>
                <Icon icon="ep:search" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="工单编码" prop="inspectOrderCode">
            <el-input
              v-model="queryParams.inspectOrderCode"
              class="library-search-input"
              clearable
              placeholder="请输入工单编码"
              @keyup.enter="handleQuery">
              <template #prefix>
                <Icon icon="ep:search" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              class="library-search-input"
              clearable
              placeholder="请选择状态">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.PMS_INSPECT_ORDER_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              class="library-date-range"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" />
          </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="inspectOrderTitle"
                min-width="220"
                align="left"
                fixed="left">
                <template #default="{ row }">
                  <span class="inspect-title">{{ row.inspectOrderTitle || '-' }}</span>
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="工单编码" prop="inspectOrderCode" min-width="160">
                <template #default="{ row }">
                  {{ row.inspectOrderCode || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="类型" prop="type" min-width="120">
                <template #default="{ row }">
                  {{ row.type || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="状态" prop="status" min-width="120">
                <template #default="{ row }">
                  <dict-tag :type="DICT_TYPE.PMS_INSPECT_ORDER_STATUS" :value="row.status" />
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="负责人" prop="chargeName" min-width="150">
                <template #default="{ row }">
                  {{ row.chargeName || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="备注" prop="remark" min-width="180">
                <template #default="{ row }">
                  {{ row.remark || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="创建时间" prop="createTime" min-width="180">
                <template #default="{ row }">
                  {{ formatRecordTime(row.createTime) }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="操作" width="120" fixed="right" action>
                <template #default="{ row }">
                  <el-button
                    link
                    type="primary"
                    class="table-action-btn"
                    @click="openDetail(row.id)">
                    <Icon icon="ep:view" />
                    查看
                  </el-button>
                </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 { IotInspectOrderApi, IotInspectOrderVO } from '@/api/pms/inspect/order'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'DeviceInspectRecord' })

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

type InspectRecordRow = IotInspectOrderVO & {
  chargeName?: string
  createTime?: Date | string | number
  type?: string | number
}

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

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  inspectOrderTitle: undefined as string | undefined,
  inspectOrderCode: undefined as string | undefined,
  status: undefined as string | undefined,
  createTime: [] as string[],
  deviceIds: undefined as number | undefined
})

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

  loading.value = true
  queryParams.deviceIds = props.deviceId
  try {
    const data = await IotInspectOrderApi.getDeviceIotInspectOrderPage(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()
  handleQuery()
}

const openDetail = (id?: number) => {
  push({ name: 'InspectOrderDetail', params: { id } })
}

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

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

<style scoped lang="scss">
.device-inspect-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-date-range {
  width: 300px;
}

.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;
  }
}

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

.table-action-btn {
  gap: 4px;

  :deep(.el-icon),
  :deep(.app-iconify) {
    margin-right: 2px;
  }
}

.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>
