<!-- 创建模型压缩 -->
<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="modelCompressDto.taskName">
      <el-input v-model="form.modelCompressDto.taskName" class="inputWidth" :size="defaultFormItemSize"></el-input>
    </el-form-item>
    <el-form-item label="任务描述:">
      <el-input v-model="form.modelCompressDto.taskDescribe" type="textarea" :rows="4" class="inputWidth" :size="defaultFormItemSize"></el-input>
    </el-form-item>

    <el-row class="title">压缩配置</el-row>
    <el-form-item label="选择源模型:" prop="modelCompressDto.sourceVersionId" >
      <el-cascader v-model="form.modelCompressDto.sourceVersionId" :options="modelList"
        :props='{ label: "name", value: "id", emitPath: false }'></el-cascader>

      <!-- <el-select v-model="form.modelCompressDto.sourceVersionId" placeholder="请选择">
        <el-option v-for="item in []" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select> -->
      <el-row> <span class="introduce">启用此功能可能会为您提供更好质量的提示词，但也会花费更长的时间进行优化</span></el-row>

    </el-form-item>
    <el-form-item label="模型创建⽅式:" class="inputWidth">
      <el-radio v-for="item in ModelCreationMode.getList()" :key="item.id" v-model="form.modelCompressDto.createMethod" :label="item.id" @change="createMethod">{{  item.name}}</el-radio>
    </el-form-item>
    <el-form-item label="选择已有模型:" v-if="form.modelCompressDto.createMethod===0" prop="modelCompressDto.targetModelId">
      <el-select v-model="form.modelCompressDto.targetModelId" placeholder="请选择" class="inputWidth" >
        <el-option v-for="item in modelList" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="新建模型名称:" v-else prop="modelCompressDto.targetModelName">
      <el-input v-model="form.modelCompressDto.targetModelName" class="inputWidth"  :size="defaultFormItemSize"></el-input>
    </el-form-item>
    <!-- <el-form-item label="压缩策略:">
      <el-select v-model="value" placeholder="请选择">
        <el-option v-for="item in []" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-row class="title">资源配置</el-row>
    <el-form-item label="压缩任务运⾏环境:">
      <el-radio v-model="value" label="0">GPU A800 -------- TeslaGPU_A800_80G显存单卡_16核_128G内存</el-radio>

    </el-form-item>
    <el-form-item label="选择计算节点数:">
      <el-input-number v-model="value" :min="1" label="迭代轮次"  :size="defaultFormItemSize"></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 { ModelCompress, MyModel} from '@/api/gptController.js';
export default {
  data () {
    return {
      modelList: [],

      form: {
        'modelCompressDto': {
          'createMethod': 0,
          'sourceVersionId': undefined,
          'targetModelId': undefined,
          'taskDescribe': undefined,
          'taskName': undefined,
          targetModelName: undefined
        },
        'modelTaskDto': {
          // 'completeTime': '',
          // 'modelId': 0,
          // 'modelVersion': 0,
          // 'taskId': 0,
          // 'taskStatus': 0,
          // 'taskType': 0,
          // 'versionId': 0,
          // 'versionName': ''
        }
      },
      rules: {
        'modelCompressDto.taskName': [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
         
        ],
        'modelCompressDto.sourceVersionId': [{ required: true, message: '请选择源模型', trigger: 'blur' }],
        'modelCompressDto.targetModelId': [{ required: true, message: '请选择已有模型', trigger: 'blur' }],
        'modelCompressDto.targetModelName': [{ required: true, message: '请输入新建模型名称', trigger: 'blur' }]
      }

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

  computed: {

  },

  mounted () {
    this.intFrom()
  },

  methods: {
    intFrom () {
      this.getModelList()
      if (this.isCopy) {
        this.form.modelCompressDto = {...this.item}
      } else if (this.modelVersionId) {
        this.form.modelCompressDto.sourceVersionId = this.modelVersionId
      }
    },
    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) {
              ModelCompress.update(this, params).then(res => {
                resolve(res);
                this.$message.success('编辑成功');
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            } else {
              ModelCompress.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.map((item) => {
          return { id: item.modelId, name: item.modelName, children: item.modelVersionList === [] ? [] : item.modelVersionList.map((item2) => { return { id: item2.versionId, name: 'V' + item2.modelVersion } }) }
        })
      }).catch(e => {
        console.log(e);
      });
    },
    createMethod (data) {
      if (data === 0) {
        this.form.modelCompressDto.targetModelName = undefined
      } else if (data === 1) {
        this.form.modelCompressDto.targetModelId = undefined
      }
    }

  }
};
</script>
<style scoped>
.inputWidth {
  width: 600px;
}
.title {
  font-size: 20px;
  margin-bottom: 16px;
}
.introduce{
  font-size: 12px;
  color: #909399;
}
</style>
