<template>
	<div class="scope-process">
		<!-- <div class="pagination">
			<button>
				<i class="el-icon-arrow-up"></i>
			</button>

			<span>{{ page + 1 }}</span>

			<button>
				<i class="el-icon-arrow-down"></i>
			</button>
		</div>-->

		<cl-scroll>
			<div
				class="block"
				v-for="(item, index) in processList"
				:key="index"
				:class="{ active: item.active }"
				:data-index="index"
				@mousedown="
					e => {
						onTap(e, item);
					}
				"
			>
				<span>{{ item.label }}</span>

				<i class="el-icon-close" v-if="index > 0" @click="onDel(index)"></i>
			</div>
		</cl-scroll>

		<ul class="context-menu" v-show="menu.visible" :style="menu.style">
			<li @click="onClose('current')" v-if="isHit">关闭当前</li>
			<li @click="onClose('other')">关闭其他</li>
			<li @click="onClose('all')">关闭所有</li>
		</ul>
	</div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex';
import _ from 'lodash';

export default {
	data() {
		return {
			menu: {
				visible: false,
				current: {},
				style: {
					left: 0,
					top: 0
				}
			},

			isHit: false,

			page: 1
		};
	},

	computed: {
		...mapGetters(['processList'])
	},

	watch: {
		processList() {
			let maxWidth = this.$el.clientWidth;

			this.$nextTick(() => {
				let list = this.$el.querySelectorAll('.block') || [];

				let sumWidth = 0;

				for (let i = 0; i < list.length; i++) {
					sumWidth += list[i].clientWidth;
				}

				if (maxWidth - sumWidth > 1000) {
					this.page += 1;
				}
			});
		}
	},

	mounted() {
		this.$el.oncontextmenu = () => {
			event.returnValue = false;
		};

		document.body.addEventListener('click', () => {
			if (this.menu.visible) {
				this.menu.visible = false;
			}
		});
	},

	methods: {
		...mapMutations(['ADD_PROCESS', 'DEL_PROCESS', 'SET_PROCESS']),

		onTap(e, item) {
			this.isHit = item.active;

			if (e.button == 0) {
				this.$router.push(item.value);
			} else {
				this.menu = {
					current: item,
					visible: true,
					style: {
						left: e.layerX + 'px',
						top: e.layerY + 'px'
					}
				};
			}
		},

		onDel(index) {
			this.DEL_PROCESS(index);

			this.toPath();
		},

		onClose(cmd) {
			const { current } = this.menu;

			switch (cmd) {
				case 'current':
					this.onDel(this.processList.findIndex(e => e.value == current.value));
					break;

				case 'other':
					this.SET_PROCESS(
						this.processList.filter(e => e.value == current.value || e.value == '/')
					);
					break;

				case 'all':
					this.SET_PROCESS(this.processList.filter(e => e.value == '/'));
					break;
			}

			this.toPath();
		},

		toPath() {
			const active = this.processList.find(e => e.active);

			if (!active) {
				const next = _.last(this.processList);

				this.$router.push(next ? next.value : '/');
			}
		}
	}
};
</script>

<style lang="stylus" scoped>
@import '../../../assets/css/theme.styl';

.scope-process {
	display: flex;
	height: 30px;
	position: relative;

	.cl-scroll {
		flex: 1;

		>>>.scroll-wrapper {
			display: flex;
		}
	}

	.block {
		border-radius: 3px;
		height: 30px;
		line-height: 30px;
		padding: 0 10px;
		background-color: #fff;
		font-size: 12px;
		margin-right: 10px;
		color: #909399;
		cursor: pointer;
		transition: all 0.3s;

		&:last-child {
			margin-right: 0;
		}

		i {
			margin-left: 5px;
			font-size: 14px;
			position: relative;
			top: 1px;
			display: none;

			&:hover {
				color: red;
			}
		}

		&.active:before {
		}

		&.active {
			color: #fff;
			background-color: $color2;

			i {
				display: inline-block;
			}

			&:before {
				content: '';
				height: 8px;
				width: 8px;
				border-radius: 8px;
				background-color: #fff;
				margin-right: 5px;
				display: inline-block;
			}
		}
	}

	.context-menu {
		margin: 0;
		background: #fff;
		z-index: 100;
		position: absolute;
		list-style-type: none;
		padding: 5px 0;
		border-radius: 4px;
		font-size: 12px;
		font-weight: 400;
		color: #333;
		box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);

		li {
			margin: 0;
			padding: 7px 16px;
			cursor: pointer;

			&:hover {
				background: #eee;
			}
		}
	}

	.pagination {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 30px;
		margin-right: 15px;
		position: relative;

		span {
			display: inline-block;
			font-size: 12px;
			position: absolute;
			transform: scale(0.9);
			left: 16px;
			top: 1px;
			background-color: $color;
			color: #fff;
			border-radius: 15px;
			height: 15px;
			min-width: 15px;
			text-align: center;
			line-height: 15px;
		}

		button {
			border: 0;
			background-color: #eaeff5;
			cursor: pointer;
			height: 14px;
			border-radius: 1px;
			color: #000;
			font-size: 14px;
			margin-right: 10px;

			&:hover {
				color: $color;
			}

			&:first-child {
				margin-bottom: 2px;
			}
		}
	}
}
</style>
