<template>
  <div class="app-container">
    <el-card shadow="never" class="search-card">
      <div slot="header" class="clearfix">
        <span class="card-title"><i class="el-icon-search"></i> 搜索条件</span>
        <el-button
          style="float: right; padding: 3px 0"
          type="text"
          @click="showSearch = !showSearch">
          {{ showSearch ? '收起' : '展开' }}
        </el-button>
      </div>
      <el-form v-show="showSearch" :model="queryParams" ref="queryForm" :inline="true" label-width="90px" class="compact-form">
      <el-form-item label="会话ID" prop="sessionId">
        <el-input
          v-model="queryParams.sessionId"
          placeholder="请输入会话ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 160px"
        />
      </el-form-item>

      <el-form-item label="满意度" prop="satisfaction">
        <el-select
          v-model="queryParams.satisfaction"
          placeholder="全部满意度"
          clearable
          size="small"
          style="width: 120px"
        >
          <el-option
            v-for="item in satisfactionOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="用户昵称" prop="qwUserNickName">
        <el-input
          v-model="queryParams.qwUserNickName"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 160px"
        />
      </el-form-item>

      <el-form-item label="外呼角色" prop="aiSopCallRoleName">
        <el-input
          v-model="queryParams.aiSopCallRoleName"
          placeholder="请输入外呼角色名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 160px"
        />
      </el-form-item>

      <el-form-item label="外部联系人" prop="externalUserName">
        <el-input
          v-model="queryParams.externalUserName"
          placeholder="请输入外部联系人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 160px"
        />
      </el-form-item>

      <el-form-item class="form-actions">
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
      </el-form>
    </el-card>

    <el-row :gutter="10" class="mb8 operation-row">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['company:details: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="['company:details: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="['company:details: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="['company:details:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-card shadow="never" class="table-card">
      <div slot="header" class="clearfix">
        <span class="card-title"><i class="el-icon-document"></i> 数据列表</span>
        <span class="total-text">共 {{ total }} 条记录</span>
      </div>
      <el-table
        v-loading="loading"
        :data="detailsList"
        @selection-change="handleSelectionChange"
        stripe
        style="width: 100%"
        class="custom-table"
      >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="会话id" align="center" prop="sessionId" />
      <el-table-column label="原语音" align="center" prop="voiceText" />
      <el-table-column label="聊天详情" align="center" prop="aiChatDetails" />
      <el-table-column label="满意度" align="center" width="80">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.satisfaction === 3 ? 'success' : scope.row.satisfaction === 2 ? 'warning' : 'danger'"
            size="small"
          >
            {{ scope.row.satisfaction === 1 ? '低' : scope.row.satisfaction === 2 ? '中' : scope.row.satisfaction === 3 ? '高' : '-' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="企微用户昵称" align="center" prop="qwUserNickName" />
      <el-table-column label="外呼角色名称" align="center" prop="aiSopCallRoleName" />
      <el-table-column label="外部联系人username" align="center" prop="externalUserName" />
      <el-table-column label="token用量" align="center" prop="totalToken" />
      <el-table-column label="记录总用量" align="center" prop="chatTotalToken" />
      <el-table-column label="记录输入量" align="center" prop="chatPromptToken" />
      <el-table-column label="记录输出用量" align="center" prop="chatCompletionToken" />
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleUpdate(scope.row)"
              v-hasPermi="['company:details:edit']"
            />
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="handleDelete(scope.row)"
              v-hasPermi="['company:details:remove']"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      </el-table>
    </el-card>

    <el-pagination
      class="pagination-container"
      :current-page="queryParams.pageNum"
      :page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 添加或修改AI外呼客服呼叫详情对话框 -->
    <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="sessionId">
          <el-input v-model="form.sessionId" placeholder="请输入会话id" />
        </el-form-item>
        <el-form-item label="原语音" prop="voiceText">
          <el-input v-model="form.voiceText" placeholder="请输入原语音" />
        </el-form-item>
        <el-form-item label="原聊天详情" prop="chatDetails">
          <el-input v-model="form.chatDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="ai质检语音" prop="aiVoiceText">
          <el-input v-model="form.aiVoiceText" placeholder="请输入ai质检语音" />
        </el-form-item>
        <el-form-item label="ai质检聊天详情" prop="aiChatDetails">
          <el-input v-model="form.aiChatDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="满意度" prop="satisfaction">
          <el-select v-model="form.satisfaction" placeholder="请选择满意度" style="width: 100%">
            <el-option
              v-for="item in satisfactionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </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="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业名" />
        </el-form-item>
        <el-form-item label="企微用户id" prop="qwUserId">
          <el-input v-model="form.qwUserId" placeholder="请输入企微用户id" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="qwUserNickName">
          <el-input v-model="form.qwUserNickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="客服id" prop="aiSopCallRoleId">
          <el-input v-model="form.aiSopCallRoleId" placeholder="请输入客服id" />
        </el-form-item>
        <el-form-item label="外呼角色名称" prop="aiSopCallRoleName">
          <el-input v-model="form.aiSopCallRoleName" placeholder="请输入外呼角色名称" />
        </el-form-item>
        <el-form-item label="外部联系人id" prop="externalUserId">
          <el-input v-model="form.externalUserId" placeholder="请输入外部联系人id" />
        </el-form-item>
        <el-form-item label="外部联系人username" prop="externalUserName">
          <el-input v-model="form.externalUserName" placeholder="请输入外部联系人username" />
        </el-form-item>
        <el-form-item label="token用量" prop="totalToken">
          <el-input v-model="form.totalToken" placeholder="请输入token用量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="语音token总用量" prop="voiceTotalToken">
          <el-input v-model="form.voiceTotalToken" placeholder="请输入语音token总用量" />
        </el-form-item>
        <el-form-item label="语音输入量" prop="voicePromptToken">
          <el-input v-model="form.voicePromptToken" placeholder="请输入语音输入量" />
        </el-form-item>
        <el-form-item label="语音输出量" prop="voiceCompletionToken">
          <el-input v-model="form.voiceCompletionToken" placeholder="请输入语音输出量" />
        </el-form-item>
        <el-form-item label="记录总用量" prop="chatTotalToken">
          <el-input v-model="form.chatTotalToken" placeholder="请输入记录总用量" />
        </el-form-item>
        <el-form-item label="记录输入量" prop="chatPromptToken">
          <el-input v-model="form.chatPromptToken" placeholder="请输入记录输入量" />
        </el-form-item>
        <el-form-item label="记录输出用量" prop="chatCompletionToken">
          <el-input v-model="form.chatCompletionToken" 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>
  </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails, exportDetails } from "@/api/qw/groupVoiceMsgUserDetails";

export default {
  name: "detailsRecord",
  data() {
    return {
      // 满意度选项
      satisfactionOptions: [
        { value: '1', label: '低' },
        { value: '2', label: '中' },
        { value: '3', label: '高' }
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // AI外呼客服呼叫详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sessionId: null,
        voiceText: null,
        chatDetails: null,
        aiVoiceText: null,
        aiChatDetails: null,
        satisfaction: undefined,
        companyId: null,
        companyName: null,
        qwUserId: null,
        qwUserNickName: null,
        aiSopCallRoleId: null,
        aiSopCallRoleName: null,
        externalUserId: null,
        externalUserName: null,
        totalToken: null,
        voiceTotalToken: null,
        voicePromptToken: null,
        voiceCompletionToken: null,
        chatTotalToken: null,
        chatPromptToken: null,
        chatCompletionToken: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qwUserNickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // 从路由参数中获取sessionId
    if (this.$route.query.sessionId) {
      this.queryParams.sessionId = this.$route.query.sessionId;
    }
    // 清理可能存在的无效参数
    if (this.$route.query.aiSopCallRoleId === '0') {
      delete this.queryParams.aiSopCallRoleId;
    }
    this.getList();
  },
  methods: {
    /** 查询AI外呼客服呼叫详情列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 分页大小变化
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    // 当前页码变化
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sessionId: null,
        voiceText: null,
        chatDetails: null,
        aiVoiceText: null,
        aiChatDetails: null,
        satisfaction: undefined,
        companyId: null,
        companyName: null,
        qwUserId: null,
        qwUserNickName: null,
        aiSopCallRoleId: null,
        aiSopCallRoleName: null,
        externalUserId: null,
        externalUserName: null,
        totalToken: null,
        remark: null,
        voiceTotalToken: null,
        createTime: null,
        voicePromptToken: null,
        voiceCompletionToken: null,
        chatTotalToken: null,
        chatPromptToken: null,
        chatCompletionToken: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加AI外呼客服呼叫详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改AI外呼客服呼叫详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetails(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除AI外呼客服呼叫详情编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetails(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有AI外呼客服呼叫详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDetails(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
  background: #f5f7fa;
}

.search-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-title {
  font-weight: 600;
  color: #303133;
}

.table-card {
  border-radius: 8px;
}

.total-text {
  float: right;
  color: #909399;
  font-size: 14px;
}

.compact-form {
  margin-bottom: -18px;
}

.compact-form .el-form-item {
  margin-bottom: 16px;
}

.form-actions {
  margin-left: auto;
}

.operation-row {
  margin-bottom: 16px;
}

.custom-table {
  border-radius: 8px;
}

.custom-table ::v-deep .el-table__header th {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
  padding: 16px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-tag {
  margin: 2px;
}
</style>

<style>
.compact-form .el-form-item__label {
  padding-right: 8px;
}
</style>
