<template>
  <!-- 菜单管理 -->
  <div>
    <actionBar menuTitle="菜单管理" :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm"
      @openModel="openModel" @searchProtocol="searchProtocol"></actionBar>
    <treeTable :treeData="treeData" :treeDataList="treeDataList" @buttonData="buttonData">
    </treeTable>
    <el-dialog :title="shopTitle" :visible.sync="shopDialogVisible" width="500px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="100px"></forms>
      <span slot="footer" class="dialog-footer" v-if="operationType != 'logs'">
        <el-button plain @click="shopDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getMenu,
    getMenuDetails,
    addMenu,
    putMenu,
    delMenu,
  } from '@/api/menu'
  import actionBar from '@/components/actionBar'
  import treeTable from '@/components/treeTable'
  import forms from '@/components/forms'
  import {
    rules,
    formRules
  } from "./menu.js";
  import {
    login
  } from '@/api/login.js'
  import {
    generatorDynamicRouter
  } from '@/router/generator-routers'
  import router, {
    resetRouter
  } from '../../router'
  export default {
    components: {
      actionBar,
      treeTable,
      forms
    },
    data() {
      return {
        formList: [{
          type: 'input',
          label: '菜单名称',
          field: 'title',
          placeholder: '请输入菜单名称查找',
        }, {
          type: 'select',
          label: '状态',
          field: 'visible',
          placeholder: '请选择菜单状态查找',
          options: [{
              label: '显示',
              value: "1",
            },
            {
              label: '隐藏',
              value: "0",
            },
          ]
        }],
        searchRuleForm: {
          title: '',
          visible: '',
        },
        operateList: [{
          type: 'add',
          title: '添加一级菜单',
          icon: 'el-icon-plus',
        }],
        treeData: rules(),
        treeDataList: [],
        shopDialogVisible: false,
        shopTitle: '添加',
        formRuleList: [],
        ruleForm: {},
        parentId: 0,
        operationType: '',
        enterpriseId: null,
        enterpriseParentId: null,
        searchForm: {},
        confirmLoading: false,
      }
    },
    mounted() {
      const dataList = formRules();
      this.formRuleList = dataList;
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchForm = value
        this.getList()
      },
      // 获取菜单列表
      getList() {
        getMenu(this.searchForm).then(res => {
          if (res.code == 200) {
            this.treeDataList = res.data
          }
        })
      },
      openModel(type) {
        this.operationType = type
        if (type == 'add') {
          this.parentId = 0
          this.shopTitle = '添加'
        }
        this.shopDialogVisible = true
      },
      // 弹窗表单添加
      handleAdd() {
        if (this.operationType == 'add') {
          let flag = this.$refs['childRules'].validateForm();
          if (flag) {
            this.confirmLoading = true
            var params = {
              ...this.ruleForm
            }
            params.sort = Number(this.ruleForm.sort)
            params.parentId = this.parentId
            addMenu(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
                this.refreshMenu()
              }
              this.shopDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          } else {
            this.$message.error('表单信息不完整，请继续填写完整');
          }
        } else if (this.operationType == 'edit') {
          this.editItem()
        }
      },
      editItem() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          this.confirmLoading = true
          var params = {}
          params.id = this.enterpriseId
          params.parentId = this.enterpriseParentId
          params.component = this.ruleForm.component
          params.icon = this.ruleForm.icon
          params.menuType = this.ruleForm.menuType
          params.title = this.ruleForm.title
          params.name = this.ruleForm.name
          params.sort = Number(this.ruleForm.sort)
          params.visible = this.ruleForm.visible
          putMenu(params).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
              this.refreshMenu()
            }
            this.shopDialogVisible = false
            this.confirmLoading = false
          }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      // 删除
      deleteItem(id) {
        this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delMenu({
            id: id,
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
              this.refreshMenu()
            }
          })
        }).catch(() => {});
      },
      // 刷新菜单
      refreshMenu() {
        const {
          token
        } = localStorage.getItem('T_tokey')
        generatorDynamicRouter(token).then(routers => {
          resetRouter()
          router.addRoutes(routers)
          this.$store.commit('setMenu', routers)
          this.$store.commit('SET_ROUTERS', routers)
        }).catch(e => {
          // console.log(e, '获取失败')
        })
      },
      // 表格操作按钮
      buttonData(row, type) {
        this.enterpriseId = row.id
        this.enterpriseParentId = row.parentId
        this.operationType = type
        if (type == 'add') {
          this.shopTitle = '添加'
        } else if (type == 'edit') {
          this.shopTitle = '修改'
        }
        if (type == 'del') {
          this.deleteItem(row.id)
        } else if (type == 'edit') {
          this.ruleForm = JSON.parse(JSON.stringify(row))
          this.shopDialogVisible = true
        } else if (type == 'add') {
          this.parentId = row.id
          this.shopDialogVisible = true
        }
      },
      closeDialog() {
        this.ruleForm = {}
        this.$refs.childRules.resetCheck();
      }
    }
  }
</script>

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