<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客服名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入客服名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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-table v-loading="loading" :data="fastGptRoleList">
      <el-table-column label="ID" align="center" prop="roleId" />
      <el-table-column label="客服名称" align="center" prop="roleName" />
      <el-table-column label="客服类型" align="center" prop="roleType" >
        <template slot-scope="scope">
          <el-tag prop="type" v-for="(item, index) in typeOptions"    v-if="scope.row.roleType==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="客服应用头像" align="center" prop="avatar" >
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="height: 80px">
        </template>
      </el-table-column>
      <el-table-column label="提示词" align="center" prop="reminderWords" >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.reminderWords" placement="top">
            <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; text-overflow: ellipsis;">
              <span>{{ scope.row.reminderWords }}</span>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="绑定的公司" align="center" prop="corpName" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            icon="el-icon-edit"
            @click="handleBindAi(scope.row)"
          >绑定AI客服</el-button>

          <el-button
            style="margin-top: 2%"
            v-if="scope.row.corpName!=null"
            size="mini"
            type="primary"
            plain
            icon="el-icon-edit"
            @click="handleRelieveAi(scope.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"
    />

  </div>
</template>

<script>
import { listFastGptRole, getFastGptRole, delFastGptRole, addFastGptRole, updateFastGptRole, exportFastGptRole,relieveFastGptRole } from "@/api/fastGpt/fastGptRole";
import {BindAi, qwUserBindAi} from "../../../api/qw/user";

export default {
  name: "FastGptRole",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      //AI客服类型
      typeOptions:[],
      qwUserId:null,
      corpId:null,
      //AI模型
      modeOptions:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用表格数据
      fastGptRoleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: null,
        companyId: null,
        roleType: null,
        mode: null,
        kfId: null,
        kfUrl: null,
        avatar: null,
        kfMediaId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        roleType: [
          { required: true, message: "角色类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    //客服类型
    this.getDicts("chat_role_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询应用列表 */
    getList() {
      this.loading = true;
      listFastGptRole(this.queryParams).then(response => {
        this.fastGptRoleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    handleRelieveAi(row){

      if (row.corpId!=this.corpId){
        return this.$message.error("当前企业无法解绑 其他企业 ");
      }

      this.$confirm('此处解绑-会将【所有】已绑定此应用的员工 的状态清除！确定要解除 应用："' + row.roleId +"("+ row.roleName +')"的绑定?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return relieveFastGptRole(row.roleId);
      }).then(() => {
        this.getList();
        this.msgSuccess("解绑成功");
      }).catch(() => {});
    },

    // 表单重置
    reset() {
      this.form = {
        roleId: null,
        roleName: null,
        companyId: null,
        createTime: null,
        updateTime: null,
        roleType: null,
        modeConfigJson:{APPKey:null,Key:null},
        mode: null,
        kfId: null,
        kfUrl: null,
        avatar: null,
        kfMediaId: null,
        reminderWords: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //重置参数
    resetParam(){
      this.qwUserId = null;
      this.corpId = null;
      this.getList();
    },

    handleBindAi(row){
      qwUserBindAi({id:this.qwUserId,fastGptRoleId:row.roleId,corpId:this.corpId}).then(res => {
        this.$message.success(res.msg);
        this.resetParam();

        this.$emit("refreshFastGptList");

      });
    },

    handleBindAiData(val){
      this.qwUserId=val.id;
      this.corpId=val.corpId;
    }
  }
};
</script>
