<template>
  <div class="table-list">
    <div class="filter-container">
      <div class="filter-control">
        <span>菜单状态：</span>
        <el-select
          v-model="listQuery.status"
          placeholder="启用状态"
          clearable
          style="width: 200px"
          class="filter-item"
          @change="getList"
        >
          <el-option label="全部" value="" />
          <el-option label="已启用" :value="0" />
          <el-option label="未启用" :value="1" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="handleAddMenu()">添加菜单</el-button>
      </div>
    </div>
    <!-- 搜索区域END -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      header-row-class-name="tableHeader"
      class="table-cell"
    >
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center" />
      <el-table-column prop="title" label="菜单名称" align="center" />
      <el-table-column prop="name" label="路由名称" align="center" />
      <el-table-column label="展示图标" align="center" width="100">
        <template slot-scope="{ row }">
          <i v-if="row.icon" :class="row.icon" />
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column v-if="listQuery.menuType === 2" prop="name" label="会员功能" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.baseFlag === 1">基础功能</span>
          <span v-else>订制功能</span>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-switch v-model="row.status" :active-value="0" :inactive-value="1" @change="updateMenuStatus(row)" />
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center" width="100">
        <template slot-scope="{ row }">
          <el-input v-model="row.sort" @change="updateMenuSort(row)" />
        </template>
      </el-table-column>
      <el-table-column label="子菜单管理" align="center" width="240">
        <template slot-scope="{ row }">
          <el-button size="mini" @click="handleSearchChildren(row)">查看子菜单</el-button>
          <el-button size="mini" @click="handleAddMenu(row)">添加子菜单</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">

          <el-button
            type="primary"
            size="mini"
            @click="handleEditMenu(row)"
          >编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteMenu(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchMenuList, deleteMenu, updateMenuSelective } from '@/api/system'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'TableList',
  components: {
    Pagination
  },
  props: {
    menuType: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      listLoading: false,
      // 查询参数
      listQuery: {
        parentId: '',
        menuType: '', // 1：管理员 2：供应商
        status: '',
        pageSize: 10,
        pageNum: 1
      },
      total: 0,
      // 菜单列表
      list: []
    }
  },
  created() {
    this.initQuery()
    this.getList()
  },
  methods: {
    // 初始化参数
    initQuery() {
      const { id = '', title = '' } = this.$route.params
      this.listQuery.menuType = this.menuType
      this.parentTitle = title
      this.listQuery.parentId = id
    },
    // 添加菜单
    handleAddMenu(row) {
      if (row) {
        this.$router.push({
          path: '/settings/menus/children/add',
          query: {
            type: 'add',
            parentId: row.id,
            menuType: this.menuType
          }
        })
      } else if (this.listQuery.parentId) {
        this.$router.push({
          path: '/settings/menus/children/add',
          query: {
            type: 'add',
            parentId: this.listQuery.parentId,
            menuType: this.menuType
          }
        })
      } else {
        this.$router.push({
          path: '/settings/menus/add',
          query: {
            type: 'add',
            menuType: this.menuType
          }
        })
      }
    },
    // 查看子菜单
    handleSearchChildren(row) {
      this.listQuery.parentId = row.id
      this.$router.replace(`/settings/menus/children/${this.menuType}/${row.id}/${row.title}`)
    },
    // 修改菜单
    handleEditMenu(row) {
      // $_navigationTo(`/settings/menus/edit?id=${row.id}&type=edit`)
      this.$router.push({
        path: '/settings/menus/edit',
        query: {
          id: row.id,
          type: 'edit',
          menuType: this.menuType
        }
      })
    },
    // 获取列表
    getList() {
      this.listQuery.pageNum = 1
      this.list = []
      this.fetchMenuList()
    },
    // 获取菜单列表
    fetchMenuList() {
      fetchMenuList(this.listQuery).then(res => {
        this.list = [...this.list, ...res.data.list]
        this.total = res.data.total
      })
    },
    // 更新状态
    updateMenuStatus(row) {
      updateMenuSelective(row.id, { status: row.status }).then(() => {
        this.$message.success('操作成功')
      })
    },
    // 更新排序
    updateMenuSort(row) {
      updateMenuSelective(row.id, { sort: row.sort }).then(() => {
        this.$message.success('操作成功')
        this.getList()
      })
    },
    // 删除菜单
    deleteMenu(row) {
      this.$confirm('此操作将永久删除该菜单及其子菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteMenu(row.id).then(res => {
            this.$message.success('删除成功')
            this.getList()
          })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 表格索引
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>

<style scoped></style>
