<template>
  <div class="oplog pagePadding">
    <ex-table
      v-loading="loading"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row>
            <el-col :span="24" style="padding: 0 0 10px 80px">
              <el-col :span="3" style="width: 160px">
                <el-input
                  :size="searchSize"
                  v-model="parmValue.role"
                  :maxlength="40"
                  placeholder="用户角色"
                />
              </el-col>
              <el-col :span="3" style="width: 170px; padding: 0 0 0 10px">
                <el-input
                  v-model="parmValue.action"
                  :maxlength="40"
                  :size="searchSize"
                  placeholder="模块名称"
                />
              </el-col>
              <el-col :span="3" style="width: 170px; padding: 0 0 0 10px">
                <el-input
                  v-model="parmValue.event"
                  :maxlength="40"
                  :size="searchSize"
                  placeholder="操作事件"
                />
              </el-col>

              <el-col :span="3" style="width: 54px" class="fr">
                <el-button
                  type="primary"
                  :size="searchSize"
                  class="fr"
                  icon="el-icon-search"
                  @click="searchList"
                />
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="6" style="width: 322px; padding-right: 0">
                <period-date-picker
                  :start="parmValue.startTime"
                  :end="parmValue.endTime"
                  :type="1"
                  :width="'145px'"
                  :size="searchSize"
                  @timeReturned="timeReturned($event)"
                />
              </el-col>

              <el-col
                :span="3"
                style="width: 66px; float: right"
              >
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
              <el-col
                :span="3"
                style="width: 66px"
              >
                <el-button
                  type="warning"
                  class="fr"
                  :size="searchSize"
                  @click="restSearch"
                >
                  重置
                </el-button>
              </el-col>
            </el-col>
          </el-row>
        </div>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/system/oplog";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "oplog",
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "oplog"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        role: "", // 账户
        action: "", // 姓名
        event: "",
        startTime: "",
        endTime: "",
        name: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "name",
          label: "账号",
        },
        {
          prop: "rolename",
          label: "角色",
        },
        {
          prop: "info",
          label: "模块",
        },
        {
          prop: "url",
          label: "事件",
        },
        {
          prop: "addtime",
          label: "操作时间",
          sortable: true,
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        role: "", // 账户
        action: "", // 姓名
        event: "",
        startTime: "",
        endTime: "",
        name: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },
    async timeReturned(e) {
      if (e.startTime !== "") {
        this.parmValue.startTime = e.startTime;
      } else {
        this.parmValue.startTime = "";
      }

      if (e.endTime !== "") {
        this.parmValue.endTime = e.endTime;
      } else {
        this.parmValue.endTime = "";
      }
      if (this.parmValue.startTime !== "" && this.parmValue.endTime !== "") {
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    // 新建/编辑/详情
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    /**
     * 启用/禁用
     * @param {String} id id
     * @param {String} status 0-禁用 1-启用
     */
    async changeStatus(id, status) {
      await this.$confirm(`确定要改为${status === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            roleid: id,
            status: status === "1" ? "0" : "1",
          };
          const res = await asyncRequest.delete(model);
          if (res.code === 0) {
            this.$notify.success({
              title: "状态修改成功",
              message: "",
            });
            this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },

    // 刷新表格
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        // this.tableData.map((v1) => {
        //   v1.addtime = v1.addtime.replace(".000", "");
        //   return v1;
        // });
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

