<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :before-open="beforeOpen"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          size="small"
          @click="viewErrorMsg(scope.row)"
        >查看错误信息
        </el-button>
      </template>
    </avue-crud>

    <el-dialog title="错误信息" @close="refreshChange"
               append-to-body
               :visible.sync="messageBox"
               :before-close="messageBox == false"
               width="60%"
               heigh="40%">
      <el-input  v-model="errorMsg" autocomplete="off" :rows="30" type="textarea"></el-input>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {mapGetters} from "vuex";
  import {getAppErrorLogList, viewAppErrorLog} from "../../../api/logs";

  export default {
    data() {
      return {
        messageBox: false,
        errorMsg: "",
        form: {},
        selectionList: [],
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          menuWidth: 120,
          dialogType: 'drawer',
          column: [
            {
              label: "日志名称",
              prop: "fileName"
            },
            {
              label: "路径",
              prop: "path",
              hide: true
            },
            {
              label: "日志时间",
              prop: "createTime",
              width:'180',
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          viewBtn: this.vaildData(this.permission.log_usual_view, false)
        };
      }
    },
    methods: {
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          viewAppErrorLog(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getAppErrorLogList().then(res => {
          this.data = res.data.data;
          this.loading = false;
        });
      },
      viewErrorMsg(row) {
        viewAppErrorLog(row.fileName).then(res => {
          this.errorMsg = res.data.data;
        });
        this.messageBox = true;
      }
    }
  };
</script>

<style>
</style>
