<template>
	<div class="hui-flex hui-dialog">
		<div class="hui-flex-box hui-dialog-content">
			<el-form :model="form" label-position="top">
				<el-form-item label="年份">
					<el-date-picker v-model="form.year" type="year" placeholder="选择年份" :clearable="false">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="类别">
					<el-select v-model="form.type" allow-create filterable placeholder="请选择"
						@blur="e=>selectBlur(e,form.type)">
						<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.name">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="位置">
					<el-select v-model="form.position" allow-create filterable placeholder="请选择"
						@blur="e=>selectBlur(e,form.position)">
						<el-option v-for="item in positionList" :key="item.id" :label="item.name" :value="item.name">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="序号">
					<el-input type="text" v-model="form.number"></el-input>
				</el-form-item>
			</el-form>
		</div>
		<div class="hui-dialog-submit">
			<el-button size="medium" @click="$emit('callback')">取 消</el-button>
			<el-button size="medium" type="primary" @click="submit" :loading="loading">生 成</el-button>
		</div>
	</div>
</template>

<script>
	import {
		getCodingList,
		insertCoding
	} from '@/httpApi/property'
	export default {
		props: ['code'],
		data() {
			return {
				form: {
					year: '',
					type: '',
					position: '',
					number: ''
				},
				loading: false,
				typeList: [],
				positionList: []
			}
		},
		created() {
			this.form.year = this.$dayjs(new Date()).format('YYYY');
			this.init()
		},
		methods: {
			init() {
				getCodingList({}).then(res => {
					if (res.state) {
						this.typeList = res.data.filter(node => node.type === 1);
						this.form.type = this.typeList[0].name;
						this.positionList = res.data.filter(node => node.type === 2);
						this.form.position = this.positionList[0].name;
					}
				})
			},
			selectBlur(e, item) {
				item = e.target.value;
			},
			submit() {
				this.loading = true;
				let postData = JSON.parse(JSON.stringify(this.form));
				if (!postData.number) {
					delete postData.number;
				}
				insertCoding(postData).then(this.successFunc);
			},
			successFunc(res) {
				this.loading = false;
				if (res.state) {
					this.$message.success('操作成功');
					this.$emit('callback', 'set', res.data);
				}
			}
		}
	}
</script>

<style lang="scss"></style>