<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px" @close="close">
    <el-form ref="form" :model="form" :rules="rules">
      <div class="lists">
        <el-form-item label="id" prop="id">
          <el-input class="inputs" v-model="form.id" autocomplete="off" :disabled="!idAdd ? true : false"></el-input>
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input class="inputs" v-model.trim="form.code" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="名称" prop="name">
          <el-input class="inputs" v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input class="inputs" v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="容量" prop="powerproduction">
          <el-input class="inputs" v-model.trim="form.powerproduction" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="厂商编号" prop="windturbinemanufacturerid">
          <el-input class="inputs" v-model.trim="form.windturbinemanufacturerid" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="图片" prop="photo">
          <el-input class="inputs" v-model="form.photo" autocomplete="off">
          </el-input>
        </el-form-item>
        <el-form-item label="传输总发电量单位" prop="unit">
          <el-input class="inputs" v-model.trim="form.unit" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="切入风速" prop="cutinwindspeed">
          <el-input class="inputs" v-model="form.cutinwindspeed" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="额定风速" prop="ratedwindspeed">
          <el-input class="inputs" v-model.trim="form.ratedwindspeed" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="切出风速" prop="cutoutwindspeed">
          <el-input class="inputs" v-model="form.cutoutwindspeed" autocomplete="off">
          </el-input>
        </el-form-item>
        <el-form-item label="叶片切入面积" prop="sweptarea">
          <el-input class="inputs" v-model.trim="form.sweptarea" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="设备类别" prop="equipmentcategory">
          <el-input class="inputs" v-model="form.equipmentcategory" autocomplete="off">
          </el-input>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/api/station'
import dayjs from "dayjs";
export default {
  name: 'TableEdit',
  props: {
    options: {
      type: Array,
      default: []
    },
    stationList: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      form: {
        id: '',
        code: '',
        name: '',
        description: '',
        powerproduction: '',
        windturbinemanufacturerid: '',
        photo: '',
        unit: '',
        cutinwindspeed: '',
        ratedwindspeed: '',
        cutoutwindspeed: '',
        sweptarea: '',
        equipmentcategory: '',
      },
      rules: {
        id: [{ required: true, trigger: 'blur', message: '请输入' }],
        code: [{ required: true, trigger: 'blur', message: '请输入' }],
        name: [{ required: true, trigger: 'blur', message: '请输入' }],
        description: [{ required: true, trigger: 'blur', message: '请输入' }],
        powerproduction: [{ required: true, trigger: 'blur', message: '请输入' }],
        windturbinemanufacturerid: [{ required: true, trigger: 'blur', message: '请输入' }],
        photo: [{ required: true, trigger: 'blur', message: '请输入' }],
        unit: [{ required: true, trigger: 'blur', message: '请输入' }],
        cutinwindspeed: [{ required: true, trigger: 'blur', message: '请输入' }],
        ratedwindspeed: [{ required: true, trigger: 'blur', message: '请输入' }],
        cutoutwindspeed: [{ required: true, trigger: 'blur', message: '请输入' }],
        sweptarea: [{ required: true, trigger: 'blur', message: '请输入' }],
        equipmentcategory: [{ required: true, trigger: 'blur', message: '请输入' }],
      },
      title: '',
      dialogFormVisible: false,
      idAdd: true,
    }
  },
  created() { },
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加'
        this.idAdd = true
      } else {
        this.title = '编辑'
        this.idAdd = false
        this.form = Object.assign({}, row)
      }
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          api.addEquipmentmodel(this.form).then(res => {
            if (res.data) {
              this.$message({
                type: 'success',
                message: '添加成功!'
              });
              this.$emit('save-success');
              this.dialogFormVisible = false
            }
          })
        } else {
          return false
        }
      })
    },
    handleChange() {
    },
    onBlur(val, param) {
      if (typeof (this.form[param]) !== 'number') {
        this.form[param] = val.replace(/[^0-9]/ig, "")
      }
    },
  },
}
</script>
<style lang="less" scoped>
.inputs {
  width: 200px;
}

.lists {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}
</style>