<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="status">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.status" 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="firstintegratedtime">
          <el-date-picker style="width: 200px" v-model="form.firstintegratedtime" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="风场编号" prop="windpowerstationid">
          <!-- <el-select 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 
            @change="windpowerstationidChange"
            placeholder="请选择"
            v-model="form.windpowerstationid" 
            :options="windpowerstationid_options" 
            style="width: 200px" 
            :props="{ checkStrictly: true, label: 'name', value: 'id' , children:'children'}"
            clearable />
        </el-form-item>
        <el-form-item label="工程编号" prop="projectid">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.projectid" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="线路编号" prop="lineid">
          <!-- <el-input class="inputs" v-model.trim="form.lineid" autocomplete="off"></el-input> -->
          <el-cascader 
            placeholder="请选择"
            v-model="form.lineid" 
            :options="lineid_options" 
            style="width: 200px" 
            :props="{ checkStrictly: true, label: 'name', value: 'id' , children:'children'}"
            clearable />
        </el-form-item>
        <el-form-item label="地球纬度" prop="latitude">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.latitude" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="地球经度" prop="longitude">
          <el-input placeholder="请输入" class="inputs" v-model="form.longitude" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="机型" prop="modelid">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.modelid" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="lists">
        <el-form-item label="图片" prop="photo">
          <el-input placeholder="请输入" class="inputs" v-model="form.photo" autocomplete="off">
          </el-input>
        </el-form-item>
        <el-form-item label="标杆风机" prop="standardid">
          <el-input placeholder="请输入" class="inputs" v-model.trim="form.standardid" 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";
import { recursionTree, recursionTreeChildren } from '@/utils/deepTree'

export default {
  name: 'TableEdit',
  props: {
    // options: {
    //   type: Array,
    //   default: []
    // },
    stationList: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      optionsAll: [],
      windpowerstationid_options: [],
      lineid_options: [],
      form: {
        id: '',
        name: '',
        code: '',
      },
      rules: {
        id: [{ required: true, trigger: 'blur', message: '请输入' }],
        name: [{ required: true, trigger: 'blur', message: '请输入' }],
        status: [{ required: true, trigger: 'blur', message: '请输入' }],
        firstintegratedtime: [{ required: true, trigger: 'blur', message: '请输入' }],
        windpowerstationid: [{ required: true, trigger: 'blur', message: '请输入' }],
        projectid: [{ required: true, trigger: 'blur', message: '请输入' }],
        lineid: [{ required: true, trigger: 'blur', message: '请输入' }],
        latitude: [{ required: true, trigger: 'blur', message: '请输入' }],
        longitude: [{ required: true, trigger: 'blur', message: '请输入' }],
        modelid: [{ required: true, trigger: 'blur', message: '请输入' }],
        photo: [{ required: true, trigger: 'blur', message: '请输入' }],
        standardid: [{ required: true, trigger: 'blur', message: '请输入' }],
      },
      title: '',
      dialogFormVisible: false,
      idAdd: true,
    }
  },
  created() {
  },
  methods: {
    windpowerstationidChange(data){
      if(data){
        this.lineid_options = recursionTreeChildren(data[data.length - 1], this.optionsAll)
      }
    },
    getTree() {
      api.newtreeTreels({tag:2}).then(res => {
        if (res.data) {
          this.windpowerstationid_options = res.data
        }
      })
      api.newtreeTreels({tag:4}).then(res => {
        if (res.data) {
          this.optionsAll = res.data
        }
      })
    },
    showEdit(row) {
      this.getTree()
      if (!row) {
        this.title = '添加'
        this.idAdd = true
      } else {
        this.title = '编辑'
        this.idAdd = false
        this.form = Object.assign({}, row)
        // this.lineid_options = recursionTreeChildren(this.form.windpowerstationid, this.optionsAll)
      }
      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.lineid = this.form.lineid[this.form.lineid.length - 1]
          this.form.firstintegratedtime = dayjs(this.form.firstintegratedtime).format("YYYY-MM-DD")
          api.addWindturbine(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>
