<template>
  <div class="base-filter-keyword-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.filter_find"
              clearable
              :placeholder="__('请输入敏感词')"
              style="width: 160px"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button
            :disabled="row.filter_buildin === 1"
            type="text"
            @click="handleEdit(row)"
          >
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="row.filter_buildin === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('编号')"
        prop="filter_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('查找敏感词')"
        prop="filter_find"
        show-overflow-tooltip
      />
      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        show-overflow-tooltip-->
      <!--        prop="filter_replace"-->
      <!--        :label="__('替换内容')"-->
      <!--      />-->
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('过滤类型')"
        prop="filter_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('添加时间')"
        prop="filter_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('是否系统内置')"
        prop="filter_buildin"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="filter_enable"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.filter_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doDelete, doEdit } from '@/api/base/filter/keyword'
  import Edit from './components/BaseFilterKeywordEdit'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'BaseFilterKeyword',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          filter_find: "",
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatTime(row) {
        return renderTime(row.filter_time)
      },
      formatType(row) {
        return row.filter_type === 1
          ? this.__('banned禁止')
          : this.__('替换replace')
      },
      formatBuildin(row) {
        return row.filter_buildin === 1 ? this.__('是') : this.__('否')
      },

      handleEdit(row) {
        if (row.filter_id) {
          //row.filter_time = this.formatFiltertime(row.filter_time);
          row.filter_time = null
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ filter_id: row.filter_id })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async sChange(row) {
        const param = {
          filter_id: row.filter_id,
          filter_enable: row.filter_enable,
        }

        const { msg, status } = await doEdit(param)

        if (status == 200) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
