<!-- 基本信息-->
<template>

  <el-form label-position="left" ref="form" label-width="100px" :model="form" :size="defaultFormItemSize" :rules="rules">
    <el-row class="title">基本信息</el-row>
    <el-form-item label="模型名称:">
    <span>{{ tableItem.modelName }}</span>
    </el-form-item>
    <el-form-item label="模型类型:"  >
      <span>{{ ModelType.getValue(tableItem.modelType) }}</span>
    </el-form-item>
    <el-form-item label="业务标签:">
      <span>{{ tableItem.businessLabel}}</span>
        </el-form-item>
    <el-row class="title">模型配置</el-row>
    <el-form-item label="模型版本:">
      V{{ form.modelVersionDto.modelVersion}}
    </el-form-item>
    <el-form-item label="模型版本描述:">
      <el-input v-model="form.modelVersionDto.versionDescribe" type="textarea" class="inputWidth" :rows="5" :size="defaultFormItemSize"></el-input>
    </el-form-item>

  <el-form-item label="模型训练方式:">
      <el-select v-model="form.modelVersionDto.modelTrainingMethod" placeholder="请选择">
        <el-option v-for="item in ModelTrainingMode.getList()" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="训练任务:" prop="modelVersionDto.taskId">
      <el-select v-model="form.modelVersionDto.taskId" placeholder="请选择" @change="getTuningRun(form.modelVersionDto.taskId)">
        <el-option v-for="item in taskList" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="运行名称:" prop="modelVersionDto.runId">
      <el-select v-model="form.modelVersionDto.runId" placeholder="请选择"  :disabled="!form.modelVersionDto.taskId">
        <el-option v-for="item in  runNameList" :key="item.value" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </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 { ModelVersion, TuningTask, TuningRun } from '@/api/gptController.js';
export default {
  data () {
    return {
      taskList: [],
      runNameList: [],
      form: {
        'modelVersionDto': {
          taskId: undefined,
          runId: undefined,
          'modelId': '',
          'modelTrainingMethod': 0, //
          'modelVersion': '', //
          'runName': '', //
          'trainingTask': '', //
          'versionDescribe': '' //

        }
   
      },
      rules: {
        'modelVersionDto.taskId': [
          { required: true, message: '请选择训练任务', trigger: 'blur' }
        ],
        'modelVersionDto.runId': [
          { required: true, message: '请选择运行名称', trigger: 'blur' }
        ]
      }

    };
  },
  props: ['tableItem', 'versionIndex'],
  components: {},

  computed: {

  },

  mounted () {
    this.intFrom()

    this.getTaskList();
  },

  methods: {
    intFrom () {
      this.form.modelVersionDto.modelId = this.tableItem.modelId
      this.form.modelVersionDto.modelVersion = this.versionIndex + 1
    },
    onCancel (isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess);
      }
    },
    onSubmit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let params = {};
            params = { ...this.form };

            if (this.isEdit) {
              ModelVersion.update(this, params).then(res => {
                resolve(res);
                this.$message.success('编辑成功');
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            } else {
              ModelVersion.add(this, params).then(res => {
                resolve(res);
                this.$message.success('添加成功');
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            }
          } else {
            reject();
          }
        });
      });
    },
    getTaskList () {
      TuningTask.listForTree(this, {}).then(res => {
        this.taskList = res.data.map((item) => {
          return { id: item.taskId, name: item.taskName }
        })
      }).catch(e => {
        console.log(e);
      });
    },
    getTuningRun (taskId) {
      TuningRun.listForTree(this, {tuningRunDtoFilter: {taskId: taskId}}).then(res => {
        // console.log(res.data);
        this.runNameList = res.data.map((item) => {
          return { id: item.runId, name: item.runName }
        })
      }).catch(e => {
        console.log(e);
      });
    }
  }
};
</script>
<style scoped>
.inputWidth {
  width: 600px;
}
.title {
  font-size: 20px;
  margin-bottom: 16px;
}
.introduce {
  font-size: 12px;
  color: #909399;
}
</style>
