<!-- 创建sft模板 -->
<template>
  <el-form label-position="left" ref="form" label-width="120px" :model="form" :size="defaultFormItemSize" :rules="rules">
    <el-row class="title">基本信息</el-row>
    <el-form-item label="任务名称:" prop="tuningTaskDto.taskName">
      <el-input v-model="form.tuningTaskDto.taskName" :readonly="isEdit || existingTask" class="inputWidth" :size="defaultFormItemSize" v-if="!(isEdit || existingTask)"></el-input>
      <span v-else>{{ form.tuningTaskDto.taskName }}</span>
    </el-form-item>
    <!-- <el-form-item label="任务类型:">
      <el-select v-model="form.tuningTaskDto.taskType" placeholder="请选择">
        <el-option v-for="item in ModelType.getList()" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item> -->
    <el-form-item label="任务描述:">
      <el-input v-model="form.tuningTaskDto.taskDescribe" :readonly="isEdit || existingTask" type="textarea" :rows="4" class="inputWidth" :size="defaultFormItemSize" v-if="!(isEdit || existingTask)"></el-input>
      <span v-else>{{ form.tuningTaskDto.taskDescribe }}</span>
    </el-form-item>

    <el-row class="title">训练配置</el-row>
    <el-row>
      <span class="introduce">训练任务的算法选择、参数及相关配置，训练配置参数影响训练速度及模型效果。</span></el-row>
    <el-form-item label="选择基础模型版本:" prop="tuningRunDto.modelVersionId">
      <el-cascader v-model="form.tuningRunDto.modelVersionId" :options="modelList" :props="{ label: 'name', value: 'id', emitPath: false }"></el-cascader>
    </el-form-item>

    <el-form-item label="训练方法:">
      <el-radio v-for="item in TrainingMethod.getList()" :key="item.id" v-model="form.tuningRunDto.trainMethod" :label="item.name">{{ item.name }}</el-radio>
    </el-form-item>
    <el-form-item label="参数配置:">
      <parameterConfiguration v-model="form.tuningRunDto.configuration" :existingTask="existingTask" />
    </el-form-item>

    <el-row class="title">数据配置</el-row>
    <el-row>
      <span class="introduce">
        训练任务的选择数据及相关配置，支持选择该模型可使用的数据。</span></el-row>

    <el-form-item label="选择数据集版本" prop="tuningRunDto.datasetVersionId">
      <el-cascader v-model="form.tuningRunDto.datasetVersionId" :options="dataList" :props="{ label: 'name', value: 'id', emitPath: false }"></el-cascader>
    </el-form-item>
    <el-form-item label="数据拆分比例:">
      <el-input-number v-model="form.tuningRunDto.splitRatio" class="inputWidth" :size="defaultFormItemSize" :min="0" :max="100"></el-input-number>
    </el-form-item>
    <el-row type="flex" justify="end" class="dialog-btn-layer mt20">
      <el-button :size="defaultFormItemSize" :plain="true" @click="onCancel(false)">取消</el-button>
      <el-button type="primary" :size="defaultFormItemSize" @click="onSubmit">确定</el-button>
    </el-row>
  </el-form>
</template>

<script>
import { TuningTask, TuningRun, MyModel, MyDataSet } from '@/api/gptController.js'
import parameterConfiguration from './parameterConfiguration/index.vue'
export default {
  data() {
    return {
      modelList: [],
      dataList: [],

      form: {
        tuningRunDto: {
          configuration: undefined,
          modelId: undefined,
          publishStatus: 0,
          runName: '',
          runStatus: 0,
          runTime: 0,
          runVersion: 0,
          splitRatio: 0,
          taskId: undefined,
          trainMethod: 'full',
          trainMode: '',
          datasetVersionId: undefined,
          modelVersionId: undefined
        },
        tuningTaskDto: {
          taskDescribe: '',
          taskName: '',
          taskType: 0
        }
      },
      rules: {
        'tuningTaskDto.taskName': [{ required: true, message: '请输入任务名称', trigger: 'blur' }],

        'tuningRunDto.modelVersionId': [{ required: true, message: '请选择基础模型版', trigger: 'blur' }],
        'tuningRunDto.datasetVersionId': [{ required: true, message: '请选择数据集版本', trigger: 'blur' }]
      }
    }
  },
  props: ['isEdit', 'item', 'existingTask'],
  components: { parameterConfiguration },

  computed: {},

  mounted() {
    this.getModelList()
    this.getDataList()
    this.intFrom()
  },

  methods: {
    intFrom() {
      if (this.item) {
        this.form.tuningTaskDto = this.item.tuningTaskDto
        if (this.isEdit) {
          this.form.tuningRunDto = {
            ...this.form.tuningRunDto,
            ...this.item.tuningRunDto
          }
        }
        if (this.existingTask) {
          this.form.tuningRunDto.taskId = this.item.tuningTaskDto.taskId
        }
      }
    },
    onCancel(isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess)
      }
    },
    onSubmit() {
      let apiFunction = this.existingTask || this.isEdit ? TuningRun : TuningTask
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let params = {}
            params = this.existingTask ? { tuningRunDto: this.form.tuningRunDto } : { ...this.form }
            if (this.isEdit) {
              apiFunction
                .update(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('编辑成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            } else {
              apiFunction
                .add(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('添加成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            }
          } else {
            // reject();
          }
        })
      })
    },
    getModelList() {
      MyModel.listForTree(this, {})
        .then((res) => {
          this.modelList = res.data
            .filter((item) => item.isBaseModel === 1)
            .map((item) => {
              return {
                id: item.modelId,
                name: item.modelName,
                // prettier-ignore
                children: item.modelVersionList === [] ? [] : item.modelVersionList.map((item2) => {
                  return { id: item2.versionId, name: 'V' + item2.modelVersion }
                })
              }
            })
        })
        .catch((e) => {
          console.log(e)
        })
    },
    getDataList() {
      MyDataSet.listForTree(this, {})
        .then((res) => {
          this.dataList = res.data.map((item) => {
            return {
              id: item.datasetId,
              name: item.datasetName,
              // prettier-ignore
              children: item.datasetVersionList === [] ? [] : item.datasetVersionList.map((item2) => {
                return { id: item2.versionId, name: 'V' + item2.datasetVersion }
              })
            }
          })
        })
        .catch((e) => {
          console.log(e)
        })
    }
  }
}
</script>
<style scoped>
.inputWidth {
  width: 600px;
}

.title {
  font-size: 20px;
  margin-bottom: 16px;
}

.introduce {
  font-size: 12px;
  color: #909399;
  margin-bottom: 10px;
  display: block;
}

.isReadonly input {
  border: none;
}
</style>
