<template>
  <div class="full-container-p24">
    <div style="display: flex; justify-content: space-between">
      <div style="display: flex">
        <el-input
          placeholder="请输入部门/职位名称"
          prefix-icon="el-icon-search"
          v-model="term"
          clearable
          style="height: 32px; width: 330px; line-height: 32px"
          @keyup.enter.native="getRoleList"
        ></el-input>
        <div class="seach-btn" @click="getRoleList">查询</div>
      </div>
      <el-button v-auth="'ADDUPDATEROLE'" type="primary" @click="addRole"
        >添加职位</el-button
      >
    </div>
    <div style="margin-top: 24px">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          min-width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="roleName"
          label="职位名称"
          min-width="100"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="departmentName"
          label="部门"
          min-width="100"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          min-width="160"
          align="center"
        >
          <template v-slot="scope">
            {{ subTimeStr(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          v-auth="'ADDUPDATEROLE'"
          label="操作"
          min-width="120"
          align="center"
        >
          <template v-slot="scope">
            <el-button
              @click="roleDetail(scope.row.id)"
              size="small"
              type="primary"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="width: 100%; text-align: right; margin-top: 43px">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import api from "../../apis/fetch";
import { ref, onMounted, reactive } from "vue";
import { ElNotification, ElMessageBox } from "element-plus";
import router from "../../router";
import store from "../../store";
import { subTimeStr } from "../../utils/utils";

let tableData = ref([]);
let currentPage = ref(1);
let total = ref(0);
let term = ref("");
let loginAccountId = ref(0);

async function getRoleList() {
  let res = await api.getRoleList({
    term: term.value,
    currentPage: currentPage.value,
    size: 10,
    loginAccountId: loginAccountId.value,
  });
  if (res.data.status == 0) {
    tableData.value = res.data.result;
    total.value = res.data.total;
  } else {
    tableData.value = [];
    total.value = 0;
  }
}

function pageChange(e) {
  currentPage.value = e;
  getRoleList();
}

function addRole() {
  router.push("/authManage/addRole");
}

function roleDetail(roleId) {
  router.push({
    path: "/authManage/addRole",
    query: {
      roleId,
    },
  });
}

onMounted(() => {
  loginAccountId.value = localStorage.loginAccountId;
  getRoleList();
});
</script>

<style scoped>
.seach-btn {
  display: inline-block;
  width: 60px;
  height: 38px;
  background: #0094fe;
  border-radius: 2px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  text-align: center;
  line-height: 38px;
  margin-left: 10px;
  cursor: pointer;
}
</style>