<template>
  <div>
    <div class="titleBox">
      <div class="title">菜单管理</div>
      <div class="btnBox">
        <el-button type="primary" @click="addItemMenu">
          <el-icon><CirclePlus /></el-icon>
          <span>新增</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="菜单名称">
          <el-input
            placeholder="请输入菜单名称"
            v-model="listQuery.menuName"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        v-if="refreshTable"
        :data="tableData"
        border
        v-loading="loading"
        ref="dataTreeList"
        row-key="id"
        :tree-props="{ children: 'sysMenuList', hasChildren: true }"
        style="width: 100%"
        :height="tableHeight"
        :key="keyCount"
      >
        <el-table-column
          sortable
          prop="menuName"
          show-overflow-tooltip
          width="200px"
          label="菜单名称"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="menuCode"
          show-overflow-tooltip
          align="center"
          width="150px"
          label="菜单编码"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="orderNo"
          label="排序"
          width="140px"
        ></el-table-column>
        <el-table-column
          sortable
          prop="menuUrl"
          show-overflow-tooltip
          label="菜单地址"
          align="center"
          width="140px"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="menuType"
          label="类型"
          align="center"
          width="140px"
        >
          <template #default="{ row }">
            <el-tag v-if="row.menuType === 1" size="small">菜单组</el-tag>
            <el-tag v-else-if="row.menuType === 2" type="success" size="small"
              >菜单</el-tag
            >
            <el-tag v-else-if="row.menuType === 3" type="success" size="small"
              >页面</el-tag
            >
            <el-tag v-else-if="row.menuType === 4" type="success" size="small"
              >资源</el-tag
            >
            <el-tag v-else type="info" size="small">未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="showMode"
          label="可见"
          align="center"
          width="140px"
        >
          <template #default="{ row }">
            <el-tag v-if="row.showMode === 1" type="success" size="small"
              >展示</el-tag
            >
            <el-tag v-else-if="row.showMode === 2" type="info" size="small"
              >不展示</el-tag
            >
            <el-tag v-else type="warning">未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="bz"
          show-overflow-tooltip
          label="备注"
        ></el-table-column>
        <el-table-column
          sortable
          prop="qxbs"
          label="权限标识"
          show-overflow-tooltip
          align="center"
          width="140px"
        ></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="220">
          <template #default="{ row }">
            <el-button @click="update(row)" size="small">编辑</el-button>
            <el-button @click="addItemMenu(row)" size="small"
              >添加下级</el-button
            >
            <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon-color="red"
              title="确定要删除?"
              @confirm="delDictItem(row)"
            >
              <template #reference>
                <el-button type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">{{ isEdit ? "修改" : "新增" }}</span>
        <div>
          <!-- 修改 -->
          <el-button
            v-if="isEdit"
            type="primary"
            size="small"
            @click="onSave"
            :loading="buttonLoading"
            >保存</el-button
          >
          <!-- 新增 -->
          <el-button
            v-else
            type="primary"
            size="small"
            @click="onAdd"
            :loading="buttonLoading"
            >保存</el-button
          >
          <el-button size="small" @click="closeDialog">关闭</el-button>
        </div>
      </div>
      <el-form
        class="mosty-from-wrap"
        :inline="true"
        label-position="top"
        ref="editRef"
        :rules="rules"
        :model="dialogForm"
      >
        <el-form-item
          class="one"
          label="菜单名称"
          prop="menuName"
          label-width="140px"
        >
          <el-input
            v-model="dialogForm.menuName"
            show-word-limit
            maxlength="30"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="菜单编码" label-width="140px">
          <el-input v-model="dialogForm.menuCode" show-word-limit maxlength="50" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item
          class="one"
          label="菜单地址"
          prop="menuUrl"
          label-width="140px"
        >
          <el-input
            v-model="dialogForm.menuUrl"
            show-word-limit
            maxlength="50"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          class="one"
          label="是否展示"
          prop="showMode"
          label-width="140px"
        >
          <el-radio-group v-model="dialogForm.showMode">
            <el-radio :label="1">展示</el-radio>
            <el-radio :label="2">不展示</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          class="one"
          label="菜单类型"
          prop="menuType"
          label-width="140px"
        >
          <el-radio-group :disabled="isEdit" v-model="dialogForm.menuType">
            <el-radio :label="1">菜单组</el-radio>
            <el-radio :label="2">菜单</el-radio>
            <el-radio :label="3">页面</el-radio>
            <el-radio :label="4">资源</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          class="one"
          v-if="dialogForm.menuType == 4"
          prop="buttonResource"
          label="按钮标识"
          label-width="140px"
        >
          <el-input
            v-model="dialogForm.buttonResource"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          class="one"
          label="菜单编码"
          prop="menuCode"
          label-width="140px"
        >
          <el-input
            v-model="dialogForm.menuCode"
            show-word-limit
            maxlength="50"
            placeholder="权限标识对应路由name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          class="one"
          label="选择图标"
          prop="iconName"
          label-width="140px"
        >
          <ChooseIcon
            width="400"
            :limit="13"
            :isImg="false"
            clearable=""
            v-model="dialogForm.iconName"
          ></ChooseIcon>
        </el-form-item>
        <el-form-item
          class="one"
          label="排序"
          prop="orderNo"
          label-width="140px"
        >
          <el-input-number
            v-model="dialogForm.orderNo"
            class="mx-4"
            :min="1"
            :max="100"
            controls-position="right"
          />
        </el-form-item>
        <!-- <el-form-item label="状态" label-width="140px">
          <el-select v-model="dialogForm.zdywlb" placeholder="请选择业务类别">
            <el-option label="正常" value="0"></el-option>
            <el-option label="注销" value="1"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item class="one" label="备注" label-width="140px">
          <el-input
            v-model="dialogForm.bz"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            show-word-limit
            maxlength="200"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import ChooseIcon from "@/components/MyComponents/ChooseIcon";
import { ElMessage } from "element-plus";
import {
  ref,
  reactive,
  watch,
  nextTick,
  onMounted,
  getCurrentInstance,
  onUnmounted
} from "vue";
import {
  getSystemMeny,
  addSysMenu,
  updateSysMenu,
  deleteSysMenu
} from "@/api/user-manage";
import { useRouter, useRoute } from "vue-router";
const { proxy } = getCurrentInstance();
const keyCount = ref(0); //tabel组件刷新值
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const rules = ref({
  menuName: [{ required: true, message: "请输入菜单名称 " }],
  menuUrl: [{ required: true, message: "请输入菜单地址 " }],
  showMode: [{ required: true, message: " 请选择是否展示" }],
  menuType: [{ required: true, message: "请选择菜单类型 " }],
  buttonResource: [{ required: true, message: "请填写菜单编码" }],
  menuCode: [{ required: true, message: "请填写菜单编码 " }],
  iconName: [{ required: true, message: " 请选择图标" }],
  orderNo: [{ required: true, message: "请填写排序号 " }]
});
const router = useRouter();
const route = useRoute();
//查询参数
const total = ref(0);
const currentRow = ref({});
const listQuery = ref({
  dictName: "",
  dictCode: "",
  xtZxbz: "",
  current: 1,
  size:20
});
const buttonLoading = ref(false);
const topParentId = ref("");
const id = ref("");
const zdlx = ref();
const refreshTable = ref(true);
const isExpand = ref(false); //展开折叠
const isEdit = ref(true);
const dialogForm = ref({});
// 数据相关
const tableData = ref([]);
const loading = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
// 获取数据的方法
const getListData = async () => {
  const params = listQuery.value;
  loading.value = true
  const res = await getSystemMeny(params);
  tableData.value = res?.records;
  total.value = Number(res.total);
  loading.value = false
};
const editRef = ref(null);
const handleFilter = () => {
  listQuery.value.current = 1;
  getListData();
};
getListData();
//isExpand 监听 时候展开table
watch(isExpand, (val) => {
  refreshTable.value = false;
  nextTick(() => {
    refreshTable.value = true;
  });
});
const reset = () => {
  listQuery.value = {
    current: 1,
    size: 20,
    menuName: ""
  };
  getListData();
};
// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.size = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.current = currentPage;
  getListData();
};
/**修改字典 */
const update = (row) => {
  isEdit.value = true;
  dialogForm.value = { ...row };
  dialogFormVisible.value = true;
};
function isNull(data) {
  if (!data) return true;
  if (JSON.stringify(data) === "{}") return true;
  if (JSON.stringify(data) === "[]") return true;
  return false;
}
const addItemMenu = (row) => {
  id.value = isNull(row) ? topParentId.value : row.id;
  isEdit.value = false;
  dialogForm.value = {};
  dialogFormVisible.value = true;
};
const onSave = () => {
  editRef.value.validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      updateSysMenu({
        ...dialogForm.value
      })
        .then((res) => {
          dialogFormVisible.value = false;
          ElMessage.success("修改成功");
          buttonLoading.value = false;
          handleFilter();
        })
        .finally(() => {
          buttonLoading.value = false;
        });
    } else {
      ElMessage.error("请填写完必填项!");
      return false;
    }
  });
};

const onAdd = () => {
  editRef.value.validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      addSysMenu({
        ...dialogForm.value,
        parentId: id.value
      })
        .then((res) => {
          dialogFormVisible.value = false;
          ElMessage.success("新增成功");
          handleFilter();
        })
        .finally(() => {
          buttonLoading.value = false;
        });
    } else {
      ElMessage.error("请填写完必填项!");
      return false;
    }
  });
};

const delDictItem = (row) => {
  deleteSysMenu({
    id: Number(row.id)
  }).then((res) => {
    ElMessage.success("删除成功");
    handleFilter();
  });
};

const closeDialog = () => {
  dialogForm.value = {};
  dialogFormVisible.value = false;
};

const toGoPage = (row) => {
  router.push("/user/role?zdbh=" + row.zdbh);
};
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
onMounted(() => {
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
.user-manage-container {
  .table-header-wrap {
    margin-bottom: 22px;
  }

  ::v-deep .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  ::v-deep .el-tag {
    margin-right: 6px;
  }

  .pagination {
    margin-top: 20px;
  }
}
::v-deep .el-loading-mask{
  background:rgba(0,0,0,.5)
}
</style>
