<template>
	<el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px" @close="close">
		<el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <!-- id
      projectId
      generatingCapacity
      outageHours
      year
      month
      windpowerstationId -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="编码" prop="id">
            <el-input class="inputs" v-model="form.id" autocomplete="off" :disabled="!idAdd?true:false"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工程编号" prop="projectId">
            <el-input class="inputs" v-model="form.projectId" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划发电量" prop="generatingCapacity">
            <el-input class="inputs" v-model="form.generatingCapacity" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划停运小时" prop="outageHours">
            <el-input class="inputs" v-model="form.outageHours" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日期" prop="dateVal">
            <el-date-picker key="100" style="width:200px" @input="changeDate" v-model="form.dateVal" type="month" placeholder="选择月"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="场站编号冗余" prop="windpowerstationId">
            <el-input class="inputs" v-model="form.windpowerstationId" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
		</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/realtimeDatabaseConfiguration'
import dayjs from "dayjs";
export default {
	name: 'TableEdit',
	props: {
		options: {
			type: Array,
			default: []
		},
		stationList: {
			type: Array,
			default: []
		},
	},
	data() {
		return {
			form: {
				id: "",
        projectId: "",
        generatingCapacity: "",
        outageHours: "",
        year: "",
        month: "",
        windpowerstationId: "",
        dateVal: ""
			},
			rules: {
				id: [{ required: true, trigger: 'blur', message: '请输入' }],
				projectId: [{ required: true, trigger: 'blur', message: '请输入' }],
				generatingCapacity: [{ required: true, trigger: 'blur', message: '请输入' }],
				outageHours: [{ required: true, trigger: 'blur', message: '请输入' }],
				dateVal: [{ required: true, trigger: 'blur', message: '请输入' }],
				windpowerstationId: [{ required: true, trigger: 'blur', message: '请输入' }],
			},
			title: '',
			dialogFormVisible: false,
			idAdd: true,
		}
	},
	created() { },
	methods: {
    changeDate() {
      this.$forceUpdate()
    },
		showEdit(row) {
			if (!row) {
				this.title = '添加'
				this.idAdd = true
			} else {
				this.title = '编辑'
				this.idAdd = false
				this.form = Object.assign({}, row)
        this.form.dateVal = this.form.year && this.form.month ? new Date(`${this.form.year}-${this.form.month}-01`).getTime() : ''
			}
			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.year = new Date(this.form.dateVal).getFullYear()
          this.form.month = new Date(this.form.dateVal).getMonth() + 1
					api.addProjectstudy(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>
