<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="频次" prop="frequency">
        <el-input
          v-model="queryParams.frequency"
          placeholder="请输入频次"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否解决" prop="isResolve">
        <el-select v-model="queryParams.isResolve" placeholder="请选择" clearable size="small" style="width: 180px">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="fastGptChatQuestionStatisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="所属类别" align="center" prop="questionCategory" />
<!--      <el-table-column label="来源" align="center" prop="source" />-->
      <el-table-column label="内容摘要" align="center" prop="contentSummary" />
      <el-table-column label="频次" align="center" prop="frequency" width="90" />
      <el-table-column label="是否解决" align="center" prop="isResolve" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.isResolve === 1 ? '是' : (scope.row.isResolve === 0 ? '否' : '-') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="170" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fastGpt:fastGptChatQuestionStatistics:remove']"
          >删除</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"
    />

    <!-- 详情弹窗（问题明细） -->
    <el-dialog :title="detailTitle" :visible.sync="detailOpen" width="1100px" append-to-body>
      <el-table border v-loading="detailLoading" :data="detailList">
<!--        <el-table-column label="分类内容摘要" prop="user_content" min-width="160" />-->
        <el-table-column label="所属公司" prop="companyName" min-width="140" />
        <el-table-column label="所属销售" prop="companyUserNickName" min-width="120" />
        <el-table-column label="客户昵称" prop="nickName" min-width="120" />
        <el-table-column label="客户内容" prop="userContent" min-width="180" />
        <el-table-column label="销售回复内容" prop="companyUserContent" min-width="180" />
        <el-table-column label="创建时间" prop="createTime" width="170" />
        <el-table-column label="操作" width="110" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleSalesReply(scope.row)">销售回复</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="detailTotal > 0"
        :total="detailTotal"
        :page.sync="detailQuery.pageNum"
        :limit.sync="detailQuery.pageSize"
        @pagination="getDetailList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFastGptChatQuestionStatistics, delFastGptChatQuestionStatistics, listFastGptChatQuestionStatisticsDetail, saveFastGptChatQuestionStatisticsSalesReply } from "@/api/fastGpt/fastGptChatQuestionStatistics";

export default {
  name: "fastGptChatQuestionStatistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 高频聊天问题统计表格数据
      fastGptChatQuestionStatisticsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        frequency: null,
        isResolve: null,
      },

      detailOpen: false,
      detailTitle: "问题明细",
      detailLoading: false,
      detailTotal: 0,
      detailQuery: {
        questionStatisticsId: null,
        pageNum: 1,
        pageSize: 10
      },
      detailList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询高频聊天问题统计列表 */
    getList() {
      this.loading = true;
      listFastGptChatQuestionStatistics(this.queryParams).then(response => {
        this.fastGptChatQuestionStatisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    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
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除高频聊天问题统计编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFastGptChatQuestionStatistics(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },

    handleDetail(row) {
      this.detailQuery.questionStatisticsId = row.id || null;
      this.detailQuery.pageNum = 1;
      this.detailQuery.pageSize = 10;
      this.detailOpen = true;
      this.detailTitle = `问题明细`;
      this.getDetailList();
    },
    getDetailList() {
      this.detailLoading = true;
      listFastGptChatQuestionStatisticsDetail(this.detailQuery).then(res => {
        this.detailList = res.rows || res.data || [];
        this.detailTotal = res.total != null ? res.total : 0;
      }).finally(() => {
        this.detailLoading = false;
      });
    },
    handleSalesReply(detailRow) {
      this.$prompt('请输入销售回复内容', '销售回复', {
        confirmButtonText: '保存',
        cancelButtonText: '取消',
        inputType: 'textarea',
        inputValue: detailRow.companyUserContent || ''
      }).then(({ value }) => {
        return saveFastGptChatQuestionStatisticsSalesReply({
          id: detailRow.id,
          companyUserContent: value,
          questionStatisticsId:detailRow.questionStatisticsId
        });
      }).then(() => {
        this.msgSuccess("保存成功");
        this.getDetailList();
      }).catch(() => {});
    }
  }
};
</script>
