<template>
  <router-view v-if="isRouterAlive" />
  <div ref="box" @mousedown="startDrag" class="topapps" :style="{ top: `${boxTop}px`, left: `${boxLeft}px` }">

    网络状态：{{ isOnline ? netWorkStatus.effectiveType : '离线' }}<br>

    延迟：{{ isOnline ? netWorkStatus.rtt + 'ms' : '离线' }}<br>

    带宽：{{ isOnline ? netWorkStatus.downlink + 'mb/s' : '离线' }}<br>

  </div>
  <el-dialog v-model="dialogTableVisible" title="上报结果" :close-on-click-modal="false" :close-on-press-escape="false"
    :before-close="handleClose">
    <el-descriptions class="mb10" :title="item.dataname" :column="3" :size="size" border
      v-for="(item, index) in dialogTableData" :key="index">
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">上报人</div>
        </template>
        {{ item.username }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">上报时间</div>
        </template>
        {{ item.sbsj }}
      </el-descriptions-item>
      <el-descriptions-item width="110px">
        <template #label>
          <div class="cell-item">上报结果</div>
        </template>
        <span v-if="item.returnHttpCode == '200'" style="color: green">上报成功</span>
        <span v-else style="color: red">上报失败</span>
      </el-descriptions-item>
      <el-descriptions-item v-if="item.returnHttpCode != '200'" width="110px">
        <template #label>
          <div class="cell-item">
            <el-icon>
              <office-building />
            </el-icon>
            失败原因
          </div>
        </template>
        {{ item.returnOperationResult }}
      </el-descriptions-item>
    </el-descriptions>
  </el-dialog>

</template>

<script setup>
import { ref, nextTick, provide, onMounted, reactive, onBeforeUnmount } from "vue"
import { useStore } from "vuex"
import { getItem } from "@/utils/storage"
import { filterqx } from "@/utils/auth.js"
import { getApi } from "./api/tobAcco_api"
import { ElMessageBox, ElMessage } from "element-plus"
import { generateNewStyle, writeNewStyle } from "@/utils/theme"
const BGcolor = ref(getItem("ZtColor") ? getItem("ZtColor") : 'rgba(0, 116, 87, 1)')
const dialogTableVisible = ref(false)
const dialogTableData = ref([])
const store = useStore()
const timer = ref(null)
const visible = ref(true)
const iSTrue = ref(false)
const boxTop = ref(2)
const boxLeft = ref(-9)
const startX = ref(0)
const startY = ref(0)
const startBoxLeft = ref(0)
const startBoxTop = ref(0)
const isDragging = ref(false)
const box = ref(null);
const isRouterAlive = ref(true)
//网络监控
const netWorkStatus = reactive({
  effectiveType: null,
  rtt: null,
  downlink: null,
})
const isOnline = ref(true)
function startDrag(event) {
  isDragging.value = true;
  startX.value = event.clientX;
  startY.value = event.clientY;
  startBoxLeft.value = boxLeft.value;
  startBoxTop.value = boxTop.value;
  document.addEventListener('mousemove', onDrag);
  document.addEventListener('mouseup', stopDrag);
}
function onDrag(event) {
  if (!isDragging.value) return;
  const deltaX = event.clientX - startX.value;
  const deltaY = event.clientY - startY.value;
  boxLeft.value = startBoxLeft.value + deltaX;
  boxTop.value = startBoxTop.value + deltaY;
}
function stopDrag() {
  isDragging.value = false;
  document.removeEventListener('mousemove', onDrag);
  document.removeEventListener('mouseup', stopDrag);
}

//网络发生变动
navigator.connection.addEventListener('change', function (e) {
  updateNetWorkStatus(e.target)
})
//在线状态
window.addEventListener('online', (e) => {
  isOnline.value = true
})
//离线状态
window.addEventListener('offline', () => {
  isOnline.value = false
  ElMessage({
    message: "网络链接失败，请检查网络是否正常",
    type: "warning",
    duration: 5e3
  })
})
//获取网络参数
function updateNetWorkStatus(data) {
  netWorkStatus.effectiveType = data.effectiveType
  netWorkStatus.rtt = data.rtt
  netWorkStatus.downlink = data.downlink
}
generateNewStyle(store.getters.mainColor).then(newStyle => {
  writeNewStyle(newStyle)
})

const reload = () => {
  isRouterAlive.value = false
  nextTick(() => {
    isRouterAlive.value = true
  })
}

const handleClose = done => {
  ElMessageBox.confirm("你确定要关闭弹窗吗？").then(() => {
    done()
  })
}
provide("reload", reload)
const platformTip = () => {
  let platform = window.navigator.platform.toLowerCase()
  if (platform.includes("mac")) {
    if (window.devicePixelRatio !== 2)
      ElMessage({
        message: "当前显示不处于100%缩放状态！",
        type: "warning",
        duration: 5e3
      })
  } else if (platform.includes("win")) {
    if (window.devicePixelRatio !== 1)
      ElMessage({
        message: "当前显示不处于100%缩放状态！",
        type: "warning",
        duration: 5e3
      })
  }
}

onMounted(() => {
    //获取网络参数
  updateNetWorkStatus(navigator.connection)
  // 生产
  // if (process.env.NODE_ENV === 'production') {
  timer.value = setInterval(() => {
    if (getItem("deptId")) {
      if (filterqx("isaqbm") == "1" && filterqx("isSgs") == "1") {
        getApi({}, "/mosty-jcgl/dataCommon/getDataForRedis").then(res => {
          if (res && res.length) {
            dialogTableVisible.value = true
            dialogTableData.value = res
          }
        })
      }

    }
  }, 1e4)
  // }
  document.title = "六盘水烟草商业安全管理系统"
  if (BGcolor.value && BGcolor.value != null) {
    getcolorinit()
  }
  platformTip()
})
onBeforeUnmount(() => {
  clearInterval(timer.value)
  updateNetWorkStatus(navigator.connection)
})
function getcolorinit() {
  const html = document.documentElement
  html.style.setProperty("--el-color-primary", BGcolor.value)
  html.style.setProperty("--el-button-bg-color", BGcolor.value)
  html.style.setProperty("--el-bg-color", BGcolor.value)
  // const match = BGcolor.value.match(/\(([^)]+)\)/)[1].split(',');
  const match = BGcolor.value.substring(5, BGcolor.value.indexOf(")")).split(",")
  if (match) {
    const boderColor = `rgba(${match[0]}, ${match[1]}, ${match[2]}, ${0.75})` // 更新第四个参数的值
    const tabbackgroundColor = `rgba(${match[0]}, ${match[1]}, ${match[2]}, ${0.2})` // 更新第四个参数的值
    html.style.setProperty("--el-table-border-color", boderColor)
    html.style.setProperty("--el-border-color-lighter", boderColor)
    html.style.setProperty("--el-table-row-hover-bg-color", boderColor)
    html.style.setProperty("--el-table-cell-bmx-backgroundcolor", tabbackgroundColor)
  } else {
    return 1 // 如果匹配失败，返回默认值
  }
}
</script>

<style lang="scss">
@import "./styles/index.scss";

// @import '@/assets/style/main.scss';
@font-face {
  font-family: "DigifaceWide";
  src: url("~@/assets/font/DigifaceWide.ttf");
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: "DINAlternateBold";
  src: url("~@/assets/font/DINAlternateBold.ttf");
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: "title_family";
  src: url("~@/assets/font/交通标志专用字体.ttf");
  font-weight: normal;
  font-style: normal;
}

#app {
  overflow: hidden;
}

body {
  margin: 0px;
  padding: 0px;
  font-size: 14px;
  background: #efefef;
  overflow: hidden;
}

.el-input-group__append {
  width: 69px !important;
}

.el-button>span>i {
  color: #fff !important;
}

// 弹窗主题颜色
.el-dialog__header {
  background: var(--el-color-primary) !important;
  box-shadow: 0px 9px 16px 0px rgba(159, 162, 191, 0.1) !important;
  border-radius: 0px 0px 6px 6px !important;
  opacity: 1 !important;
  color: #fff;
}

.el-input {
  --el-input-placeholder-color: #75777b !important;
}

::-webkit-scrollbar-track {
  background-color: var(--el-table-cell-bmx-backgroundcolor) !important;
}

::-webkit-scrollbar-thumb {
  background-color: var(--el-color-primary) !important;
}

.container-dialog {
  background: red;
}

.el-dialog {
  .el-form-item__label {
    color: #060606;
  }
}

.qx-dialog {
  .el-dialog {
    height: 550px !important;
    overflow: hidden;

    .el-dialog__body {
      height: 80%;

      .el-tree {
        height: 100%;
        overflow: auto;
      }
    }
  }
}

ul {
  margin: 0px;
  padding: 0px;
}

li {
  list-style-type: none;
}

#app {
  font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #fff;
  overflow-x: auto;
}

//全局最外层盒子padding 字体  样式
.overall {
  font-size: 15px;

  // padding:0 20px;
  .titleBox {
    font-size: 20px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
  }
}

//只显示两排内容
.text_detail {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  text-overflow: ellipsis;
}

//只显示一排内容
.one_text_detail {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

//指令列表与时间样式
.bh_and_sj {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.fx_yh_model {
  padding: 5px 10px;
  position: relative;

  &::before {
    content: "";
    width: 0px;
    height: 0px;
    border-top: 10px solid #2b836b;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    position: absolute;
    bottom: -8px;
    left: 42px;
  }
}

.dialog-box {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 9;
  width: 100%;
  // padding: 0 10px;
  box-sizing: border-box;
  height: 100%;
  background-color: #fff;
  // background: url("~@/assets/system/bg1.png") no-repeat;
  // background-position: -300px 0;
}

::v-deep .el-cascader-menu__empty-text {
  display: none !important;
}

.el-collapse-item__header {
  border-bottom-color: var(--el-color-primary);
  color: #000 !important;
}

.el-collapse-item__content {
  color: #000 !important;
}

.el-table td.el-table__cell div span {}

.el-button--default {
  --el-button-hover-text-color: #fff !important;
}

//删除
.operdel {
  cursor: pointer;
  margin-left: 10px;
  line-height: 19px;
  color: #e71111 !important;
}

//下载
.operedown {
  cursor: pointer;
  margin-left: 10px;
  line-height: 19px;
  color: #234fa0 !important;
}

//修改
.operedit {
  cursor: pointer;
  line-height: 19px;
  color: #dc9507 !important;
  margin-left: 10px;
}

//详情
.operesee {
  cursor: pointer;
  line-height: 19px;
  color: #0b6c51 !important;
  margin-left: 10px;
}

// 复制
.operecopy {
  cursor: pointer;
  line-height: 19px;
  color: #23a07d !important;
  margin-left: 10px;
}

.pageRight {
  padding: 0 14px 14px;
  background: #fff;
}

.bigBox,
.usermanage {
  .title {
    padding-right: 6px;
  }
}

.model_box {
  background: #fff;
}

::v-deep .el-menu-item {
  color: #e71111 !important;
}

.el-menu--vertical {

  .el-tooltip__trigger,
  .el-menu-item {
    color: #000 !important;
  }
}

.but_bg {
  color: var(--el-color-primary);
}

.topapps {
  z-index: 20003;
  position: absolute;
  inset: 0px auto auto 0px;
  cursor: pointer;
  margin: 0px;
  transform: translate(1743px, 81px);
  padding: 5px 11px;
  border-radius: 5px;
  font-size: 12px;
  line-height: 20px;
  min-width: 10px;
  word-wrap: break-word;
  visibility: visible;
  color: #000;
  background-color: #d9d3d369 !important;
  border: 1px solid var(--el-color-primary);
  box-shadow: 0 0 2px var(--el-color-primary),
    /* 左上角 */
    0 0 2px var(--el-color-primary),
    /* 右下角 */
    0 0 2px var(--el-color-primary),
    /* 右上角 */
    0 0 2px var(--el-color-primary);
  /* 左下角 */
}
</style>
<style>
.el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {
  background-color: var(--el-color-primary) !important;
  color: #fff !important;
}
</style>
