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

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      row-key="wk_id"
    >
      <el-table-column align="center" :label="__('操作')" width="160">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="row.wk_is_buildin === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(row, row.wk_id, row.wk_name)"
          >
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('触发关键字')"
        prop="wk_name"
        show-overflow-tooltip
        width="130"
      />
      <el-table-column
        align="center"
        :label="__('触发类型')"
        prop="wk_type_id"
        show-overflow-tooltip
        width="130"
      />
      <el-table-column
        align="center"
        :label="__('链接')"
        prop="wk_url"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('关键词描述')"
        prop="wk_desc"
        show-overflow-tooltip
        width="130"
      />
      <el-table-column
        align="center"
        :label="__('标题[图文有效]')"
        prop="wk_title"
        show-overflow-tooltip
        width="130"
      />
      <el-table-column
        align="center"
        :label="__('封面[图文有效]')"
        prop="wk_image"
        show-overflow-tooltip
        width="130"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.wk_image"
            :preview-src-list="[scope.row.wk_image]"
            :src="scope.row.wk_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="wk_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('系统内置')"
        prop="wk_is_buildin"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('备注')"
        prop="wk_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商城编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="wk_sort"
        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/keyword'
  import Edit from './components/WechatKeywordEdit'

  export default {
    name: 'WechatKeyword',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatEnable(row) {
        return row.wk_enable === 1 ? this.__('是') : this.__('否')
      },
      formatBuildin(row) {
        return row.wk_is_buildin === 1 ? this.__('是') : this.__('否')
      },

      handleEdit(row, wk_parent_id, wk_name) {
        if (wk_parent_id === 0 || wk_parent_id) {
          this.$refs['edit'].showEdit(null, wk_parent_id, wk_name)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ wk_id: row.wk_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>
