<template>
  <el-dialog :title=" '修改算法信息'" :close-on-click-modal="false" :visible.sync="visible" :before-close="cleanFileList">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="算法名称" prop="algorithmName">
        <el-input v-model="dataForm.algorithmName" placeholder="请输入算法名称"></el-input>
      </el-form-item>
      <el-form-item label="类别" size="mini" prop="categoryId">
        <el-radio-group v-model="dataForm.categoryId">
          <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="描述信息" prop="remark">
        <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" placeholder="请输入内容" v-model="dataForm.remark">
        </el-input>

      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false,cleanFileList()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { MessageBox } from 'element-ui'
import { Message } from "element-ui"
export default {
  data() {
    return {
      visible: false,
      // 获取所有类别列表
      categoryList: [],
      dataForm: {
        id: 0,
        algorithmName: "",
        // 本模板的类别
        categoryId: 1, 
        remark: "",
      },
      textarea2: "",
      fileList: [],
      dataRule: {
        algorithmName: [
          { required: true, message: "算法名不能为空", trigger: "blur" },
        ],

      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.fileList=[];
      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(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/algs/info/${this.dataForm.id}`),
              method: "get",
              params: this.$http.adornParams(),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.algorithmName = data.algs.algorithmName;
                this.dataForm.categoryId = data.algs.categoryId;
                this.dataForm.remark = data.algs.remark;
                for(var i=0;i<data.fileList.length;i++){
                  this.fileList.push({
                    name:data.fileList[i].filename,
                    url:''
                  });
                }
                // 模板文件地址
                // this.dataForm.dfsUrlv = data.temp.dfsUrlv;
              }
            });
          }
        });
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.visible=false;
          MessageBox.confirm(`是否下载该数据集?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let formData=new FormData();
          formData.append('algorithmId',this.dataForm.id || undefined);
          formData.append('algorithmName',this.dataForm.algorithmName);
          formData.append('categoryId',this.dataForm.categoryId);
          formData.append('remark',this.dataForm.remark);
          formData.append('uid',this.$store.state.user.id);
          this.$http({
            url: this.$http.adornUrl(
              `/algs/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data:formData,
          }).then(({ data }) => {
            if (data && data.code === 0) {
              Message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              Message.error(data.msg);
            }
          });
        }).catch(() => {})
        }
      });
    },

    // 以下4个函数是文件上传功能的
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      
    },
    handleExceed(files, fileList) {
      Message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return MessageBox.confirm(`确定移除 ${file.name}？`);
    },
    cleanFileList(done){
      this.fileList=[];
      done();
    }
  },
};
</script>
