<template>
  <div>
    <div class="titleBox">
      <div class="title">服务监控</div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="服务名称">
          <el-input placeholder="请输入服务名称" v-model="listQuery.serviceNameParam" clearable></el-input>
        </el-form-item>
        <el-form-item label="分组名称">
          <el-input placeholder="请输入分组名称" v-model="listQuery.groupNameParam" clearable></el-input>
        </el-form-item>
        <el-form-item label="隐藏空服务" prop="hasIpCount">
          <el-switch v-model="listQuery.hasIpCount" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleFilter"> 查询 </el-button>
          <el-button type="primary" @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table :data="tableData" border stripe row-key="id" :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%" :height="tableHeight" :key="keyCount">
        <!-- <el-table-column label="#" type="index" /> -->
        <!-- <el-table-column prop="id" label="id"> </el-table-column> -->
        <el-table-column sortable prop="name" label="服务名" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="groupName" label="分组名称" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="clusterCount" label="集群数目" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="ipCount" label="实例数" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="healthyInstanceCount" label="健康实例数" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="triggerFlag" label="触发保护阈值" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="260">
          <template #default="{ row }">
            <el-button type="primary" size="mini" @click="seeClick(row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination size class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent" :page-sizes="[2, 5, 10, 20]" :page-size="listQuery.pageSize"
          layout="total, prev, pager, next, sizes, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">服务详情</span>
        <div>
          <el-button size="small" @click="closeDialog">关闭</el-button>
        </div>
      </div>
      <el-table :data="infoTableData" border stripe row-key="id" :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%" height="500px" :key="keyCount">
        <!-- <el-table-column label="#" type="index" /> -->
        <!-- <el-table-column prop="id" label="id"> </el-table-column> -->
        <el-table-column sortable prop="ip" label="ip" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="port" label="端口" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="ephemeral" label="临时实例" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="weight" label="权重" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable prop="healthy" label="健康状态" show-overflow-tooltip>
        </el-table-column>
        <el-table-column sortable width="320px" prop="newmetadata" label="元数据" show-overflow-tooltip>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, reactive, onMounted, getCurrentInstance, onUnmounted } from "vue";
import {
  getAllSysDict,
  updateSysDict,
  addSysDict,
  deleteSysDict
} from "@/api/sysDict";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const keyCount = ref(0); //tabel组件刷新值
const elform = ref(null);
const router = useRouter();
//查询参数
const total = ref(0);
const buttonLoading = ref(false);
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20,
  groupNameParam: "",
  serviceNameParam: "",
  hasIpCount: true,
  accessToken: ""
});
const reset = () => {
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 20,
    groupNameParam: "",
    serviceNameParam: "",
    hasIpCount: true
  };
  getListData();
};
const infoTableData = ref([]); //详情得表格数据
const isEdit = ref(true);
const dialogForm = ref({});
// 数据相关
const tableData = ref([]);
const dialogFormVisible = ref(false);
// 获取数据的方法
const getListData = async () => {
  listQuery.value.accessToken = localStorage.getItem("accessToken");
  getApi(listQuery.value, "/mosty-base/monitor/getCloudService").then((res) => {
    localStorage.setItem("accessToken", res.accessToken);
    tableData.value = res?.data.serviceList;
    total.value = Number(res.data.count);
  });
};

const handleFilter = () => {
  listQuery.value.current = 1;
  getListData();
};
getListData();

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};

const seeClick = (row) => {
  dialogFormVisible.value = true;
  let accessToken = localStorage.getItem("accessToken");
  getApi(
    { accessToken, serviceName: row.name, groupName: row.groupName },
    "/mosty-base/monitor/getCloudServiceInfo"
  ).then((res) => {
    infoTableData.value = res.data.list.map((item) => {
      for (let j in item.metadata) {
        item.newmetadata = j + "=" + item.metadata[j];
      }
      return item;
    });
  });
};

/**修改字典 */
const updateDict = (row) => {
  isEdit.value = true;
  dialogForm.value = { ...row };
  dialogForm.value.zdywlb = Number(dialogForm.value.zdywlb);

  dialogFormVisible.value = true;
};

const delDict = (row) => {
  deleteSysDict({
    id: Number(row.id)
  }).then((res) => {
    ElMessage.success("注销成功");
    handleFilter();
  });
};

const addDict = (row) => {
  isEdit.value = false;
  dialogForm.value = {};
  dialogFormVisible.value = true;
};
const isAvailable = (val) => {
  if (val === 0) return true;
  if (
    val == null ||
    val == undefined ||
    val == "" ||
    val == "undefined" ||
    val == "null"
  ) {
    return false;
  } else {
    return true;
  }
};
const onSave = () => {
  elform.value.validate((valid) => {
    if (!valid) {
      ElMessage.error("请完成必填项！");
      return false;
    }
    buttonLoading.value = true;
    updateSysDict({
      bz: dialogForm.value.bz,
      id: dialogForm.value.id,
      yybz: dialogForm.value.yybz,
      zdbh: dialogForm.value.zdbh,
      zdlx: dialogForm.value.zdlx,
      zdmc: dialogForm.value.zdmc,
      zdywlb: dialogForm.value.zdywlb,
      hasIpCount: dialogForm.value.hasIpCount
    })
      .then((res) => {
        dialogFormVisible.value = false;
        ElMessage.success("修改成功");
        buttonLoading.value = false;
        handleFilter();
      })
      .finally(() => {
        buttonLoading.value = false;
      });
  });
};

const onAdd = () => {
  elform.value.validate((valid) => {
    if (!valid) {
      ElMessage.error("请完成必填项！");
      return false;
    }
    buttonLoading.value = true;
    addSysDict({
      bz: dialogForm.value.bz,
      // id: dialogForm.value.id,
      yybz: dialogForm.value.yybz,
      zdbh: dialogForm.value.zdbh,
      zdlx: dialogForm.value.zdlx,
      zdmc: dialogForm.value.zdmc,
      zdywlb: dialogForm.value.zdywlb
    })
      .then((res) => {
        ElMessage.success("添加成功");
        dialogFormVisible.value = false;
        buttonLoading.value = false;
        handleFilter();
      })
      .finally(() => {
        buttonLoading.value = false;
      });
  });
};

const closeDialog = () => {
  dialogForm.value = {};
  dialogFormVisible.value = false;
};

const toGoPage = (row) => {
  router.push(`/dict/detail/${row.zdbh}`);
};
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value =
    window.innerHeight - 57 - 37 - searchBox.value.offsetHeight - 130;
};
onMounted(() => {
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
// .user-manage-container {
//   .table-header-wrap {
//     margin-bottom: 22px;
//   }

//   ::v-deep .avatar {
//     width: 60px;
//     height: 60px;
//     border-radius: 50%;
//   }

//   ::v-deep .el-tag {
//     margin-right: 6px;
//   }

//   .pagination {
//     margin-top: 20px;
//   }
// }
.dialog {
  padding: 0 20px;
  box-sizing: border-box;
}

.head_box {
  margin: 0 !important;
  margin-bottom: 20px !important;
}
</style>
