<template>
    <!-- 全局下拉框控件 -->
    <view class="picker" :class="{ 'u-border': ps.border }" :style="style">
        <global-popup v-model="show" :height="592" :customStyle="{ padding: '28rpx 32rpx' }" v-if="filter" :distance="distance">
            <template #top>
                <view>
                    <up-search :placeholder="placeholder" :showAction="false" v-model="searchKey"></up-search>
                </view>
            </template>
            <view class="popup-content-list" v-if="getFilterColumns.length">
                <view
                    class="popup-content-item u-line-1"
                    :class="{ 'mocp-color-primary-6': modelValue == item[valueField] }"
                    v-for="(item, index) in getFilterColumns"
                    :key="index"
                    @tap="handleChoose(item[valueField])"
                >
                    {{ item[labelField] }}
                </view>
            </view>
            <view style="width: 100%; height: 100%" v-else>
                <global-empty></global-empty>
            </view>
        </global-popup>
        <up-picker
            v-else
            :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="open" :style="getContentStyle">
            <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, nextTick, ref, watch } from 'vue'
import * as dictData from 'mocp/hooks/use-dict/dict-data'
import { cloneDeep } from 'lodash'

const es = defineEmits(['update:modelValue', 'change', 'getScrollTop'])
const ps = defineProps({
    //是否筛选
    filter: {
        type: Boolean,
        default: false
    },
    //是否有边框
    border: {
        type: Boolean,
        default: false
    },
    //是否多选(只对filter有效)---待优化
    // multiple: {
    //     type: Boolean,
    //     default: false
    // },
    style: {
        type: Object,
        default: () => {
            return {}
        }
    },
    //显示为空的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'
    },
    //打开前判断
    onBeforeOpen: {
        type: Function,
        default: null
    }
})
//获取下拉框样式
const getContentStyle = 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(() => {
    if (labelValue.value == ps.emptyValue) {
        return ps.placeholder
    }
    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 open = async () => {
    if (ps.onBeforeOpen) {
        const flag = await ps.onBeforeOpen()
        if (!flag) return
    }
    searchKey.value = ''
    show.value = true
}
//设置下拉框打开的默认值
const defaultIndex = ref(0)
//获取下拉框的内容Columns
const getColumns = computed(() => {
    if (ps.dictkey) {
        if (dictData[ps.dictkey]) {
            return cloneDeep(dictData[ps.dictkey])
        } else {
            throw new Error('请输入存在的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]
            nextTick(() => {
                defaultIndex.value = index
            })
        } else {
            labelValue.value = ps.modelValue
            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)
}
//弹出层输入框搜索
const searchKey = ref('')
const getFilterColumns = computed(() => {
    return getColumns.value.filter((item) => item[ps.labelField].toLowerCase().includes(searchKey.value.toLowerCase()))
})
//弹出层列表点击
const distance = ref(0)
const handleChoose = (value) => {
    es('update:modelValue', value)
    es('change', value)
    show.value = false
}
//打开默认滑动到选中的位置
watch(
    defaultIndex,
    (value) => {
        distance.value = value * 39
    },
    { immediate: true }
)
</script>
<style lang="scss" scoped>
.picker {
    flex: auto;
    height: 100%;
    border-radius: 4px;
    padding-left: 18rpx;
    box-sizing: border-box;
    &-content {
        height: 100%;
        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;
        }
    }
    .popup-content {
        &-list {
            padding-top: 20rpx;
            color: $mocp-text-4;
        }
        &-item {
            margin-bottom: 40rpx;
            &:last-child {
                margin-bottom: 0;
            }
        }
    }
}
</style>
