<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">操作日志</span>
    </div>
    <div class="scroll">
      <div class="middle">
        <div class="filter">
          <div class="condition">
            <span>操作人 :</span>
            <el-input
              clearable
              v-model.trim="searchInput.userName"
              class="w-50 m-2"
              placeholder="请输入操作人"
              style="width: 180px"
            />
          </div>
          <div class="condition">
            <span>操作模块 :</span>
            <el-input
              clearable
              v-model.trim="searchInput.operMode"
              class="w-50 m-2"
              placeholder="请输入操作模块"
              style="width: 180px"
            />
          </div>
          <div class="condition">
            <span>日志级别 :</span>
            <el-select
              clearable
              v-model="searchInput.olevelId"
              placeholder="请选择日志级别"
            >
              <el-option
                v-for="i in logList"
                :key="i.id"
                :label="i.name"
                :value="i.id"
              />
            </el-select>
          </div>
          <div class="condition">
            <span>操作时间 :</span>
            <el-date-picker
              v-model="searchInput.createTime"
              unlink-panels
              type="datetimerange"
              range-separator="-"
              start-placeholder="起始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择日期"
            />
          </div>
          <el-button
            style="margin-left: 20px"
            color="rgba(38, 151, 255, 1)"
            type="primary"
            class="search"
            @click="searchBtn"
            ><span>查询</span></el-button
          >
          <el-button @click="resetBtn" plain color="rgba(43, 153, 255, 1)"
            >重置</el-button
          >
        </div>
      </div>
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '50px',
            border: 0,
          }"
        >
          <!-- <el-table-column type="selection" align="center" width="55" /> -->
          <el-table-column align="center" prop="createTime" label="操作时间" />
          <el-table-column
            width="100"
            align="center"
            prop="userName"
            label="操作人"
          >
          </el-table-column>
          <el-table-column align="center" prop="operMode" label="操作模块" />
          <el-table-column align="center" prop="otype" label="操作类型" />
          <el-table-column align="center" prop="operation" label="操作内容" />
          <el-table-column align="center" prop="ip" label="IP地址" />
          <el-table-column align="center" prop="olevel" label="日志级别">
            <template #default="{ row }">
              <span v-if="row.olevel == '信息'" style="color: #409eff">{{row.olevel}}</span>
              <span v-if="row.olevel == '警告'" style="color: #e6a23c">{{row.olevel}}</span>
              <span v-if="row.olevel == '错误'" style="color: #f56c6c">{{row.olevel}}</span>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="pageSize">
          <span></span>
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            layout="total,sizes, prev, pager, next, jumper, slot"
            :total="total"
            @size-change="handleSizeChange"
            @update:current-page="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  watch,
  reactive,
  nextTick,
  onBeforeMount,
  onUnmounted,
} from "vue";
import { useRouter } from "vue-router";
import { genFileId, ElMessage, ElMessageBox } from "element-plus";
import { dayjs } from "element-plus";
import lodash from "lodash";

import { storeToRefs } from "pinia";
import { useCounterStore } from "@/stores/index";

import { getQueryLogPages, getQueryLogLevel } from "@/api/operation-log";

const router = useRouter();
const store = useCounterStore();

// 为避免解构时失去响应性
const { name, age, isCollapse, realAge, collegeRole } = storeToRefs(store);

const menuList = ref({
  canAll: null,
  canApply: null,
  canDeleted: null,
  canDetail: null,
  canExport: null,
  canImport: null,
  canInsert: null,
  canTop: null,
  canUpdate: null,
  canView: null,
});

// 表格数据
const loading = ref(false);
const tableData = reactive({
  list: [{}],
});
const logList = ref([]); // 日志级别下拉列表数据

const searchInput = reactive({
  userName: "",
  operMode: null,
  olevelId: null,
  createTime: null,
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(0); // 当前总数
const selectIds = ref([]); // 勾选的全部数据

// 获取账户列表
const getList = async () => {
  loading.value = true;
  let params = {
    currentPage: currentPage.value, // 当前页
    pageCount: pageSize.value, // 一页数据条数
    userName: searchInput.userName,
    operMode: searchInput.operMode,
    olevelId: searchInput.olevelId,
  };
  if (searchInput.createTime) {
    params.startTime = searchInput.createTime[0];
    params.endTime = searchInput.createTime[1];
  }
  let res = await getQueryLogPages(params);
  console.log(res);
  if (res.code == 200) {
    tableData.list = res.data.list;
    total.value = res.data.totalCount;
    loading.value = false;
  } else {
    loading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};

// 搜索功能
const searchBtn = lodash.debounce(async () => {
  currentPage.value = 1;
  getList();
}, 300);
const resetBtn = lodash.debounce(async () => {
  searchInput.userName = "";
  searchInput.operMode = "";
  searchInput.olevelId = "";
  searchInput.createTime = null;
  currentPage.value = 1;
  getList();
}, 300);

// 多选框功能
const handleSelectionChange = (val) => {
  // console.log(val);
  selectIds.value = val.map((i) => i.id);
  console.log(selectIds.value);
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 每页显示条数
const handleSizeChange = (value) => {
  console.log(value, "每页显示条数");
  pageSize.value = value;
  getList();
};
// 分页
const handleCurrentChange = (value) => {
  // console.log(value);
  currentPage.value = value;
  getList();
};
const allLogLevel = async () => {
  let res = await getQueryLogLevel();
  console.log(res, "日志级别下拉列表数据");
  if (res.code == 200) {
    logList.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
onBeforeMount(() => {
  getList();
  allLogLevel();
  let list = JSON.parse(sessionStorage.getItem("roleList"));
  console.log(list);
  list.forEach((i) => {
    if (i.menuName == "操作日志") {
      i.childs.forEach((item) => {
        if (item.menuName == "操作日志") {
          menuList.value = item;
        }
      });
    }
  });
  console.log(menuList.value);
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  width: calc(100% - 40px);
  height: calc(100% - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #000;
  display: flex;
  flex-direction: column;

  .svg {
    width: 22px;
    height: 22px;
  }

  .left {
    width: calc(100% - 60px);
    height: 60px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;
    .camera {
      margin-right: 15px;
      color: #4392f7;
    }
  }
  .scroll {
    width: calc(100% - 60px);
    height: calc(100% - 61px);
    margin: 0 auto;
    display: flex;
    flex-direction: column;

    .middle {
      width: calc(100%);
      color: #000;
      .filter {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        .search {
          margin-left: 0 !important;
          color: #fff;
        }
        .condition {
          display: flex;
          align-items: center;
          margin: 10px 30px 10px 0;
          :deep(.el-input .el-input__inner) {
            font-size: 14px;
          }
          .el-select {
            width: 220px;
          }
          span {
            margin: 0 10px 0 0;
          }
        }
      }
      .gongneng {
        margin: 10px 0;
        .el-button {
          color: #fff;
          margin-right: 15px;
        }
      }
    }
    .footer {
      width: calc(100%);
      flex: 1;
      margin: 0 auto;
      overflow: auto;

      .el-table--fit {
        height: calc(100% - 60px);
        :deep(.el-table__header-wrapper) {
          background-color: #000;
          font-size: 15px;
          color: #000;
          .cell {
            color: #000;
          }
        }
        :deep(.el-table__row) {
          height: 50px;
          font-size: 15px;
          color: #000;
        }
        :deep(.el-table__row td) {
          padding: 0;
          border: 0;
        }

        .el-button--primary {
          margin-left: 5px;
        }
        :deep(.el-table__body .even) {
          background-color: #fff;
        }
        :deep(.el-table__body .odd) {
          background-color: rgba(240, 243, 247, 1);
        }
        :deep(.options) {
          display: flex;
          justify-content: center;
          align-items: center;
          .open {
            color: #2697ff;
            cursor: pointer;
            margin: 0 15px 0 0;
          }
          .edit {
            margin: 0 15px 0 0;
            color: #2697ff;
            cursor: pointer;
          }
          .delete {
            color: #f56c6c;
            cursor: pointer;
          }
        }
      }

      .pageSize {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin: 0 30px;
        height: 60px;

        span {
          color: #000;
        }

        .el-pagination {
          // width: 1600px;
          :deep(.el-pagination__total) {
            color: #000;
          }

          :deep(.el-pagination__goto) {
            color: #000;
          }

          :deep(.el-pagination__classifier) {
            color: #000;
          }

          :deep(.el-input__wrapper) {
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            box-shadow: none;
          }

          :deep(.el-pager li) {
            margin: 0 5px;
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            background-color: transparent;
          }

          :deep(.el-pager li.is-active) {
            // background-color: rgba(0, 97, 255, 0.8);
            border: 1px solid rgba(0, 97, 255, 1);
            color: rgba(0, 97, 255, 1);
          }

          :deep(.btn-prev) {
            margin-right: 5px;
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            background-color: transparent;
          }

          :deep(.btn-next) {
            margin-left: 5px;
            border: 1px solid rgba(0, 0, 0, 1);
            border-radius: 5px;
            background-color: transparent;
          }
        }
      }
    }
  }

  // 添加员工弹窗样式
  :deep(.addStaff) {
    .el-dialog__body {
      padding: 20px 20px 10px 20px;
      .el-input {
        width: 400px;
        .el-input__suffix-inner {
          color: rgba(61, 81, 232, 1);
        }
      }
      .el-select {
        width: 400px;
      }
      .el-tree {
        width: 400px;
      }
    }
  }

  // 导入
  :deep(.projectImport) {
    .el-dialog__body {
      padding: 20px 30px 0 30px;
      .title {
        color: red;
      }
      .down {
        span:nth-child(1) {
          color: rgba(127, 127, 127, 1);
        }
        span:nth-child(2) {
          margin-left: 25px;
          color: rgba(2, 167, 240, 1);
          cursor: pointer;
        }
      }
      .options {
        margin: 50px 0 30px 0;
        display: flex;
        flex-direction: row-reverse;
        .queding {
          margin-left: 20px;
          background: linear-gradient(
            90deg,
            rgba(38, 151, 255, 1) 0%,
            rgba(102, 181, 255, 1) 100%
          );
          border: none;
        }
        .quxiao {
          border: 1px solid rgba(43, 151, 252, 1);
          color: rgba(43, 151, 252, 1);
        }
      }
    }
  }
}
</style>
