
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="机架号" prop="label">
        <el-input v-model="queryParams.label" placeholder="请输入机架号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="设备类型" prop="type">
        <!-- <el-input v-model="queryParams.type" placeholder="请输入设备类型" clearable style="width: 200px"
                  @keyup.enter="handleQuery" /> -->
        <el-select v-model="queryParams.type" value-key="label" style="width: 200px" filterable placeholder="选择设备类型"
          @keyup.enter="handleQuery">
          <el-option value="" label="请选择"></el-option>
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.label" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="部门名称" prop="dept">
              <el-input v-model="queryParams.dept" placeholder="请输入部门名称" clearable style="width: 200px"
                  @keyup.enter="handleQuery" />
          </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
              <el-button type="primary" plain icon="Plus" @click="handleAdd"
                  v-hasPermi="['alarm:sendlog:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
              <el-button type="success" plain icon="Edit" :disabled="single" v-hasPermi="['alarm:sendlog:update']"
                  @click="handleUpdate(ids[0])">修改</el-button>
          </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" v-hasPermi="['alarm:sendlog:del']"
          @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="120" />
      <!-- <el-table-column label="部门id" prop="id" width="120" /> -->
      <el-table-column label="设备名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="类型" prop="type" :show-overflow-tooltip="true" />
          <el-table-column label="型号" prop="model" align="center" :show-overflow-tooltip="true" /> -->
      <el-table-column label="是否启用" align="center" :show-overflow-tooltip="true">
        <template #default="scope">
          <el-tag v-if="scope.row.enable == '启用'" type="success">启用</el-tag>
          <el-tag v-else type="warning">未启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <!-- <el-tooltip content="修改" placement="top" v-if="scope.row.roleId !== 1">
                      <el-button link type="primary" v-hasPermi="['alarm:sendlog:update']" icon="Edit"
                          @click="handleUpdate(scope.row.deviceId)"></el-button>
                  </el-tooltip> -->
          <el-tooltip content="删除" placement="top" v-if="scope.row.userId !== 1">
            <el-button link type="primary" icon="Delete" v-hasPermi="['alarm:sendlog:del']"
              @click="handleDelete(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > queryParams.pageSize" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script setup>
import { ref, reactive, toRefs, getCurrentInstance, onMounted } from 'vue'
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { getDeviceList, delDevice } from '@/api/big/device'
import { getEmailList, getEmailInfo, AddEmailAlarm, EditEmailAlarm, DeleteEmailAlarm } from '@/api/alarm/index'
import useDeviceStore from '@/store/modules/device'
const store = useDeviceStore()
const { proxy } = getCurrentInstance();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: '',
    dept: '',
    type: '',
    label: '',
    userId: 1,
  },
});

const { queryParams, form } = toRefs(data);

// testScreen({}).then(res => {
//  alert(JSON.stringify(res))
// })

let deviceList = ref([])

const open = ref(false);
const loading = ref(false);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const file_list = ref([])
const type = ref("add");
const typeList = ref([])
/** 查询设备列表 */
function getList() {
  loading.value = true;
  // console.log(JSON.stringify(queryParams.value));
  getEmailList(queryParams.value).then(res => {
    loading.value = false;
    deviceList.value = res.rows;
    total.value = res.total;
  });

}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 添加 */
function handleAdd() {
  title.value = "添加发送计划";
  type.value = "add";
  open.value = true;
}
/** 修改 */
function handleUpdate(id) {
  title.value = "修改设备类型";
  type.value = id;
  open.value = true;
}




/** 删除按钮操作 */
function handleDelete(row) {
  let rows = deviceList.value.filter(item => ids.value.includes(item.deviceId));
  const names = row.name || rows.map(item => item.name).join(',');
  const deviceTypeIds = row.deviceId || ids.value.join(',');
  proxy.$modal.confirm('是否确认删除名称为"' + names + '"的数据项？').then(function () {
    return DeleteEmailAlarm(deviceTypeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });

}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.deviceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}











onMounted(() => {
  getList();
})

</script>

<style lang="scss" scoped>
:deep(.el-table .el-table__cell) {
  z-index: auto;
}

.img-preview {
  max-width: 100%;
  max-height: 100%;
  display: block;
  margin: 0 auto;
}
</style>
