<template>
  <div class="h-full w-full">
    <table-header>
      <div slot="left">
        <el-button
          size="small"
          type="primary"
          v-hasPermi="['system:menu:add']"
          @click="handleAdd"
          >新增</el-button
        >
        <!-- <el-button size="small" type="danger" @click="handleDelAll"
          >批量删除</el-button
        > -->
      </div>
      <!-- <div slot="search">
        <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"
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
        @selection-change="handleSelectionChange"
      ></y-table>
    </div>
    <y-pagination
      :total="total"
      :page.sync="page"
      :pageSize.sync="size"
      layout="total,prev,pager,next"
      @change="getMenuList"
    ></y-pagination>
    <AddMenu
      ref="AddMenu"
      :show.sync="show"
      :title="title"
      :dict="dict"
      :menuList="menuList"
      @success="getMenuList"
    ></AddMenu>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import AddMenu from "./components/AddMenu.vue";
import {
  getMenuList,
  deleteMenu,
  saveMenu,
  changeMenuSort,
} from "@/api/system";
import { buildTree } from "@/utils";
export default {
  components: {
    TableHeader,
    AddMenu,
  },
  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: "树形展开",
        //   width: "80",
        //   align: "center",
        // },
        {
          label: "菜单名称",
          prop: "name",
          // align: "center",
        },
        // {
        //   label: 'ID',
        //   prop: 'id',
        //   align: 'center'
        // },
        {
          label: "图标",
          align: "center",
          prop: "imgPath",
          formatter: (row) => {
            if (row.imgPath) {
              return <i class={row.imgPath}></i>;
            }
          },
        },
        // {
        //   label: '权限类型',
        //   prop: 'authType',
        //   align: 'center',
        //   formatter: (row) => {
        //     return this.dict.authType[row.authType];
        //   }
        // },
        // {
        //   label: '父ID',
        //   prop: 'parentId',
        //   align: 'center'
        // },
        {
          label: "地址",
          prop: "url",
          align: "center",
        },
        {
          label: "类型",
          prop: "menuType",
          align: "center",
          formatter: (row) => {
            if (row.menuType === 0) {
              return (
                <el-tag size="small" type="info">
                  {this.dict.menuType[row.menuType]}
                </el-tag>
              );
            } else if (row.menuType === 1) {
              return (
                <el-tag size="small" type="success">
                  {this.dict.menuType[row.menuType]}
                </el-tag>
              );
            } else if (row.menuType === 2) {
              return (
                <el-tag size="small">{this.dict.menuType[row.menuType]}</el-tag>
              );
            }
          },
        },
        {
          label: "权限字符",
          prop: "perms",
          align: "center",
        },
        {
          label: "状态",
          prop: "status",
          align: "center",
          formatter: (row) => {
            return (
              <el-switch
                active-value={1}
                inactive-value={0}
                value={row.status}
                onChange={(val) => {
                  this.handleStatus(row, val);
                }}
              ></el-switch>
            );
          },
        },

        {
          label: "操作",
          align: "center",
          width: "180",
          formatter: (row) => {
            return (
              <div class="table-control">
                <i
                  v-hasPermi={["system:menu:sort"]}
                  class="el-icon-top primary pointer"
                  onClick={() => this.changeMenuSort(row.id, 0)}
                ></i>
                <i
                  v-hasPermi={["system:menu:sort"]}
                  class="el-icon-bottom primary pointer"
                  onClick={() => this.changeMenuSort(row.id, 1)}
                ></i>
                <span
                  v-hasPermi={["system:menu:edit"]}
                  class="primary pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  v-hasPermi={["system:menu:remove"]}
                  class="delete pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          },
        },
      ],
      searchForm: {
        name: "",
      },
      tableData: [],
      menuList: [],
      page: 1,
      size: 10000,
      total: 0,
      loading: false,
      selectRows: [],
      show: false,
      title: "新增",
      dict: {}, // 字典
      isAllSelected: false,
    };
  },
  created() {
    this.getMenuList();
  },
  computed: {},
  methods: {
    // 获取列表
    async getMenuList() {
      this.loading = true;
      this.menuList = [
        {
          id: 0,
          name: "无",
        },
      ];
      let res = await getMenuList({
        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.getMenuList();
        }
        this.menuList = [...this.menuList, ...buildTree(data)];
        this.tableData = buildTree(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.getMenuList();
    },
    // 重置
    handleReset() {
      this.page = 1;
      this.$clearSelection("MyTable");
      this.$resetForm("searchForm");
      this.getMenuList();
    },
    // 选中
    handleSelectionChange(selection) {
      this.selectRows = selection;
    },

    // 新增
    handleAdd() {
      this.title = "新增";
      this.$refs.AddMenu.onAdd();
      this.show = true;
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑";
      let data = { ...row };
      this.$refs.AddMenu.onEdit(data);
      this.show = true;
    },
    // 移除
    handleDel(id) {
      this.$confirm("此操作将删除所选数据，是否继续？", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteMenu({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getMenuList();
            this.$clearSelection("MyTable");
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
    async handleStatus(row, status) {
      row.status = status;
      let res = await saveMenu(row);
      if (res.data.code == 1) {
        this.$message.success(res.data.msg);
      }
      this.getMenuList();
    },

    // 改变菜单排序
    async changeMenuSort(id, type) {
      let res = await changeMenuSort({ id, type });
      if (res.data.code == 1) {
        this.$message.success("更改排序成功");
        this.getMenuList();
      }
    },
  },
};
</script>

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