<template>
  <div class="upanapp-color-picker">
    <ua-flexbox>
      <ua-flexbox-item v-for="color in colors" :key="color" class="upanapp-color-box">
        <span class="upanapp-color-item" :style="{borderRadius: width/2 + 'px',backgroundColor: color, width: width + 'px', height: width + 'px'}" @click="change(color)" :class="{'upanapp-color-white': color === '#fff' || color === '#fff', 'upanapp-color-picker-small': size === 'small', 'upanapp-color-picker-middle': size === 'middle'}">
          <ua-icon v-if="color === value" class="upanapp-color-checked" :style="{lineHeight: width + 'px'}" type="success-no-circle"></ua-icon>
        </span>
      </ua-flexbox-item>
    </ua-flexbox>
  </div>
</template>

<script>
import UaIcon from '../icon'
import { UaFlexbox, UaFlexboxItem } from '../flexbox'

const sizeMap = {
  'large': 40,
  'middle': 30,
  'small': 20
}
export default {
  name: 'ua-color-picker',
  components: {
    UaIcon,
    UaFlexbox,
    UaFlexboxItem
  },
  created () {
    this.currentValue = this.value
  },
  props: {
    colors: {
      type: Array,
      required: true
    },
    size: {
      type: String,
      default: 'large'
    },
    value: String
  },
  computed: {
    width () {
      return sizeMap[this.size]
    }
  },
  data () {
    return {
      currentValue: ''
    }
  },
  watch: {
    value (val) {
      this.currentValue = val
    },
    currentValue (color) {
      this.$emit('on-change', color)
      this.$emit('input', color)
    }
  },
  methods: {
    change (color) {
      this.currentValue = color
    }
  }
}
</script>

<style lang="less">
@import '../../styles/weui/icon/weui_icon_font';

.upanapp-color-box {
  text-align: center;
}
.upanapp-color-picker {
  font-size: 0;
}
.upanapp-color-item {
  display: inline-block;
  text-align: center;
  box-sizing: border-box;
  position: relative
}
.upanapp-color-checked.weui-icon-success-no-circle:before {
  color: #fff;
}
.upanapp-color-checked {
  width: 100%;
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}
.upanapp-color-white {
  border: 1px solid #ccc;
}
.upanapp-color-white .upanapp-color-checked:before {
  color: #ccc;
}
.upanapp-color-picker-small .upanapp-color-checked:before{
  font-size: 10px;
}
.upanapp-color-picker-middle .upanapp-color-checked:before{
  font-size: 18px;
}
</style>
