<!-- 任务列表 -->

<template>
  <div style="position: relative">

    <el-button type="primary" :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="runName" title="运行名称"></vxe-column>
      <vxe-column field="runStatus" title="运行状态">
       <template slot-scope="scope">
          {{ RunningStatus.getValue(scope.row.runStatus) }}
        </template>
      </vxe-column>
      <vxe-column field="modelId" title="基础模型">

      </vxe-column>
      <vxe-column field="runTime" title="运行时长">
        <!-- <template slot-scope="scope">
          {{ TaskStatus.getValue(scope.row.taskStatus) }}
        </template> -->
      </vxe-column>
      <vxe-column field="createUserId" title="创建人"></vxe-column>
      <vxe-column field="createTime" title="创建时间"></vxe-column>
      <vxe-column field="operation" title="操作">
        <template slot-scope="scope">

          <el-button type="text" :size="defaultFormItemSize" @click="particulars(scope.row)">详情</el-button>
          <el-button type="text" :size="defaultFormItemSize" @click="edit(scope.row)">编辑</el-button>

          <el-button type="text" :size="defaultFormItemSize" @click="publish(scope.row)">发布</el-button>
          <el-button type="text" :size="defaultFormItemSize" @click="assessmentReport(scope.row)">评估报告</el-button>

          <el-dropdown>
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item  @click.native="trainingLog(scope.row)">训练日志</el-dropdown-item>
              <el-dropdown-item>训练过程可视化</el-dropdown-item>
              <el-dropdown-item @click.native="del(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>
      </vxe-column>
    </vxe-table>
    <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px;">
      <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 { TuningRun } from '@/api/gptController.js';
// import editOrAdd from './dialog/editOrAdd';
import taskOperate from './dialog/taskOperate.vue';
import publish from './dialog/publish.vue';

import editOrAdd from '../../dialog/editOrAdd.vue';
export default {
  data () {
    return {
      myDataSetPage: {
        formFilter: {

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

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

  },

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

        tuningRunDtoFilter: { taskId: this.tableItem.taskId }
      }

      return new Promise((resolve, reject) => {
        TuningRun.list(this, params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          });
        }).catch(e => {
          reject(e);
        });
      });
    },
    add () {
      this.$dialog.show('创建SFT任务', editOrAdd, {
        area: ['100%', '100%']
      }, {
        isEdit: false,
        existingTask: true,
        item: {
          tuningTaskDto: {
            taskName: this.tableItem.taskName,
            taskDescribe: this.tableItem.taskDescribe,
            taskType: this.tableItem.taskType,
            taskId: this.tableItem.taskId
          }
        }
      }).then(res => {
        this.refresh();
      }).catch(e => { });
    },
    edit (item) {
      this.$dialog.show('修改模版', editOrAdd, {
        area: ['100%', '100%']
      }, {
        isEdit: true,
        item: {
          tuningRunDto: {...item },
          tuningTaskDto: {
            taskName: this.tableItem.taskName,
            taskDescribe: this.tableItem.taskDescribe,
            taskType: this.tableItem.taskType,
            taskId: this.tableItem.taskId
          }
        }
      }).then(res => {
        this.refresh();
      }).catch(e => { });
    },
    del (item) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { runId: item.runId }
        TuningRun.delete(this, params).then(res => {
          this.$message.success('删除成功');
          this.refresh()
        }).catch(e => {
          console.log(e);
        });
      })
    },
    particulars (item) {
      this.$dialog.show('任务详情', taskOperate, {
        area: ['100%', '100%']
      }, {page: 'particulars', item: {...item, createUserIdDictMap: this.tableItem.createUserIdDictMap} }).then(res => {
        this.refresh();
      }).catch(e => {
        this.refresh();
      });
    },
    trainingLog (item) {
      console.log('trainingLog');
      this.$dialog.show('任务详情', taskOperate, {
        area: ['100%', '100%']
      }, { page: 'trainingLog', item: item }).then(res => {
        this.refresh();
      }).catch(e => {
        this.refresh();
      });
    },
    assessmentReport (item) {
      this.$dialog.show('任务详情', taskOperate, {
        area: ['100%', '100%']
      }, {page: 'assessmentReport', item: item }).then(res => {
        this.refresh();
      }).catch(e => {
        this.refresh();
      });
    },
    publish (item) {
      this.$dialog.show('发布', publish, {
        area: ['50%', '70%']
      }, { item: item }).then(res => {
        this.refresh();
      }).catch(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 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 {
  background-color: white;
  width: 100%;
  padding: 20px;
  overflow: auto;
  .add {
    margin-bottom: 20px;
  }
}
.el-dropdown{
  color: #0092ff;
    font-size: 14px;
    margin-left: 10px;
}
</style>
