<template>
  <div class="slide-con">
    <a class="ctrl up" @click.stop="up" v-if="screens.length>1">
      <slot name="up" />
    </a>
    <div class="slide-layout" :style="{minHeight:!ismobile?'660px':'none'}">
      <div :style="layoutStyle" class="continure" :class="{'m-continure':ismobile}">
        <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 class="sum-style"><span>{{index+1}} / {{screens.length}}</span></div>
    </div>
    <a class="ctrl next" @click.stop="next" v-if="screens.length>1">
      <slot name="next" />
    </a>
  </div>
</template>

<script>
import browser from '@/util/browser'

export default {
  props: {
    screens: {
      default: () => [],
      type: Array
    },
    current: {
      default: 0,
      type: Number
    }
  },
  data () {
    return {
      index: this.current,
      ismobile: browser.mobile

    }
  },
  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-con{
  width: 100%;
}
.slide-layout {
  width: 100%;
  max-width: 980px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}
.sum-style{
  text-align: center;
  position: absolute;
  bottom: 3.5vh;
  width: 100%;
}

.sum-style span{
  color: #fff;
}

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

.ctrl {
  position: absolute;
  top: 50%;
  z-index: 9;
  color: #fff;
  background-color: #fa3800;
  padding: 12px 15px;
  cursor: pointer;
  transform: translateY(-50%);
  border-radius: 4px;
}

.up {
  left: 40px;
}

.next {
  right: 40px;
}

.continure {
  overflow: hidden;
  transition: transform .5s ease;
  display: flex;
  align-items: center;
  position: relative;
}

.m-continure{
  height: 40vh;
}
.m-continure .item{
  text-align: center;
}

</style>
