<!-- 我的数据集 -->
<template>
  <div style="position: relative">
    <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="标注类型" prop="formFilter.dimensionType">
          <el-select v-model="myDataSetPage.formFilter.dimensionType" placeholder="请选择">
            <el-option v-for="item in LabelType.getList()" :key="item.value" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据集名称或id" prop="formFilter.searchString" label-width="120px">
          <el-input class="filter-item" v-model="myDataSetPage.formFilter.searchString" :clearable="true" placeholder="数据集名称或id" />
        </el-form-item>
      </filter-box>
    </el-form>
    <div class="tableBox" :style="{ height: tableHeight }">
      <el-button class="add" type="primary" icon="el-icon-plus" :size="defaultFormItemSize" @click="add()">创建数据集</el-button>
      <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="datasetVersionList" title="最新版本">
          <template slot-scope="scope">
            V{{ scope.row.datasetVersionList[scope.row.datasetVersionList.length-1].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="createTime" title="创建时间"></vxe-column>
        <vxe-column field="operation" title="操作" width="280">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-plus" @click="addVersions(scope.row)">新增版本</el-button>
            <el-button type="text" icon="el-icon-menu" @click="all(scope.row)">全部版本</el-button>
            <el-button type="text" icon="el-icon-delete" @click="delDataset(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 { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js'
import { MyDataSet } from '@/api/gptController.js'
import editOrAdd from './dialog/editOrAdd'
import addVersions from './tableItem/dialog/add.vue'
import tableBox from './tableItem/tableBox/index.vue'
/* eslint-disable-next-line */
import tableItem from './tableItem'
export default {
  data() {
    return {
      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 - 118 + 'px'
    }
  },

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

      return new Promise((resolve, reject) => {
        MyDataSet.list(this, params)
          .then((res) => {
            resolve({
              dataList: res.data.dataList.map((item) => {
                return { ...item, isEdit: false }
              }),
              totalCount: res.data.totalCount
            })
          })
          .catch((e) => {
            reject(e)
          })
      })
    },
    addVersions(row) {
      this.$dialog
        .show(
          '新增数据集版本',
          addVersions,
          {
            area: ['600px', 'auto']
          },
          { isEdit: false, datasetId: row.datasetId, versionList: row.datasetVersionList }
        )
        .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)
          })
      })
    },

    all(row) {
      // this.dialogTitle = this.value.datasetName
      // this.dialogVisible = true
      this.$dialog
        .show(
          row.datasetName,
          tableBox,
          {
            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)
          })
      })
    },
    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()
    }
  },
  mounted() {
    this.formInit()
  }
}
</script>
<style scoped>
.tableBox {
  background-color: white;
  width: 100%;
  padding: 20px;
  overflow: auto;
}
.add {
  margin: 0 0 20px 0;
}
</style>
