<template>
  <div class="page_info">
    <div class="search_pannel">
      <el-form
        ref="dataForm"
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
        @submit.native.prevent
      >
        <el-form-item prop="name" label="">
          <el-input
            v-model="dataForm.name"
            maxlength="100"
            placeholder="请输入编码或名称"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn">
        <el-button type="primary" @click="getDataList">{{
          $t("query")
        }}</el-button>
        <el-button plain @click="resetForm('dataForm')">重置</el-button>
      </div>
    </div>
    <div class="card_info">
      <div class="function_btn">
        <el-button
          v-if="$hasPermission('sys:role:save')"
          icon="el-icon-plus"
          type="primary"
          @click="addOrUpdateHandle()"
          >{{ $t("add") }}</el-button
        >
        <el-button
          v-if="$hasPermission('sys:role:delete')"
          icon="el-icon-delete"
          plain
          class="btn_color"
          @click="deleteHandle()"
          >{{ $t("deleteBatch") }}</el-button
        >
      </div>
      <div class="info_list">
        <el-table
          v-loading="dataListLoading"
          :data="dataList"
          stripe
          :header-cell-style="{ background: '#e6e7ea', color: '#0C1935' }"
          @selection-change="dataListSelectionChangeHandle"
          @sort-change="dataListSortChangeHandle"
          style="width: 100%"
          :height="screenHeight"
        >
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="编码"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="remark"
            label="名称"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="createDate"
            label="创建时间"
            header-align="center"
            align="center"
            width="180"
          ></el-table-column>
          <el-table-column
            label="操作"
            fixed="right"
            header-align="center"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <el-link
                v-if="$hasPermission('sys:role:update')"
                type="primary"
                :underline="false"
                @click="addOrUpdateHandle(scope.row.id)"
                >编辑</el-link
              >
              <el-link
                v-if="$hasPermission('sys:role:delete')"
                type="primary"
                :underline="false"
                @click="deleteHandle(scope.row.id)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <template v-if="total > 0">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        >
        </el-pagination>
      </template>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <el-drawer
      size="25%"
      :visible.sync="drawer"
      :with-header="false"
      :modal="false"
      @close="closedrawer"
    >
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
      ></add-or-update>
    </el-drawer>
  </div>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./role-add-or-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      screenHeight: "512px",
      mixinViewModuleOptions: {
        getDataListURL: "/sys/role/page",
        getDataListIsPage: true,
        deleteURL: "/sys/role",
        deleteIsBatch: true,
      },
      dataForm: {
        name: "",
      },
    };
  },
  components: {
    AddOrUpdate,
  },
  mounted() {
    this.setTableHeight();
    // 页面创建完成后添加监听
    window.addEventListener("resize", this.setTableHeight);
  },
  methods: {
    setTableHeight() {
      setTimeout(() => {
        this.screenHeight = getComputedStyle(
          document.querySelector(".info_list")
        ).height;
      }, 1000);
    },
    //表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  destroyed() {
    window.removeEventListener("resize", this.setTableHeight);
  },
};
</script>
