<template>
  <div>
    <el-dialog :title="title" :visible.sync="isOpen" width="500px" append-to-body @close="cancel"
               :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="evaluateName">
          <el-select v-model="form.evaluateName" placeholder="请选择模型名称" clearable style="width: 100%"
                     @change="changModelName"
          >
            <el-option v-for="dict in modelNameList" :key="dict.modelName" :label="dict.modelName"
                       :value="dict.modelName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="版本号" prop="evaluateVersion">
          <el-select v-model="form.evaluateVersion" placeholder="请选择版本号" clearable style="width: 100%">
            <el-option v-for="dict in modelVersionList" :key="dict.modelVersion" :label="dict.modelVersion"
                       :value="dict.modelVersion"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="处理速度" prop="processSpeed">
          <el-input v-model="form.processSpeed" maxlength="10" placeholder="请输入处理速度"/>
        </el-form-item>
        <el-form-item label="内存占用" prop="memoryUsage">
          <el-input v-model="form.memoryUsage" maxlength="10" placeholder="请输入内存占用"/>
        </el-form-item>
        <el-form-item label="环境条件" prop="enviroConditions">
          <el-select v-model="form.enviroConditions" placeholder="请选择环境条件" clearable style="width: 100%">
            <el-option v-for="dict in environmental" :key="dict.label" :label="dict.label"
                       :value="dict.label"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getEvaluate, addEvaluate, updateEvaluate } from '@/api/dataCenter/evaluate'
import { getModelNameList, getModelVersionList } from '@/api/dataCenter/mirror'

export default {
  props: {
    open: { type: Boolean, required: true },
    getList: { type: Function, required: true },
    title: { type: String, required: true },
    id: { type: [Number, null], default: 0 }
  },
  computed: {
    isOpen: {
      get() {
        return this.open
      },
      set(value) {
        this.$emit('dialog-cancel')
      }
    }
  },
  data() {
    return {
      modelNameList: [],
      modelVersionList: [],
      environmental: [{
        value: '0',
        label: '正常光照'
      },
        {
          value: '1',
          label: '下雨天'
        },
        {
          value: '2',
          label: '阴天'
        }],
      form: {},
      rules: {
        evaluateName: [
          { required: true, message: '请输入模型名称', trigger: 'blur' }
        ],
        evaluateVersion: [
          { required: true, message: '请输入版本号', trigger: 'blur' }
        ],
        processSpeed: [
          { required: true, message: '请输入处理速度', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: '只能输入正整数', trigger: 'blur' }
        ],
        memoryUsage: [{ required: true, message: '请输入内存占用', trigger: 'blur' },
          { pattern: /^[1-9]$|^[1-9]\d$|^100$/, message: '请输入 1 到 100 的正整数', trigger: 'blur' }],
        enviroConditions: [{ required: true, message: '请输入环境条件', trigger: 'blur' }]
      }
    }
  },
  watch: {
    isOpen(newVal) {
      if (newVal) {
        getModelNameList().then(res => {
          console.log(res)
          this.modelNameList = res.data
        })
        if (this.id) {
          getEvaluate(this.id).then(response => {
            this.form = response.data
          })
        }
      }
    }
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvaluate(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.isOpen = false
              this.getList()
            })
          } else {
            addEvaluate(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.isOpen = false
              this.getList()
            })
          }
        }
      })
    },
    changModelName(name) {
      getModelVersionList({ modelName: name }).then(response => {
        this.modelVersionList = response.data
      })
    },
    cancel() {
      this.isOpen = false
      this.resetForm()
    },
    resetForm() {
      this.modelNameList = []
      this.modelVersionList = []
      this.form = {}
    }
  }
}
</script>

<style scoped lang="scss"></style>
