<template>
  <el-dialog
    title="添加搜索词"
    :visible.sync="visible"
    width="1200px"
    :close-on-click-modal="false"
    :show-close="false"
  >
    <div class="filter-container">
      <div class="filter-control">
        <span>搜索词:</span>
        <el-input
          v-model="listQuery.keyword"
          placeholder="搜索词"
          clearable
          style="width: 160px"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
      </div>
    </div>
    <el-table
      ref="table"
      v-loading="isLoading"
      :data="list"
      height="400px"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" :selectable="selectable" />
      <el-table-column label="关键词" prop="keyword" align="center" />
      <el-table-column label="搜索次数" prop="frequency" align="center" width="150" />
      <el-table-column label="最近搜索时间" prop="searchTime" align="center" width="200" />
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getSearchFrequencyList"
    />
    <div slot="footer">
      <el-button @click="handleCanle"> 取 消 </el-button>
      <el-button type="primary" :disabled="disabled" @click="handleConfirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getSearchFrequencyList } from '@/api/user/customer/customer'

export default {
  name: 'SearchDialog',
  filters: {},
  data() {
    return {
      visible: true,
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 20, // 页面数据数
        keyword: '' // 关键词
      },
      list: [],
      total: 0,
      productRadio: null,
      isLoading: true
    }
  },
  computed: {
    disabled() {
      return this.productRadio === null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取所有供应商列表
    async getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getSearchFrequencyList()
    },
    // 获取所有供应商列表
    async getSearchFrequencyList() {
      try {
        const res = await getSearchFrequencyList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 选择关键词
    handleSelectionChange(row) {
      this.productRadio = row
      console.log('row', row)
    },
    // 确认选择关键词
    handleConfirm() {
      this.$emit('confirm', this.productRadio)
    },
    // 取消选择
    handleCanle() {
      this.$emit('cancel')
    },
    // 已选择的禁用勾选框
    selectable(row) {
      if (row.flag) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>
