<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 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 @click="$refs.menuForm.edit(record)">编辑</a>
            <a-divider type="vertical"/>
            <a-popconfirm 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'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        return menuList(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      }
    }
  },
  created () {
    // 根据权限加载，待处理
    // if (this.hasPerm('sysMenu:edit') || this.hasPerm('sysMenu:delete')) {
    //   this.columns.push({
    //     title: '操作',
    //     dataIndex: 'action',
    //     width: '150px',
    //     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>
