<template>
  <div style="position: relative">
    <el-collapse v-model="activeName" accordion ref="collapse" v-resize="resize" style="margin-bottom:20px">
      <el-collapse-item name="1">
        <template slot="title">
          <div class="title">模型评估</div>
        </template>
        <div class="instructions">
          <div class="describe">
            对⽣成式⼤模型的输出效果进⾏全⽅位评价，提供⾯向事实类或开放性问答的多种打分模式；当前⽀持⽂本类⽣成模型，暂不⽀持图像或跨模态⽣成模型。
          </div>
          <div class="instructionsList">
            <div class="item" v-for="(item, index) in instructionsList" :key="index">
              <div style="height:100%">
                <img class="img" :src="item.img" alt="" />
                <div class="itemTitle">
                  <span class="head-index">{{ index + 1 }} &nbsp;</span>{{ item.title }}
                </div>
                <div class="itemDescribe">{{ item.describe }}</div>
              </div>
              <i v-if="instructionsList.length !== index + 1" class="el-icon-arrow-right" style="margin-left: 50px; font-size: 30px; font-weight: bolder;color:#b8babf"></i>
            </div>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div class="tableBox" :style="{ height: tableHeight }">
      <el-form ref="myDataSetPage" :model="myDataSetPage" label-width="75px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
        <filter-box :item-width="350" @search="refresh()" @reset="onReset">
          <el-form-item label-width="0px">
            <el-button class="add" type="primary" icon="el-icon-plus" :size="defaultFormItemSize" @click="add()">创建评估任务</el-button>
          </el-form-item>
          <el-form-item label="任务名称" prop="formFilter.taskName" label-width="120px">
            <el-input class="filter-item" v-model="myDataSetPage.formFilter.taskName" :clearable="true" placeholder="任务名称" />
          </el-form-item>
        </filter-box>
      </el-form>

      <vxe-table border show-header-overflow show-overflow :row-config="{ isHover: true }" :data="myDataSetPage.tableData.impl.dataList" min-height="96">
        <vxe-column field="taskName" title="任务名称"></vxe-column>
        <vxe-column field="taskStatus" title="任务状态">
          <template slot-scope="scope">
            {{ TaskStatus.getValue(scope.row.taskStatus) }}
          </template>
        </vxe-column>
        <vxe-column field="modelVersionId" title="评估模型"></vxe-column>
        <vxe-column field="createTime" title="创建时间"></vxe-column>
        <!-- <vxe-column field="scoringMode" title="打分模式"></vxe-column> -->
        <vxe-column field="operation" title="操作"  width="210px">
          <template slot-scope="scope">
            <el-button type="text"  icon="el-icon-menu" :size="defaultFormItemSize" @click="particulars(scope.row)">详情</el-button>

            <el-button type="text" icon="el-icon-document-copy" :size="defaultFormItemSize" @click="copy(scope.row)">复制</el-button>

            <el-button type="text" icon="el-icon-delete" :size="defaultFormItemSize" @click="del(scope.row)">删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px;width: 100%;">
        <el-pagination :total="myDataSetPage.tableData.impl.totalCount" :current-page="myDataSetPage.tableData.impl.currentPage" :page-size="myDataSetPage.tableData.impl.pageSize" :page-sizes="[10, 20, 50, 100]" layout="total, prev, pager, next, sizes" @current-change="myDataSetPage.tableData.impl.onCurrentPageChange" @size-change="myDataSetPage.tableData.impl.onPageSizeChange">
        </el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
/* eslint-disable-next-line */
import { ModelEstimate } from '@/api/gptController.js'
import { TableWidget } from '@/utils/widget.js'
import editOrAdd from './dialog/editOrAdd'
import particulars from './dialog/particulars'

export default {
  data() {
    return {
      activeName: '1',
      collapseHeight: 0,
      instructionsList: [
        {
          title: '评估数据准备',
          describe: '准备用于评估模型能力的验证数据集，并在 数据集管理 中导入和发布',
          img: require('@/assets/img/pg1.svg')
        },
        {
          title: '模型结果生成',
          describe: '使用评估数据集，批量生成模型推理结果，以便进行下一步打分',
          img: require('@/assets/img/pg2.svg')
        },
        {
          title: '评估指标计算',
          describe: '根据评估任务设定的打分模式，对模型生成结果进行打分，并汇总计算评估指标、产出评估报告',
          img: require('@/assets/img/pg3.svg')
        }
      ],
      myDataSetPage: {
        formFilter: {
          taskName: ''
        },

        tableData: {
          impl: new TableWidget(this.getwidgetData, true, true, false, undefined, false)
        }
      }
    }
  },

  components: {},

  computed: {
    ...mapGetters(['getMainContextHeight']),
    tableHeight() {
      return this.getMainContextHeight - this.collapseHeight - 55 + 'px'
    }
  },

  methods: {
    getwidgetData(params) {
      if (params == null) params = {}
      params = {
        ...params,
        // orderParam: [
        //   {
        //     asc: true,
        //     dateAggregateBy: '',
        //     fieldName: ''
        //   }
        // ],
        // pageParam: {
        //   pageNum: 0,
        //   pageSize: 0
        // },
        modelEstimateDtoFilter: { ...this.myDataSetPage.formFilter }
      }

      return new Promise((resolve, reject) => {
        ModelEstimate.list(this, params)
          .then((res) => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            })
          })
          .catch((e) => {
            reject(e)
          })
      })
    },
    particulars(row) {
      this.$dialog
        .show(
          '详情',
          particulars,
          {
            area: ['100%', '100%']
          },
          { item: row, page: 'particulars' }
        )
        .then((res) => {})
        .catch((e) => {
          this.refresh()
        })
    },
    add() {
      this.$dialog
        .show(
          '创建任务',
          editOrAdd,
          {
            area: ['100%', '100%']
          },
          { isEdit: false }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    edit(item) {
      this.$dialog
        .show(
          '修改模版',
          editOrAdd,
          {
            area: ['100%', '100%']
          },
          { isEdit: true, item: item }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    copy(item) {
      this.$dialog
        .show(
          '复制任务',
          editOrAdd,
          {
            area: ['100%', '100%']
          },
          {
            isCopy: true,
            item: {
              taskName: item.taskName,
              taskDescribe: item.taskDescribe,
              modelVersionId: item.modelVersionId,
              datasetVersionId: item.datasetVersionId
            }
          }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    del(item) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { taskId: item.taskId }
        ModelEstimate.delete(this, params)
          .then((res) => {
            this.$message.success('删除成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    refresh(reloadData = false) {
      if (reloadData) {
        this.myDataSetPage.tableData.impl.refreshTable(true, 1)
      } else {
        this.myDataSetPage.tableData.impl.refreshTable()
      }
    },
    onReset() {
      this.$refs.myDataSetPage.resetFields()
      this.refresh(true)
    },
    formInit() {
      this.refresh()
    },
    resize(e) {
      setTimeout(() => {
        this.collapseHeight = e?.height
      }, 300)
    }
  },
  mounted() {
    this.resize()
    this.formInit()
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/style/element-variables.scss';
.title {
  font-size: 16px;
}
.instructions {
  background-color: white;
  width: 100%;
  padding: 0px 20px 20px 20px;
  height: 250px;

  .instructionsList {
    display: flex;
    flex-direction: row;
    justify-content: space-around;

    .item {
      display: flex;
      align-items: center;
    }
  }

  .title {
    color: $--color-text-primary;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
  }

  .describe {
    color: $--color-text-secondary;
    margin-bottom: 16px;
  }

  .itemTitle {
    color: $--color-text-primary;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
  }

  .head-index {
    font-size: 24px;
    color: #e8e9eb;
    line-height: 32px;
  }

  .itemDescribe {
    width: 170px;
    color: $--color-text-secondary;
  }

  .img {
    height: 72px;
    width: 140px;
  }
}

.tableBox {
  background-color: white;
  width: 100%;
  padding: 20px;
  overflow: auto;
  .add {
    margin-bottom: 20px;
  }
}

div /deep/ .el-collapse-item__content {
  padding-bottom: 0;
}

div /deep/ .el-collapse-item__header {
  padding-left: 20px;
}
</style>
