<template>
	<div class='admin_categories'>
		<modal-window v-model='showAddModal' :loading='loading'>
      <div slot="header">
        <header>
          <p class="modal-card-title">Create {{add.name}}</p>
        </header>
      </div>
			<div slot='main' class="box-content">
        <b-input v-model='add.name' placeholder='Category name' maxlength="15"></b-input>
        <colour-picker v-model='add.color'></colour-picker>
			</div>
			<div slot='footer'>
				<button class='button button--modal button--green' @click='addCategory'>Add category</button>
				<button class='button button--modal' @click='toggleAddModal'>Cancel</button>
			</div>
		</modal-window>

		<modal-window v-model='showEditModal' :loading='loading' style='z-index: 99; '>
      <div slot="header">
        Edit {{edit.name}}
      </div>
			<div slot='main'>
        <br>
        <div>
          <p>Name:</p>
          <b-input v-model='edit.name' placeholder='Category name' maxlength="15"></b-input>
        </div>
        <div>
         <colour-picker v-model='edit.color'></colour-picker>
        </div>
			</div>
			<div slot='footer'>
				<button class='button button--modal button--green' @click='editCategory'>Update category</button>
				<button class='button button--modal' @click='toggleEditModal(null)'>Cancel</button>
			</div>
		</modal-window>
    <br>

		<div class='category_widget__box'>
			<div class='category_widget__text'>
				<h2>Categories</h2>
				Hover to remove or edit a category. <br/>
				Removing a category will place any threads in that category into 'Other'
			</div>

			<transition-group name='slide'>
				<div
					class='admin_categories__category'
					v-for='(category, $index) in categories'
					:key='"category-box-" + category.id'
				>
					<div class='admin_categories__category__actions_holder'>
						<div class='admin_categories__category__actions'>
							<div
								class='admin_categories__category__action'
								@click='removeCategory(category.id, $index)'
							>Delete</div>
							<div
								class='admin_categories__category__action'
								@click='toggleEditModal(category, $index)'
							>Edit</div>
						</div>
					</div>

					<div
						class='admin_categories__category__color'
						:style='{ "background-color": category.color }'
					></div>
					<div class='admin_categories__category__name'>{{category.name}}</div>
				</div>
			</transition-group>
			<div style="margin-top: 0.5rem;">
				<div class='button is-info' @click='toggleAddModal'>
					<div class='admin_categories__category__color'>
						<font-awesome-icon :icon='["fa", "plus"]' />
					</div>
					<div class='admin_categories__category__name'>Add new category</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import ModalWindow from './ModalWindow'
	import ColourPicker from './ColourPicker'

  import AjaxErrorHandler from '../assets/js/errorHandler'

	export default {
		name: 'AdminCategories',
		components: {
			ModalWindow,
			ColourPicker
		},
		data () {
			return {
				loading: false,
				showAddModal: false,
				showEditModal: false,

				add: {
					name: '',
          locked: '',
					color: '#ffffff'
				},
				edit: {
					name: '',
					color: '#ffffff',
          locked: '',
					id: null,
					index: null
				},

				categories: []
			}
		},
		methods: {
			toggleAddModal () {
				this.add.name = ''
				this.add.color = '#ffffff'
        this.add.locked = ''
        this.showAddModal = !this.showAddModal
			},
			toggleEditModal (category, index) {
				if(category) {
					this.edit.name = category.name
					this.edit.color = category.color
					this.edit.id = category.id
					this.edit.index = index
          this.edit.locked = category.locked
				} else {
					this.edit.name = ''
					this.edit.color = '#ffffff'
					this.edit.id = null
					this.edit.index = null
          this.edit.locked = ''
				}

				this.showEditModal = !this.showEditModal
			},
			addCategory () {
				this.loading = true

				this.axios
					.post(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'forums/category', { name: this.add.name, color: this.add.color, locked: this.add.locked })
					.then(res => {
						this.toggleAddModal()
						this.loading = false
						this.categories.push(res.data)
						this.$store.commit('addCategories', res.data)
					})
					.catch(AjaxErrorHandler(this.$store))
			},
			removeCategory (id, index) {
				this.axios
					.delete(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'forums/category/' + id)
					.then(res => {
						this.categories.splice(index, 1)
						this.$store.commit('removeCategory', id)

						if(res.data.otherCategoryCreated) {
							this.$store.commit('addCategories', res.data.otherCategoryCreated)
						}
					})
					.catch(AjaxErrorHandler(this.$store))
			},
			editCategory () {
				this.loading = true

				this.axios
					.put(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'forums/category/' + this.edit.id ,{
						name: this.edit.name,
						color: this.edit.color,
            locked: this.edit.locked
					})
					.then(res => {
						this.loading = false
						this.categories.splice(this.edit.index, 1, res.data)
						this.$store.commit('updateCategory', res.data)
						this.toggleEditModal()
					})
					.catch(AjaxErrorHandler(this.$store))
			}

		},
		mounted () {
			this.axios
				.get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'forums/category')
				.then(res => {
					this.categories = res.data.filter(c => c.name !== 'Other')
				})
				.catch(AjaxErrorHandler(this.$store))
		}
	}
</script>

<style lang='scss' scoped>
	@import '../assets/scss/variables.scss';

	.slide-enter-active, .slide-leave-active, .slice-move {
		transition: all 1s;
	}
	.slide-enter, .slide-leave-to {
		opacity: 0;
	}

	.admin_categories {


		@at-root #{&}__category {
			display: inline-flex;
			position: relative;
			align-items: center;
			background-color: rgba($color__lightgray--primary, 0.5);
			justify-content: center;
			border-radius: 5rem;
			padding: 0.25rem 0.5rem 0.25rem 0.5rem;
			border: thin solid $color__gray--darker;
			margin-right: 0.5rem;
			margin-bottom: 0.5rem;
			transition: all 0.2s;
			cursor: default;

			&:hover {
				background-color: $color__lightgray--primary;

				& .admin_categories__category__actions_holder {
					opacity: 1;
					margin-top: 0;
					pointer-events: all;
				}
			}

			@at-root #{&}--add {
				top: -0.25rem;
				cursor: pointer;
			}

			@at-root #{&}__actions_holder {
				position: absolute;
				top: -2.25rem;
				opacity: 0;
				pointer-events: none;
				margin-top: 1rem;
				padding-bottom: 1rem;
				transition: all 0.2s;
			}
			@at-root #{&}__actions {
				border-radius: 3rem;
				border: thin solid $color__gray--darker;
				overflow: hidden;
				display: flex;
				background-color: #fff;
				box-shadow: 0 0.2rem 3px 0px rgba(224, 224, 224, 0.4);
			}
			@at-root #{&}__action {
				padding: 0.25rem 0.5rem;
				cursor: pointer;
				transition: all 0.2s;

				&:first-of-type {
					border-right: 0.1rem solid $color__gray--primary;
				}

				&:hover {
					background-color: $color__lightgray--primary;
				}
			}

			@at-root #{&}__color {
				height: 1.25rem;
				width: 1.25rem;
				border-radius: 100%;
				margin-left: 0rem;
				margin-right: 0.25rem;

				display: flex;
				align-items: center;
				justify-content: center;
			}

			@at-root #{&}__name {
				position: relative;
				bottom: 0.1rem;
			}
		}
	}
</style>
