<template>
  <div class="app-container">
    <div class="filter-container">
      回复素材标题:
      <el-input v-model="listQuery.title" class="filter-item" placeholder="请输入" @change="handleFilter" />
      事件类型:
      <el-select v-model="listQuery.responseType" class="filter-item" @change="handleFilter">
        <el-option label="请选择事件类型" value="" />
        <el-option label="文本素材" value="text" />
        <el-option label="图文素材" value="news" />
      </el-select>
      回复类型:
      <el-select v-model="listQuery.msgType" class="filter-item" @change="handleFilter">
        <el-option label="请选择回复类型" value="" />
        <el-option label="文本输入" value="input" />
        <el-option label="点击事件" value="click" />
        <el-option label="关注" value="subscribe" />
        <el-option label="扫码" value="scan" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加回复配置</el-button>
    </div>
    <el-table v-loading="listLoading" row-key="id" :data="list" border fit highlight-current-row>
      <el-table-column label="关键词" prop="keyword" align="center" />
      <el-table-column label="事件类型" prop="responseType" align="center">
        <template slot-scope="{ row }">
          <span v-text="showResponseTypeText(row.responseType)"></span>
        </template>
      </el-table-column>
      <el-table-column label="回复类型" prop="msgType" align="center">
        <template slot-scope="{ row }">
          <span v-text="showMsgTypeText(row.msgType)"></span>
        </template>
      </el-table-column>
      <el-table-column label="回复素材标题" prop="title" align="center" />
      <el-table-column label="最后更新时间" prop="updateTime" align="center" />
      <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>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, deleteReply } from '@/api/wechat/reply'
import Pagination from '@/components/Pagination'

export default {
  name: 'WeChatReplyList',
  components: { Pagination },
  props: {
    // type 类型： 1采美，2呵呵商城
    listType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        // type 类型： 1采美，2呵呵商城
        type: '0',
        title: '',
        responseType: '',
        msgType: '',
        pageNum: 1,
        pageSize: 20
      },
      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: {
    showResponseTypeText(type) {
      if (type === 'text') return '文本素材'
      if (type === 'news') return '图文素材'
    },
    showMsgTypeText(type) {
      if (type === 'input') return '文本输入'
      if (type === 'click') return '点击事件'
      if (type === 'subscribe') return '关注'
      if (type === 'scan') return '扫码'
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false
        this.list = response.data.results
        this.total = response.data.totalRecord
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleCreate() {
      this.$router.push({ path: `/wechat/${this.linkPath}/reply/add` })
    },
    handleUpdate(row) {
      this.$router.push({
        path: `/wechat/${this.linkPath}/reply/edit`,
        query: { id: row.id }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该回复配置', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteReply(row.id).then((response) => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.filter-item {
  width: 160px;
  margin-right: 20px;
}
</style>
