<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogOpen"
    width="500px"
    :close-on-click-modal="false"
    append-to-body
    @opened="initData"
    @close="handleCancel"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入任务名称" />
      </el-form-item>
      <el-form-item label="模型" prop="modelId">
        <el-select v-model="formData.modelId" placeholder="请选择模型" @change="onModelChange">
          <el-option
            v-for="item in faultPhysicalOptions"
            :key="item.modelId"
            :label="item.modelName"
            :value="item.modelId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模型远程地址" prop="name">
        <el-input v-model="formData.apiPath" placeholder="请输入远程地址" />
      </el-form-item>
      <el-form-item  label="模型文件" prop="filePath">
        <multi-file-upload   @upload-success="handleUploadSuccess"/>
      </el-form-item>
<!--      <el-row>-->
<!--        <el-form-item label="开始时间" prop="startTime">-->
<!--          <el-date-picker-->
<!--            clearable-->
<!--            v-model="formData.startTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd HH:mm:ss"-->
<!--            placeholder="请选择开始时间"-->
<!--          />-->
<!--        </el-form-item>-->
<!--      </el-row>-->

<!--      <el-row>-->
<!--        <el-form-item label="结束时间" prop="endTime">-->
<!--          <el-date-picker-->
<!--            clearable-->
<!--            v-model="formData.endTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd HH:mm:ss"-->
<!--            placeholder="请选择结束时间"-->
<!--          />-->
<!--        </el-form-item>-->
<!--      </el-row>-->
      <el-form-item label="说明" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addDdAlgorithm,
  updateDdAlgorithm,
  fileCopys
} from "@/api/test/ddAlgorithm";
import { getFaultPhysicalOptions } from '@/api/model/faultPhysical'

import { addData } from '@/api/data/data'

export default {
  props: {
    value: {
      type: Boolean,
      default: false
    },
    dataInfo: {
      type: Object,
      default: null
    },
    options: {
      type: String,
      default: 'add'
    },
    dataOptions: {
      type: Array,
      default: () => []
    },

  },
  data() {
    return {
      faultPhysicalOptions:[],
      //指定文件上传路径
      customPath:"",
      formData: {
        apiPath:null,
        id: null,
        name: null,
        modelId: null,
        dataId: null,
        truthLabels: null,
        testLabels: null,
        remark: null,
        startTime: null,
        endTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        outputPath:null
      },
      selectedModel:{},
      fileForm:{},
      rules: {},
      dialogOpen: false
    };
  },
  model: {
    prop: 'value',
    event: 'callback'
  },
  watch: {
    value(newVal) {
      this.dialogOpen = newVal;
    }
  },
  computed: {
    title() {
      if (this.options === 'add') {
        return '添加功能验证';
      }
      if (this.options === 'edit') {
        return '修改功能验证';
      }
      return '';
    }
  },
  methods: {
    onModelChange(){
       this.selectedModel = this.faultPhysicalOptions.find(
        item => item.modelId === this.formData.modelId
      );
      console.log(this.selectedModel)
    },
    handleUploadSuccess(datas){
      console.log('handleChildData',datas);
      const fileData= {
        fileName : datas.fileName,
        filelocalPath : datas.filelocalPath,
        modelPath:this.selectedModel.modelPath
      }
      const addDataform={
        name:'验证数据',
        type:'3',
        fileName:datas.fileName,
        fileSize:datas.fileSize,
        fileSizeBytes:datas.fileSizeBytes,
        fileSuffix:datas.fileSuffix,
        filelocalPath:datas.filelocalPath,
        filePath:datas.filelocalPath+'\\'+datas.fileName,
      }
      addData(addDataform);
      console.log('fileData',fileData)
      fileCopys(fileData);
      this.formData.outputPath = this.selectedModel.modelPath
    },
    loadOptions() {


      // 加载模型选项
      getFaultPhysicalOptions(1).then(resp => {
        this.faultPhysicalOptions = resp.data || [];
        console.log(this.faultPhysicalOptions)
      });
    },
    initData() {
      this.loadOptions();
      if (this.options!== 'add') {
        this.formData = { ...this.dataInfo };
      }
    },
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.formData.id != null) {
            updateDdAlgorithm(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.handleCancel();
            });
          } else {
            addDdAlgorithm(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.handleCancel();
            });
          }
        }
      });
    },
    handleCancel() {
      this.formData = {
        id: null,
        name: null,
        modelId: null,
        dataId: null,
        truthLabels: null,
        testLabels: null,
        remark: null,
        startTime: null,
        endTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.$emit('callback', false);
    }
  }
};
</script>
