<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="cascaderSel" v-if="idAdd">
				<el-cascader v-model="form.cascaderSel" :options="options" style="width: 200px;"
					:props="{ checkStrictly: true, label: 'name', value: 'id' }" clearable>
				</el-cascader>
			</el-form-item>
			<el-form-item label="场站" prop="generatingcapacity" v-if="!idAdd">
				<el-input class="inputs" v-model="form.windpower" disabled></el-input>
			</el-form-item>
			<el-form-item label="期次" prop="generatingcapacity" v-if="!idAdd">
				<el-input class="inputs" v-model="form.projectid" disabled></el-input>
			</el-form-item>
			<el-form-item label="计划发电量" prop="generatingcapacity">
				<el-input class="inputs" v-model="form.generatingcapacity"></el-input>
			</el-form-item>
			<el-form-item label="计划停运小时" prop="outagehours">
				<el-input class="inputs" v-model="form.outagehours"></el-input>
			</el-form-item>
			<el-form-item label="请选择日期" prop="dateValues">
				<el-date-picker style="width: 200px;" v-model="form.dateValues" type="month" placeholder="选择月" :disabled="!idAdd?true:false">
				</el-date-picker>
			</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 api from '@/api/table'
export default {
	name: 'TableEdit',
	props: {
		options: {
			type: Array,
			default: []
		},
	},
	data() {
		return {
			form: {
				generatingcapacity: '',
				outagehours: '',
				cascaderSel: [],
				dateValues: '',
			},
			rules: {
				generatingcapacity: [{ required: true, trigger: 'blur', message: '请输入发电量' }],
				outagehours: [{ required: true, trigger: 'blur', message: '请输入停运小时' }],
				cascaderSel: [{ required: true, trigger: 'blur', message: '请选择场站' }],
				dateValues: [{ 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.dateValues = 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')
		},
		handleSave() {
			this.$refs['form'].validate(async (valid) => {
				if (valid) {
					this.form.year = new Date(this.form.dateValues).getFullYear()
					this.form.month = new Date(this.form.dateValues).getMonth() + 1
					if (!this.form.projectid) {
						this.form.projectid = this.form.cascaderSel[2]
					}
					if (!this.form.windpower) {
						this.form.windpower = this.form.cascaderSel[1]
					}

					api.addProjectstudy(this.form).then(res => {
						if (res.code == 200) {
							this.$message({
								type: 'success',
								message: '添加成功!'
							});
							this.$emit('handleSuccess');
							this.dialogFormVisible = false
						}
					})
				} else {
					return false
				}
			})
		},
	},
}
</script>
<style lang="less" scoped>
.inputs {
	width: 200px;
}
</style>
