<!-- 我的数据集 -->
<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="instructionsList">
            <div class="item" v-for="(item, index) in instructionsList" :key="index">
              <div>
                <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.searchString">
            <el-input class="filter-item" v-model="myDataSetPage.formFilter.searchString" :clearable="true" placeholder="数据集名称" />
          </el-form-item>
          <el-form-item label="标注类型" prop="formFilter.dimensionType">
            <el-select v-model="myDataSetPage.formFilter.dimensionType" placeholder="请选择" label-width="120px">
              <el-option v-for="item in LabelType.getList()" :key="item.value" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </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="datasetName" title="数据集名称"></vxe-column>
        <vxe-column field="datasetVersion" title="最新版本">
          <template slot-scope="scope">
            V{{ scope.row.datasetVersion  }}
          </template>
        </vxe-column>
        <vxe-column field="dataType" title="数据类型">
          <template slot-scope="scope">
            {{ DataType.getValue(scope.row.dataType )  }}
          </template>
        </vxe-column>
        <vxe-column field="dimensionType" title="标注类型">
          <template slot-scope="scope">
            {{ LabelType.getValue(scope.row.dimensionType )  }}
          </template>
        </vxe-column>
        <vxe-column field="dataVolume" title="数据量"></vxe-column>
        <vxe-column field="inputStatus" title="导入状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.inputStatus!==0" size="mini"> {{ ImportStatus.getValue(scope.row.inputStatus )  }}</el-tag>
            <el-tag v-else type="info" size="mini"> {{ ImportStatus.getValue(scope.row.inputStatus )  }}</el-tag>
          </template>
        </vxe-column>
        <vxe-column field="releaseStatus" title="发布状态">
          <template slot-scope="scope">

            <el-tag v-if="scope.row.releaseStatus!==0" type="danger" size="mini">{{ PublishStatus.getValue(scope.row.releaseStatus )  }}</el-tag>
            <el-tag v-else type="info" size="mini"> {{ PublishStatus.getValue(scope.row.releaseStatus )  }}</el-tag>
          </template>
        </vxe-column>
        <vxe-column field="createTime" title="创建时间"></vxe-column>
        <vxe-column field="operation" title="操作" width="230">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-menu" @click="all(scope.row)" :size="defaultFormItemSize">详情</el-button>
            <el-button v-if="scope.row.releaseStatus==0" type="text" icon="el-icon-plus" @click="dataImport(scope.row)" :size="defaultFormItemSize">导入</el-button>
            <el-button type="text" icon="el-icon-reading" v-else :size="defaultFormItemSize" @click='drill(scope.row)'>训练</el-button>
            <template v-if="scope.row.inputStatus !== 0">
              <el-dropdown style="margin-left:15px;color: #0092FF;" :size="defaultFormItemSize">
                <span class="el-dropdown-link">
                  更多<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-if="scope.row.releaseStatus!==1" @click.native="putOut(scope.row)">发布</el-dropdown-item>
                  <el-dropdown-item @click.native="derived(scope.row)">导出</el-dropdown-item>
                  <el-dropdown-item>清洗</el-dropdown-item>
                  <el-dropdown-item @click.native="delDataset(scope.row)">删除数据集</el-dropdown-item>
                  <el-dropdown-item @click.native="delVersions(scope.row)">删除版本</el-dropdown-item>

                  <!-- <el-dropdown-item @click.native="analyse(scope.row)">分析</el-dropdown-item>
                  <el-dropdown-item @click.native="enhance(scope.row)">增强</el-dropdown-item> -->
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </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 { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js'
import { MyDataSet, DatasetVersion } from '@/api/gptController.js'
import editOrAdd from './dialog/editOrAdd'
import addVersions from './versionsTableBox/dialog/add.vue'
import versionsTableBox from './versionsTableBox/index.vue'
import derived from './versionsTableBox/dialog/derived.vue'
import dataImport from "./versionsTableBox/dialog/dataImport";
export default {
  data() {
    return {
      activeName: '1',
      collapseHeight: 0,
      instructionsList: [
        {
          title: '创建数据集',
          describe: '指定适用于大语言模型或者文生图大模型的数据类型，创建对应数据集',
          img: require('@/assets/img/create.e4129a2c.png')
        },
        {
          title: '加工数据集',
          describe: '通过数据分析/清洗标注，对数据资产进行加工处理，提升数据质量',
          img: require('@/assets/img/machining.946ba7b5.png')
        },
        {
          title: '发布并使用数据集',
          describe: '发布该版本数据集，供后续大模型精调等环节使用',
          img: require('@/assets/img/fb.png')
        }
      ],
      myDataSetPage: {
        formFilter: {
          searchString: '',
          dimensionType: undefined
        },

        tableData: {
          impl: new TableWidget(this.getwidgetData, true, true, false, 'datasetId', false)
        }
      }
    }
  },
  provide() {
    return {
      refresh: this.refresh
    }
  },
  components: {},

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

  methods: {
    getwidgetData(params) {
      if (params == null) params = {}
      params = {
        ...params,
        datasetManageDtoFilter: { ...this.myDataSetPage.formFilter }
      }

      return new Promise((resolve, reject) => {
        MyDataSet.findDatasets(this, params)
          .then((res) => {
            resolve({
              dataList: res.data.dataList.map((item) => {
                return { ...item, isEdit: false }
              }),
              totalCount: res.data.totalCount
            })
          })
          .catch((e) => {
            reject(e)
          })
      })
    },
    dataImport(data) {
      this.$dialog
        .show(
          '导入',
          dataImport,
          {
            area: ['600px', '200px']
          },
          { row: data }
        )
        .then((res) => {
          this.refresh()
          this.getList()
        })
        .catch((e) => {})
    },
    addVersions(row) {
      this.$dialog
        .show(
          '新增数据集版本',
          addVersions,
          {
            area: ['600px', 'auto']
          },
          { isEdit: false, datasetId: row.datasetId }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    delDataset(item) {
      this.$confirm('是否确认删除数据集', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { datasetId: item.datasetId }
        MyDataSet.delete(this, params)
          .then((res) => {
            this.$message.success('删除成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    delVersions(item) {
      this.$confirm('是否确认删除最新版本', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { versionId: item.versionId }
        MyDataSet.deleteVersion(this, params)
          .then((res) => {
            this.$message.success('删除成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    all(row) {
      // this.dialogTitle = this.value.datasetName
      // this.dialogVisible = true
      this.$dialog
        .show(
          row.datasetName,
          versionsTableBox,
          {
            area: ['100%', '100%']
          },
          { tableData: row, refresh: this.refresh }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },

    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) => {})
    },
    del(item) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { batchTaskId: item.batchTaskId }
        MyDataSet.delete(this, params)
          .then((res) => {
            this.$message.success('删除成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    label(data) {
      // 标注
    },
    derived(data) {
      // 导出
      this.$dialog
        .show(
          '导出',
          derived,
          {
            area: ['100%', '100%']
          },
          { row: data }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    analyse(data) {
      // 分析
    },
    enhance(data) {
      // 增强
    },
    drill(data) {
      // 去训练
      console.log(data)
      this.$router.push({ name: 'sft' })
    },
    putOut(data) {
      console.log(data)
      this.$confirm('是否确认发布', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { versionId: data.versionId }
        DatasetVersion.publish(this, params)
          .then((res) => {
            this.$message.success('发布成功')
            this.refresh()
            this.getList()
          })
          .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)
    },
    resize(e) {
      setTimeout(() => {
        this.collapseHeight = e?.height
      }, 300)
    },
    formInit() {
      this.refresh()
    }
  },
  mounted() {
    this.resize()
    this.formInit()
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/style/element-variables.scss';
.tableBox {
  background-color: white;
  width: 100%;
  padding: 20px;
  overflow: auto;
}
.add {
  margin: 0 0 20px 0;
}
.instructions {
  background-color: white;
  width: 100%;
  padding: 0px 20px 20px 20px;
  height: 200px;

  .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>
