<template>
  <div class="app-container">
    <!-- <doc-alert title="系统日志" url="https://doc.iocoder.cn/system-log/" /> -->
    <!-- 搜索工作栏 -->
    <search-bar
      :formConfigs="formConfig"
      ref="searchBarForm"
      @headBtnClick="buttonClick"
    />
    <!-- 列表 -->
    <base-table
      :page="queryParams.pageNo"
      :limit="queryParams.pageSize"
      :table-props="tableProps"
      :table-data="list"
      :max-height="tableH"
    >
      <method-btn
        v-if="tableBtn.length"
        slot="handleBtn"
        :width="80"
        label="操作"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>

    <pagination
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <!-- 操作日志详细 -->
    <base-dialog
      dialogTitle="访问日志详细"
      :dialogVisible="open"
      width="50%"
      @close="open = false"
    >
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="日志主键：">{{ form.id }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="链路追踪：">{{ form.traceId }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="用户信息："
              >{{ form.userId }} | {{ form.userNickname }} | {{ form.userIp }} |
              {{ form.userAgent }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作信息：">
              {{ form.module }} | {{ form.name }}
              <dict-tag
                :type="DICT_TYPE.SYSTEM_OPERATE_TYPE"
                :value="form.type"
              />
              <br />
              {{ form.content }} <br />
              {{ form.exts }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求信息："
              >{{ form.requestMethod }} | {{ form.requestUrl }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="方法名：">{{ form.javaMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="方法参数：">{{
              form.javaMethodArgs
            }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="开始时间：">
              {{ parseTime(form.startTime) }} | {{ form.duration }} ms
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作结果：">
              <div v-if="form.resultCode === 0">
                正常 | {{ form.resultData }}
              </div>
              <div v-else-if="form.resultCode > 0">
                失败 | {{ form.resultCode }} || {{ form.resultMsg }}
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </base-dialog>
  </div>
</template>

<script>
import { listOperateLog, exportOperateLog } from "@/api/system/operatelog";
import tableHeightMixin from "@/mixins/tableHeightMixin";
import statusBtn3 from "./../components/statusBtn3.vue";
const tableProps = [
  {
    prop: "id",
    label: "日志编号",
  },
  {
    prop: "module",
    label: "操作模块",
    minWidth: 150,
    showOverflowtooltip: true,
  },
  {
    prop: "name",
    label: "操作名",
    minWidth: 200,
    showOverflowtooltip: true,
  },
  {
    prop: "userNickname",
    label: "操作人",
    minWidth: 80,
    showOverflowtooltip: true,
  },
  {
    prop: "resultCode",
    label: "操作结果",
    subcomponent: statusBtn3,
  },
  {
    prop: "duration",
    label: "执行时长",
    minWidth: 80,
    showOverflowtooltip: true,
  },
];
export default {
  name: "SystemOperateLog",
  mixins: [tableHeightMixin],
  data() {
    return {
      formConfig: [
        {
          type: "input",
          label: "系统模块",
          placeholder: "系统模块",
          param: "module",
        },
        {
          type: "input",
          label: "操作人员",
          placeholder: "操作人员",
          param: "userNickname",
        },
        {
          type: "select",
          label: "类型",
          selectOptions: this.getDictDatas(this.DICT_TYPE.SYSTEM_OPERATE_TYPE),
          labelField: "label",
          valueField: "value",
          param: "type",
        },
        {
          type: "select",
          label: "状态",
          selectOptions: [
            { id: true, name: "成功" },
            { id: false, name: "失败" },
          ],
          param: "success",
        },
        {
          type: "datePicker",
          label: "操作时间",
          dateType: "daterange",
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
          rangeSeparator: "-",
          startPlaceholder: "开始日期",
          endPlaceholder: "结束日期",
          param: "startTime",
          defaultSelect: [],
          defaultTime: ["00:00:00", "23:59:59"],
          width: 250,
        },
        {
          type: "button",
          btnName: "查询",
          name: "search",
          color: "primary",
        },
        {
          type: this.$auth.hasPermi("system:operate-log:export")
            ? "separate"
            : "",
        },
        {
          type: this.$auth.hasPermi("system:operate-log:export")
            ? "button"
            : "",
          btnName: "导出",
          name: "export",
          color: "primary",
          plain: true,
        },
      ],
      tableProps,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      tableBtn: [
        {
          type: "detail",
          btnName: "详情",
        },
      ],
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        module: undefined,
        userNickname: undefined,
        businessType: undefined,
        success: undefined,
        startTime: [],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志 */
    getList() {
      listOperateLog(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal
        .confirm("是否确认导出所有操作日志数据项?")
        .then(() => {
          // 处理查询参数
          let params = { ...this.queryParams };
          params.pageNo = undefined;
          params.pageSize = undefined;
          this.exportLoading = true;
          return exportOperateLog(params);
        })
        .then((response) => {
          this.$download.excel(response, "操作日志.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    buttonClick(val) {
      this.queryParams.module = val.module;
      this.queryParams.userNickname = val.userNickname;
      this.queryParams.type = val.type;
      this.queryParams.success = val.success;
      this.queryParams.startTime = val.startTime;
      if (val.btnName === "search") {
        this.queryParams.pageNo = 1;
        this.getList();
      } else {
        this.handleExport();
      }
    },
    handleClick(val) {
      this.handleView(val.data);
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  width: 100%;
  height: calc(100vh - 120px - 8px);
  background-color: #fff;
  border-radius: 8px;
  padding: 8px;
}
</style>
