<template>
  <el-dialog
    :title="!dataForm.algorithmId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <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="类别" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="类别"></el-input>
    </el-form-item>
    <el-form-item label="版本数量" prop="number">
      <el-input v-model="dataForm.number" placeholder="版本数量"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="所属用户id" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="所属用户id"></el-input>
    </el-form-item>
    <el-form-item label="框架名称即就是镜像名称" prop="frameId">
      <el-input v-model="dataForm.frameId" placeholder="框架名称即就是镜像名称"></el-input>
    </el-form-item>
    <el-form-item label="文件系统路径" prop="dfsUrl">
      <el-input v-model="dataForm.dfsUrl" placeholder="文件系统路径"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          algorithmId: 0,
          algorithmName: '',
          categoryId: '',
          number: '',
          remark: '',
          uid: '',
          frameId: '',
          dfsUrl: ''
        },
        dataRule: {
          algorithmName: [
            { required: true, message: '算法名称不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '类别不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '版本数量不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '所属用户id不能为空', trigger: 'blur' }
          ],
          frameId: [
            { required: true, message: '框架名称即就是镜像名称不能为空', trigger: 'blur' }
          ],
          dfsUrl: [
            { required: true, message: '文件系统路径不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.algorithmId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.algorithmId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/algorithm/info/${this.dataForm.algorithmId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.algorithmName = data.algorithm.algorithmName
                this.dataForm.categoryId = data.algorithm.categoryId
                this.dataForm.number = data.algorithm.number
                this.dataForm.remark = data.algorithm.remark
                this.dataForm.uid = data.algorithm.uid
                this.dataForm.frameId = data.algorithm.frameId
                this.dataForm.dfsUrl = data.algorithm.dfsUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/algorithm/${!this.dataForm.algorithmId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'algorithmId': this.dataForm.algorithmId || undefined,
                'algorithmName': this.dataForm.algorithmName,
                'categoryId': this.dataForm.categoryId,
                'number': this.dataForm.number,
                'remark': this.dataForm.remark,
                'uid': this.dataForm.uid,
                'frameId': this.dataForm.frameId,
                'dfsUrl': this.dataForm.dfsUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
