<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <div class="lists">
        <el-form-item label="id" prop="id">
          <el-input placeholder="请输入" class="inputs" v-model="form.id" autocomplete="off" :disabled="!idAdd?true:false"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="别称" prop="aname">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.aname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="编码" prop="code">
          <el-input placeholder="请输入" class="inputs" v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="地址" prop="address">
          <el-input placeholder="请输入" class="inputs" v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input placeholder="请输入" class="inputs" v-model.number="form.telephone" autocomplete="off"
            @blur="onBlur(form.telephone,'telephone')"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="装机容量" prop="capacity">
          <el-input placeholder="请输入" class="inputs" v-model="form.capacity" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="容量单位" prop="capacityunit">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.capacityunit" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="装机数量" prop="quantity">
          <el-input  placeholder="请输入"class="inputs" v-model.number="form.quantity" autocomplete="off"
            @blur="onBlur(form.quantity,'quantity')">
          </el-input>
        </el-form-item>
        <el-form-item label="地球经度" prop="longitude">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.longitude" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="地球纬度" prop="latitude">
          <el-input placeholder="请输入" class="inputs" v-model="form.latitude" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="photo">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.photo" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="公司编号" prop="companyid">
          <el-select style="width: 200px;" v-model="form.companyid" placeholder="请选择" clearable>
            <el-option v-for="item in stationList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系统厂家" prop="systemmanufact">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.systemmanufact" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="场长" prop="headfarm">
          <el-input placeholder="请输入" class="inputs" v-model="form.headfarm" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="场长电话" prop="headfarmphone">
          <el-input placeholder="请输入" class="inputs" v-model.number="form.headfarmphone" autocomplete="off"
            @blur="onBlur(form.headfarmphone,'headfarmphone')"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="机型" prop="model">
          <el-input placeholder="请输入" class="inputs" v-model="form.model" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="ordernum">
          <el-input placeholder="请输入" class="inputs" v-model.number="form.ordernum" autocomplete="off"
            @blur="onBlur(form.ordernum,'ordernum')">
          </el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="海拔高度" prop="height">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.height" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="区域编号" prop="regionid">
          <!-- <el-input  placeholder="请输入"class="inputs" v-model.trim="form.regionid" autocomplete="off"></el-input> -->
          <el-cascader 
            v-model="form.regionid" 
            :options="options" 
            style="width: 200px" 
            :props="{ checkStrictly: true, emitPath: true, label: 'name', value: 'id' , children:'children'}" 
          />
        </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'
export default {
  name: 'TableEdit',
  props: {
    options: {
      type: Array,
      default: []
    },
    stationList: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      form: {
        id: '',
        name: '',
        regionid: '',
        aname: '',
        code: '',
        address: '',
        telephone: '',
        capacity: '',
        capacityunit: '',
        quantity: '',
        longitude: '',
        latitude: '',
        photo: '',
        companyid: '',
        systemmanufact: '',
        headfarm: '',
        headfarmphone: '',
        model: '',
        ordernum: '',
        height: '',
      },
      rules: {
        id: [{ required: true, trigger: 'blur', message: '请输入' }],
        name: [{ required: true, trigger: 'blur', message: '请输入' }],
        regionid: [{ required: true, trigger: 'blur', message: '请输入' }],
        aname: [{ required: true, trigger: 'blur', message: '请输入' }],
        code: [{ required: true, trigger: 'blur', message: '请输入' }],
        address: [{ required: true, trigger: 'blur', message: '请输入' }],
        telephone: [{ required: true, trigger: 'blur', message: '请输入' }],
        capacity: [{ required: true, trigger: 'blur', message: '请输入' }],
        capacityunit: [{ required: true, trigger: 'blur', message: '请输入' }],
        quantity: [{ required: true, trigger: 'blur', message: '请输入' }],
        longitude: [{ required: true, trigger: 'blur', message: '请输入' }],
        latitude: [{ required: true, trigger: 'blur', message: '请输入' }],
        photo: [{ required: true, trigger: 'blur', message: '请输入' }],
        companyid: [{ required: true, trigger: 'blur', message: '请输入' }],
        systemmanufact: [{ required: true, trigger: 'blur', message: '请输入' }],
        headfarm: [{ required: true, trigger: 'blur', message: '请输入' }],
        headfarmphone: [{ required: true, trigger: 'blur', message: '请输入' }],
        model: [{ required: true, trigger: 'blur', message: '请输入' }],
        ordernum: [{ required: true, trigger: 'blur', message: '请输入' }],
        height: [{ 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.form.regionid = row.regionid.split(",")
      }
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    save() {
      console.log(this.form);
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          this.form.regionid = this.form.regionid.join(",")
          api.addWindpowerstation(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>
