<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="菜单名称">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入菜单名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button @click="() => {queryParam = {}, this.loadData()}">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button v-if="hasPerm('sys:menu:add')" type="primary" icon="plus" @click="$refs.menuForm.add()">新增菜单</a-button>
      </div>

      <s-table
        ref="table"
        :rowKey="(record) => record.id"
        :columns="columns"
        :alert="false"
        :data="loadData"
        :showPagination="false"
        :expandRowByClick="true"
      >
        <span slot="type" slot-scope="text, record">
          <a-tag color="cyan" v-if="text === 0">
            {{ record.type | typeFilter }}
          </a-tag>
          <a-tag color="blue" v-if="text === 1">
            {{ record.type | typeFilter }}
          </a-tag>
          <a-tag color="purple" v-if="text === 2">
            {{ record.type | typeFilter }}
          </a-tag>
        </span>
        <span slot="action" slot-scope="text, record" >
          <template>
            <a v-if="hasPerm('sys:menu:add') && record.type !== 2" @click="$refs.menuForm.add(record.id)">新增下级</a>
            <a-divider type="vertical" v-if="hasPerm('sys:menu:add') && record.type !== 2 && hasPerm('sys:menu:edit')" />
            <a v-if="hasPerm('sys:menu:edit')" @click="$refs.menuForm.edit(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('sys:menu:edit') && hasPerm('sys:menu:delete') " />
            <a-popconfirm v-if="hasPerm('sys:menu:delete')" placement="topRight" title="删除本菜单与下级？" @confirm="() => handleDel(record)">
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
      <menu-form ref="menuForm" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
import { STable } from '@/components'
import { menuList, menuDel } from '@/api/security/menu'
import MenuForm from './MenuForm'
import { listToTree } from '@/utils/util'
import { menuTypeMap } from '@/views/filterMap/menuTypeMap'
const rootParentId = 0

export default {
  components: {
    STable,
    MenuForm
  },
  data () {
    return {
      data: [],
      queryParam: {},
      loading: true,
      columns: [
        {
          title: '菜单名称',
          dataIndex: 'name',
          width: '20%'
        },
        {
          title: '菜单类型',
          dataIndex: 'type',
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '组件',
          dataIndex: 'component',
          width: '20%',
          ellipsis: true
        },
        {
          title: '路由地址',
          dataIndex: 'router',
          key: 'router',
          ellipsis: true
        },
        {
          title: '排序',
          dataIndex: 'intCode',
          width: '5%',
        },
      ],
      loadData: parameter => {
        return menuList(Object.assign(parameter, this.queryParam)).then((res) => {
          const menuList = listToTree(res.data, [], rootParentId)
          return menuList
        })
      }
    }
  },
  created () {
    // 根据权限加载
    if (this.hasPerm('sys:menu:edit') || this.hasPerm('sys:menu:delete')) {
      this.columns.push({
        title: '操作',
        dataIndex: 'action',
        width: '180px',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  filters: {
    typeFilter (type) {
      return menuTypeMap[type]
    }
  },
  methods: {
    handleDel (record) {
      menuDel({ id: record.id, deleteReason: '' }).then((res) => {
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败：' + res.msg)
        }
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    }
  }
}

</script>
<style scoped>
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
