<template>
  <view v-if="visible || inline"
    :class="['lb-picker', inline ? 'lb-picker-inline' : '']">
    <view v-if="showMask && !inline"
      :class="['lb-picker-mask', animation ? 'lb-picker-mask-animation' : '']"
      :style="{
        backgroundColor: maskBgColor,
        zIndex: zIndex - 1
      }"
      @tap.stop="handleMaskTap"
      @touchmove.stop.prevent="moveHandle">
    </view>
    <view :class="[
        'lb-picker-container',
        !inline ? 'lb-picker-container-fixed' : '',
        animation ? 'lb-picker-container-animation' : '',
        containerVisible ? 'lb-picker-container-show' : ''
      ]"
      :style="{
        borderRadius: `${radius} ${radius} 0 0`,
        zIndex: zIndex
      }">
      <view v-if="showHeader"
        class="lb-picker-header">
        <view class="lb-picker-action lb-picker-left">
          <view class="lb-picker-action-item lb-picker-action-cancel"
            @tap.stop="handleCancel">
            <slot v-if="$slots['cancel-text']"
              name="cancel-text">
            </slot>
            <text v-else
              class="lb-picker-action-cancel-text"
              :style="{ color: cancelColor }">
              {{ cancelText }}
            </text>
          </view>
        </view>

        <view class="lb-picker-action lb-picker-center"
          v-if="$slots['action-center']">
          <slot name="action-center"></slot>
        </view>

        <view class="lb-picker-action lb-picker-right">
          <view class="lb-picker-action-item lb-picker-action-confirm"
            @tap.stop="handleConfirm">
            <slot v-if="$slots['confirm-text']"
              name="confirm-text">
            </slot>
            <text v-else
              class="lb-picker-action-confirm-text"
              :style="{ color: confirmColor }">
              {{ confirmText }}
            </text>
          </view>
        </view>
      </view>
      <view class="lb-picker-content"
        :style="{ height: pickerContentHeight }">
        <!-- loading -->
        <view v-if="loading"
          class="lb-picker-loading">
          <slot name="loading">
            <image class="lb-picker-loading-img"
              src="data:image/png;base64, ">
            </image>
          </slot>
        </view>

        <!-- 暂无数据 -->
        <view v-if="isEmpty && !loading"
          class="lb-picker-empty">
          <slot name="empty">
            <text class="lb-picker-empty-text"
              :style="{ color: emptyColor }">
              {{ emptyText }}
            </text>
          </slot>
        </view>

        <!-- 单选 -->
        <selector-picker v-if="mode === 'selector' && !loading && !isEmpty"
          :value="value"
          :list="list"
          :mode="mode"
          :props="pickerProps"
          :height="pickerContentHeight"
          :inline="inline"
          :is-confirm-change="isConfirmChange"
          @change="handleChange">
        </selector-picker>

        <!-- 多列联动 -->
        <multi-selector-picker v-if="mode === 'multiSelector' && !loading && !isEmpty"
          :value="value"
          :list="list"
          :mode="mode"
          :level="level"
          :visible="visible"
          :props="pickerProps"
          :height="pickerContentHeight"
          :inline="inline"
          :is-confirm-change="isConfirmChange"
          @change="handleChange">
        </multi-selector-picker>

        <!-- 非联动选择 -->
        <unlinked-selector-picker v-if="mode === 'unlinkedSelector' && !loading && !isEmpty"
          :value="value"
          :list="list"
          :mode="mode"
          :visible="visible"
          :props="pickerProps"
          :height="pickerContentHeight"
          :inline="inline"
          :is-confirm-change="isConfirmChange"
          @change="handleChange">
        </unlinked-selector-picker>
      </view>
    </view>
  </view>
</template>

<script>
const defaultProps = {
  label: 'label',
  value: 'value',
  children: 'children'
}
import { getColumns } from './utils'
import SelectorPicker from './pickers/selector-picker'
import MultiSelectorPicker from './pickers/multi-selector-picker'
import UnlinkedSelectorPicker from './pickers/unlinked-selector-picker'
export default {
  components: {
    SelectorPicker,
    MultiSelectorPicker,
    UnlinkedSelectorPicker
  },
  props: {
    value: [String, Number, Array],
    list: Array,
    mode: {
      type: String,
      default: 'selector'
    },
    level: {
      type: Number,
      default: 1
    },
    props: {
      type: Object
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    cancelColor: String,
    confirmText: {
      type: String,
      default: '确定'
    },
    confirmColor: String,
    canHide: {
      type: Boolean,
      default: true
    },
    emptyColor: String,
    emptyText: {
      type: String,
      default: '暂无数据'
    },
    radius: String,
    columnNum: {
      type: Number,
      default: 5
    },
    loading: Boolean,
    closeOnClickMask: {
      type: Boolean,
      default: true
    },
    showMask: {
      type: Boolean,
      default: true
    },
    maskColor: {
      type: String,
      default: 'rgba(0, 0, 0, 0.4)'
    },
    dataset: Object,
    inline: Boolean,
    showHeader: {
      type: Boolean,
      default: true
    },
    animation: {
      type: Boolean,
      default: true
    },
    zIndex: {
      type: Number,
      default: 999
    }
  },
  data () {
    return {
      visible: false,
      containerVisible: false,
      maskBgColor: '',
      isConfirmChange: false,
      myValue: this.value,
      picker: {},
      pickerProps: Object.assign({}, defaultProps, this.props),
      pickerContentHeight: 34 * this.columnNum + 'px'
    }
  },
  computed: {
    isEmpty () {
      if (!this.list) return true
      if (this.list && !this.list.length) return true
      return false
    }
  },
  methods: {
    show () {
      if (this.inline) return
      this.visible = true
      setTimeout(() => {
        this.maskBgColor = this.maskColor
        this.containerVisible = true
      }, 20)
    },
    hide () {
      if (this.inline) return
      this.maskBgColor = ''
      this.containerVisible = false
      setTimeout(() => {
        this.visible = false
      }, 200)
    },
    handleCancel () {
      this.$emit('cancel', this.picker)
      if (this.canHide && !this.inline) {
        this.hide()
      }
    },
    handleConfirm () {
      if (this.isEmpty) {
        this.$emit('confirm', null)
        this.hide()
      } else {
        const picker = JSON.parse(JSON.stringify(this.picker))
        this.myValue = picker.value
        this.isConfirmChange = true
        this.$emit('confirm', this.picker)
        if (this.canHide) this.hide()
      }
    },
    handleChange ({ value, item, index, change }) {
      this.picker.value = value
      this.picker.item = item
      this.picker.index = index
      this.picker.change = change
      this.picker.dataset = this.dataset || {}
      this.isConfirmChange = false
      this.$emit('change', this.picker)
    },
    handleMaskTap () {
      if (this.closeOnClickMask) {
        this.hide()
      }
    },
    moveHandle () {},
    getColumnsInfo (value, type = 1) {
      let columnsInfo = getColumns({
        value,
        list: this.list,
        mode: this.mode,
        props: this.pickerProps,
        level: this.level
      }, type)
      if (columnsInfo) {
        if (this.mode === 'selector') {
          columnsInfo.index = columnsInfo.index[0]
        }
      } else {
        columnsInfo = {}
      }
      columnsInfo.dataset = this.dataset || {}
      return columnsInfo
    }
  },
  watch: {
    value (newVal) {
      this.myValue = newVal
    },
    myValue (newVal) {
      this.$emit('input', newVal)
    },
    visible (newVisible) {
      if (newVisible) {
        this.$emit('show')
      } else {
        this.$emit('hide')
      }
    },
    props (newProps) {
      this.pickerProps = Object.assign({}, defaultProps, newProps)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "./style/picker.scss";
</style>
