<template>
  <div class="sy-content">
    <el-row>
      <el-col :span="24">
        <!-- 表单查询 -->
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          size="small"
        >
          <el-form-item
            label="菜单名称"
            prop="menuName"
          >
            <el-input
              v-model="queryParams.menuName"
              placeholder="请输入菜单名称"
              clear
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handelSearch"
            >搜索</el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="handelReset"
            >重置</el-button>
          </el-form-item>
        </el-form>

        <el-button
          size="mini"
          icon="el-icon-plus"
          type="primary"
          plain
          @click="handelAdd"
        >新增</el-button>

        <!-- 表格数据信息 -->
        <el-table
          v-loading="loading"
          :data="menuList"
          row-key="id"
        >
          <el-table-column
            type="index"
            width="50"
            align="center"
          />
          <el-table-column
            key="menuName"
            label="菜单名称"
            align="center"
            prop="menuName"
          />
          <el-table-column
            key="menuIcon"
            label="菜单图标"
            align="center"
            prop="menuIcon"
          />
          <el-table-column
            key="menuUrl"
            label="路由地址"
            align="center"
            prop="menuUrl"
          />
          <el-table-column
            key="sortNum"
            label="排序号"
            align="center"
            prop="sortNum"
          />
          <el-table-column
            key="createTime"
            label="创建时间"
            align="center"
            prop="createTime"
            width="160"
          />
          <el-table-column
            key="updateTime"
            label="更新时间"
            align="center"
            prop="updateTime"
            width="160"
          >
            <template slot-scope="scope">
              {{ scope.row.updateTime === null ? scope.row.createTime : scope.row.updateTime }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="250"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handelEdit(scope.row.id)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handelDel(scope.row.id)"
              >删除
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-zoom-in"
                @click="handelAuth(scope.row.id)"
              >分配接口
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页信息 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          align="right"
          @pagination="getMenuList"
        />
      </el-col>
    </el-row>

    <!-- 弹出框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="上级菜单"
              prop="parentId"
            >
              <selectTree
                v-model="form.parentId"
                :options="options"
                placeholder="请选择菜单"
                key-name="menuName"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="菜单图标"
              prop="menuIcon"
            >
              <el-input
                v-model="form.menuIcon"
                placeholder="请输入图标名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="菜单名称"
              prop="menuName"
            >
              <el-input
                v-model="form.menuName"
                placeholder="请输入菜单名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="显示排序"
              prop="sortNum"
            >
              <el-input-number
                v-model="form.sortNum"
                placeholder="0"
                controls-position="right"
                :min="1"
                :max="10"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="路由地址"
              prop="menuUrl"
            >
              <el-input
                v-model="form.menuUrl"
                placeholder="请输入路由地址"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="handelSubmit('form')"
        >确 定</el-button>
        <el-button @click="handelCancel('form')">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuList, addtMenuList, detailtMenuList, delMenuList, editMenuList } from '@/api/system/menu.js'
import selectTree from '@/components/SelectTree'
export default {
  components: {
    selectTree
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10
      },
      // 总条数
      total: 0,
      // 表格数据
      menuList: [],
      // 弹出框
      dialogVisible: false,
      // 弹出框标题
      title: '',
      // 弹出框表单
      form: {},
      // 菜单类别
      options: [],
      // 表单验证
      rules: {
        menuName: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        sortNum: [
          { required: true, message: '显示排序不能为空', trigger: ['change', 'blur'] }
        ],
        menuUrl: [
          { required: true, message: '路由地址不能为空', trigger: 'blur' }
        ]
      },
      // 修改菜单id
      id: null
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    /** 获取表格数据 */
    getMenuList() {
      getMenuList(this.queryParams).then(response => {
        const data = response.data
        this.menuList = this.$utils.toArrayTree(data.menuList)
        this.total = data.count
        this.loading = false
        this.getOptionsList()
      })
    },
    /** 获取上级菜单 */
    getOptionsList() {
      getMenuList({
        page: 1,
        limit: 999
      }).then(res => {
        if (res.code === 200) {
          const data = res.data
          this.options = this.$utils.toArrayTree(data.menuList)
        }
      })
    },
    /** 搜索 */
    handelSearch() {
      this.queryParams.page = 1
      this.getMenuList()
    },
    /** 重置按钮操作 */
    handelReset() {
      this.$refs['queryForm'].resetFields()
      this.handelSearch()
    },
    /** 新增按钮 */
    handelAdd() {
      this.reset()
      this.dialogVisible = true
      this.title = '添加菜单'
    },
    /** 修改按钮 */
    handelEdit(id) {
      this.id = id
      detailtMenuList({ id }).then(res => {
        this.form = res.data
      })
      this.dialogVisible = true
      this.title = '修改菜单'
    },
    /** 删除按钮 */
    handelDel(id) {
      this.$confirm('是否删除该菜单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'btn_custom_cancel',
        type: 'warning'
      }).then(() => {
        delMenuList({ id }).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: res.data
            })
            this.getMenuList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /** 表单提交 */
    handelSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === '添加菜单') {
            this.form.parentId = this.form.parentId || 0
            addtMenuList(this.form).then(res => {
              if (res.code === 200) {
                this.$message({
                  message: res.data,
                  type: 'success'
                })
                this.getMenuList()
                this.dialogVisible = false
              }
            })
          } else {
            this.form.id = this.id
            editMenuList(this.form).then(res => {
              if (res.code === 200) {
                this.$message({
                  message: res.data,
                  type: 'success'
                })
                this.getMenuList()
                this.dialogVisible = false
              }
            })
          }
        }
      })
    },
    /** 表单取消 */
    handelCancel() {
      this.dialogVisible = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    /** 分配接口按钮 */
    handelAuth(id) {
      this.$router.push('/system/menu/menu-auth/auth/' + id)
    }
  }
}
</script>

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