<template>
	<view class="page1">
		<view v-for="(item, index) in values" class="page1__item" @click="_onClick(index)">
			<view>
				<text :style="{color:
				    index === currentIndex
				      ? styleType === 'text'
				        ? activeColor
				        : '#fff'
				      : styleType === 'text'
				        ? '#000'
				        : activeColor}" class="page1__text" :class="styleType === 'text' && index === currentIndex ? 'page1__item--text': ''">{{ item }}</text>
			</view>

		</view>
	</view>
</template>

<script>
	/**
	 * SegmentedControl 分段器
	 * @description 用作不同视图的显示
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=54
	 * @property {Number} current 当前选中的tab索引值，从0计数
	 * @property {String} styleType = [button|text] 分段器样式类型
	 * 	@value button 按钮类型
	 * 	@value text 文字类型
	 * @property {String} activeColor 选中的标签背景色与边框颜色
	 * @property {Array} values 选项数组
	 * @event {Function} clickItem 组件触发点击事件时触发，e={currentIndex}
	 */

	export default {
		name: 'UniSegmentedControl',
		emits: ['clickItem'],
		props: {
			current: {
				type: Number,
				default: 0
			},
			values: {
				type: Array,
				default () {
					return []
				}
			},
			activeColor: {
				type: String,
				default: '#2979FF'
			},
			styleType: {
				type: String,
				default: 'button'
			}
		},
		data() {
			return {
				currentIndex: 0
			}
		},
		watch: {
			current(val) {
				if (val !== this.currentIndex) {
					this.currentIndex = val
				}
			}
		},
		created() {
			this.currentIndex = this.current
		},
		methods: {
			_onClick(index) {
				if (this.currentIndex !== index) {
					this.currentIndex = index
					this.$emit('clickItem', {
						currentIndex: index
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		width: 100% ;
	}
	.page1 {
		width: 100%;
		overflow-x: scroll;
		height: 80rpx;
		line-height: 100rpx;
		background: #777;
		
	}

	.page1__item {
		float: left;
		width: 120rpx;
	}

	.page1__item--text {
		border-bottom-style: solid;
		border-bottom-width: 2px;
		padding: 12rpx 0;
	}

	.page1__text {
		font-size: 28rpx;
		line-height: 40rpx;
		text-align: center;
	}
</style>
