<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <el-button type="primary" icon="el-icon-sort" @click="getList">一键排序</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleToEdit('add')">添加菜单</el-button>
      </div>
    </div>
    <p style="margin: 0;line-height: 24px;font-size: 14px;color: #999;">注：1. 最多支持8个已上线的菜单      2. 排序值越小越靠前</p>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <el-table-column label="分类名称" align="center" prop="topic" />
      <el-table-column label="分类图标" align="center" prop="image">
        <template slot-scope="{row}">
          <img :src="row.image" alt="" width="50">
        </template>
      </el-table-column>
      <el-table-column label="排序值" align="center" prop="sort">
        <template slot-scope="{row}">
          <el-input-number v-model="row.sort" controls-position="right" :min="1" :max="999" :maxlength="3" size="mini" style="width:65px;" @change="handleSort(row)" />
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center" prop="enabledStatus">
        <template slot-scope="{row}">
          <el-tag v-if="row.enabledStatus===0" type="danger" size="small">已下线</el-tag>
          <el-tag v-else type="success" size="small">已上线</el-tag>
          <el-button v-if="row.enabledStatus === 0" type="primary" size="mini" @click="handleShangeStatus(row.id,row.enabledStatus)">上线</el-button>
          <el-button v-else type="primary" size="mini" @click="handleShangeStatus(row.id,row.enabledStatus)">下线</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间" prop="addTime">
        <template slot-scope="{row}">
          <span>{{ row.addTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleToEdit('edit',row.id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>20" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { getMenuList, getMenuStatus, getMenuSort, delMenu } from '@/api/campaign'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 20,
        enabledStatus: '',
        organizeId: this.$store.getters.organizeId
      },
      cateList: []
    }
  },
  computed: {
    disabled() {
      return this.cateList.length === 8
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取轮播图列表
    async getList() {
      this.listLoading = true
      this.cateList = []
      try {
        const res = await getMenuList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.list.forEach(ele => {
          if (ele.enabledStatus === 1) {
            this.cateList.push(ele.id)
          }
        })
        this.listLoading = false
      } catch (error) {
        console.log('error', error)
      }
    },
    // 操作上下线
    handleShangeStatus(id, status) {
      let confirmTxt = ''
      if (status === 1) {
        confirmTxt = '确定下线该菜单吗？下线后将从小程序隐藏。'
      } else {
        confirmTxt = '确定上线该菜单吗？上线后将重新在小程序显示。'
      }
      this.$confirm(confirmTxt).then(_ => {
        this.getMenuStatus(id)
      }).catch(() => {
        this.getList()
      })
    },
    // 上下线轮播图
    async getMenuStatus(id) {
      try {
        await getMenuStatus(id)
        this.$message({ message: '操作成功', type: 'success' })
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 修改排序
    async handleSort(row) {
      try {
        await getMenuSort(row.id, row.sort)
        // this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 删除菜单
    handleDelete(id) {
      this.$confirm('确定删除该菜单吗？').then(_ => {
        this.delMenu(id)
      })
    },
    async delMenu(id) {
      try {
        await delMenu(id)
        this.$message({ message: '操作成功', type: 'success' })
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 跳转添加/编辑
    handleToEdit(type, id) {
      if (type === 'add') {
        console.log('disabled', this.disabled)
        if (this.disabled) {
          this.$alert('最多支持8个已上线的菜单，请下线已有的菜单再添加。', '提示', {
            confirmButtonText: '确定'
          })
          return
        } else {
          this.$router.push({ path: '/campaign/category-edit', query: { type: type }})
        }
      } else {
        this.$router.push({ path: '/campaign/category-edit', query: { type: type, id: id }})
      }
    }
  }
}

</script>
<style>
.el-input-number.is-controls-right .el-input__inner {
    padding-left: 5px;
    padding-right: 30px;
}
</style>
<style lang="scss" scoped>
.filter-title{
  float: left;
  font-size: 12px;
  line-height: 18px;
  text-align: left;
  color: #2E3748;
}
.filter-title p{
  margin:10px 0;
}
</style>
