<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会话标识" prop="chatId">
        <el-input
          v-model="queryParams.chatId"
          placeholder="请输入会话标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入客户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <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 label="企微账号" prop="qwUserName">
	    <el-input
	      v-model="queryParams.qwUserName"
	      placeholder="请输入企微账号"
	      clearable
	      size="small"
	      @keyup.enter.native="handleQuery"
	    />
	  </el-form-item>
      <el-form-item label="会话状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="item in sessionStatusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
		<el-form-item label="是否人工" prop="isArtificial">
		  <el-select v-model="queryParams.isArtificial" placeholder="请选择状态" clearable size="small">
		    <el-option
		      v-for="item in orOptions"
		      :key="item.dictValue"
		      :label="item.dictLabel"
		      :value="item.dictValue"
		    />
		  </el-select>
		</el-form-item>
      <el-form-item label="查看状态" prop="isLook">
        <el-select v-model="queryParams.isLook" placeholder="请选择" clearable size="small">
          <el-option
            v-for="item in sessionLookOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="接待时间" prop="createTime">
        <el-date-picker
          style="width:220px"
          clearable size="small"
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
          @change="changeTime"
        </el-date-picker>
      </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"-->
<!--          v-hasPermi="['fastGpt:fastGptChatSession:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['fastGpt:fastGptChatSession:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
    <el-col :span="1.5">
       <el-button
         type="danger"
         plain
         icon="el-icon-delete"
         size="mini"
         :disabled="multiple"
         @click="handleDelete"
         v-hasPermi="['fastGpt:fastGptChatSession:remove']"
       >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fastGpt:fastGptChatSession:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="fastGptChatSessionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会话标识" align="center" prop="chatId" />
      <el-table-column label="咨询客户" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="avatar" >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.avatar != null"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
            :style="{ width: '100px', height: '100px' }"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="角色昵称" align="center" prop="roleName" />
	  <el-table-column label="企微账号" align="center" prop="qwUserName" />
	  
      <el-table-column label="接待时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span v-if="!scope.row.updateTime && scope.row.status === 1">
            {{ scope.row.createTime }} - 未结束
          </span>
          <span v-else>
            {{ scope.row.createTime }} - {{ scope.row.updateTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="会话状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag prop="status" v-for="(item, index) in sessionStatusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="查看状态" align="center" prop="isLook" >
        <template slot-scope="scope">
          <el-tag prop="isLook" v-for="(item, index) in sessionLookOptions"    v-if="scope.row.isLook==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
	  <el-table-column label="是否人工" align="center" prop="isArtificial" >
	    <template slot-scope="scope">
	      <el-tag prop="isArtificial" v-for="(item, index) in orOptions"    v-if="scope.row.isArtificial==item.dictValue">{{item.dictLabel}}</el-tag>
	    </template>
	  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >查看</el-button>
		  <el-button v-if="scope.row.isArtificial==0"
		    size="mini"
		    type="text"
		    @click="Artificial(scope.row,1)"
		  >转人工</el-button>
		  <el-button v-if="scope.row.isArtificial==1"
		    size="mini"
		    type="text"
		    @click="Artificial(scope.row,0)"
		  >转Ai</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"
    />

<!--    &lt;!&ndash; 添加或修改对话关系对话框 &ndash;&gt;-->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--        <el-form-item label="聊天id" prop="chatId">-->
<!--          <el-input v-model="form.chatId" placeholder="请输入聊天id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="客户ID uid" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入客户ID uid" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="客服ID 应用id?" prop="kfId">-->
<!--          <el-input v-model="form.kfId" placeholder="请输入客服ID 应用id?" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="状态 1会话中 2已结束">-->
<!--          <el-radio-group v-model="form.status">-->
<!--            <el-radio label="1">请选择字典生成</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="公司ID" prop="companyId">-->
<!--          <el-input v-model="form.companyId" placeholder="请输入公司ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否查看" prop="isLook">-->
<!--          <el-input v-model="form.isLook" placeholder="请输入是否查看" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="用户类型 1微信用户 2小程序用户 3销售用户" prop="userType">-->
<!--          <el-select v-model="form.userType" placeholder="请选择用户类型 1微信用户 2小程序用户 3销售用户">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="客户昵称" prop="nickName">-->
<!--          <el-input v-model="form.nickName" placeholder="请输入客户昵称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="头像" prop="avatar">-->
<!--          <el-input v-model="form.avatar" placeholder="请输入头像" />-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
    <el-drawer
      :with-header="false"
      size="75%"  @close="handleDrawerClose"
      :title="show.title" :visible.sync="show.open">
      <fastGptChatMsgDetails  ref="Details" />
    </el-drawer>
  </div>
</template>

<script>
import { listFastGptChatSession, getFastGptChatSession, delFastGptChatSession, addFastGptChatSession, updateFastGptChatSession, exportFastGptChatSession } from "@/api/fastGpt/fastGptChatSession";
import fastGptChatMsgDetails from "@/views/fastGpt/fastGptChatSession/fastGptChatMsgDetails.vue";

export default {
  name: "FastGptChatSession",
  components: {fastGptChatMsgDetails},
  data() {
    return {
      show:{
        title:"聊天详情",
        open:false,
      },
      //接待时间
      dateRange:[],
      //会话状态
      sessionStatusOptions:[],
      //查看状态
      sessionLookOptions:[],
	  orOptions:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对话关系表格数据
      fastGptChatSessionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chatId: null,
        userId: null,
        kfId: null,
        status: null,
        companyId: null,
        isLook: null,
        userType: null,
        nickName: null,
        avatar: null,
		qwUserName: null,
		roleName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("sys_chat_session_status").then(response => {
      this.sessionStatusOptions = response.data;
    });
    this.getDicts("sys_chat_session_look").then(response => {
      this.sessionLookOptions = response.data;
    });
	this.getDicts("sys_company_or").then(response => {
	  this.orOptions = response.data;
	});
    this.getList();
  },
  methods: {
	  Artificial(row,id){
		  this.form.sessionId=row.sessionId
		  this.form.isArtificial=id
		  updateFastGptChatSession(this.form).then(response => {
		    this.msgSuccess("修改成功");
		    this.open = false;
		    this.getList();
		  });
	  },
    /** 查询对话关系列表 */
    getList() {
      this.loading = true;
      listFastGptChatSession(this.queryParams).then(response => {
        this.fastGptChatSessionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查看按钮
    handleDetails(row){
      this.show.open=true;
      setTimeout(() => {
        this.$refs.Details.getDetails(row.sessionId);
      }, 500);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sessionId: null,
        chatId: null,
        userId: null,
        kfId: null,
        createTime: null,
        updateTime: null,
        status: 0,
        companyId: null,
        isLook: null,
        userType: null,
        nickName: null,
        avatar: null
      };
      this.resetForm("form");
    },
    //关闭
    handleDrawerClose(){
      this.getList();
    },
    /**
    * 会话时间
    */
    changeTime(){
      if(this.dateRange!=null){
        this.queryParams.beginTime=this.dateRange[0];
        this.queryParams.endTime=this.dateRange[1];
      }else{
        this.queryParams.beginTime=null;
        this.queryParams.endTime=null;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sessionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加对话关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sessionId = row.sessionId || this.ids
      getFastGptChatSession(sessionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改对话关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sessionId != null) {
            updateFastGptChatSession(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFastGptChatSession(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sessionIds = row.sessionId || this.ids;
      this.$confirm('是否确认删除对话关系编号为"' + sessionIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFastGptChatSession(sessionIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有对话关系数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFastGptChatSession(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
