<template>
	<modal title='Create Community Station'>
		<div slot='body'>
			<!-- validation to check if exists http://bulma.io/documentation/elements/form/ -->
			<label class='label'>Unique ID (lowercase, a-z, used in the url)</label>
			<p class='control'>
				<input class='input' type='text' placeholder='Name...' v-model='newCommunity._id' autofocus>
			</p>
			<label class='label'>Display Name</label>
			<p class='control'>
				<input class='input' type='text' placeholder='Display name...' v-model='newCommunity.displayName'>
			</p>
			<label class='label'>Description</label>
			<p class='control'>
				<input class='input' type='text' placeholder='Description...' v-model='newCommunity.description' @keyup.enter="submitModal()">
			</p>
		</div>
		<div slot='footer'>
			<a class='button is-primary' @click='submitModal()'>Create</a>
		</div>
	</modal>
</template>

<script>
	import { Toast } from 'vue-roaster';
	import Modal from './Modal.vue';
	import io from '../../io';

	export default {
		components: { Modal },
		data() {
			return {
				newCommunity: {
					_id: '',
					displayName: '',
					description: ''
				}
			}
		},
		ready: function () {
			let _this = this;
			io.getSocket((socket) => {
				_this.socket = socket;
			});
		},
		methods: {
			toggleModal: function () {
				this.$parent.modals.createCommunityStation = !this.$parent.modals.createCommunityStation;
			},
			submitModal: function () {
				let _this = this;
				if (_this.newCommunity._id == '') return Toast.methods.addToast('ID cannot be a blank field', 3000);
				if (_this.newCommunity.displayName == '') return Toast.methods.addToast('Display Name cannot be a blank field', 3000);
				if (_this.newCommunity.description == '') return Toast.methods.addToast('Description cannot be a blank field', 3000);
				this.socket.emit('stations.create', {
					_id: _this.newCommunity._id,
					type: 'community',
					displayName: _this.newCommunity.displayName,
					description: _this.newCommunity.description
				}, res => {
					if (res.status === 'success') Toast.methods.addToast(`You have added the station successfully`, 4000);
					else Toast.methods.addToast(res.message, 4000);
				});
				this.toggleModal();
			}
		},
		events: {
			closeModal: function() {
				this.$parent.modals.createCommunityStation = !this.$parent.modals.createCommunityStation;
			}
		}
	}
</script>