<template>
  <el-row :gutter="20">
    <!-- 左侧部门树 -->
    <DeptTree @node-click="handleDeptNodeClick" v-model:collapsed="isLeftContentCollapsed" />
    <el-col :xs="24" :span="isLeftContentCollapsed ? 24 : 20">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item
            :label="t('iotDevice.yfCode')"
            prop="yfDeviceCode"
            style="margin-left: 20px"
          >
            <el-input
              v-model="queryParams.yfDeviceCode"
              :placeholder="t('iotDevice.yfCodeHolder')"
              clearable
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>
          <el-form-item :label="t('iotDevice.code')" prop="deviceCode" style="margin-left: 20px">
            <el-input
              v-model="queryParams.deviceCode"
              :placeholder="t('iotDevice.codeHolder')"
              clearable
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>
          <el-form-item :label="t('iotDevice.name')" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              :placeholder="t('iotDevice.nameHolder')"
              clearable
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>
          <el-form-item label="车牌号" prop="carNo">
            <el-input
              v-model="queryParams.carNo"
              placeholder="请输入车牌号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>

          <el-form-item label="设备号" prop="deviceNo">
            <el-input
              v-model="queryParams.deviceNo"
              placeholder="请输入设备号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>
          <el-form-item :label="t('iotDevice.brand')" prop="brandName">
            <el-input
              v-model="queryParams.brandName"
              :placeholder="t('iotDevice.brandHolder')"
              clearable
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>

          <el-form-item
            v-show="ifShow"
            :label="t('iotDevice.status')"
            label-width="85px"
            prop="deviceStatus"
          >
            <el-select
              v-model="queryParams.deviceStatus"
              :placeholder="t('iotDevice.statusHolder')"
              clearable
              class="!w-200px"
            >
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.PMS_DEVICE_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item v-show="ifShow" :label="t('iotDevice.assets')" prop="assetProperty">
            <el-select
              v-model="queryParams.assetProperty"
              :placeholder="t('iotDevice.assetsHolder')"
              clearable
              class="!w-200px"
            >
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.PMS_ASSET_PROPERTY)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-show="ifShow"
            :label="t('deviceForm.category')"
            prop="assetClass"
            style="width: 15vw"
          >
            <el-tree-select
              v-model="queryParams.assetClass"
              :data="productClassifyList"
              :props="defaultProps"
              check-strictly
              node-key="id"
              :placeholder="t('deviceForm.categoryHolder')"
              filterable
            />
          </el-form-item>

          <el-form-item>
            <el-button v-if="!ifShow" @click="moreQuery(true)" type="warning"
              ><Icon icon="ep:search" class="mr-5px" /> {{ t('iotDevice.moreSearch') }}</el-button
            >
            <el-button v-if="ifShow" @click="moreQuery(false)" type="danger"
              ><Icon icon="ep:search" class="mr-5px" /> {{ t('iotDevice.closeSearch') }}</el-button
            >
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" />{{ t('operationFill.search') }}</el-button
            >
            <el-button @click="resetQuery"
              ><Icon icon="ep:refresh" class="mr-5px" /> {{ t('operationFill.reset') }}</el-button
            >
            <el-button
              type="primary"
              plain
              @click="openForm('create', undefined, queryParams.deptId)"
              v-hasPermi="['rq:iot-device:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> {{ t('operationFill.add') }}
            </el-button>
            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['rq:iot-device:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          height="calc(85vh - 215px)"
          @sort-change="handleSortChange"
        >
          <el-table-column :label="t('iotDevice.serial')" width="70" align="center" fixed="left">
            <template #default="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column
            :label="t('iotDevice.yfCode')"
            sortable
            align="center"
            prop="yfDeviceCode"
            width="150"
            fixed="left"
          >
            <template #header>
              <span
                style="display: inline-block"
                class="text-[#ad9399] w-[70px] text-[12px] cursor-pointer z-[999] justify-center flex items-center"
              >
                <el-popover placement="bottom" :width="250" trigger="click">
                  <template #reference>
                    <div class="flex items-center cursor-pointer" @click.stop>
                      <span> 油服编码 </span>
                      <Icon
                        icon="ep:arrow-down"
                        class="ml-1 cursor-pointer text-[#ad9399]"
                        @click.stop
                      />
                    </div>
                  </template>
                  <div class="flex items-center gap-2">
                    <el-input
                      v-model="queryParams.yfDeviceCode"
                      placeholder="请输入油服编码"
                      style="width: 180px"
                      clearable
                      @keyup.enter="handleQuery"
                    />
                    <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                  </div>
                </el-popover>
              </span>
            </template>
          </el-table-column>
          <el-table-column
            :label="t('iotDevice.code')"
            sortable
            align="center"
            prop="deviceCode"
            width="150"
            fixed="left"
          >
            <template #header>
              <span
                style="display: inline-block"
                class="text-[#ad9399] w-[70px] text-[12px] cursor-pointer z-[999] justify-center flex items-center"
              >
                <el-popover placement="bottom" :width="250" trigger="click">
                  <template #reference>
                    <div class="flex items-center cursor-pointer gap-1" @click.stop>
                      <span> 历史编码 </span> <Icon icon="ep:arrow-down" />
                    </div>
                  </template>
                  <div class="flex items-center gap-2">
                    <el-input
                      v-model="queryParams.deviceCode"
                      placeholder="请输入历史编码"
                      style="width: 180px"
                      clearable
                      @keyup.enter="handleQuery"
                    />
                    <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                  </div>
                </el-popover>
              </span>
            </template>
          </el-table-column>

          <el-table-column
            :label="t('iotDevice.name')"
            sortable
            align="center"
            prop="deviceName"
            min-width="280"
          >
            <template #header>
              <span
                style="display: inline-block"
                class="text-[#ad9399] w-[70px] text-[12px] cursor-pointer z-[999] justify-center flex items-center"
              >
                <el-popover placement="bottom" :width="250" trigger="click">
                  <template #reference>
                    <div class="flex items-center cursor-pointer gap-1" @click.stop>
                      <span> 设备名称 </span> <Icon icon="ep:arrow-down" />
                    </div>
                  </template>
                  <div class="flex items-center gap-2">
                    <el-input
                      v-model="queryParams.deviceName"
                      placeholder="请输入设备名称"
                      style="width: 180px"
                      clearable
                      @keyup.enter="handleQuery"
                    />
                    <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                  </div>
                </el-popover>
              </span>
            </template>

            <template #default="scope">
              <el-link :underline="false" type="primary" @click="handleDetail(scope.row.id)">
                {{ scope.row.deviceName }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="设备号" sortable align="center" prop="deviceNo" width="120">
            <template #header>
              <span
                style="display: inline-block"
                class="text-[#ad9399] w-[70px] text-[12px] cursor-pointer z-[999] justify-center flex items-center"
              >
                <el-popover placement="bottom" :width="250" trigger="click">
                  <template #reference>
                    <div class="flex items-center cursor-pointer gap-1" @click.stop>
                      <span> 设备号 </span> <Icon icon="ep:arrow-down" />
                    </div>
                  </template>
                  <div class="flex items-center gap-2">
                    <el-input
                      v-model="queryParams.deviceNo"
                      placeholder="请输入设备号"
                      style="width: 180px"
                      clearable
                      @keyup.enter="handleQuery"
                    />
                    <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                  </div>
                </el-popover>
              </span>
            </template>
          </el-table-column>
          <el-table-column
            :label="t('iotDevice.dept')"
            align="center"
            prop="deptName"
            min-width="150"
          >
            <template #header>
              <span
                style="display: inline-block"
                class="text-[#ad9399] w-[70px] text-[12px] cursor-pointer z-[999] justify-center flex items-center"
              >
                <el-popover placement="bottom" :width="250" trigger="click">
                  <template #reference>
                    <div class="flex items-center cursor-pointer gap-1" @click.stop>
                      <span> {{ t('iotDevice.dept') }} </span> <Icon icon="ep:arrow-down" />
                    </div>
                  </template>
                  <div class="flex items-center gap-2">
                    <el-tree-select
                      :teleported="false"
                      v-model="queryParams.deptId"
                      :data="deptList"
                      :props="defaultProps"
                      check-strictly
                      node-key="id"
                      filterable
                      placeholder="请选择所在部门"
                      @change="handleQuery"
                      style="width: 200px"
                    />
                  </div>
                </el-popover>
              </span>
            </template>
          </el-table-column>
          <el-table-column
            :label="t('iotDevice.status')"
            align="center"
            prop="deviceStatus"
            min-width="150"
          >
            <template #header>
              <div class="flex items-center justify-center pb-[1px]">
                <el-dropdown @command="handleCommand">
                  <span class="text-[#ad9399] text-[12px] cursor-pointer flex items-center gap-1">
                    <span> 设备状态 </span> <Icon icon="ep:arrow-down" />
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item
                        v-for="item in getStrDictOptions(DICT_TYPE.PMS_DEVICE_STATUS)"
                        :key="item.label"
                        :command="item.value"
                        >{{ item.label }}</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </template>
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.PMS_DEVICE_STATUS" :value="scope.row.deviceStatus" />
            </template>
          </el-table-column>
          <el-table-column
            :label="t('deviceForm.model')"
            align="center"
            prop="model"
            min-width="170"
          >
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1" @click.stop>
                    <span> {{ t('deviceForm.model') }} </span> <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.model"
                    placeholder="请输入规格型号"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column align="center" prop="manufacturer" min-width="200">
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1" @click.stop>
                    <span>
                      {{ t('deviceForm.mfg') }}
                    </span>
                    <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-select
                    v-model="queryParams.manufacturerId"
                    style="width: 220px"
                    filterable
                    :teleported="false"
                    :loading="brandList.length === 0"
                    loading-text="数据加载中..."
                    @change="handleQuery"
                    @visible-change="handleBrandChange"
                  >
                    <el-option
                      v-for="item in brandList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                      style="width: 300px"
                    />
                  </el-select>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="车牌号" align="center" prop="carNo" min-width="170">
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1" @click.stop>
                    <span> 车牌号 </span> <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.carNo"
                    placeholder="请输入车牌号"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            :label="t('deviceForm.brand')"
            align="center"
            prop="brandName"
            min-width="150"
          >
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1" @click.stop>
                    <span> {{ t('deviceForm.brand') }} </span> <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.brandName"
                    placeholder="请输入品牌"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            :label="t('iotDevice.assetClass')"
            align="center"
            prop="assetClassName"
            min-width="170"
          >
            <template #header>
              <span
                style="display: inline-block"
                class="text-[#ad9399] w-[70px] text-[12px] cursor-pointer z-[999] justify-center flex items-center"
              >
                <el-popover placement="bottom" :width="250" trigger="click">
                  <template #reference>
                    <div class="flex items-center cursor-pointer gap-1" @click.stop>
                      <span> {{ t('iotDevice.assetClass') }} </span> <Icon icon="ep:arrow-down" />
                    </div>
                  </template>
                  <div class="flex items-center gap-2">
                    <el-tree-select
                      :teleported="false"
                      v-model="queryParams.assetClass"
                      :data="productClassifyList"
                      :props="defaultProps"
                      check-strictly
                      node-key="id"
                      :placeholder="t('deviceForm.categoryHolder')"
                      filterable
                      @change="handleQuery"
                    />
                  </div>
                </el-popover>
              </span>
            </template>
          </el-table-column>

          <el-table-column
            :label="t('iotDevice.assets')"
            align="center"
            prop="assetProperty"
            min-width="110"
          >
            <template #header>
              <div class="flex items-center justify-center pb-[1px]">
                <el-dropdown @command="handleAssetProperty">
                  <span class="text-[#ad9399] text-[12px] cursor-pointer flex items-center gap-1">
                    <span> 资产性质 </span> <Icon icon="ep:arrow-down" />
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item
                        v-for="item in getStrDictOptions(DICT_TYPE.PMS_ASSET_PROPERTY)"
                        :key="item.label"
                        :command="item.value"
                        >{{ item.label }}</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </template>
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.PMS_ASSET_PROPERTY" :value="scope.row.assetProperty" />
            </template>
          </el-table-column>

          <el-table-column
            :label="t('devicePerson.rp')"
            align="center"
            prop="chargeName"
            min-width="170"
          >
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1" @click.stop>
                    <span> {{ t('devicePerson.rp') }} </span> <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.chargeName"
                    placeholder="请输入责任人"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            :label="t('deviceForm.useProject')"
            align="center"
            prop="useProject"
            min-width="170"
          >
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1">
                    <span> {{ t('deviceForm.useProject') }} </span> <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.useProject"
                    placeholder="请输入使用项目"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            :label="t('deviceForm.assetOwner')"
            align="center"
            prop="assetOwnership"
            min-width="170"
          >
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="flex items-center cursor-pointer gap-1">
                    <span> {{ t('deviceForm.assetOwner') }} </span>
                    <Icon icon="ep:arrow-down" />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.assetOwnership"
                    placeholder="请输入资产归属"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="所在地点" align="center" prop="address" min-width="170">
            <template #header>
              <el-popover placement="bottom" :width="250" trigger="click">
                <template #reference>
                  <div class="table-header-flex cursor-pointer">
                    <span class="pr-1">所在地点</span>
                    <Icon icon="ep:arrow-down" @click.stop />
                  </div>
                </template>
                <div class="flex items-center gap-2">
                  <el-input
                    v-model="queryParams.address"
                    placeholder="请输入所在地点"
                    style="width: 180px"
                    clearable
                  />
                  <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            :label="t('operationFill.operation')"
            align="center"
            min-width="180px"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['rq:iot-device:update']"
              >
                {{ t('iotDevice.update') }}
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['rq:iot-device:delete']"
              >
                {{ t('iotDevice.delete') }}
              </el-button>
              <!--              <el-button link type="warning" @click="handleUpload(scope.row.id)">-->
              <!--                {{t('iotDevice.upload')}}-->
              <!--              </el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>
  <!-- 表单弹窗：添加/修改 -->
  <!--  <IotDeviceForm ref="formRef" @success="getList" />-->
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { IotDeviceApi, IotDeviceVO } from '@/api/pms/device'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import DeptTree from '@/views/system/user/DeptTree2.vue'

import { buildSortingField } from '@/utils'
import { defaultProps, handleTree } from '@/utils/tree'
import * as ProductClassifyApi from '@/api/pms/productclassify'
import { useRefreshStore } from '@/store/modules/pms/refreshStore'
import { Search } from '@element-plus/icons-vue'
import * as DeptApi from '@/api/system/dept'

/** 设备台账 列表 */
defineOptions({ name: 'IotDevicePms' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由跳转
const deptList = ref<Tree[]>([]) // 树形结构

const refreshStore = useRefreshStore()
const loading = ref(true) // 列表的加载中
const ifShow = ref(false)
const isDetail = ref(false) // 是否查看详情
const list = ref<IotDeviceVO[]>([]) // 列表的数据
const productClassifyList = ref<Tree[]>([]) // 树形结构
const total = ref(0) // 列表的总页数
let isLeftContentCollapsed = ref(false)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  useProject: undefined,
  assetOwnership: undefined,
  address: undefined,
  deviceCode: undefined,
  deviceName: undefined,
  brand: undefined,
  brandName: undefined,
  model: undefined,
  deptId: undefined,
  deviceStatus: undefined,
  assetProperty: undefined,
  picUrl: undefined,
  remark: undefined,
  manufacturerId: undefined,
  supplierId: undefined,
  manDate: [],
  nameplate: undefined,
  expires: undefined,
  plPrice: undefined,
  plDate: [],
  plYear: undefined,
  plStartDate: [],
  plMonthed: undefined,
  plAmounted: undefined,
  remainAmount: undefined,
  infoId: undefined,
  infoType: undefined,
  infoName: undefined,
  infoRemark: undefined,
  infoUrl: undefined,
  templateJson: undefined,
  creator: undefined,
  sortingFields: [],
  assetClass: undefined,
  yfDeviceCode: undefined,
  carNo: undefined,
  deviceNo: undefined,
  manufacturer: undefined,
  chargeName: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const contentSpan = ref(20)
const treeShow = ref(true)
const brandList = ref([])
const shou = (tree) => {
  treeShow.value = !tree
  if (tree) {
    contentSpan.value = 20
  } else {
    contentSpan.value = 24
  }
}

const handleCommand = (command) => {
  queryParams.deviceStatus = command
  getList()
}

const handleAssetProperty = (command) => {
  queryParams.assetProperty = command
  getList()
}

const handleSortChange = (params: any) => {
  //console.log(`排序字段: ${prop}, 排序方式: ${order}`);
  queryParams.sortingFields = []
  queryParams.sortingFields = [buildSortingField(params)]
  getList()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await IotDeviceApi.getIotDevicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 处理部门被点击 */
const handleDeptNodeClick = async (row) => {
  queryParams.deptId = row.id
  await getList()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const moreQuery = (show) => {
  ifShow.value = show
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.chargeName = undefined
  queryParams.address = undefined
  queryParams.manufacturer = undefined
  queryParams.useProject = undefined
  queryParams.model = undefined
  queryParams.assetOwnership = undefined
  queryParams.manufacturerId = undefined
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, deptId?: number) => {
  //修改
  if (typeof id === 'number') {
    push({ name: 'DeviceDetailEdit', params: { type, id }, query: { source: 'devicerouter' } })
    return
  }
  // 新增
  if (deptId) {
    push({ name: 'DeviceDetailAdd', params: { type, deptId }, query: { source: 'devicerouter' } })
  } else {
    push({ name: 'DeviceDetailAddd', params: {}, query: { source: 'devicerouter' } })
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await IotDeviceApi.deleteIotDevice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleDetail = (id: number) => {
  push({ name: 'DeviceDetailInfo', params: { id } })
}

const handleBrandChange = async (visible) => {
  if (visible && brandList.value.length === 0) {
    brandList.value = await IotDeviceApi.getDeviceBrand()
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await IotDeviceApi.exportIotDevice(queryParams)
    download.excel(data, '设备台账.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  productClassifyList.value = handleTree(
    await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  )

  // brandList.value = await IotDeviceApi.getDeviceBrand()
  const sort = {
    field: 'sortColumn',
    order: 'asc'
  }
  queryParams.sortingFields.push(sort)
  await getList()
  refreshStore.registerCallback('devicerouter', getList)

  deptList.value = handleTree(await DeptApi.getSimpleDeptList())
})
</script>
<style scoped scss>
::v-deep .el-table__header-wrapper {
  position: sticky !important;
  width: 100%;
  top: 0px;
  z-index: 2000;
}

::v-deep .el-tooltip__trigger {
  border: none !important;
  outline: none !important;
}

::v-deep .el-table__header-wrapper {
  position: sticky !important;
  width: 100%;
  top: 0px;
  z-index: 2000;
}

::v-deep .el-tooltip__trigger {
  border: none !important;
  outline: none !important;
}

/* 表头对齐样式 */
.table-header-flex {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  height: 100% !important;
  width: 100% !important;
  padding: 0 !important;
}

.table-header-text {
  flex: 1;
  text-align: center;
  font-size: 12px;
  color: #606266;
}

.header-arrow-icon {
  margin-left: 4px;
  cursor: pointer;
  color: #ad9399;
  font-size: 12px;
}

.popover-content-flex {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 修复单元格对齐 */
::v-deep .el-table th,
::v-deep .el-table td {
  padding: 6px 0 !important;
}

::v-deep .el-table th .cell,
::v-deep .el-table td .cell {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 0 8px;
}
</style>
