<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item width="100" prop="operName" :label="$t('staff.yhm')">
            <el-input
              v-model="queryForm.operName"
              size="small"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="time" :label="$t('defalult.rq')">
            <el-date-picker
              v-model="queryForm.time"
              size="small"
              type="daterange"
              align="right"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="changeTime"
            ></el-date-picker>
          </el-form-item>
          <el-form-item width="130" prop="businessType" label="操作类型">
            <el-select
              v-model="queryForm.businessType"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in businessType"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item
            width="130"
            prop="businessType"
            :label="$t('dataEnquiry.zt')"
          >
            <el-select
              v-model="queryForm.status"
              size="small"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusData"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">
        {{ $t('defalult.chongz') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
    </div>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        fixed
        type="selection"
        width="40"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="title"
        label="模块"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="businessType"
        label="操作类型"
        width="100"
        align="center"
      >
        <template #default="{ row }">
          <span>{{ getBusinessInfo(row.businessType) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="method"
        label="操作方法"
        width="450"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="requestMethod"
        label="请求方式"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="operName"
        label="操作人"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="operUrl"
        label="请求地址"
        width="150"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="operIp"
        label="操作IP"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="operLocation"
        label="操作地点"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="operParam"
        label="请求参数"
        width="1000"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="result"
        label="返回结果"
        width="260"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        :label="$t('dataEnquiry.zt')"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="errorMsg"
        label="错误信息"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="operTime"
        label="操作时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        prop="details"
        :label="$t('defalult.cz')"
        width="100"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
import {
  actionLogData,
  removeActionLog,
  exportActionLog,
  getBusinessType,
} from '@/api/log';
import { getDate, getPastTime } from '@/common/times';
import ajax from '@/api/download';
export default {
  data() {
    return {
      tableData: [],
      dialogTableVisible: false,
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      ids: [],
      statusData: [
        {
          value: 2,
          label: 'all',
        },
        {
          value: 0,
          label: '成功',
        },
        {
          value: 1,
          label: '失败',
        },
      ],
      queryForm: {
        page: 1,
        size: 20,
        operName: '',
        start: '',
        end: '',
        time: '',
        businessType: '',
        status: 2,
      },
      businessType: [],
    };
  },
  created() {
    this.fetchData();
    this.getBusinessType();
  },
  methods: {
    // 操作类型
    async getBusinessType() {
      const { data } = await getBusinessType();
      this.businessType = data;
    },
    // 操作类型回显
    getBusinessInfo(status) {
      var dataValue = '';
      this.businessType.forEach(function (item) {
        if (status === item.dataCode) {
          dataValue = item.dataValue;
        }
      });
      return dataValue;
    },
    changeTime() {
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    setDefaultTime() {
      this.queryForm.time = getPastTime(1);
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    async fetchData() {
      const { data } = await actionLogData(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
    },
    //搜索
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    //导出
    async exportData() {
      const url = await exportActionLog(this.queryForm);
      console.log(url);
      ajax.downloadPostFile(url, { fileName: '操作日志.xls' });
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    //删除
    deleteData(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = removeActionLog(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      that.deleteData([row.id]);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
  margin-bottom: 30px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
      .el-cascader {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
