<template>
  <div class="role pagePadding">
     <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
    <el-row :gutter="10">
      <el-col :span="24" style="padding: 0 0 18px 0">
        <el-col :span="3">
          <el-select
            v-model="parmValue.status"
            filterable
            clearable
            :size="searchSize"
            placeholder="角色状态"
            style="width: 100%"
          >
            <el-option
              v-for="item in statusList"
              :key="'status' + item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-col>

        <el-col :span="3">
          <el-select
            :size="searchSize"
            v-model="parmValue.level"
            filterable
            placeholder="角色等级"
            style="width: 100%"
          >
            <el-option
              v-for="item in roleLevel"
              :key="'ruleid' + item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-input
            :size="searchSize"
            v-model="parmValue.role_name"
            :maxlength="40"
            placeholder="角色名称"
          />
        </el-col>

        <el-col :span="3" style="width: 152px">
          <el-button
            type="primary"
            :size="searchSize"
            icon="el-icon-search"
            @click="searchList"
          />
          <el-button type="warning" v-if="powers.some((item) => item == '024')"  :size="searchSize" @click="restSearch">
            重置
          </el-button>
        </el-col>
        <el-col :span="3" style="width: 195px; float: right">
          <el-button
          v-if="powers.some((item) => item == '002')"
            type="primary"
            :size="searchSize"
            style="float: right; margin-left: 5px"
            @click="searchList"
          >
            刷新
          </el-button>
          <el-button
          v-if="powers.some((item) => item == '003')"
            :size="searchSize"
            type="success"
            style="float: right"
            @click="openModal('add', false)"
          >
            添加
          </el-button>
        </el-col>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      :size="size"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="role_name"
        label="角色名称"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column label="角色等级" show-overflow-tooltip align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.level === '1'"
            :size="tablebtnSize"
            type="success"
            >管理员</el-tag
          >
          <el-tag v-else :size="tablebtnSize" type="warning">非管理员</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" show-overflow-tooltip align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '1'"
            type="success"
            :class="{ 'hover-tag': powers.some((item) => item == '004') }"
            :size="tablebtnSize"
            @click="
              deleteById(
                scope.row.id,
                scope.row.status,
                powers.some((item) => item == '004')
              )
            "
            ><i
                class="el-icon-thumb"
                v-if="powers.some((item) => item == '004')
                "
                style="margin-right: 1px"
              ></i>启用</el-tag
          >
          <el-tag
            v-else
            :class="{ 'hover-tag': powers.some((item) => item == '004') }"
            :size="tablebtnSize"
            type="warning"
            @click="
              deleteById(
                scope.row.id,
                scope.row.status,
                powers.some((item) => item == '004')
              )
            "
            ><i
                class="el-icon-thumb"
                v-if="powers.some((item) => item == '004')
                "
                style="margin-right: 1px"
              ></i>禁用</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column
        prop="addtime"
        label="创建时间"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-link
           v-if="powers.some((item) => item == '005')"
            type="primary"
            :size="tablebtnSize"
            :underline="false"
            style="margin: 0 4px"
            @click="openModal(scope.row.id, false)"
          >
            修改
          </el-link>

          <el-link
          v-if="powers.some((item) => item == '007')"
            type="primary"
            :size="tablebtnSize"
            :underline="false"
            style="margin: 0 4px"
            @click="openModal(scope.row.id, true)"
          >
            详情
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <div
      class="Pagination"
      style="text-align: right; margin-top: 10px"
      v-show="count > 0"
    >
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
        :current-page="parmValue.page"
        :size="searchSize"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="parmValue.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      ></el-pagination>
    </div>
    <add-edit
      :id="modelId"
      :show-model="showModel"
      :is-detail="isDetail"
      @refresh="searchList"
      @cancel="showModel = false"
    />
     </div>
      <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/interest/role";
import addEdit from "./addEdit";
import statusList from "@/assets/js/statusList";
import roleLevel from "@/assets/js/roleLevel";
import { mapGetters } from "vuex";
import mixinPage from "@/mixins/elPaginationHandle";
export default {
  name: "Role",
  components: {
    addEdit,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find((item) => item.menu_route == "role") ||
        {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  mixins: [mixinPage],
  data() {
    return {
      statusList: statusList,
      roleLevel: roleLevel,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        status: "", // 账户
        level: "", // 姓名
        role_name: "",
        page: 1, // 页码
        size: 10, // 每页显示条数
      },
      count: 0, // 总条数
      tableData: [],
    };
  },
  // mixins: [mixinPage],
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      this.parmValue = {
        status: "", // 账户
        level: "", // 姓名
        role_name: "",
        page: 1, // 页码
        size: 10, // 每页显示条数
      };
      this.searchList();
    },
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    async deleteById(id, status, type) {
      if (!type) {
        return;
      }
      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();
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list(this.parmValue);
      if (res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map((v1) => {
          v1.addtime = v1.addtime.replaceAll(".000", "");
          return v1;
        });
        this.count = Number(res.data.count);
      } else {
        this.tableData = [];
        this.count = 0;
      }
      this.loading = false;
    },
  },
};
</script>
   <style lang="scss" scoped>
.role {
}
</style>
