<style scoped>
.bActionSheet-wrapper {
  position: fixed;
  top: 0;
  width: 100vw;
  height: 100vh;
}

.mask {
  position: fixed;
  top: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease-in;
}

.title, .des {
  width: 100%;
  padding: 20px 30px;
  text-align: center;
  background: white;
  box-sizing: border-box;
  text-overflow: ellipsis;
  overflow-x: hidden;
}

.title {
  white-space: nowrap;
  font-size: 32px;
  font-weight: bold;
  color: #222;
}

.des {
  font-size: 28px;
  color: #999999;
  border-bottom: 1px #F8F8FF solid;
  max-lines: 2;
}

.content {
  position: absolute;
  width: 100vw;
  left: 0;
  z-index: 999999;
  background: #F8F8FF;
  transition: all 0.3s ease-in;
}

.customize-btns {
  display: flex;
  width: 100vw;
  flex-direction: column;
  justify-content: center;
  font-weight: bold;
}

.customize-btns > .customize-btn:not(:last-child) {
  border-bottom: 1px #F8F8FF solid;
}

.customize-btn {
  width: 100vw;
  height: 100px;
  padding:0 30px;
  line-height: 100px;
  text-align: center;
  font-size: 32px;
  color: #666;
  background: white;
  border-radius: 0%;
  box-sizing: border-box;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow-x: hidden;
}

.customize-btn::after {
  border: none;
}
.item-desc {
  font-size: 24px;
  color: #eee;
  margin-left: 20px;
}

.cancel-btn {
  width: 100vw;
  height: 100px;
  margin-top: 20px;
  padding:0 30px;
  line-height: 100px;
  text-align: center;
  font-size: 32px;
  font-weight: bold;
  color: #222;
  box-sizing: border-box;
  background: white; 
}
</style>

<template>
  <div catchtouchmove="disMove" :style="{opacity: show ? 1 : 0, zIndex: show ?  99999 : -2}" class="bActionSheet-wrapper">
    <div 
      @click="swtichActionSheet('mask')"
      :style="{opacity: show ? 1 : 0, zIndex: show ?  99999 : -2}"
      class="mask"
    >
    </div>
    <div :style="{bottom: buttonsBottom + 'px'}" class="content">
      <div v-if="title" class="title">{{title}}</div>
      <div v-if="des" class="des">{{des}}</div>
      <div class="customize-btns">
        <button
          class="customize-btn"
          @click="buttonClick(item, index)"
          :style="{background: item.backgroundColor, color: item.textColor}"
          v-for="(item, index) in buttonData"
          :key="index"
        >
          {{item.text}}
          <span class="item-desc" v-if='item.desc'>{{item.desc}}</span>
        </button>
      </div>
      <div @click="swtichActionSheet" v-if="showCancel" class="cancel-btn">{{cancelText}}</div>
    </div>
  </div>
</template>

<script>
export default {
/**
 * title：标题文字
 * des：描述文字
 * buttonData：组件内按钮，可配置文字，背景，字体颜色
 * showCancel：是否显示取消按钮
*/
  props: {
    title: {
      type: String,
      default: () => ''
    },
    des: {
      type: String,
      default: () => ''
    },
    buttonData: {
      type: Array,
      default: () => []
    },
    showCancel: {
      type: Boolean,
      default: true
    },
    cancelText: {
      type: 'String',
      default: '取消'
    }
  },
  data: () => {
    return {
      show: false,
      buttonsBottom: undefined,
      block: false
    }
  },
  mounted () {
    this.buttonsBottom = -1000
  },
  methods: {
    // actionsheet按钮点击
    buttonClick (val, index) {
      this.$emit('click', val, index)
      this.swtichActionSheet()
    },
    // 开关actionsheet
    swtichActionSheet (type) {
      if (type === 'mask' && !this.show) {
        return
      }
      if (this.block) {
        return
      }
      this.block = true
      this.buttonsBottom = this.show ? -1000 : 0
      if (this.show) {
        setTimeout(() => {
          this.show = false
          this.block = false
        }, 300)
      } else {
        this.show = true
        this.block = false
      }
    }
  }
}
</script>

