<template>
  <div class="h-full w-full">
    <table-header>
      <div slot="left">
        <el-button
          size="small"
          type="primary"
          v-hasPermi="['system:role:add']"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          size="small"
          type="danger"
          v-hasPermi="['system:role:remove']"
          @click="handleDelAll"
          >批量删除</el-button
        >
      </div>
      <div slot="right">
        <el-form ref="searchForm" :model="searchForm" inline>
          <el-form-item prop="name">
            <el-input
              size="small"
              v-model="searchForm.name"
              style="width: 200px"
              class="ml10 mr10"
              placeholder="请输入角色名称搜索"
              @keyup.native.enter="handleSearch"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="handleSearch"
              >搜 索</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="handleReset">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </table-header>
    <!-- 表格 -->
    <div class="table-content">
      <y-table
        ref="MyTable"
        :loading="loading"
        :data="tableData"
        :column="column"
        border
        tooltip-effect="dark"
        :max-height="600"
        :row-key="(row) => row.id"
        @selection-change="handleSelectionChange"
      ></y-table>
    </div>
    <y-pagination
      :total="total"
      :page.sync="page"
      :pageSize.sync="size"
      @change="getRoleList"
    ></y-pagination>
    <!-- 新增 -->
    <AddRole
      ref="AddRole"
      :show.sync="show"
      :title="title"
      @success="getRoleList"
    ></AddRole>
    <!-- 分配资源 -->
    <ApportionRes
      ref="ApportionRes"
      :show.sync="resShow"
      @success="getRoleList"
    ></ApportionRes>
    <!-- 分配菜单 -->
    <ApportionMenu
      ref="ApportionMenu"
      :menuTree="menuTree"
      :show.sync="menuShow"
      :roleInfo="roleInfo"
      @success="getRoleList"
    ></ApportionMenu>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import { getRoleList, deleteRole, getMenuTreeselect } from "@/api/system";
import AddRole from "./components/AddRole.vue";
import ApportionRes from "./components/ApportionRes.vue";
import ApportionMenu from "./components/ApportionMenu.vue";
export default {
  components: {
    TableHeader,
    AddRole,
    ApportionRes,
    ApportionMenu,
  },
  data() {
    return {
      column: [
        {
          label: "全选",
          type: "selection",
          width: "55",
          align: "center",
          reserveSelection: true,
        },
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.page - 1) * this.size + index + 1;
          },
        },
        {
          label: "角色名称",
          prop: "name",
          align: "center",
        },
        {
          label: "备注",
          prop: "remark",
          align: "center",
        },
        {
          label: "创建时间",
          prop: "createTime",
          align: "center",
          formatter: (row) => {
            return this.$moment(row.createTime).format("YYYY-MM-DD HH:mm:ss");
          },
        },
        {
          label: "操作",
          align: "center",
          width: "260",
          formatter: (row) => {
            return (
              <div class="table-control">
                <span
                  v-hasPermi={["system:role:roleRes"]}
                  class="primary pointer"
                  onClick={() => this.apportion(row)}
                >
                  分配资源
                </span>
                <span
                  v-hasPermi={["system:role:roleMenu"]}
                  class="primary pointer"
                  onClick={() => this.apportionMenu(row)}
                >
                  分配菜单
                </span>
                <span
                  v-hasPermi={["system:role:edit"]}
                  class="primary pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  v-hasPermi={["system:role:remove"]}
                  class="delete pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          },
        },
      ],
      searchForm: {
        name: "",
      },
      tableData: [],
      menuTree: [], // 菜单树
      page: 1,
      size: 10,
      total: 0,
      loading: false,
      selectRows: [],
      show: false,
      menuShow: false,
      title: "新增",
      dict: {}, // 字典
      resShow: false,
      roleInfo: {},
    };
  },
  created() {
    this.getRoleList();
    this.getMenuTreeselect();
  },
  computed: {},
  methods: {
    // 获取菜单树
    async getMenuTreeselect() {
      let res = await getMenuTreeselect({
        page: 1,
        size: -1,
      });
      if (res.data.code == 1) {
        let { result } = res.data.data;
        this.menuTree = result;
      }
    },
    async getRoleList() {
      this.loading = true;
      let res = await getRoleList({
        page: this.page,
        size: this.size,
        name: `%${this.searchForm.name}%`,
      });
      this.loading = false;
      if (res.data.code == 1) {
        let { data, total, dict } = res.data.data;
        if (!data.length && this.page > 1) {
          this.page -= 1;
          this.getRoleList();
        }
        this.tableData = data;
        this.total = total;
        this.dict = dict;
      }
    },
    // 批量移除
    handleDelAll() {
      if (!this.selectRows.length) {
        this.$message.warning("请先勾选数据");
        return;
      }
      let ids = this.selectRows.map((v) => v.id).join(",");
      this.handleDel(ids);
    },
    // 搜索
    handleSearch() {
      this.page = 1;
      this.$clearSelection("MyTable");
      this.getRoleList();
    },
    // 重置
    handleReset() {
      this.page = 1;
      this.$clearSelection("MyTable");
      this.$resetForm("searchForm");
      this.getRoleList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectRows = select;
    },

    // 新增
    handleAdd() {
      this.title = "新增";
      this.$refs.AddRole.onAdd();
      this.show = true;
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑";
      let data = { ...row };
      this.$refs.AddRole.onEdit(data);
      this.show = true;
    },
    // 移除
    handleDel(id) {
      this.$confirm("此操作将删除所选数据，是否继续？", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteRole({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getRoleList();
            this.$clearSelection("MyTable");
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
    // 分配资源
    apportion(row) {
      this.$refs.ApportionRes.onAdd(row.id);
      this.resShow = true;
    },
    // 分配菜单
    apportionMenu(row) {
      this.roleInfo = row;
      this.$refs.ApportionMenu.onAdd(row);
      this.menuShow = true;
    },
  },
};
</script>

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