<template>
  <Dialog v-model="dialogVisible" :title="t('deviceList.selectDevice')" style="width: 1100px; max-height: 800px">
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item :label="t('deviceList.deviceName')" prop="deviceName" style="margin-left: 25px">
          <el-input
            v-model="queryParams.deviceName"
            :placeholder="t('deviceList.nameHolder')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item :label="t('deviceList.deviceCode')" prop="deviceCode">
          <el-input
            v-model="queryParams.deviceCode"
            :placeholder="t('deviceList.codeHolder')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" />
            {{ t('deviceList.search') }}</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('deviceList.reset') }}</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @row-click="handleRowClick"
      >
        <el-table-column width="120" :label="t('deviceList.select')">
          <template #default="{ row }">
            <el-radio
              :model-value="selectedRow?.id"
              :label="row.id"
              @click.stop="selectRow(row)"
              class="no-label-radio"
            />
          </template>
        </el-table-column>
        <el-table-column :label="t('deviceList.deviceCode')" align="center" prop="deviceCode" />
        <el-table-column :label="t('deviceList.deviceName')" align="center" prop="deviceName" />
        <el-table-column :label="t('deviceStatus.dept')" align="center" prop="deptName" />
        <el-table-column :label="t('maintain.status')" align="center" prop="deviceStatus">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.PMS_DEVICE_STATUS" :value="scope.row.deviceStatus" />
          </template>
        </el-table-column>
        <el-table-column
          :label="t('deviceList.createTime')"
          align="center"
          prop="createTime"
          width="180"
          :formatter="dateFormatter"
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>

<script setup lang="ts">
import { IotDeviceApi, IotDeviceVO } from '@/api/pms/device'
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'

const emit = defineEmits(['choose']) // 定义 success 事件，用于操作成功后的回调
const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(true) // 列表的加载中
const queryFormRef = ref() // 搜索的表单
const list = ref<IotDeviceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const { t } = useI18n() // 国际化

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  label: '',
  status: undefined,
  deptId: undefined,
  assetClass: undefined,
  deviceName: undefined,
  deviceCode: undefined,
})

const selectedRow = ref(null)

// 处理单选逻辑
const selectRow = (row) => {
  selectedRow.value = selectedRow.value?.id === row.id ? null : row
  emit('choose', row)
  dialogVisible.value = false
}

// 点击整行选中
const handleRowClick = (row) => {
  selectRow(row)
}
const open = async (classify) => {
  dialogVisible.value = true
  queryParams.assetClass = classify
  selectedRow.value = ''
  queryParams.deviceName = undefined
  queryParams.deviceCode = undefined
  queryParams.pageNo = 1
  await getList()
}
defineExpose({ open })
const { wsCache } = useCache()
const getList = async () => {
  loading.value = true
  try {
    // const user = wsCache.get(CACHE_KEY.USER)
    // queryParams.deptId = user.user.deptId
    const data = await IotDeviceApi.getIotDevicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const choose = (row: DictDataVO) => {
  emit('choose', row)
  dialogVisible.value = false
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
<style lang="scss">
.no-label-radio .el-radio__label {
  display: none;
}
.no-label-radio .el-radio__inner {
  margin-right: 0;
}
</style>
