<template>
  <el-dialog :title=" '算法发布'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="150px">
      <el-form-item label="算法名称" prop="missName">
        <el-input v-model="dataForm.missName" :disabled=true></el-input>
      </el-form-item>
      <el-form-item label="类别" size="mini" prop="categoryId">
        <el-radio-group v-model="dataForm.categoryId" :disabled=true>
          <el-radio v-for="category in categoryList" :key="category.categoryId" :label="category.categoryId">{{ category.categoryName }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="选择模型">

        <el-tooltip class="item" effect="dark" content="选择训练产生的模型,可多选" placement="right">
          <el-select v-model="modelSelected" multiple placeholder="选择模型">
            <el-option v-for="item in models" :key="item.filename" :label="item.filename" :value="item.filename">
            </el-option>
          </el-select>
        </el-tooltip>
        
      </el-form-item>
      <el-form-item label="选择文件">

        <el-tooltip class="item" effect="dark" content="选择要发布的文件,可多选" placement="right">
          <el-select v-model="pythonFileSelected" multiple placeholder="选择文件">
            <el-option v-for="item in pythonFiles" :key="item.filename" :label="item.filename" :value="item.filename">
            </el-option>
          </el-select>
        </el-tooltip>
        
      </el-form-item>

      <el-form-item label="指定镜像主文件">
        <el-tooltip class="item" effect="dark" content="指定算法制作为镜像后的主运行文件,限单选" placement="right">
          <el-select v-model="imageFileSelected" placeholder="选择文件">
            <el-option v-for="item in pythonFiles" :key="item.filename" :label="item.filename" :value="item.filename">
            </el-option>
          </el-select>
        </el-tooltip>
        
      </el-form-item>

      <el-form-item label="指定应用主文件">
        <el-tooltip class="item" effect="dark" content="指定算法制作为应用后的主运行文件,限单选" placement="right">
          <el-select v-model="appFileSelected" placeholder="选择文件">
            <el-option v-for="item in pythonFiles" :key="item.filename" :label="item.filename" :value="item.filename">
            </el-option>
          </el-select>
        </el-tooltip>
        
      </el-form-item>
      
      <!-- <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="algPublish()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui"
export default {
  data() {
    return {
      visible: false,
      // 获取所有类别列表
      categoryList: [],
      dataForm: {
        id: 0,
        missName: "",
        // 本算法的类别
        categoryId: 1, 
        remark: "",
      },
      textarea2: "",
      models:[],
      modelSelected:[],
      pythonFiles:[],
      pythonFileSelected:[],
      appFileSelected:"",
      imageFileSelected:"",
      dataRule: {
        tmpName: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.dataForm.missName="";
      this.models=[];
      this.modelSelected=[];
      this.pythonFiles=[];
      this.pythonFileSelected=[];
      this.$http({
        url: this.$http.adornUrl("/category/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
      // 这块修改为获取类别
        .then(({ data }) => {
          this.categoryList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
                url:this.$http.adornUrl('/algstrain/getModelAndPythonFile'),
                method:'get',
                params:this.$http.adornParams({
                    algorithmTrainingId:id
                }),
                }).then(({data})=>{
                    this.dataForm.missName=data.algTrain.missName;
                    this.dataForm.categoryId=data.algTrain.categoryId;
                    this.models=data.models;
                    this.pythonFiles=data.pythonFiles;
                
            });
          }
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        });
    },
    algPublish(){
      let formData=new FormData();
        formData.append("missName",this.dataForm.missName);
        formData.append("categoryId",this.dataForm.categoryId);
        formData.append("uid",this.$store.state.user.id);
        formData.append("algorithmTrainingId",this.dataForm.id);
        formData.append("models",this.modelSelected);
        formData.append("pythonFiles",this.pythonFileSelected);
        formData.append("appFile",this.appFileSelected);
        formData.append("imageFile",this.imageFileSelected);
        this.$http({
                url:this.$http.adornUrl('/algstrain/algorithmPublish'),
                method:'post',
                data:formData,
            }).then(({data})=>{
              if(data&&data.code===0){
                Message({
                    type: 'success',
                    message: '申请发布成功',
                });
              }else{
                Message.error(data.msg);
              }
              this.visible=false;
              this.$parent.getDataList();
        });
        /* this.visible=false
        MessageBox.confirm(`是否发布该算法?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let formData=new FormData();
        formData.append("missName",this.dataForm.missName);
        formData.append("categoryId",this.dataForm.categoryId);
        formData.append("uid",this.$store.state.user.id);
        formData.append("algorithmTrainingId",this.dataForm.id);
        formData.append("models",this.modelSelected);
        formData.append("pythonFiles",this.pythonFileSelected);
        formData.append("appFile",this.appFileSelected);
        formData.append("imageFile",this.imageFileSelected);
        this.$http({
                url:this.$http.adornUrl('/algstrain/algorithmPublish'),
                method:'post',
                data:formData,
            }).then(({data})=>{
              if(data&&data.code===0){
                Message({
                    type: 'success',
                    message: '申请发布成功',
                });
              }else{
                Message.error(data.msg);
              }

                this.$parent.getDataList();
        });
      }).catch(() => {}) */

        

    },
    
  },
};
</script>
