<template>
  <div class="manage-container">
    <div class="manage-button">
      <el-button type="primary" size="small" @click="handleAdd()">
        添加
      </el-button>
      <el-button type="primary" size="small" @click="deletes">
        {{ $t('defalult.plsc') }}
      </el-button>
    </div>
    <el-table
      id="table"
      v-loading="listLoading"
      :data="tableData"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        fixed
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="keyword"
        label="关键词"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="matchType.dataValue"
        label="关键词匹配类型"
        width="130"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="msgType.dataValue"
        label="回复消息类型"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column prop="textContent" label="文本内容" align="center">
        <template #default="{ row }">
          <span>{{ row.textContent }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="materialType.dataValue"
        label="素材类型"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <repair-edit ref="edit"></repair-edit>
    <show-data ref="show"></show-data>
  </div>
</template>

<script>
import { getKeywordReplyList, keywordReplyRemove } from '@/api/wechat';
import repairEdit from './add';
import showData from './show_data';
export default {
  components: {
    repairEdit,
    showData,
  },
  data() {
    return {
      tableData: [],
      repairTypeData: [],
      ids: [],
      dialogVisible: false,
      dialogVisibleImg: false,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
      },
    };
  },
  created() {},
  methods: {
    keywordsIndex() {
      this.fetchData();
    },
    showData(row) {
      this.$refs['show'].showMaterialData(row);
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getKeywordReplyList(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
      console.log(this.ids);
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    //删除
    deleteData(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = keywordReplyRemove(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      this.ids = [row.id];
      that.deletes();
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  margin-bottom: 30px;
}
</style>
