<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>搜索词:</span>
        <el-input
          v-model="listQuery.keyword"
          placeholder="搜索词"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>统计状态:</span>
        <el-select v-model="listQuery.status" style="width: 120px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="0" label="统计中" />
          <el-option :value="1" label="暂停统计" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="searchDialogVisible = true"> 添加搜索词 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="600">
      <el-table-column prop="keyword" fixed label="搜索词" align="center" />
      <el-table-column prop="number" fixed label="搜索次数" align="center" width="100" />
      <el-table-column prop="status" label="统计状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 0" type="success" size="small">统计中</el-tag>
          <el-tag v-if="row.status === 1" type="danger" size="small">暂停统计</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="searchTime" label="最近搜索时间" align="center" width="200">
        <template slot-scope="{ row }">
          {{ row.searchTime ? row.searchTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="添加时间" align="center" width="200">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleSitting(row)">
            设置统计状态
          </el-button>
          <el-button type="danger" size="mini" style="margin: 2px" @click="handleDelete(row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[100]"
      :page-size="100"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getShopKeywordList"
    />
    <!--  选择搜索词弹窗 -->
    <search-dialog
      v-if="searchDialogVisible"
      ref="searchDialog"
      @confirm="handleSearchConfirm"
      @cancel="handleCancel"
    />
    <!-- 设置统计状态 -->
    <el-dialog title="设置统计状态" :visible.sync="dialogFormVisible" width="500px">
      <el-form ref="dataForm" :model="renewCustome" label-position="right">
        <el-form-item prop="auditNote">
          <el-radio v-model="renewCustome.status" :label="0">统计中</el-radio>
          <el-radio v-model="renewCustome.status" :label="1">暂停统计</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getShopKeywordList, saveShopKeyword, renewShopKeyword, delShopKeyword } from '@/api/user/customer/customer'
import SearchDialog from './components/search-dialog'
export default {
  name: 'CustomerSearchList',
  components: {
    SearchDialog
  },
  filters: {},
  data() {
    return {
      isLoading: true,
      time: '',
      listQuery: {
        shopId: this.$route.query.shopId,
        keyword: '',
        status: '',
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      searchDialogVisible: false,
      dialogFormVisible: false,
      renewCustome: {
        id: '',
        status: ''
      },
      rules: {
        status: [{ required: true, message: '请设置统计状态', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取供应商关键词列表
    async getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getShopKeywordList()
    },
    // 获取供应商关键词列表
    async getShopKeywordList() {
      try {
        this.isLoading = true
        const res = await getShopKeywordList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 确认选择供应商关键词
    handleSearchConfirm(data) {
      console.log('data', data)
      const keyWordIds = []
      data.forEach((ele) => {
        keyWordIds.push(ele.id)
      })
      this.saveShopKeyword({ shopId: this.listQuery.shopId, keyWordIds: keyWordIds.join(',') })
    },
    // 保存添加供应商关键词
    async saveShopKeyword(params) {
      try {
        await saveShopKeyword(params)
        this.searchDialogVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消选择供应商关键词
    handleCancel() {
      this.searchDialogVisible = false
      this.$refs.searchDialog.visible = false
    },
    // 设置状态
    handleSitting(row) {
      this.renewCustome.id = row.id
      this.renewCustome.status = row.status
      this.dialogFormVisible = true
    },
    // 确认设置统计状态
    handleConfirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.renewShopKeyword(this.renewCustome)
        } else {
          return false
        }
      })
    },
    // 调用关键词统计状态API
    async renewShopKeyword(params) {
      try {
        await renewShopKeyword(params)
        this.dialogFormVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确定删除该搜索词吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.delShopKeyword(row)
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 调用删除文章
    async delShopKeyword(row) {
      try {
        await delShopKeyword(row.id)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style></style>
