<template>
  <div class="container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch('listQuery')"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          ref="listQuery"
          size="small"
          label-width="140px"
          @submit.native.prevent
        >
          <el-form-item label="单词：" prop="customerName">
            <el-input
              style="width: 203px"
              v-model="listQuery.key"
              placeholder=""
                 @keyup.enter.native="handleSearchList()"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="complex" size="mini">
        单词缓存
      </el-button>
      <!-- <el-button class="btn-add" @click="handleStuDetail()" size="mini">
            stuDetail
          </el-button> -->
    </el-card>

    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="单词" align="center">
          <template slot-scope="scope">
            <div
              class="handleWordDetail"
              @click="handleWordDetail(scope.row.id)"
            >
              {{ scope.row.word }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="美式音标" align="center">
          <template slot-scope="scope">{{ scope.row.usSoundmark }}</template>
        </el-table-column>
        <el-table-column label="英式音标" align="center">
          <template slot-scope="scope">{{ scope.row.ukSoundmark }}</template>
        </el-table-column>
        <!-- <el-table-column label="美式发音" align="center">
          <template slot-scope="scope">{{ scope.row.usPronounce }}</template>
        </el-table-column>
        <el-table-column label="英式发音" align="center">
          <template slot-scope="scope">{{ scope.row.ukPronounce }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="英式发音" align="center">
          <template slot-scope="scope">{{ scope.row.ukPronounce }}</template>
        </el-table-column>
        <el-table-column label="英式发音" align="center">
          <template slot-scope="scope">{{ scope.row.ukPronounce }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="词根" align="center">
          <template slot-scope="scope">{{ scope.row.etyma }}</template>
        </el-table-column> -->
        <el-table-column label="添加时间" align="center">
          <template slot-scope="scope">{{
            scope.row.addTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="handleWordDetail(scope.row.id)" type="mini"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next,jumper"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getPageBaseWord, baseWordRedisData } from "@/api/pandaWords";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};

export default {
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      test: "errorWords",
      list: [],
      listLoading: true,
      pageSize: null,
      pageNum: null,
      total: null
    };
  },
  methods: {
    getData() {
      getPageBaseWord(this.listQuery).then(res => {
        // console.log(res);
        this.list = JSON.parse(JSON.stringify(res.data.list));
        this.listLoading = false;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
      });
    },
    edit(id) {
      console.log(id, "edit");
    },
    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;
      this.getData();
    },
    handleResetSearch(formName) {
      this.$refs[formName].resetFields();
      // 重置
      // this.selectProductCateValue = [];

      // this.chatTime = undefined;
      // this.paymentTime = undefined;
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getData();
    },
    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.listLoading = true;
      this.getData();
    },
    handleSizeChange(val) {
      //切换展示条数
      this.listQuery.pageSize = val;
      this.getData();
    },
    handleWordDetail(id) {
      this.$router.push({
        path: "/wordController/wordDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },
    complex() {
      baseWordRedisData().then(res => {
        this.$message({
          message: "缓存成功",
          type: "success"
        });
      });
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
      // return formatDate(date, "yyyy-MM-dd");
    }
  },
  mounted() {
    this.getData();
  }
};
</script>

<style lang="scss" scoped>
.handleWordDetail {
  cursor: pointer;
}
.container {
  padding: 30rem;
}
</style>
