<template>
	<view
    :class="[styleType === 'text'?'xd-segmented-control--text' : 'xd-segmented-control--button' ]"
    :style="{ borderColor: styleType === 'text' ? '' : uiActiveColor }"
    class="xd-segmented-control"
  >
		<view
      v-for="(item, index) in values"
      :class="[
       styleType === 'text'?'xd-segmented-control__item--text': 'xd-segmented-control__item--button' ,
       index === currentIndex&&styleType === 'button'?'xd-segmented-control__item--button--active': '' ,
        index === 0&&styleType === 'button'?'xd-segmented-control__item--button--first': '',
        index === values.length - 1&&styleType === 'button'?'xd-segmented-control__item--button--last': '' ]"
		  :key="index"
      :style="{
        backgroundColor: index === currentIndex && styleType === 'button' ? uiActiveColor : '',
        borderColor: index === currentIndex&&styleType === 'text'||styleType === 'button'?uiActiveColor:'transparent'
      }"
		  class="xd-segmented-control__item"
      @click="_onClick(index)"
    >
			<text
        :style="{color: index === currentIndex
            ? styleType === 'text'
              ? uiActiveColor
              : '#fff'
            : styleType === 'text'
              ? '#000'
              : uiActiveColor}"
			 class="xd-segmented-control__text">{{ item }}</text>
		</view>
	</view>
</template>

<script>
  import {
    getParentsStyle, //获取页面风格单个键值值
  } from '@/utils/xd.base';
  
	export default {
		name: 'XdCustomTab',
		props: {
			current: {
				type: Number,
				default: 0
			},
			values: {
				type: Array,
				default () {
					return []
				}
			},
			activeColor: {
				type: String,
				default: ''
			},
			styleType: {
				type: String,
				default: 'button'
			}
		},
		data() {
			return {
				currentIndex: 0,
        uiActiveColor: null,
        styleMainColor: '', //插件主题风格样式
			}
		},
		watch: {
			current(val) {
        this.currentIndex = val
			}
		},
    async created() {
      this.styleMainColor = await getParentsStyle(this.$parent, '$mainColor');
      
			this.currentIndex = this.current;
			this.uiActiveColor = this.styleMainColor;
			if(this.activeColor) {
        this.uiActiveColor = this.activeColor;
      }

		},
		methods: {
			_onClick(index) {
			  if(this.currentIndex === index) return;
        this.currentIndex = index;
        this.$emit('clickItem', this.currentIndex)
			}
		}
	}
</script>

<style lang="less">
  @name: xd-segmented-control;
  @radius: 10;
  .@{name} {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    height: unit(72,rpx);
    box-sizing: border-box;
    overflow: hidden;


    &__item {
      background: @xd-text-color-inverse;
      display: flex;
      flex-direction: row;
      flex-wrap: nowrap;
      justify-content: center;
      align-items: center;
      height: unit(72, rpx);
      box-sizing: border-box;
      position: relative;
      flex: 1;


      &--button {
        border-style: solid;
        border-top-width: 1px;
        border-bottom-width: 1px;
        border-right-width: 1px;
        border-left-width: 0;

        &--first {
          border-left-width: 1px;
          border-top-left-radius: unit(@radius, rpx);
          border-bottom-left-radius: unit(@radius, rpx);
        }

        &--last {
          border-top-right-radius: unit(@radius, rpx);
          border-bottom-right-radius: unit(@radius, rpx);
        }
      }

      &--text {
        border-bottom-style: solid;
        border-bottom-width: 3px;
      }
    }

    &__text {
      font-size: @xd-font-size-base;
      line-height: unit(40, rpx);
      text-align: center;
    }

  }

</style>
