<template>
  <section class="AppKeywordManagement">
    <ai-list>
      <template slot="title">
        <ai-title title="百度AI关键词" isShowBottomBorder></ai-title>
      </template>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button size="small" type="primary" icon="iconfont iconAdd" @click="add('')">添加</el-button>
          </template>
          <template #right>
            <el-input size="small" placeholder="关键词搜索" v-model="search.name" clearable
              @clear="current = 1, search.name = '', getTableData()" suffix-icon="iconfont iconSearch"
              v-throttle="() => {(current = 1), getTableData();}"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="total" :current.sync="current" :size.sync="size"
                  @getList="getTableData()" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="status" label="状态"  align="center">
            <template slot-scope="{ row }">
              <el-switch v-model="row.status" @change="changeStatus(row)" active-value="1" inactive-value="0"
                active-color="#5088FF" inactive-color="#D0D4DC"></el-switch>
            </template>
          </el-table-column>
          <el-table-column slot="options" label="操作"  align="center">
            <template slot-scope="{ row }">
              <el-button type="text" @click="edit(row)">编辑</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
    <ai-dialog
        :title="form.id ? '编辑敏感词' : '添加敏感词'"
        :visible.sync="dialog"
        :destroyOnClose="true"
        width="720px"
        @onConfirm="onConfirm"
        @closed="form={}">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="敏感词" prop="wordName">
          <el-input v-model.trim="form.wordName" placeholder="请输入敏感词" size="small"></el-input>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>

</template>

<script>
import { mapState } from "vuex";
export default {
  name: "AppKeywordManagementAI",
  label: '百度AI关键词',
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
    menuName:String
  },
  data() {
    return {
      search: {
        wordName: '',
        current: 1,
        size: 10,
      },
      tableData: [],
      size: 10,
      total: 0,
      current: 1,
      form: {
        wordName: '',
        // areaList: [],
        deptList: [],
        departmentFullIds: []
      },
      dialog: false,
      flag: false,
    }
  },
  created() {
    this.getTableData()
  },
  computed: {
    ...mapState(['user']),
    colConfigs() {
      return [
        { prop: "wordName", label: '关键词', align: "left"},
        { prop: "createTime", label: '创建时间'},
        { prop: "createUserName", label: '创建人'},
        { slot: "status" },
        // { prop: "areaNames", label: '生效地区'},
        // { prop: "departmentNames", label: '生效部门'},
        { slot: "options" },
      ]
    },
    rules() {
      return {
        wordName: [{required: true, message: '请输入敏感词', trigger: 'blur' }],
        // areaList: [{required: true, message: '请选择生效地区', trigger: 'blur' }],
        deptList: [{required: true, message: '请选择生效部门', trigger: 'blur' }],
      }
    },
  },
  methods: {
    getTableData() {
      this.instance.post(`/app/appsessionarchiveaikeyword/list`,null,{
        params: {
          ...this.search,
          current: this.current,
          size: this.size,
          total: this.total,
          wordName: this.search.name
        }
      }).then(res => {
        if(res?.data) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },
    del(row) {
      this.$confirm('确定删除该数据？').then(() => {
        this.instance.post(`/app/appsessionarchiveaikeyword/delete?ids=${row.id}`).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getTableData()
          }
        })
      })
    },
    changeStatus(row) {
      this.$confirm(`确定${row.status == 1 ? '启用' : '停用'}该关键词？`).then(() => {
        this.instance.post(`/app/appsessionarchiveaikeyword/enable?id=${row.id}`).then(res => {
          if (res.code == 0) {
            this.$message.success('操作成功！')
            this.getTableData()
          }
        })
      })
    },
    edit(row) {
      this.form = {...row}
      // this.form.deptList = []
      // this.form.departmentFullIds = []
      // this.form.deptList = row.departmentIds.split(',')
      // if(row.departmentFullIds) {
      //   this.form.departmentFullIds = row.departmentFullIds.split(',')
      // }
      this.dialog = true
    },
    add() {
      this.dialog = true
    },
    onUserChange (e) {
      this.form.deptList = []
      this.form.departmentFullIds = []
      e.map((item) => {
        console.log(item)
        this.form.deptList.push(item.id)
        this.form.departmentFullIds.push(item.fullId)
      })
      this.$forceUpdate()
      // this.form.deptList = e
      // this.$forceUpdate()
    },
    onConfirm() {
      if(this.flag) return

      this.$refs.form.validate((valid)=> {
        if(valid) {
          this.flag = true
          this.instance.post(`/app/appsessionarchiveaikeyword/addOrUpdate`,{
            ...this.form,
            status: this.form.id ? null : 1          
          }).then(res => {
            if(res?.code == 0) {
              this.$message.success(this.form.id ? '关键词编辑成功' : '关键词添加成功')
              setTimeout(() =>{
                this.form.deptList = []
                this.dialog = false
                this.getTableData()
                this.flag = false
              }, 600)
            } else {
              this.flag = false
            }
          })
        }
      })

    },
  },
}
</script>

<style lang="scss" scoped>
.AppKeywordManagement {
  height: 100%;

  .time-select {
    padding: 0 16px;
    height: 32px;
    line-height: 32px;
    border: 1px solid #d0d4dc;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    cursor: pointer;
    .el-icon-arrow-down {
      line-height: 32px;
    }
  }

  :deep .is-error {
    .time-select {
      border: 1px solid #f46!important;
    }
  }
}
</style>
