<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="场站" prop="windpowerstationId">
        <el-select
          v-model="form.windpowerstationId"
          style="width: 200px"
          placeholder="请选择"
          clearable
          :disabled="!idAdd ? true : false"
        >
          <el-option
            v-for="item in wplist"
            :key="item.nemCode"
            :label="item.name"
            :value="item.nemCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="填报日期" prop="recordDate">
        <el-date-picker
          v-model="form.recordDate"
          :disabled="!idAdd ? true : false"
          style="width: 200px"
          type="date"
          placeholder="选择日期时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="日发电量" prop="generationDay">
        <el-input
          v-model="form.generationDay"
          style="width: 200px"
          class="inputs"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="月发电量" prop="generationMonth">
        <el-input
          v-model="form.generationMonth"
          style="width: 200px"
          class="inputs"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="年发电量" prop="generationYear">
        <el-input
          v-model="form.generationYear"
          style="width: 200px"
          class="inputs"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="功率负荷" prop="power">
        <el-input
          v-model="form.power"
          style="width: 200px"
          class="inputs"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="风速/光照" prop="speedOrBeam">
        <el-input
          v-model="form.speedOrBeam"
          style="width: 200px"
          class="inputs"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleSave()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit, doPut } from '@/api/zhibiao.js'
  import dayjs from 'dayjs'
  export default {
    name: 'TableEdit',
    props: {
      wplist: {
        type: Array,
        default: () => [],
      },
      //   options: {
      //     type: Array,
      //     default: [],
      //   },
      //   standardList: {
      //     type: Array,
      //     default: [],
      //   },
    },
    data() {
      return {
        form: {
          windpowerstationId: '',
          windpowerstationName: '',
          recordDate: '',
          generationDay: '',
          generationMonth: '',
          generationYear: '',
          power: '',
          speedOrBeam: '',
        },
        rules: {
          windpowerstationId: [
            { required: true, trigger: 'blur', message: '请选择' },
          ],
          generationDay: [
            { required: true, trigger: 'blur', message: '请选择' },
          ],
          generationMonth: [
            { required: true, trigger: 'blur', message: '请输入' },
          ],
          generationYear: [
            { required: true, trigger: 'blur', message: '请输入' },
          ],
          power: [{ required: true, trigger: 'blur', message: '请输入' }],
          recordDate: [{ required: true, trigger: 'blur', message: '请选择' }],
          speedOrBeam: [{ required: true, trigger: 'blur', message: '请选择' }],
        },
        title: '',
        dialogFormVisible: false,
        idAdd: true,
      }
    },
    created() {},
    methods: {
      setFormItem(wpid, recordDate) {
        this.form.windpowerstationId = wpid
        this.form.recordDate = recordDate
      },
      openDialog(row) {
        if (!row) {
          this.title = '添加'
          this.idAdd = true
        } else {
          this.title = '编辑'
          this.idAdd = false
          this.form = Object.assign({}, row)
          this.form.windpowerstationId = row.windpowerstationId
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      handleSave() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.idAdd) {
              let wpinfo = this.wplist.find(
                (item) => item.nemCode == this.form.windpowerstationId
              )
              this.form.windpowerstationName = wpinfo.name
              this.form.recordDate = dayjs(this.form.recordDate).format(
                'YYYY-MM-DD'
              )
              doEdit(this.form).then(({ data: res }) => {
                if (res.code == 200) {
                  this.$message({
                    type: 'success',
                    message: '添加成功!',
                  })
                  this.$emit('handleSuccess')
                  this.dialogFormVisible = false
                }
              })
            } else {
              console.log(this.form)
              doPut(this.form).then(({ data: res }) => {
                console.log(res)
                if (res.code == 200) {
                  this.$message({
                    type: 'success',
                    message: '修改成功!',
                  })
                  this.$emit('handleSuccess')
                  this.dialogFormVisible = false
                }
              })
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  .inputs {
    width: 200px;
  }
</style>
