<template>
  <div class="page-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="search-form_container">
      <el-form-item label="助手标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="类型" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-card shadow="never" body-class="card-table_container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="plus"
            size="small"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="edit"
            size="small"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="delete"
            size="small"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="download"
            size="small"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="assistantList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="icon" align="center" prop="id" width="60">
          <template #default="scope">
            <img :src="scope.row.banner" alt="" style="width: 20px; display: inline-block; text-align: center;">
          </template>
        </el-table-column>
        <el-table-column label="助手标题" align="center" prop="title" />
        <el-table-column label="展示提示词" align="center" prop="content" width="300">
          <template #default="scope">
            <el-popover effect="light" trigger="hover" placement="top" width="600">
              <template #default>
                <div>{{ scope.row.content }}</div>
              </template>
              <template #reference>
                <el-tag style="display: block;">
                  <p class="singe-line">{{ scope.row.content }}</p>
                </el-tag>
              </template>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="模型提示词" align="center" prop="prompt" width="300">
          <template #default="scope">
            <el-popover effect="light" trigger="hover" placement="top" width="600">
              <template #default>
                <div>{{ scope.row.prompt }}</div>
              </template>
              <template #reference>
                  <p class="singe-line">{{ scope.row.prompt }}</p>
              </template>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="工具值" align="center" prop="tools" />
        <el-table-column label="类型" align="center" prop="category" />
        <el-table-column label="添加时间" align="center" prop="createTime" width="160"/>
        <el-table-column label="权重值" align="center" prop="sort" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
          <template #default="scope">
            <el-button
              size="small"
              icon="edit"
              type="primary"
              text
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="small"
              icon="delete"
              type="primary"
              text
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    
    <!-- 添加或修改信义智能体助手配置对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="类型" prop="category">
          <el-input v-model="form.category" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="助手标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="展示提示词">
          <el-input
            v-model="form.content"
            :rows="4"
            resize="none"
            type="textarea"
            placeholder="请输入展示提示词"
          />
        </el-form-item>
        <el-form-item label="模型提示词">
          <el-input
            v-model="form.prompt"
            :rows="4"
            resize="none"
            type="textarea"
            placeholder="请输入展示提示词"
          />
        </el-form-item>
        <el-form-item label="工具">
          <el-input
            v-model="form.tools"
            :rows="4"
            resize="none"
            placeholder="请输入工具名称"
          />
        </el-form-item>
        <el-form-item label="icon地址" prop="banner">
          <el-upload
            class="avatar-uploader"
            accept="image/gif,image/jpeg,image/jpg,image/bmp,image/png"
            :headers="headers"
            :action="path"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.banner" :src="form.banner" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
          <!-- <el-input v-model="form.banner" placeholder="请输入banner地址" /> -->
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" 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 { listAssistant, getAssistant, delAssistant, addAssistant, updateAssistant } from "@/api/business/assistant";
import { getToken } from "@/utils/auth";

export default {
  name: "Assistant",
  data() {
    return {
      path: import.meta.env.VITE_APP_BASE_API + 'qiniuyun/upLoadImage',
      imageUrl: '',
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信义智能体助手配置表格数据
      assistantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        category: null,
        title: null,
        content: null,
        banner: null,
        sort: null,
        revision: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res) {
      this.imageUrl = res.data;
      this.form.banner = res.data;
    },
    beforeAvatarUpload (rawFile){
      if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('图片大小最大 2MB!')
        return false
      }
      return true
    },
    /** 查询信义智能体助手配置列表 */
    getList() {
      this.loading = true;
      listAssistant(this.queryParams).then(response => {
        this.assistantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        category: null,
        title: null,
        content: null,
        banner: null,
        sort: null,
        delFlag: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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
      getAssistant(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) {
            updateAssistant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssistant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该数据项？').then(function() {
        return delAssistant(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/assistant/export', {
        ...this.queryParams
      }, `assistant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.singe-line {
  line-height: 22px;
  text-overflow: ellipsis;
  overflow: hidden;
  word-break: break-all;
  white-space: nowrap;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  text-align: center;
}
</style>

<style scoped>
.avatar-uploader .avatar {
  width: 60px;
  height: 60px;
  display: block;
}
</style>