<template>
  <div>
    <a class="ctrl up" @click.stop="up">
      <slot name="up" />
    </a>
    <div class="slide-layout">
      <div :style="layoutStyle" class="continure">
        <div v-for="(iscreen, index) in screens" :key="index" :style="itemStyle" class="item">
          <slot name="item" :data="iscreen" :index="index" />
        </div>
      </div>
      <slot name="foot" :data="screens" :index="index" />
    </div>
    <a class="ctrl next" @click.stop="next">
      <slot name="next" />
    </a>
  </div>
</template>

<script>
export default {
  props: {
    screens: {
      default: () => [],
      type: Array
    },
    current: {
      default: 0,
      type: Number
    }
  },
  data () {
    return {
      index: this.current
    }
  },
  computed: {
    left () {
      return this.index / this.screens.length * 100 + '%'
    },
    layoutStyle () {
      return {width: this.screens.length + '00%', transform: `translateX(-${this.left})`}
    },
    itemStyle () {
      return {width: 1 / this.screens.length * 100 + '%'}
    }
  },
  watch: {
    current (newVal) {
      this.index = newVal
    }
  },
  methods: {
    next () {
      this.index++
      if (this.index === this.screens.length) {
        this.index = 0
      }
    },
    up () {
      this.index--
      if (!~this.index) {
        this.index = this.screens.length - 1
      }
    }
  }
}
</script>

<style scoped>
.slide-layout {
  width: 100%;
  max-width: 980px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}

.item {
  height: 100%;
  float: left;
  position: relative;
}

.ctrl {
  position: absolute;
  top: 50%;
  z-index: 9;
  color: #fff;
  cursor: pointer;
  transform: translateY(-50%)
}

.up {
  left: 40px;
}

.next {
  right: 40px;
}

.continure {
  overflow: hidden;
  transition: transform .5s ease;
}
</style>
