<template>
    <div class="nus_addresslist"
        v-if="list && list.length > 0">
        <nu-cellgroup
            class="nu_address-group"
            :list="list"
            :filterItem="filterItem">
            <template slot-scope="{itemData, data, index}">
                <nu-cell>
                    <address-item
                        class="nu_address-item"
                        v-if="itemData"
                        v-bind="itemData"
                        @setDefault="_emitEvent($event, 'default', data, index, itemData)"
                        @addEdit="_emitEvent($event, 'edit', data, index, itemData)"
                        @delete="_emitEvent($event, 'delete', data, index, itemData)"
                        @click="_click($event, data, index, itemData)"
                        @tap="_tap($event, data, index, itemData)"
                        @clickItem="_clickItem($event, data, index, itemData)">
                    </address-item>
                </nu-cell>
            </template>
        </nu-cellgroup>
    </div>
</template>

<script>
import BusinessTmp from '../../../_comm/BusinessTmp';
import AddressItem from '../addressItem/AddressItem';
import NuColview from '../../../../layout/colview';
import NuCellgroup from '../../../../widget/cellgroup';
import NuCell from '../../../../widget/cell';
import { EVENT_TYPE } from '../../constants';
export default {
    extends: BusinessTmp,
    components: {
        NuColview,
        AddressItem,
        NuCellgroup,
        NuCell
    },
    props: {
        /**
         * 地址信息列表
         */
        list: {
            type: Array
        },
        /**
         * 间隙
         */
        gap: {
            type: [Number, String],
            default: 0
        },

        /**
         * 过滤地址item项的数据
         */
        filterItem: {
            type: Function
        }
    },
    methods: {
        /**
         * 冒泡事件
         * @param {Event} evt 事件对象
         * @param {String} type 事件类型
         * @param {Object} item item项数据
         * @param {Number} index 索引
         * @param {Object} itemData item项数据
         */
        _emitEvent (evt, type, item, index, itemData) {
            const _that = this;
            let _evtType;
            evt = evt || {};
            switch ((type || '') + '') {
                case 'default':
                    _evtType = EVENT_TYPE.SET_DEFAULT;
                    break;
                case 'edit':
                    _evtType = EVENT_TYPE.ADD_EDIT;
                    break;
                case 'delete':
                    _evtType = EVENT_TYPE.DELETE;
                    break;
            }
            if (_evtType) {
                evt = evt || {};
                if (evt instanceof Event) {
                    evt = {
                        Event: evt,
                        data: item,
                        index: index,
                        itemData: itemData
                    };
                } else {
                    evt = Object.assign({}, evt, {
                        data: item,
                        index: index,
                        itemData: itemData
                    });
                }
                _that.$emit(_evtType, evt);
            }
        },
        _click(evt, data, index, itemData) {
            this._clickItem(evt, data, index, itemData);
            this._onClick(evt);
        },
        _tap(evt, data, index, itemData) {
            this._clickItem(evt, data, index, itemData);
            this._onTap(evt);
        },
        /**
         * 点击item项的事件回调
         * @param {Event} evt 事件对象
         * @param {Object} item item项数据
         * @param {Number} index 索引
         * @param {Object} itemData item项数据
         */
        _clickItem (evt, item, index, itemData) {
            const _that = this;
            _that._onClickItem({
                Event: evt,
                data: item,
                index: index,
                itemData: itemData
            });
        }
    }
};
</script>
