<template>
  <div class="slider-container">
    <el-button @click='onOperation("create")'
               style="position: fixed; z-index: 100;">添加按钮</el-button>

    <el-table :data="tableList"
              style="width: 100%;margin-bottom: 20px;"
              row-key="id"
              border
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column width="300px"
                       prop="name"
                       label="菜单名称">
      </el-table-column>
      <el-table-column prop="id"
                       width="100px"
                       label="元素ID">
      </el-table-column>
      <el-table-column label="菜单类型"
                       width="100px">
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? '按钮' : '菜单' }}
        </template>
      </el-table-column>
      <el-table-column prop="sort"
                       width="100px"
                       label="排序">
      </el-table-column>
      <el-table-column prop="path"
                       label="文件路径">
      </el-table-column>
      <el-table-column prop="parentPermission"
                       label="高亮路径">
      </el-table-column>
      <el-table-column prop="permission"
                       label="接口权限">
      </el-table-column>
      <el-table-column prop="memo"
                       label="页面权限">
      </el-table-column>
      <el-table-column prop="hid"
                       width="100px"
                       label="是否隐藏">
        <template slot-scope="scope">
          {{ scope.row.hid == 1 ? '隐藏' : '不隐藏' }}
        </template>
      </el-table-column>
      <el-table-column width="100px"
                       label="是否缓存">
        <template slot-scope="scope">
          <div>
            {{ parseInt(scope.row.keepAlive)  == 0 ? '缓存' : '不缓存' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="onOperation('update', scope.row)"
                     type="text">修改</el-button>
          <el-button @click="onDelete(scope.row)"
                     type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="侧边栏管理"
               :close-on-click-modal="false"
               :visible.sync="branchStatus"
               default-expand-all
               @close="onFormClose('ruleForm')"
               width="500px">
      <el-form :model="form"
               :rules="rules"
               ref="ruleForm">
        <el-form-item label="菜单名"
                      prop="name"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="文件路径"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.path"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="接口权限"
                      prop="permission"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.permission"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面权限"
                      prop="memo"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.memo"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="高亮路径"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.parentPermission"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件名"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.component"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父元素ID"
                      prop="parentId"
                      :label-width="formLabelWidth">
          <!-- <el-input v-model.trim="form.parentId"
                    autocomplete="off"></el-input> -->
          <el-cascader v-model="form.parentId"
                       style="width:100%"
                       :options="cascaderList"
                       :props="optionProps"></el-cascader>
        </el-form-item>
        <el-form-item label="是否隐藏"
                      :label-width="formLabelWidth">
          <el-select v-model.trim="form.hid">
            <el-option label="否"
                       :value="0"></el-option>
            <el-option label="是"
                       :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否缓存"
                      :label-width="formLabelWidth">
          <el-select v-model.trim="form.keepAlive">
            <el-option label="是"
                       value="0"></el-option>
            <el-option label="否"
                       value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型"
                      :label-width="formLabelWidth">
          <el-select v-model.trim.number="form.type">
            <el-option label="菜单"
                       :value="0"></el-option>
            <el-option label="按钮"
                       :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.sort"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="branchStatus = false">取 消</el-button>
        <el-button @click="onBranchSubmit"
                   type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addSilder, getSilder, deleteSilder, resetSilder } from '@/api/silder'
export default {
  name: 'addSilder',
  data () {
    return {
      tableList: [], // 数据列表
      cascaderList: [], // 父级元素
      formLabelWidth: '100px',
      branchStatus: false,
      operationType: 'create',
      form: {
        name: null,
        path: null,
        permission: null,
        parentId: null,
        sort: 1,
        component: null,
        hid: 1,
        type: 1,
        parentPermission: null,
        keepAlive: '0'
      },
      rules: {
        name: [{ required: true, message: "请输入菜单名", trigger: "blur" }],
        permission: [{ required: true, message: "请输入接口权限", trigger: "blur" }],
        memo: [{ required: true, message: "请输入页面权限", trigger: "blur" }],
        parentId: [{ required: true, message: "请输入父元素ID", trigger: "blur" }],
      },
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  mounted () {
    this.lookSilder() // 查询
  },
  methods: {
    /**
     * {
        path:'/teamDetail',
        component: 'Layout',
        name:'业务管理',
        sysMenus:[
          // 乐团管理
          // 小课管理
          // 老师管理
          // 学员管理
        ]
      }
     */
    onOperation (type, row) {
      this.branchStatus = true
      this.operationType = type
      if (type == 'update') {
        this.form = {
          name: row.name,
          path: row.path,
          permission: row.permission,
          parentId: row.parentId,
          sort: row.sort,
          component: row.component,
          id: row.id,
          hid: row.hid,
          type: Number(row.type),
          parentPermission: row.parentPermission,
          keepAlive: row.keepAlive,
          memo: row.memo
        }
      }
    },
    onBranchSubmit (type) {
      // ruleForm
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          let form = this.form
          if (this.operationType == 'create') {
            addSilder({
              path: form.path,
              component: form.component,
              name: form.name,
              permission: form.permission,
              sort: form.sort,
              parentId: form.parentId.pop(),
              hid: form.hid,
              type: form.type,
              parentPermission: form.parentPermission,
              keepAlive: form.keepAlive,
              memo: form.memo
            }).then(res => {
              if (res.code == 200) {
                this.$message.success('添加成功')
                this.lookSilder()
                this.branchStatus = false
              }
            })
          } else if (this.operationType == 'update') {
            if(typeof form.parentId === 'object') {
              form.parentId = form.parentId.pop()
            }
            resetSilder(form).then(res => {
              if (res.code == 200) {
                this.$message.success('修改成功')
                this.lookSilder()
                this.branchStatus = false
              }
            })
          }
        } else {
          return false;
        }
      });

    },

    lookSilder () {
      getSilder().then(res => {
        if (res.code != 200) return
        this.tableList = this.setTableData(res.data)
        this.cascaderList = this.setTableData(res.data)
        this.cascaderList.unshift({
          id: 0,
          name: '根结点',
          children: []
        })
        console.log(this.cascaderList)
      })
    },
    setTableData (result) {
      let list = []
      list = result.map(res => {
        let tempList = {}
        tempList = {
          id: res.id,
          name: res.name,
          type: res.type,
          path: res.path,
          permission: res.permission,
          icon: res.icon,
          parentId: res.parentId,
          component: res.component,
          sort: res.sort,
          hid: res.hid,
          type: res.type,
          keepAlive: res.keepAlive,
          parentPermission: res.parentPermission,
          memo: res.memo
        }
        if (res.sysMenus && res.sysMenus.length > 0) {
          tempList.children = this.setTableData(res.sysMenus)
        }
        return tempList
      })
      return list
    },
    onDelete (row) { // 删除左则菜单
      deleteSilder(row.id).then(res => {
        if (res.code == 200) {
          this.$message.success('删除成功')
          this.lookSilder()
          this.branchStatus = false
        }
      })
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.form = {
        name: null,
        path: null,
        permission: null,
        parentId: null,
        sort: 1,
        component: null,
        hid: 1,
        type: 1,
        parentPermission: null,
        keepAlive: null
      }
      this.$refs[formName].resetFields()
    }

  },
}
</script>
<style lang="scss">
.slider-container {
  background: #fff;
  padding: 20px;
  .el-table {
    margin-top: 20px;
  }
}
</style>