<template>
  <div>
    <el-form ref="powerRationingForm" :rules="rules" :model="form" class="demo-form-inline"
             label-width="130px">
      <el-form-item label="mongo库" prop="mongodb_database">
        <el-input v-model="form['mongodb_database']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="读取的mongo表" prop="mongodb_read_table">
        <el-input v-model="form['mongodb_read_table']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="写入的mongo表" prop="mongodb_write_table">
        <el-input v-model="form['mongodb_write_table']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item :label="stationType===0?'实测辐照度列':'功率列'" prop="col_power">
        <el-input v-model="form['col_power']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="功率列" prop="col_radiance" v-if="stationType === 0">
        <el-input v-model="form['col_radiance']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="平滑参数" v-if="stationType ===0">
        <el-input v-model="form['sigma']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="实测风速列"  v-if="stationType === 1">
        <el-input v-model="form['col_ws']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="划分半径" v-if="stationType === 1">
        <el-input v-model="form['eps']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="半径内最小样本点" v-if="stationType === 1">
        <el-input v-model="form['min_samples']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
      <el-form-item label="装机容量" prop="cap" v-if="stationType === 1">
        <el-input v-model="form['cap']" size="mini"
                  :disabled="!powerRationing.isEnable" type="textarea"
                  :autosize="{ minRows: 1 }"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: {
    formData: {
      type: Object,
      default: {}
    }
  },
  destroyed() {
  },
  watch: {
    formData: {
      handler(newVal, oldVal) {
        console.log(newVal)
        // this.$refs.powerRationingForm.resetFields()
        this.powerRationing = newVal
        this.stationType = newVal.componentType.includes('光伏')?0:1
        this.rules = {}
        if(this.stationType === 0){
          this.rules = { ...this.commonRules, ...this.solarRules }
        }else{
          this.rules = { ...this.commonRules, ...this.windRules }
        }
        this.form = JSON.parse(JSON.stringify(newVal.params))
        this.$nextTick(() => {
          this.$refs.powerRationingForm.resetFields();
        });
      },
      immediate: true,
      deep:true
    }
  },
  data() {
    return {
      stationType:0,
      powerRationing: {},
      form: {},
      rules: {},
      commonRules: {
        mongodb_database: [
          {required: true, message: '请填写mongo库', trigger: 'blur'}
        ],
        mongodb_write_table: [
          {required: true, message: '请填写写入的mongo表', trigger: 'blur'}
        ],
        mongodb_read_table: [
          {required: true, message: '请填写读取的mongo表', trigger: 'blur'}
        ],

      },
      solarRules:{
        col_power: [
          {required: true, message: '请填写实测辐照度', trigger: 'blur'}
        ],
        col_radiance: [
          {required: true, message: '请填写功率列', trigger: 'blur'}
        ],
      },
      windRules:{
        col_power: [
          {required: true, message: '请填写功率列', trigger: 'blur'}
        ],
        col_ws: [
          {required: true, message: '请填写实测风速列', trigger: 'blur'}
        ],
        cap: [
          {required: true, message: '请填写装机容量', trigger: 'blur'}
        ],
      },
    }
  },
  mounted() {
  },
  methods: {
    saveTask() {
      this.$refs.powerRationingForm.validate((valid) => {
        if (valid) {
          // console.log('child submit!');
          this.$emit('child-event', this.form)
          return true
        } else {
          this.$emit('child-event', {})
          this.$message.warning(this.powerRationing.componentType+'模块校验未通过')
          return false;
        }
      });

      // this.dialogVisible = false
    },
  }
}
</script>

<style scoped lang="scss">

</style>
