<template>
    <!-- 全局下拉框控件 -->
    <view class="picker">
        <up-picker
            :show="show"
            :columns="[getColumns]"
            :keyName="labelField"
            @confirm="confirm"
            closeOnClickOverlay
            @close="show = false"
            @cancel="show = false"
            :defaultIndex="[defaultIndex]"
            :cancelText="cancelText"
            :confirmText="confirmText"
            :cancelColor="cancelColor"
            :confirmColor="confirmColor"
        ></up-picker>
        <view class="picker-content" :class="getPickerClass" @tap="show = true" :style="getStyle">
            <text class="picker-value">{{ getLabelValue }}</text>
            <view class="picker-icon">
                <view class="picker-icon-close" v-if="clearable && !showPlaceholder && !disabled" @tap.stop="clear">
                    <up-icon name="close-circle-fill" color="#c0c4cc" size="36rpx"></up-icon>
                </view>
                <up-icon name="arrow-right" color="#86909C" size="36rpx"></up-icon>
            </view>
        </view>
    </view>
</template>

<script setup>
import { computed, ref, watch } from 'vue'
import * as dictData from 'mocp/hooks/use-dict/dict-data'
import { cloneDeep } from 'lodash'

const es = defineEmits(['update:modelValue', 'change'])
const ps = defineProps({
    //显示为空的value值
    emptyValue: {
        type: [String, Number],
        default: ''
    },
    dictkey: {
        type: String,
        default: ''
    },
    modelValue: {
        type: [String, Number],
        default: ''
    },
    options: {
        type: Array,
        default: () => {
            return []
        }
    },
    clearable: {
        type: Boolean,
        default: false
    },
    labelField: {
        type: String,
        default: 'label'
    },
    valueField: {
        type: String,
        default: 'value'
    },
    pickAlign: {
        type: String,
        default: 'left'
    },
    placeholder: {
        type: String,
        default: '请选择'
    },
    disabled: {
        type: Boolean,
        default: false
    },
    cancelText: {
        type: String,
        default: '取消'
    },
    confirmText: {
        type: String,
        default: '确认'
    },
    cancelColor: {
        type: String,
        default: '#909193'
    },
    confirmColor: {
        type: String,
        default: '#165dff'
    }
})
//获取下拉框样式
const getStyle = computed(() => {
    let pickAlign = 'flex-start'
    if (ps.pickAlign == 'center') {
        pickAlign = 'center'
    } else if (ps.pickAlign == 'right') {
        pickAlign = 'flex-end'
    }
    return {
        justifyContent: pickAlign
    }
})
//下拉框显示的内容
const labelValue = ref('')
const getLabelValue = computed(() => {
    return labelValue.value || ps.placeholder
})
//获取下拉框class
const showPlaceholder = computed(() => {
    return !labelValue.value || labelValue.value == ps.emptyValue
})
const getPickerClass = computed(() => {
    return {
        disabled: ps.disabled,
        placeholder: showPlaceholder.value
    }
})
const show = ref(false)
//设置下拉框打开的默认值
const defaultIndex = ref(0)
//获取下拉框的内容Columns
const getColumns = computed(() => {
    if (ps.dictkey) {
        return cloneDeep(dictData[ps.dictkey])
    } else {
        if (ps.options && Object.prototype.toString.call(ps.options[0]) == '[object Object]') {
            return ps.options
        } else {
            return ps.options.map((item) => {
                return {
                    [ps.labelField]: item,
                    [ps.valueField]: item
                }
            })
        }
    }
})
//监听设置labelValue的值
watch(
    [() => ps.modelValue, () => ps.options],
    () => {
        const option = getColumns.value.find((option) => String(option[ps.valueField]) === String(ps.modelValue))
        const index = getColumns.value.findIndex((option) => String(option[ps.valueField]) === String(ps.modelValue))
        if (option) {
            labelValue.value = option[ps.labelField]
            defaultIndex.value = index
        } else {
            labelValue.value = ''
            defaultIndex.value = 0
        }
    },
    { immediate: true }
)
//点击确定按钮
const confirm = (e) => {
    show.value = false
    const index = e.indexs[0]
    labelValue.value = getColumns.value[index][ps.labelField]
    es('update:modelValue', getColumns.value[index][ps.valueField])
    es('change', getColumns.value[index][ps.valueField])
}
//点击清空按钮
const clear = () => {
    labelValue.value = ''
    defaultIndex.value = 0
    es('update:modelValue', ps.emptyValue)
    es('change', ps.emptyValue)
}
</script>
<style lang="scss" scoped>
.picker {
    flex: auto;
    &-content {
        display: flex;
        align-items: center;
        line-height: 48rpx;
        &.placeholder {
            .picker-value {
                color: $mocp-text-3;
            }
        }
        &.disabled {
            background: #f5f7fa;
        }
    }
    &-value {
        color: $mocp-text-4;
        margin-right: 8rpx;
    }
    &-icon {
        display: flex;
        align-items: center;
        &-close {
            margin-left: 4rpx;
        }
    }
}
</style>
