<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="编码" 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="code">
          <el-input placeholder="请输入" 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 placeholder="请输入" class="inputs" v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="别称" prop="aname">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.aname" autocomplete="off"></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" @blur="onBlur(form.capacity,'number')">
          </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="commissiondate">
          <el-date-picker style="width:200px" v-model="form.commissiondate" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="主控电话" prop="masterphone">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.masterphone" autocomplete="off"></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.trim="form.ordernum" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="装机数量" prop="quantity">
          <el-input placeholder="请输入" class="inputs" v-model="form.quantity" autocomplete="off" @blur="onBlur(form.quantity,'quantity')">
          </el-input>
        </el-form-item>
        <el-form-item label="值长" prop="shiftforeman">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.shiftforeman" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="值长电话" prop="shiftforemanphone">
          <el-input placeholder="请输入" class="inputs" v-model.number="form.shiftforemanphone" autocomplete="off"
            @blur="onBlur(form.shiftforemanphone,'shiftforemanphone')">
          </el-input>
        </el-form-item>
        <el-form-item label="风场编号" prop="windpowerstationid">
          <!-- <el-select style="width:200px" v-model="form.windpowerstationid" placeholder="请选择" clearable>
            <el-option v-for="item in stationList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select> -->
          <el-cascader 
            placeholder="请选择"
            v-model="form.windpowerstationid" 
            :options="options" 
            style="width: 200px" 
            :props="{ checkStrictly: true, label: 'name', value: 'id' , children:'children'}"
            clearable />
        </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: '',
        name: '',
        code: '',
      },
      rules: {
        id: [{ required: true, trigger: 'blur', message: '请输入' }],
        name: [{ required: true, trigger: 'blur', message: '请输入' }],
        code: [{ required: true, trigger: 'blur', message: '请输入' }],
        aname: [{ required: true, trigger: 'blur', message: '请输入' }],
        capacity: [{ required: true, trigger: 'blur', message: '请输入' }],
        capacityunit: [{ required: true, trigger: 'blur', message: '请输入' }],
        commissiondate: [{ required: true, trigger: 'blur', message: '请输入' }],
        masterphone: [{ required: true, trigger: 'blur', message: '请输入' }],
        model: [{ required: true, trigger: 'blur', message: '请输入' }],
        ordernum: [{ required: true, trigger: 'blur', message: '请输入' }],
        quantity: [{ required: true, trigger: 'blur', message: '请输入' }],
        shiftforeman: [{ required: true, trigger: 'blur', message: '请输入' }],
        shiftforemanphone: [{ required: true, trigger: 'blur', message: '请输入' }],
        windpowerstationid: [{ 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.windpowerstationid = this.form.windpowerstationid.split(",")
      }
      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) {
          this.form.windpowerstationid = this.form.windpowerstationid[this.form.windpowerstationid.length - 1]
          this.form.commissiondate = dayjs(this.form.commissiondate).format("YYYY-MM-DD")
          api.addProject(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>
