<template>
    <view class="da-dropdown-picker" v-if="viewCol && viewCol.length">
        <view class="da-dropdown-picker-inner" v-for="(vc, vci) in viewCol" :key="vci">
            <scroll-view class="da-dropdown-picker-view" scroll-y>
                <view
                    class="da-dropdown-picker-item"
                    :class="[vr.checked ? 'is-actived' : '', vr.disabled ? 'is-disabled' : '']"
                    v-for="(vr, vri) in viewRow[vci]"
                    :key="vri"
                    @click="handleSelect(vr, vci, vri)"
                >
                    <text class="da-dropdown-picker-item--name">{{ vr.label }}</text>
                    <text
                        class="da-dropdown-picker-item--icon"
                        v-if="vr.children && vr.children.length"
                    ></text>
                    <text
                        class="da-dropdown-picker-item--check"
                        v-if="vr.checked && (!vr.children || vr.children.length === 0)"
                    />
                </view>
            </scroll-view>
        </view>
    </view>
    <dropdown-footer @reset="handleReset" @confirm="handleConfirm" />
</template>

<script>
import { deepClone } from '@/components/da-dropdown/utils'
import { defineComponent, ref, watch } from 'vue'
import dropdownFooter from './dropdown-footer.vue'

export default defineComponent({
    components: { dropdownFooter },
    props: {
        dropdownItem: {
            type: Object,
            default: null
        },
        dropdownIndex: {
            type: Number
        }
    },
    emits: ['success', 'reset', 'confirm'],
    setup(props, { emit }) {
        const viewCol = ref([])
        const viewRow = ref([])
        const selectedNode = ref()

        function checkData(selected, list) {
            for (let i = 0; i < list.length; i++) {
                const k = list[i]
                for (let j = 0; j < selected.length; j++) {
                    const x = selected[j]
                    if (k.value === x) {
                        k.checked = true
                        viewCol.value.push(k.value)
                        viewRow.value.push(list)
                        if (k.children?.length) {
                            checkData(selected, k.children)
                        }
                        break
                    }
                }
            }
        }

        function initData(item) {
            const list = deepClone(item || [])
            if (list?.length) {
                if (item.value?.length) {
                    viewCol.value = []
                    viewRow.value = []

                    checkData(item.value, list)
                } else {
                    viewCol.value.push('tmpValue')
                    viewRow.value.push(list)
                }
            } else {
                viewCol.value = []
                viewRow.value = []
            }
        }

        function handleSelect(item, colIndex, _rowIndex) {
            if (item.disabled) return
            let lastItem = false
            viewCol.value.splice(colIndex)
            viewCol.value[colIndex] = item.value

            if (viewRow.value[colIndex]?.length) {
                viewRow.value[colIndex].forEach(k => {
                    k.checked = false
                })
            }

            item.checked = true
            const list = item?.children || null

            if (list?.length) {
                viewCol.value[colIndex + 1] = 'tmpValue'
                viewRow.value[colIndex + 1] = list
                lastItem = false
            } else {
                // console.warn('最后一项', item)
                lastItem = true
                selectedNode.value = item
                emit('success', item)
            }

            try {
                if (viewRow.value[colIndex + 1]?.length) {
                    viewRow.value[colIndex + 1].forEach(k => {
                        k.checked = false
                    })
                }
            } catch (e) {
                console.warn('try clean row data', e)
                // --
            }

            if (lastItem) {
                if (props.dropdownItem?.prop) {
                    // const res = { [props.dropdownItem.prop]: deepClone(viewCol.value) }
                    // emit('success', res, viewCol.value, props.dropdownIndex)
                } else {
                    // console.error(`菜单项${props.dropdownItem.title}未定义prop，返回内容失败`)
                }
            }
        }
        function handleReset() {
            viewRow.value.forEach(row => {
                row.forEach(item => {
                    item.checked = false
                    if (item.children && item.children.length > 0) {
                        item.children.forEach(child => {
                            child.checked = false
                        })
                    }
                })
            })
            emit('reset')
        }
        function handleConfirm() {
            emit('confirm', selectedNode.value)
        }

        watch(
            () => props.dropdownItem,
            v => {
                console.log(v)
                initData(v)
            },
            { immediate: true }
        )

        return {
            viewCol,
            viewRow,

            handleSelect,
            handleReset,
            handleConfirm
        }
    }
})
</script>

<style lang="scss" scoped>
.da-dropdown-picker {
    display: flex;
    width: 100%;
    max-height: 60vh;
    overflow: hidden;
    line-height: 1;

    &-inner {
        flex-grow: 1;
    }

    &-view {
        display: flex;

        /* #ifdef MP-ALIPAY */
        flex-direction: column;
        flex-wrap: wrap;

        /* #endif */

        width: 100%;
        height: 100%;

        + .da-dropdown-picker-view {
            border-left: 1px solid #eee;
        }
    }

    &-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 24rpx;
        // font-size: 24rpx;
        color: var(--dropdown-text-color);
        text-align: left;

        &--icon {
            width: 24rpx;
            height: 24rpx;

            &::after {
                /* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
                font-family: 'iconfont' !important;
                font-size: 24rpx;
                font-style: normal;
                content: '\e600';
                -webkit-font-smoothing: antialiased;
                -moz-osx-font-smoothing: grayscale;
            }
        }

        &--check {
            flex-shrink: 0;
            width: 24rpx;
            height: 24rpx;

            &::after {
                /* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
                font-family: 'iconfont' !important;
                font-size: 24rpx;
                font-style: normal;
                content: '\e677';
                -webkit-font-smoothing: antialiased;
                -moz-osx-font-smoothing: grayscale;
            }
        }

        &:hover {
            background: #eee;
        }

        &.is-actived {
            color: $blue-1;
        }
        &.is-disabled {
            cursor: not-allowed;
            opacity: 0.5;
        }
    }
}
</style>
