<!--详情 -->
<template>

  <div class="tableBox">
    <el-breadcrumb separator="|">
      <el-breadcrumb-item :class="nowPage==0?'isactive':''" @click.native="breadcrumbClick(0)">全部（{{ allNum}}）</el-breadcrumb-item>
      <el-breadcrumb-item :class="nowPage==1?'isactive':''" @click.native="breadcrumbClick(1)">无标注信息（{{ unmarkedNum}}）</el-breadcrumb-item>
      <el-breadcrumb-item :class="nowPage==2?'isactive':''" @click.native="breadcrumbClick(2)">有标注信息（{{ markedNum}}）</el-breadcrumb-item>
    </el-breadcrumb>
    <vxe-table border show-header-overflow show-overflow :row-config="{ isHover: true }" :data="myDataSetPage.tableData.impl.dataList" min-height="96">
      <!-- <vxe-column field="index" title="序号"></vxe-column> -->
      <!-- <vxe-column :field="'data.'+item" :title="item" v-for="item in columnList" :key="item"></vxe-column> -->
      <vxe-column field="data.instruction" title="问题"></vxe-column>
      <vxe-column field="data.output" title="回答"></vxe-column>

      <vxe-column field="operation" title="操作" width="150px">
        <template slot-scope="scope">
          <el-button type="text" :size="defaultFormItemSize" @click="check(scope.row)">查看</el-button>
          <el-button type="text" :size="defaultFormItemSize" @click="label(scope.row)" v-if="versionItem.dimensionType===0">标注</el-button>
          <el-button type="text" :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>

</template>

<script>
import { mapGetters } from 'vuex'
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js'
import { DatasetData } from '@/api/gptController.js'
import label from './label.vue'
export default {
  data() {
    return {
      unmarkedNum: 0,
      allNum: 0,
      markedNum: 0,
      nowPage: 0,
      markStatus: undefined,
      myDataSetPage: {
        tableData: {
          impl: new TableWidget(this.getwidgetData, true, true, false, undefined, false)
        }
      },
      columnList: []
    }
  },

  components: {},
  props: ['versionItem'],
  computed: {
    ...mapGetters(['getMainContextHeight']),
    tableHeight() {
      return this.getMainContextHeight + 'px'
    }
  },

  methods: {
    getwidgetData(params) {
      if (params == null) params = {}
      params = {
        datasetDataDtoFilter: {
          versionId: this.versionItem.versionId,
          markStatus: this.markStatus
        },
        ...params
      }
      return new Promise((resolve, reject) => {
        DatasetData.load(this, params)
          .then((res) => {
            let dataList = res.data.dataList.map((item) => {
              try {
                var obj = JSON.parse(item.data)
              } catch (err) {
                console.log('JSON解析错误：' + err)
              }
              return {
                ...item,
                data: obj
              }
            })
            if (this.columnList.length === 0) {
              for (const key in dataList[0].data) {
                if (Object.hasOwnProperty.call(dataList[0].data, key)) {
                  if (key !== '_X_ROW_KEY') {
                    this.columnList.push(key)
                  }
                }
              }
            }
            // dataList.forEach((element) => {
            //   for (const key in element.data) {
            //     if (Object.hasOwnProperty.call(element.data, key)) {
            //       element.data[key] = JSON.stringify(element.data[key])
            //     }
            //   }
            // })
            resolve({
              dataList,
              totalCount: res.data.totalCount
            })
          })
          .catch((e) => {
            reject(e)
          })
      })
    },
    check(row) {
      // let data = []
      // for (const key in row.data) {
      //   if (key !== '_X_ROW_KEY') {
      //     // data.push({ [key]: row.data[key] })
      //     data += `${[key]}: ${row.data[key]}\n\n`
      //   }
      // }
      // console.log(data)
      // this.$alert(data, '详情', {
      //   confirmButtonText: '确定',
      //   callback: (action) => {}
      // })
      this.$dialog
        .show(
          '查看',
          label,
          {
            area: ['70%', '50%']
          },
          { row, columnList: this.columnList, isEdit: false }
        )
        .then((res) => {})
        .catch((e) => {})
    },
    label(row) {
      // 标注
      // let newRow = JSON.parse(JSON.stringify(row))
      this.$dialog
        .show(
          '标注',
          label,
          {
            area: ['70%', '50%']
          },
          { row, columnList: this.columnList, refresh: this.refresh, isEdit: true }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    del(item) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { id: item._id, versionId: item.versionId }
        DatasetData.delete(this, params)
          .then((res) => {
            this.$message.success('删除成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    breadcrumbClick(index) {
      this.nowPage = index
      this.markStatus = index === 0 ? undefined : index === 1 ? 0 : index === 2 ? 1 : undefined
      this.refresh()
    },
    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()
    },
    summaryCount() {
      let params = { versionId: this.versionItem.versionId }
      DatasetData.summaryCount(this, params)
        .then((res) => {
          this.allNum = res.data.allMarkCount
          this.unmarkedNum = res.data.unMarkCount
          this.markedNum = res.data.markCount
        })
        .catch((e) => {
          console.log(e)
        })
    }
  },

  mounted() {
    this.formInit()
    this.summaryCount()
  },
  watch: {
    versionItem: {
      handler() {
        this.formInit()
        this.summaryCount()
      }
    },
    deep: true
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/style/element-variables.scss';
.topBox {
  background-color: white;
  width: 100%;
  height: 95px;
  margin-bottom: 16px;
  .title {
    color: $--color-text-primary;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
    padding: 20px 20px 10px 20px;
  }
}
.instructions {
  padding: 0px 20px 20px 20px;

  .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 {
  position: relative;
  background-color: white;
  width: 100%;
  padding: 0 20px 20px 20px;
  height: 100%;
  overflow: auto;
  .add {
    margin-bottom: 20px;
  }
}
/deep/ .el-message-box__message {
  white-space: pre-line;
}
/deep/ .el-breadcrumb {
  height: 40px;
  line-height: 40px;
  margin-bottom: 10px;
}
/deep/ .isactive .el-breadcrumb__inner {
  color: #0092ff !important;
}
</style>
