<template>
  <div class="page_info">
    <div class="search_pannel">
      <el-form
        ref="dataForm"
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="dataForm.username"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="gender" label="性别">
          <el-select v-model="dataForm.gender" clearable>
            <el-option label="男" value="0"></el-option>
            <el-option label="女" value="1"></el-option>
          </el-select>
        </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:user:save')"
          icon="el-icon-plus"
          type="primary"
          @click="addOrUpdateHandle()"
          >{{ $t("add") }}</el-button
        >
        <el-button
          v-if="$hasPermission('sys:user: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="username"
            label="用户名"
            header-align="center"
            align="center"
          ></el-table-column>

          <el-table-column
                  prop="realName"
                  label="名称"
                  header-align="center"
                  align="center"
          ></el-table-column>

          <el-table-column
            prop="roleNames"
            label="角色"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="mobile"
            label="手机号"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="gender"
            label="性别"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              {{
                scope.row.gender == 0
                  ? "男"
                  : scope.row.gender == 1
                  ? "女"
                  : "保密"
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="状态"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag
                v-if="scope.row.status === 0"
                size="small"
                type="danger"
                >{{ $t("user.status0") }}</el-tag
              >
              <el-tag v-else size="small" type="success">{{
                $t("user.status1")
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="createDate"
            label="创建时间"
            header-align="center"
            align="center"
            width="150"
          ></el-table-column>
          <el-table-column
            label="操作"
            :resizable="false"
            fixed="right"
            header-align="center"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <el-link
                v-if="$hasPermission('sys:user:update')"
                type="primary"
                :underline="false"
                @click="addOrUpdateHandle(scope.row.id)"
                >编辑</el-link
              >
              <el-link
                v-if="$hasPermission('sys:user: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 "./user-add-or-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      screenHeight: "512px",
      mixinViewModuleOptions: {
        getDataListURL: "/sys/user/page",
        getDataListIsPage: true,
        deleteURL: "/sys/user",
        deleteIsBatch: true,
        exportURL: "/sys/user/export",
      },
      dataForm: {
        username: "",
        gender: "",
      },
    };
  },
  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>

