<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="主要数据Q" prop="question">
        <el-input
          v-model="queryParams.question"
          placeholder="请输入主要数据Q"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅助数据A" prop="answer">
        <el-input
          v-model="queryParams.answer"
          placeholder="请输入辅助数据A"
          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-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:fastGptCollentionData: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:fastGptCollentionData: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:fastGptCollentionData:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-setting"
          size="mini"
          @click="handleSyncCollentionData(queryParams.collectionId)"
          v-hasPermi="['fastGpt:fastGptCollection:sync']"
        >同步集合内数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fastGptCollentionDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="内容id" align="center" prop="id" />
      <el-table-column label="主要数据 q" align="center" prop="question"  width="350px">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.question" placement="top">
            <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; text-overflow: ellipsis;">
              <span>{{ scope.row.question }}</span>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="辅助数据 a" align="center" prop="answer" width="350px">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.answer" placement="top">
            <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; text-overflow: ellipsis;">
              <span>{{ scope.row.answer }}</span>
            </div>
          </el-tooltip>
        </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"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fastGpt:fastGptCollentionData:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fastGpt:fastGptCollentionData: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="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="训练类型" prop="trainingType">
          <el-select v-model="form.trainingType" placeholder="请选择chunk(按文本长度进行分割)，qa(QA拆分)">
            <el-option
              v-for="item in trainingTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <div v-for="(item, index) in form.data" :key="index" class="data-item">
          <el-row>
            <el-col :span="22">
              <div style="background-color: #fbfbfb;padding: 15px;  border: 1px solid #e6e6e6; margin-bottom: 20px;">
                <el-form-item :label="'主要数据 q ' + (index + 1)" :prop="'q' + index">
                  <el-input v-model="item.q" placeholder="请输入主要数据 q" />
                </el-form-item>

                <el-form-item :label="'辅助数据 a ' + (index + 1)" :prop="'a' + index">
                  <el-input v-model="item.a" placeholder="请输入辅助数据 a" />
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="1" :offset="1">
              <i class="el-icon-delete-solid" @click="removeDataItem(index)"  v-if="form.data.length>1"></i>
            </el-col>
          </el-row>

        </div>

<!--        <el-form-item label="主要数据 q" prop="question">-->
<!--          <el-input v-model="form.question" placeholder="请输入主要数据 q" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="辅助数据 a" prop="answer">-->
<!--          <el-input v-model="form.answer" placeholder="请输入辅助数据 a" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDataItem">添加数据项</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFastGptCollentionData, getFastGptCollentionData, delFastGptCollentionData, addFastGptCollentionData, updateFastGptCollentionData, exportFastGptCollentionData,SyncCollentionData } from "@/api/fastGpt/fastGptCollentionData";

export default {
  name: "FastGptCollentionData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 集合里数据表格数据
      fastGptCollentionDataList: [],
      //训练类型
      trainingTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collectionId: null,
        trainingType: null,
        prompt: null,
        question: null,
        answer: null,
        indexes: null
      },
      // 表单参数
      form: {
        data:[{q:"",a:""}]
      },
      // 表单校验
      rules: {
        trainingType: [
          { required: true, message: "训练类型不能为空", trigger: "blur" }
        ],
        question: [
          { required: true, message: "问题不能为空", trigger: "blur" }
        ],
        answer: [
          { required: true, message: "答案不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getDicts("sys_training_type").then((response) => {
      this.trainingTypeOptions = response.data;
    });
  },
  methods: {

    //父类调用
    getCollentionDataList(collectionId){
      this.queryParams.collectionId=collectionId;
      this.getList();
    },

    //添加问答
    addDataItem() {
      this.form.data.push({ q: '', a: '' });
    },
    //删除问答
    removeDataItem(index) {
      this.form.data.splice(index, 1);
    },
    //同步集合内的数据
    handleSyncCollentionData(collectionId){
      this.loading=true;
      SyncCollentionData(collectionId).then(res => {
        this.msgSuccess("同步成功");
        this.queryParams.collectionId=collectionId;
        this.getList();
        this.loading=false;
      })
    },
    /** 查询集合里数据列表 */
    getList() {
      this.loading = true;
      listFastGptCollentionData(this.queryParams).then(response => {
        this.fastGptCollentionDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        collectionId: null,
        trainingType: null,
        prompt: null,
        data:[{q:"",a:""}],
        indexes: 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 = "添加集合里数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFastGptCollentionData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改集合里数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFastGptCollentionData(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.open = false;
            this.form.collectionId=this.queryParams.collectionId
            addFastGptCollentionData(this.form).then(res => {
              this.msgSuccess("新增成功,正在同步数据，请稍后~ 成功数：【"+res.suc+"】");
              this.handleSyncCollentionData(this.form.collectionId);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除集合里数据编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFastGptCollentionData(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有集合里数据数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFastGptCollentionData(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
