<template>
  <div class="app-container">
    <!-- ===== 搜索栏 ===== -->
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="queryParams.keyword" placeholder="请输入关键词" clearable @keyup.enter.native="loadList" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="loadList">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- ===== 操作栏 ===== -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="openDialog(null)">新增关键词</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="loadList" />
    </el-row>

    <!-- ===== 列表 ===== -->
    <el-table border v-loading="loading" :data="dataList" size="small" style="width:100%">
      <el-table-column label="关键词ID" prop="keywordId" min-width="70" align="center" />
      <el-table-column label="关键词内容" prop="keyword" min-width="200" />
      <el-table-column label="类型" prop="keywordType" min-width="100" align="center">
        <template slot-scope="s">
          <el-tag v-if="s.row.keywordType == 1" type="danger" size="mini">{{ getTypeLabel(s.row.keywordType) }}</el-tag>
          <el-tag v-else-if="s.row.keywordType == 2" type="warning" size="mini">{{ getTypeLabel(s.row.keywordType) }}</el-tag>
          <el-tag v-else size="mini">{{ getTypeLabel(s.row.keywordType) || '-' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" min-width="150" align="center" />
      <el-table-column label="操作" align="center" width="160" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="s">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openDialog(s.row)">编辑</el-button>
          <el-button size="mini" type="text" style="color:#f5222d" icon="el-icon-delete" @click="handleDelete(s.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="loadList" />

    <!-- ===== 编辑弹窗 ===== -->
    <el-dialog :title="formTitle" :visible.sync="dialogVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="关键词内容" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键词内容" />
        </el-form-item>
        <el-form-item label="类型" prop="keywordType">
          <el-select v-model="form.keywordType" placeholder="请选择类型" style="width:100%">
            <el-option v-for="item in typeOptions" :key="item.dictValue"
              :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="submitting" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import { exportKeyword } from '@/api/system/keyword'

export default {
  name: 'AdminKeywordManage',
  data() {
    return {
      showSearch: true,
      loading: false,
      exportLoading: false,
      typeOptions: [],
      dataList: [],
      total: 0,
      queryParams: { pageNum: 1, pageSize: 10, keyword: null },
      dialogVisible: false,
      formTitle: '',
      submitting: false,
      form: { keywordId: null, keyword: '', keywordType: 1, remark: '' },
      rules: {
        keyword: [{ required: true, message: '请输入关键词内容', trigger: 'blur' }],
        keywordType: [{ required: true, message: '请选择类型', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getDicts('keyword_type').then(response => {
      this.typeOptions = response.data
    })
    this.loadList()
  },
  methods: {
    loadList() {
      this.loading = true
      request({ url: '/system/keyword/list', method: 'get', params: this.queryParams }).then(r => {
        this.dataList = r.rows
        this.total = r.total
        this.loading = false
      })
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.loadList()
    },
    openDialog(row) {
      this.reset()
      if (row) {
        this.formTitle = '编辑关键词'
        request({ url: `/system/keyword/${row.keywordId}`, method: 'get' }).then(r => {
          this.form = { ...this.form, ...r.data }
          this.dialogVisible = true
        })
      } else {
        this.formTitle = '新增关键词'
        this.dialogVisible = true
      }
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        this.submitting = true
        const req = this.form.keywordId
          ? request({ url: '/system/keyword', method: 'put', data: this.form })
          : request({ url: '/system/keyword', method: 'post', data: this.form })
        req.then(() => {
          this.$message.success(this.form.keywordId ? '修改成功' : '新增成功')
          this.dialogVisible = false
          this.loadList()
        }).finally(() => { this.submitting = false })
      })
    },
    handleDelete(row) {
      this.$confirm(`确认删除关键词 "${row.keyword}"？`, '提示', { type: 'warning' }).then(() => {
        request({ url: `/system/keyword/${row.keywordId}`, method: 'delete' }).then(() => {
          this.$message.success('删除成功')
          this.loadList()
        })
      })
    },
    handleExport() {
      this.exportLoading = true
      exportKeyword(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    },
    getTypeLabel(value) {
      const item = this.typeOptions.find(o => o.dictValue == value)
      return item ? item.dictLabel : null
    },
    reset() {
      this.form = { keywordId: null, keyword: '', keywordType: 1, remark: '' }
      if (this.$refs['form']) this.$refs['form'].resetFields()
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
