<view class="wux-class {{ classes.wrap }}">
    <wux-backdrop id="wux-backdrop" data-index="{{ index }}" bind:click="onClose" />
    <view class="{{ classes.bd }}">
        <block wx:for="{{ options }}" wx:key="index">
            <view class="{{ classes.item }} {{ common.getItemCls(prefixCls, item) }}" bindtap="onClick" data-index="{{ index }}">
                <view class="{{ classes.text }}">
                    {{ item.type !== 'filter' ? displayValues[index] : item.label }}
                    <view class="{{ classes.icon }} {{ common.getIconCls(prefixCls, item) }}" wx:if="{{ item.type !== 'text' }}"></view>
                </view>
            </view>
        </block>
    </view>
    <block wx:for="{{ options }}" wx:key="index">
        <wux-animation-group wux-class="{{ classes.pop }}" in="{{ item.visible }}" class-names="wux-animate--slideInRight" bind:enter="onEnter" bind:exit="onExit" wx:if="{{ item.type === 'filter' }}">
            <scroll-view class="{{ classes.scrollView }}" scroll-y bindscroll="onScroll">
                <block wx:for="{{ item.children }}" wx:for-item="p" wx:for-index="pIndex" wx:key="pIndex">
                    <view class="{{ classes.panel }}">
                        <view class="{{ classes.panelHd }}">
                            <view class="{{ classes.panelTitle }}">{{ p.label }}</view>
                            <view class="{{ classes.panelSelected }}">{{ displayValues[index] && displayValues[index][pIndex] }}</view>
                        </view>
                        <view class="{{ classes.panelBd }}">
                            <radio-group bindchange="onPopupSelectChange" data-parent-index="{{ index }}" data-index="{{ pIndex }}" wx:if="{{ p.type === 'radio' }}">
                                <view class="{{ classes.groups }}">
                                    <block wx:for="{{ p.children }}" wx:for-item="g" wx:for-index="cIndex" wx:key="cIndex">
                                        <view class="{{ classes.group }}">
                                            <radio
                                                class="{{ classes.radio }}"
                                                value="{{ g.value }}"
                                                checked="{{ common.getChecked(values[index] && values[index][pIndex], g.value) }}"
                                            />
                                            <view class="{{ classes.btn }} {{ common.getChecked(values[index] && values[index][pIndex], g.value) ? prefixCls + '__btn--checked' : '' }}">{{ g.label }}</view>
                                        </view>
                                    </block>
                                </view>
                            </radio-group>
                            <checkbox-group bindchange="onPopupSelectChange" data-parent-index="{{ index }}" data-index="{{ pIndex }}" wx:else="{{ p.type === 'checkbox' }}">
                                <view class="{{ classes.groups }}">
                                    <block wx:for="{{ p.children }}" wx:for-item="g" wx:for-index="cIndex" wx:key="cIndex">
                                        <view class="{{ classes.group }}">
                                            <checkbox
                                                class="{{ classes.check }}"
                                                value="{{ g.value }}"
                                                checked="{{ common.getChecked(values[index] && values[index][pIndex], g.value) }}"
                                            />
                                            <view class="{{ classes.btn }} {{ common.getChecked(values[index] && values[index][pIndex], g.value) ? prefixCls + '__btn--checked' : '' }}">{{ g.label }}</view>
                                        </view>
                                    </block>
                                </view>
                            </checkbox-group>
                        </view>
                    </view>
                </block>
            </scroll-view>
            <view class="{{ classes.btns }}">
                <view class="{{ classes.btn }}" data-index="{{ index }}" bindtap="onSelectReset" catchtouchmove="noop">{{ cancelText }}</view>
                <view class="{{ classes.btn }} {{ prefixCls + '__btn--danger' }}" data-index="{{ index }}" bindtap="onSelectConfirm" catchtouchmove="noop">{{ confirmText }}</view>
            </view>
        </wux-animation-group>
        <wux-animation-group wux-class="{{ classes.select }}" in="{{ item.visible }}" class-names="wux-animate--fadeIn" bind:enter="onEnter" bind:exit="onExit" wx:if="{{ item.type === 'radio' }}">
            <scroll-view class="{{ classes.scrollView }}" scroll-y>
                <wux-radio-group
                    value="{{ values[index] }}"
                    options="{{ common.getOptions(item.children) }}"
                    bind:change="onSelectChange"
                    data-index="{{ index }}"
                    data-type="{{ item.type }}"
                />
            </scroll-view>
        </wux-animation-group>
        <wux-animation-group wux-class="{{ classes.select }}" in="{{ item.visible }}" class-names="wux-animate--fadeIn" bind:enter="onEnter" bind:exit="onExit" wx:if="{{ item.type === 'checkbox' }}">
            <scroll-view class="{{ classes.scrollView }}" scroll-y>
                <wux-checkbox-group
                    value="{{ values[index] }}"
                    options="{{ common.getOptions(item.children) }}"
                    bind:change="onSelectChange"
                    data-index="{{ index }}"
                    data-type="{{ item.type }}"
                />
                <view class="{{ classes.btns }}">
                    <view class="{{ classes.btn }}" data-index="{{ index }}" bindtap="onSelectReset" catchtouchmove="noop">{{ cancelText }}</view>
                    <view class="{{ classes.btn }} {{ prefixCls + '__btn--danger' }}" data-index="{{ index }}" data-type="{{ item.type }}" bindtap="onSelectConfirm" catchtouchmove="noop">{{ confirmText }}</view>
                </view>
            </scroll-view>
        </wux-animation-group>
    </block>
</view>

<wxs module="common">
    var util = require("../helpers/wxs/util.wxs")

    module.exports = {
        getOptions: function(options) {
            return options.map(function(option) {
                if (util.type(option) === 'string') {
                    return {
                        title: option,
                        value: option,
                        color: '#f15353'
                    }
                }
                return {
                    title: option.label,
                    value: option.value,
                    color: '#f15353'
                }
            })
        },
        getChecked: function(values, value) {
            if (!values || !values.length) { return false }
            if (util.type(values) === 'array') {
                return values.indexOf(value) !== -1
            }
            return values === value
        },
        getItemCls: function(prefixCls, item) {
            var itemCls = []
            if (item.type !== 'text') {
                itemCls.push(prefixCls + '__item--has-icon')
            }
            if (item.checked) {
                itemCls.push(prefixCls + '__item--checked')
            }
            if (item.visible) {
                itemCls.push(prefixCls + '__item--visible')
            }
            return itemCls.join(' ')
        },
        getIconCls: function(prefixCls, icon) {
            var iconCls = [prefixCls + '__icon--' + icon.type]
            if (icon.type === 'sort') {
                if (icon.sort === 1) {
                    iconCls.push(prefixCls + '__icon--sort-asc')
                } else if (icon.sort === -1) {
                    iconCls.push(prefixCls + '__icon--sort-desc')
                }
            }
            return iconCls.join(' ')
        },
    }
</wxs>
