<template>
  <view :class="['lb-picker', inline ? 'lb-picker-inline' : '']">

    <!-- 默认插槽 -->
    <view v-if="!inline"
      class="lb-picker-default-slot fn-xs"
      @tap="show">
      <slot></slot>
    </view>

    <!-- 遮罩层 -->
    <view v-if="visible && 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 v-if="visible || inline"
      :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">

        <!-- 头部顶部插槽 -->
        <slot name="header-top"></slot>

        <view class="lb-picker-header-actions">
          <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>

        <!-- 头部底部插槽 -->
        <slot name="header-bottom"></slot>
      </view>

      <view class="lb-picker-content">

        <!-- 选择器顶部插槽 -->
        <slot name="picker-top"></slot>

        <view class="lb-picker-content-main"
          :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,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAPFklEQVR4Xu2dTXbUOBeGpToHF7OGFTSsAFhBJytoWAFkUnKNoFdAWEGHUVmZJKygwwpIVkB6BYQVdDIj4Ry5zw3210W++pFtSb6S3pqWpHv9Xj2WZMtXUuAHBaDAWgUktIECUGC9AgAEvQMKbFAAgKB7QAEAgj4ABfopgBGkn26olYkCACSTQOMy+ykAQPrphlqZKABARg700dHRg+vr69+klE/run5E7kgpL+q6Pp9Op2d7e3uXI7uYtXkAMmL4tdZvhRBvhBAP1rhBcBwopd6N6GbWpgHICOGnUePm5uaTEOKppfnzoih2MZpYquWwGABxKKZtU1VVfZJS7tiWp3J1XZ+WZbnbpQ7KDlcAgAzXsFMLi8Xi1WQyOepUqSlsjNmbz+fHfeqiTj8FAEg/3XrX0lp/7jC1umvnXCn1rLdxVOysAADpLFn/Cs3a45/+LQhRFMVDrEWGKNitLgDppteg0ovFYmcymdDivPfPGLM7n89PezeAip0UACCd5BpWGIAM02+M2gAkoOoAJKDYjkwBEEdC2jQDQGxU4lUGgASMBwAJKLYjUwDEkZA2zQAQG5V4lQEgAeMBQAKK7cgUAHEkpE0zAMRGJV5lAEjAeACQgGI7MgVAHAlp0wwAsVGJVxkAEjAeACSg2I5MARBHQto0A0BsVOJVBoAEjAcACSi2I1MAxJGQNs0AEBuVeJUBIAHjAUACiu3IFABxJKRNMwDERiVeZQBIwHgAkIBiOzIFQBwJadMMALFRiVcZloA0ydReSilfLX+/Xdf1hZTyuCiK9zF+dpo7IIvF4tFkMnlZ1/VyRpcLIcRJWZYfeaHxwxt2gDRZP/7ckEyNUuBcTCaTF7PZ7JyjqOt8yhUQAkNKebQp1VFz8/tDKXXCKaasAKmqikSkUcPmdyml3I0JkhwBOTw8pJSq9B3+uuyRP8W6ruvjs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gTENyRFUTwcexRhAcjA7ReroGIHR6qAeIbkxdjZ5FkAorWmj3V++mBowFDCEo6UAfEIyej5AFIDhC0cqQPiCRIAQsI6ShzAGo4cAPEACQBpOk6XsymiWXOk+h5k2/TX4XsSANKKrbW+7Jk0gP3I0V5jKi8KtwHiaiThkNGSxRqEBO35qDcaOHKZYi3DM2QkoZxkZVnSGYmj/tgA0kDS5W16VHDkCMiQkST0dy/rKGQFSAMJJTSgF4e/bLh1nBljXoX4Ptvl7SunKdaKkYROx7VKTscpsQM7QJo7LSWL3pFS0lHEt5sX67qmTWynxpiT2MDIcQ1y98bSbEalG9/abC51XX+cTCb7nLa8swTE5V2bU1u5jiDLMWhA2Vk+atoYcyGEOOV44wMgAQkCIAHFdmQKgDgS0qYZAGKjEq8yACRgPABIQLEdmQIgjoS0aQaA2KjEqwwACRgPABJQbEemAIgjIW2aASA2KvEqA0ACxgOABBTbkSkA4khIm2YAiI1KvMoAkIDxACABxXZkCoA4EtKmGQBioxKvMgAkYDwASECxHZkCII6EtGkGgNioxKsMAAkYDwASUGxHpgCIIyFtmgEgNirxKgNAAsYDgAQU25EpAOJISJtmAIiNSrzKAJCA8QAgAcV2ZAqAOBLSppnma7p/bMquK8MhofMQ/2OrC0ACR6yqqnMp5ZM+Zrmkwunje6x1AEjgyC0Wi95ZJDll+wgs22jmAMgI0vfMZn+mlKJkB/gFVACABBS7NUVrkevr61PbqRZNrabT6c7Yh8mMINXoJgHIiCHQWm9LknclhDhQSlE5/EZQAICMIPqyyTt5oihhHv0oT9R5URSnGDXGDRAAGVd/WGeuAABhHiC4N64CAGRc/WGduQIAhHmA4N64CgCQcfWHdeYKABDmAYJ74yoAQMbVH9aZKwBAmAcI7o2rwL9NuZ5QQgPItwAAAABJRU5ErkJggg==">
              </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"
            :ref="mode"
            :value="value"
            :list="list"
            :mode="mode"
            :props="pickerProps"
            :height="pickerContentHeight"
            :inline="inline"
            @change="handleChange">
          </selector-picker>

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

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

        <!-- 选择器底部插槽 -->
        <slot name="picker-bottom"></slot>
      </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: 'notData'
    },
    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: '',
      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.$emit('confirm', this.picker)
        this.$nextTick(() => {
          this.$refs[this.mode].isConfirmChange = true
        })
        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.$emit('change', this.picker)
      this.$nextTick(() => {
        this.$refs[this.mode].isConfirmChange = false
      })
    },
    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>
