<!-- 基本信息-->
<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="模型名称:"  prop="modelManageDto.modelName">
      <el-input v-model="form.modelManageDto.modelName" class="inputWidth" :size="defaultFormItemSize"></el-input>
      <el-row> <span class="introduce">支持中英文、数字、下划线(_)，2-20个字符，不能以下划线为开头</span></el-row>
    </el-form-item>
    <el-form-item label="模型类型:">
      <el-radio v-for="item in ModelType.getList()" v-model="form.modelManageDto.modelType" :key="item.id" :label="item.id">{{item.name}}</el-radio>

    </el-form-item>
    <el-form-item label="业务标签:">
      <el-input v-model="form.modelManageDto.businessLabel" class="inputWidth" :size="defaultFormItemSize"></el-input>
      <el-row> <span class="introduce">标签由中英文或数字组成，1-10个字符，回车添加，最多添加5个标签</span></el-row>
    </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.id" :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 { MyModel, TuningTask, TuningRun } from '@/api/gptController.js';
export default {
  data () {
    return {
      taskList: [],
      runNameList: [],
      form: {
        'modelManageDto': {
          'businessLabel': '',
          'isBaseModel': 0,
          'lastVersionSource': '',
          'modelDescribe': '',
          'modelName': '',
          'modelType': 0,
          'versionCount': 0
        
        },
        'modelVersionDto': {
          taskId: undefined,
          runId: undefined,
          'baseModel': '',
          'businessLabel': '',
          'copyright': '',
          'ioMode': '',
          'modelTrainingMethod': 0,
          'modelUrl': '',
          'modelVersion': 1,
          'protocol': '',
          'protocolUrl': '',
          'runName': '',
          'status': 0,
          'trainingTask': '',
          'versionDescribe': '',
          'versionName': '',
          'versionSource': ''
       
        }
     
      },
      rules: {
        'modelManageDto.modelName': [
          { required: true, message: '请输入模型名称', trigger: 'blur' }
        ],
        'modelVersionDto.taskId': [
          { required: true, message: '请选择训练任务', trigger: 'blur' }
        ],
        'modelVersionDto.runId': [
          { required: true, message: '请选择运行名称', trigger: 'blur' }
        ]
      }

    };
  },
  props: ['isEdit', 'item'],
  components: {},

  computed: {

  },

  mounted () {
    this.intFrom();
    this.getTaskList();
    // this.getTuningRun()
  },

  methods: {
    intFrom () {
      this.form = { ...this.form, ...this.item }
      try {
        this.form.templateLabel = JSON.parse(this.item.templateLabel)
      } catch (error) {
        // console.log(error);
      }
    },
    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) {
              MyModel.update(this, params).then(res => {
                resolve(res);
                this.$message.success('编辑成功');
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            } else {
              MyModel.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>
