<template>
  <div>
    <div class="titleBox">
      <div class="title">操作日志</div>
      <div class="btnBox"></div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="系统模块">
          <el-input placeholder="请输入系统模块" v-model="listQuery.title" clearable></el-input>
        </el-form-item>
        <el-form-item label="操作人员">
          <el-input placeholder="请输入操作人员" v-model="listQuery.operName" clearable></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select clearable v-model="listQuery.businessType" placeholder="请选择类型">
            <el-option v-for="item in businessTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select clearable v-model="listQuery.status" placeholder="请选择状态">
            <el-option label="正常" value="0"></el-option>
            <el-option label="异常" value="1"></el-option>
          </el-select>
        </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 stripe :data="tableData" border style="width: 100%" :height="tableHeight" :key="keyCount">
        <!-- <el-table-column label="#" type="index" /> -->
        <el-table-column sortable prop="operId" label="日志编号"></el-table-column>
        <el-table-column sortable prop="title" label="系统模块"></el-table-column>
        <el-table-column sortable label="操作类型" width="120px">
          <template #default="{ row }">
            <div>
              {{
                businessTypeArr.find((item) => item.value == row.businessType).label || "未知" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column sortable prop="requestMethod" label="请求方式"></el-table-column>
        <el-table-column sortable prop="operName" label="操作人员"></el-table-column>
        <el-table-column sortable prop="operIp" label="操作地址"></el-table-column>
        <el-table-column sortable label="操作状态" width="120px">
          <template #default="{ row }">
            <div>
              <el-tag size="small" type="success" v-if="row.status == 0">正常</el-tag>
              <el-tag size="small" type="danger" v-else-if="row.status == 1">异常</el-tag>
              <el-tag size="small" type="info" v-else>未知</el-tag>
            </div>
          </template>
        </el-table-column>

        <el-table-column sortable prop="operTime" label="操作时间" width="200px"></el-table-column>
        <!-- <template #default="{ row }">{{ $filters.dateFilter(row.createTime) }}</template> -->
        <el-table-column label="操作" fixed="right" width="100px">
          <template #default="{ row }">
            <el-button size="small" @click="handleDetail(row)">详情</el-button>
            <!-- <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon-color="red"
              title="确定要删除?"
              @confirm="delHandler(row)"
            >
              <template #reference>
                <el-button type="danger" size="small">删除</el-button>
              </template>
  </el-popconfirm> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="listQuery.page" :page-sizes="[2, 5, 10, 20]" :page-size="listQuery.size"
          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">{{ isEdit ? "详情" : "新增" }}</span>
        <div>
          <el-button size="small" @click="closeDialog">关闭</el-button>
        </div>
      </div>
      <el-form :model="dialogForm" class="mosty-from-wrap" :inline="true">
        <el-form-item class="one" label="系统模块" label-width="140px">{{
          dialogForm.title
        }}</el-form-item>
        <el-form-item class="one" label="请求方式" label-width="140px">{{
          dialogForm.requestMethod
        }}</el-form-item>
        <el-form-item class="one" label="操作人员" label-width="140px">{{
          dialogForm.operName
        }}</el-form-item>
        <el-form-item class="one" label="使用方法" label-width="140px">{{
          dialogForm.method
        }}</el-form-item>
        <el-form-item class="one" label="jsonResult" label-width="140px">
          {{ dialogForm.jsonResult }}
        </el-form-item>
        <el-form-item label="操作地址" label-width="140px">
          {{ dialogForm.operIp }}
        </el-form-item>

        <el-form-item class="one" label="操作状态" label-width="140px">
          {{ dialogForm.statu == 0 ? "正常" : "注销" }}
        </el-form-item>
        <el-form-item class="one" label="操作时间" label-width="140px">{{
          $filters.dateFilter(dialogForm.createTime)
        }}</el-form-item>
      </el-form>
    </div>
    <!-- <el-dialog
      draggable
      custom-class="way"
      v-model="dialogFormVisible"
      @closed="closeDialog"
      title="详情"
    >
      <el-form :model="dialogForm">
        <el-form-item label="系统模块" label-width="140px">{{
          dialogForm.title
        }}</el-form-item>
        <el-form-item label="请求方式" label-width="140px">{{
          dialogForm.requestMethod
        }}</el-form-item>
        <el-form-item label="操作人员" label-width="140px">{{
          dialogForm.operName
        }}</el-form-item>
        <el-form-item label="使用方法" label-width="140px">{{
          dialogForm.method
        }}</el-form-item>
        <el-form-item label="jsonResult" label-width="140px">
          <el-input
            readonly
            v-model="dialogForm.jsonResult"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="操作地址" label-width="140px">
          {{ dialogForm.operIp }}
        </el-form-item>

        <el-form-item label="操作状态" label-width="140px">
          <el-select
            readonly
            v-model="dialogForm.status"
            placeholder="请选择状态"
          >
            <el-option label="正常" :value="0"></el-option>
            <el-option label="注销" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间" label-width="140px">{{
          $filters.dateFilter(dialogForm.createTime)
        }}</el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog> -->
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
import {
  getOperlogList,
  operlogRemove,
  operlogClean,
  operlogDetail
} from "@/api/user-manage";
const { proxy } = getCurrentInstance();
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const keyCount = ref(0); //tabel组件刷新值
//查询参数
const total = ref(0);
const page = ref(1);
const size = ref(20);
const currentRow = ref({});
const listQuery = ref({
  page: 1,
  size: 20
});
const isEdit = ref(true);
const dialogForm = ref({});
const businessTypeArr = ref([
  {
    value: "0",
    label: "其他"
  },
  {
    value: "1",
    label: "新增"
  },
  {
    value: "2",
    label: "修改"
  },
  {
    value: "3",
    label: "删除"
  },
  {
    value: "4",
    label: "授权"
  },
  {
    value: "5",
    label: "导出"
  },
  {
    value: "6",
    label: "导入"
  },
  {
    value: "7",
    label: "强退"
  },
  {
    value: "8",
    label: "生成代码"
  },
  {
    value: "9",
    label: "清空数据"
  },
  {
    value: "10",
    label: "查询"
  }
]);
// 数据相关
const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
// 获取数据的方法
const getListData = async () => {
  const params = listQuery.value;
  params.current = params.page;
  const res = await getOperlogList(params);
  tableData.value = res?.records;
  total.value = Number(res.total);
};

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

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.size = currentSize;
  getListData();
};
function reset() {
  listQuery.value = {};
  listQuery.value.page = 1;
  listQuery.value.size = 20;
  getListData();
}
/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.page = currentPage;
  getListData();
};

const clearAll = async () => {
  const res = await operlogClean();
  ElMessage.success("删除成功");
  handleFilter();
};

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

const handleDetail = (row) => {
  dialogForm.value = { ...row };
  dialogFormVisible.value = true;
};
const onSave = () => {
  updateSysRole({
    ...dialogForm.value
  }).then((res) => {
    dialogFormVisible.value = false;
    ElMessage.success("修改成功");
    handleFilter();
  });
};

const onAdd = () => {
  addSysRole({
    ...dialogForm.value
  }).then((res) => {
    dialogFormVisible.value = false;
    ElMessage.success("新增成功");
    handleFilter();
  });
};

const delHandler = (row) => {
  operlogRemove({
    ids: [row.operId]
  }).then((res) => {
    ElMessage.success("删除成功");
    handleFilter();
  });
};

const closeDialog = () => {
  dialogForm.value = {};
  dialogFormVisible.value = false;
};
// 表格高度计算
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;
  }
}
</style>
