<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加一级菜单</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-upload2" @click="handlePush">发布菜单到微信公众号</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      row-key="id"
      :data="list"
      default-expand-all
      :row-class-name="rowClassName"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="菜单名称" prop="name" align="center" />
      <el-table-column label="按钮类型" prop="type" align="center">
        <template slot-scope="{ row }">
          <span v-text="showTypeText(row.type)"></span>
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" width="80">
        <template slot-scope="{ row }">
          <el-input v-model="row.sort" maxlength="4" minlength="1" @blur="handleOnInputBlur(row)" />
        </template>
      </el-table-column>
      <el-table-column label="设置" align="center">
        <template slot-scope="{ row }">
          <el-button plain size="mini" :disabled="row.parentId | disableAddSub" @click="handleCreateNextLevel(row)">
            添加子菜单
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" @click="handleUpdate(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { fetchList, deleteMenu, updateMenu, pushWechatMenu } from '@/api/wechat/menu'

export default {
  name: 'WeChatMenuList',
  filters: {
    disableAddSub(value) {
      if (value !== 1 && value !== 2) {
        return true
      } else {
        return false
      }
    }
  },
  props: {
    // type 类型： 1采美，2呵呵商城
    listType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      list: [],
      listQuery: {
        // type 类型： 1采美，2呵呵商城
        type: '0'
      },
      createTitle: '',
      linkPath: ''
    }
  },
  watch: {
    $route(route) {
      this.getList()
    }
  },
  created() {
    this.listQuery.type = this.listType
    if (this.listQuery.type === 1) {
      this.linkPath = 'caimei'
    } else {
      this.linkPath = 'hehe'
    }
    this.getList()
  },
  methods: {
    showTypeText(type) {
      if (type === 'view') return '跳转URL'
      if (type === 'miniprogram') return '跳转小程序'
      if (type === 'media_id') return '下发媒体消息（图片、音频、视频）'
      if (type === 'article_id') return '下发图文消息'
      if (type === 'article_view_limited') return '跳转图文消息URL'
      if (type === 'click') return '点击推事件'
      if (type === 'scancode_push') return '扫码推事件'
      if (type === 'scancode_waitmsg') return '扫码推事件且弹出“消息接收中”提示框'
      if (type === 'pic_sysphoto') return '弹出系统拍照发图'
      if (type === 'pic_photo_or_album') return '弹出拍照或者相册发图'
      if (type === 'pic_weixin') return '弹出微信相册发图器'
      if (type === 'location_select') return '弹出地理位置选择器'
    },
    rowClassName(scope) {
      if (scope.row.parentId === 1 || scope.row.parentId === 2) {
        return 'parent-row'
      }
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false
        this.list = response.data
        if (response.data[0]) {
          this.createTitle = response.data[0].parentName
        }
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleCreate() {
      this.$router.push({
        path: `/wechat/${this.linkPath}/menus/add`,
        query: { parentId: this.listQuery.type, title: this.createTitle }
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: `/wechat/${this.linkPath}/menus/edit`,
        query: { id: row.id, parentId: row.parentId, title: row.parentName }
      })
    },
    handleCreateNextLevel(row) {
      this.$router.push({ path: `/wechat/${this.linkPath}/menus/add`, query: { parentId: row.id, title: row.name }})
    },
    handleDelete(row) {
      this.$confirm('是否要删除该菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMenu(row.id).then((response) => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handlePush() {
      this.$confirm('是否要发布菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pushWechatMenu(this.listQuery.type).then((response) => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handleOnInputBlur(row) {
      // 更新排序
      updateMenu(row.id, { sort: row.sort }).then((response) => {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1000
        })
        this.getList()
      })
    }
  }
}
</script>

<style>
.el-table .parent-row {
  background-color: #fdf6ec;
}
</style>
