<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属公司" prop="companyId" v-if="shouldShowCompanySelect">
        <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable size="small">
          <el-option v-for="item in companyList" :key="item.companyId" :label="item.companyName"
            :value="item.companyId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceId">
        <el-input v-model="queryParams.deviceId" placeholder="请输入设备编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="卡号" prop="iccid">
        <el-input v-model="queryParams.iccid" placeholder="请输入卡号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['watchApi:deviceInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['watchApi:deviceInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['watchApi:deviceInfo:export']">导出</el-button>
      </el-col>

      

      <el-col :span="1.5">
        <el-button plain icon="el-icon-setting" size="mini" @click="showSettingBtn = !showSettingBtn,showTimeSettingBtn = false"
          v-hasPermi="['watchApi:deviceInfo:edit']">系统设置</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button plain icon="el-icon-setting" size="mini" @click="showTimeSettingBtn = !showTimeSettingBtn,showSettingBtn = false"
          v-hasPermi="['watchApi:deviceInfo:edit']">发送消息时间设置</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button plain icon="el-icon-setting" size="mini" @click="sendMsg" :disabled="sendMsgButtonDisabled"
          v-hasPermi="['watchApi:deviceInfo:edit']">发送设备消息</el-button>
      </el-col>

      <el-col :span="1.5">
            <el-button
                type="warning"
              icon="el-icon-upload"
              size="mini"
              v-hasPermi="['watchApi:deviceInfo:edit']"
              @click="handleImport"  
            >导入修改公司</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    

    

    <el-table v-loading="loading" :data="deviceInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="设备编号" align="center" prop="deviceId" />
      <el-table-column label="ICCID号" align="center" prop="iccid" width="178px"/>
      <el-table-column label="运营商" align="center" prop="operatorType">
        <template v-slot="scope">
          <span v-if="scope.row.operatorType === 1">移动</span>
          <span v-else-if="scope.row.operatorType === 2">联通</span>
          <span v-else-if="scope.row.operatorType === 3">电信</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.status === 5">已激活</span>
          <span v-else-if="scope.row.status === 6">已停用</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="当月总流量" align="center">
        <template v-slot="scope">
          {{ scope.row.totalFlowSize }}MB
        </template>
      </el-table-column>
      <el-table-column label="当月已使用流量" align="center">
        <template v-slot="scope">
          {{ scope.row.usedFlowSize }}MB
        </template>
      </el-table-column>
      <el-table-column label="当前套餐" align="center" prop="currentPlanName" />
      <el-table-column label="套餐开始时间" align="center" prop="currentPlanStartTime" width="150px"/>
      <el-table-column label="套餐失效时间" align="center" prop="currentPlanExpireTime" width="150px"/>
      <el-table-column label="下期套餐" align="center" prop="nextPlanName" />
      <el-table-column label="实名状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.certified === 0">未认证</span>
          <span v-else-if="scope.row.certified === 1">已认证</span>
          <span v-else-if="scope.row.certified === 2">人工审核中</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗展示紧急联系人列表 -->
    <el-dialog :visible.sync="sosVisible" title="紧急联系人" width="50%">
        <el-table v-loading="sosLoading" :data="sosPeopleList">
          <el-table-column prop="name" label="联系人" align="center" />
          <el-table-column prop="number" label="电话" align="center" />
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="sosVisible = false">关闭</el-button>
        </div>
    </el-dialog>
    

    

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

    
    
  </div>
</template>

<script>
import {importTemplate, listDeviceInfo, getDeviceInfo, delDeviceInfo, addDeviceInfo, updateDeviceInfo, exportDeviceInfo,queryIotList } from "@/api/watch/deviceInfo";
import { listCompany } from "@/api/company/company";

import { getToken } from "@/utils/auth";

export default {
  name: "iot",
  
  data() {
    return {
      deviceInfoList: [],  // 设备信息列表
      healthDataDialogVisible: false,  // 控制弹窗显示
      selectedHealthData: [],  // 选中设备的健康数据列表
      importMsgOpen:false,
      importMsg:"",
      
      roles: null, // 登录用户信息
      // 查询参数
      queryAlarmParams: {
        pageNum: 1,
        pageSize: 10
      },
      alarmTotal: 0,
      alarmData: [],
      hasMore: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      deviceInfoList: [],
      // 公司信息表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        iccid: null,
      },
      // 表单参数
      form: {
        companyId: '',
        battery: '',
        rssi: '',
        status: 1,
      },
      
      
    };
  },
  created() {
    this.getList();
  },
  
  methods: {
    

    /** 查询物联网信息列表 */
    getList() {
      this.loading = true;
      queryIotList(this.queryParams).then(response => {
        this.deviceInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        companyName: null,
        battery: null,
        rssi: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.deviceNumbers = selection.map(item => item.deviceNumber)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加设备信息";
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getDeviceInfo(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceId != null) {
            updateDeviceInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$confirm('是否确认删除设备信息编号为"' + deviceIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delDeviceInfo(deviceIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有设备信息数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportDeviceInfo(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => { });
    },
    /** 查询所有公司 */
    getCompanyList() {
      this.loading = true;
      // 根据登录用户信息判断是否显示所属公司选择框
      if (this.shouldShowCompanySelect()) {
        listCompany().then(response => {
          this.companyList = response.rows;
          this.loading = false;
        });
      }

    },
    
    confirmFun(text, fun) {
      this.$confirm(text, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        fun().then(res => {
          if (res.code == 200) {
            this.msgSuccess("操作成功");
          } else {
            this.msgError(res.msg);
          }
        }).catch(() => { })
      }).catch(() => { });
    }
  }
};
</script>
<style>
.text-red {
  color: red;
}
</style>
<style lang="scss" scoped>
.notification-container {
  padding: 10px 0;
  height: 80vh;
  overflow-y: auto;
}

.notification-container::-webkit-scrollbar {
  width: 4px;
  /*  设置纵轴（y轴）轴滚动条 */
  height: 4px;
  /*  设置横轴（x轴）轴滚动条 */
}

/* 滚动条滑块（里面小方块） */
.notification-container::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: rgba(144, 147, 153, 0.3);
}

/* 滚动条轨道 */
.notification-container::-webkit-scrollbar-track {
  border-radius: 0;
}

.notification-card {
  position: relative;
  padding-right: 10px;
  margin-bottom: 10px;
  cursor: pointer;

  &:hover {
    background: #edf8ff;
  }
}

.red-dot {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 8px;
  height: 8px;
  background-color: red;
  border-radius: 50%;
}

.notification-title {
  font-weight: bold;
  font-size: 16px;
}

.notification-desc {
  margin-top: 5px;
  color: #666;
}

.notification-time {
  margin-top: 10px;
  font-size: 12px;
  color: #999;
}

.notification-status {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
}
</style>
