<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
      <el-form-item label="租户" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="全部租户" filterable clearable size="small">
          <el-option v-for="c in companyOptions" :key="c.value" :label="c.label" :value="c.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="搜索敏感词" clearable size="small" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增词条</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-upload" size="mini">批量导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="!selectedIds.length" @click="handleBatchDelete">批量删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" />
      <el-table-column label="ID" prop="id" width="60" />
      <el-table-column label="租户" prop="companyName" width="120" />
      <el-table-column label="敏感词" prop="word" min-width="160" show-overflow-tooltip />
      <el-table-column label="类别" prop="category" width="100" />
      <el-table-column label="严重度" width="90">
        <template slot-scope="{row}">
          <el-tag size="mini" :type="row.severity === 3 ? 'danger' : row.severity === 2 ? 'warning' : 'info'">
            {{ row.severity === 3 ? '高危' : row.severity === 2 ? '中危' : '低危' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80">
        <template slot-scope="{row}"><el-switch v-model="row.enabled" :active-value="1" :inactive-value="0" /></template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" style="color:#F56C6C" @click="handleDelete(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="getList" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户" prop="companyId" v-if="isAdd">
          <el-select v-model="form.companyId" placeholder="选择租户" filterable style="width:100%">
            <el-option v-for="c in companyOptions" :key="c.value" :label="c.label" :value="c.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="敏感词" prop="word">
          <el-input v-model="form.word" placeholder="请输入敏感词" />
        </el-form-item>
        <el-form-item label="类别">
          <el-input v-model="form.category" placeholder="如 广告/色情/暴力/政治" />
        </el-form-item>
        <el-form-item label="严重度">
          <el-radio-group v-model="form.severity">
            <el-radio :label="1">低危</el-radio>
            <el-radio :label="2">中危</el-radio>
            <el-radio :label="3">高危</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" :rows="2" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAllCompanies } from '@/api/workflow/lobster-admin'
import request from '@/utils/request'

export default {
  name: 'SensitiveWords',
  data() {
    return {
      loading: false, list: [], total: 0, selectedIds: [],
      companyOptions: [],
      queryParams: { pageNum: 1, pageSize: 20, companyId: null, keyword: '' },
      dialogVisible: false, dialogTitle: '', isAdd: false,
      form: { companyId: null, word: '', category: '', severity: 1, remark: '' },
      rules: { word: [{ required: true, message: '不能为空', trigger: 'blur' }] }
    }
  },
  created() { this.loadCompanies(); this.getList() },
  methods: {
    async loadCompanies() { try { const res = await listAllCompanies(); this.companyOptions = (res.data || []).map(c => ({ value: c.companyId, label: c.companyName || '未知' })) } catch (e) {} },
    async getList() {
      this.loading = true
      try {
        const res = await request({ url: '/workflow/lobster/sensitive-words/list', method: 'get', params: this.queryParams })
        this.list = (res.data || [])
        this.total = this.list.length
      } catch (e) { this.list = []
      } finally { this.loading = false }
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.$refs.queryForm.resetFields(); this.handleQuery() },
    handleSelectionChange(selection) { this.selectedIds = selection.map(s => s.id) },
    handleAdd() { this.isAdd = true; this.dialogTitle = '新增敏感词'; this.form = { companyId: this.queryParams.companyId, word: '', category: '', severity: 1, remark: '', enabled: 1 }; this.dialogVisible = true },
    handleEdit(row) { this.isAdd = false; this.dialogTitle = '修改敏感词'; this.form = { ...row }; this.dialogVisible = true },
    async submitForm() {
      this.$refs.formRef.validate(async v => {
        if (!v) return
        try {
          await request({ url: '/workflow/lobster/sensitive-words/save', method: 'post', data: this.form })
          this.$message.success(this.isAdd ? '新增成功' : '修改成功')
          this.dialogVisible = false; this.getList()
        } catch (e) { this.$message.error('保存失败') }
      })
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除?', '警告', { type: 'warning' })
        await request({ url: `/workflow/lobster/sensitive-words/${row.id}`, method: 'delete', params: { companyId: row.companyId } })
        this.$message.success('删除成功'); this.getList()
      } catch (e) { if (e !== 'cancel') this.$message.error('删除失败') }
    },
    async handleBatchDelete() {
      if (!this.selectedIds.length) return
      try {
        await this.$confirm(`确认批量删除 ${this.selectedIds.length} 条?`, '警告', { type: 'warning' })
        for (const id of this.selectedIds) {
          await request({ url: `/workflow/lobster/sensitive-words/${id}`, method: 'delete', params: { companyId: this.queryParams.companyId } })
        }
        this.$message.success('批量删除成功'); this.getList()
      } catch (e) { if (e !== 'cancel') this.$message.error('批量删除失败') }
    }
  }
}
</script>
