<template>
  <div class="wechat-menu-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(null, 0)"
        >
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      row-key="wm_id"
    >
      <el-table-column align="center" :label="__('操作')" width="180">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="row.wm_is_buildin === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
          <el-button type="text" @click="handleEdit(row, row.wm_id)">
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('菜单编号')"
        prop="wm_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('上级菜单')"
        prop="wm_parent_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('菜单名称')"
        prop="wm_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatId"
        :label="__('动作类型')"
        prop="wm_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('动作操作')"
        prop="wm_target"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="wm_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商城编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="wm_sort"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuidin"
        :label="__('系统内置')"
        prop="wm_is_buildin"
        show-overflow-tooltip
      />
    </el-table>

    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doDelete } from '@/api/wechat/menu'
  import Edit from './components/WechatMenuEdit'

  export default {
    name: 'WechatMenu',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatBuidin(row) {
        return row.wm_is_buildin === 1 ? this.__('是') : this.__('否')
      },
      formatEnable(row) {
        return row.wm_enable === 1 ? this.__('启用') : this.__('停用')
      },
      formatId(row) {
        return row.wm_type_id === 1
          ? this.__('链接')
          : row.wm_type_id === 2
          ? this.__('模拟关键字')
          : row.wm_type_id === 3
          ? this.__('扫码')
          : row.wm_type_id === 4
          ? this.__('扫码[等待信息]')
          : row.wm_type_id === 5
          ? this.__('系统拍照发图')
          : row.wm_type_id === 6
          ? this.__('拍照或者相册发图')
          : row.wm_type_id === 7
          ? this.__('微信相册发图')
          : row.wm_type_id === 8
          ? this.__('地理位置')
          : this.__('微信小程序')
      },
      handleEdit(row, wm_parent_id) {
        if (wm_parent_id === 0 || wm_parent_id) {
          this.$refs['edit'].showEdit(null, wm_parent_id)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ wm_id: row.wm_id })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },

      async fetchData() {
        this.listLoading = true
        const { data } = await getList()
        this.list = data
        this.listLoading = false
      },
    },
  }
</script>
