<template>
  <el-dialog v-model="open" title="绑定设备列表" width="50%" append-to-body close-on-click-modal @closed="info = null">
    <!-- 搜索栏 -->
    <div class="head-container">
      <el-input v-model="queryParams.deviceName" clearable placeholder="请输入设备名称" style="width: 200px;"
        class="filter-item" />
      <el-input v-model="queryParams.deviceMac" clearable placeholder="请输入设备MAC" style="width: 200px;"
        class="filter-item" />
      <el-input v-model="queryParams.deviceImei" clearable placeholder="请输入设备IMEI" style="width: 200px;"
        class="filter-item" />
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
    </div>
    <!-- 表格 -->
    <el-table v-loading="bindingLoading" :data="boundDevices" style="margin-top: 20px;">
      <el-table-column prop="deviceName" label="设备名称" />
      <el-table-column prop="devicePic" label="设备图片">
        <template #default="scope">
          <el-popover placement="right" trigger="click">
            <template #reference>
              <img :src="scope.row.devicePic" style="width: 40px; height: 40px; cursor: pointer;"
                class="hover:opacity-80 transition-opacity" />
            </template>
            <img :src="scope.row.devicePic" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="deviceMac" label="设备MAC" />
      <el-table-column prop="deviceImei" label="设备IMEI" />
      <el-table-column prop="typeName" label="设备类型" />
      <el-table-column prop="bindingTime" label="绑定日期" />
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="danger" plain @click="handleUnbind(scope.row)">解绑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      :total="total" @pagination="getList" />
  </el-dialog>
</template>

<script setup lang="ts">
import type { OperLogForm } from '@/api/monitor/operlog/types';
import 'vue-json-pretty/lib/styles.css';
import api from '@/api/userApp';
const open = ref(false);
const info = ref<OperLogForm | null>(null);
const total = ref(0);
const boundDevices = ref()
const bindingLoading = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  deviceName: '',
  deviceMac: '',
  deviceImei: ''
})
function openDialog(row: OperLogForm) {
  console.log(row,'roweee');
  info.value = row;
  open.value = true;
  getList();
}

function closeDialog() {
  open.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.pageNum = 1;
  queryParams.deviceImei = '',
    queryParams.deviceMac = '',
    queryParams.deviceName = '',
    getList()
};
// 列表
const getList = async () => {
  bindingLoading.value = true;
  let data = {
    bindingUserId: info.value.userId,
    pageNum: queryParams.pageNum,
    pageSize: queryParams.pageSize,
    deviceName: queryParams.deviceName,
    deviceMac: queryParams.deviceMac,
    deviceImei: queryParams.deviceImei

  }
  const res = await api.deviceList(data);
  boundDevices.value = res.rows;
  total.value = res.total;
  bindingLoading.value = false;
};
// 解绑
const handleUnbind = (row) => {
  proxy?.$modal.confirm('此操作将解绑设备 "' + row.deviceName + '", 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const data = {
      // deviceMac: row.deviceMac,
      id: row.id,
      userId:info.value.userId
    }
    api.deviceUnBind(data).then(() => {
      proxy?.$modal.msgSuccess({ type: 'success', message: '解绑成功!' })
      getList()
    })
  }).catch(() => { })
}
defineExpose({
  openDialog,
  closeDialog
});

/**
 * json转为对象
 * @param data 原始数据
 */
function formatToJsonObject(data: string) {
  try {
    return JSON.parse(data);
  } catch (error) {
    return data;
  }
}

/**
 * 字典信息
 */
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_oper_type } = toRefs<any>(proxy?.useDict('sys_oper_type'));
const typeFormat = (row: OperLogForm) => {
  return proxy?.selectDictLabel(sys_oper_type.value, row.businessType);
};
</script>

<style lang="scss" scoped>
/**
label宽度固定
*/
:deep(.el-descriptions__label) {
  min-width: 100px;
}

/**
文字超过 换行显示
*/
:deep(.el-descriptions__content) {
  max-width: 300px;
}

.filter-item {
  margin-right: 10px;
}
</style>
