<template>
    <app-view class="mine-address">
        <template #header>
            <app-navbar title="收货地址管理" />
        </template>
        <RadioGroup class="mine-address__container" v-model="selectedRow" v-if="dataList.length">
            <ul class="list">
                <li class="list-item" v-for="(item, index) in dataList" :key="index">
                    <div class="list-item__checkbox" @click="onChange(item)" v-if="showRadio">
                        <Radio :name="item" checked-color="#ee0a24" />
                    </div>
                    <div class="list-item__info" @click="onChange(item)">
                        <div class="list-item__info-name">
                            <span>{{ item.receivername }}</span>
                            <span>{{ item.phonenum }}</span>
                            <Tag type="danger" round v-if="item.isdefault" />
                        </div>
                        <div class="list-item__info-address">
                            <span>{{ [item.provincename, item.cityname, item.districtname, item.address].join(' ')
                            }}</span>
                        </div>
                    </div>
                    <div class="list-item__button">
                        <Button icon="edit" @click="onEdit(item)" />
                    </div>
                </li>
            </ul>
        </RadioGroup>
        <Empty v-else />
        <template #footer>
            <div class="g-form__footer">
                <Button type="primary" @click="onEdit" round block>新增地址</Button>
            </div>
            <component ref="componentRef" v-bind="{ selectedRow }" :is="componentMap.get(componentId)"
                @closed="closeComponent" v-if="componentId" />
        </template>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, defineAsyncComponent, PropType } from 'vue'
import { RadioGroup, Radio, Button, Tag, Empty } from 'vant'
import { useNavigation } from '@/hooks/navigation'
import { useComponent } from '@/hooks/component'
import { useAddress } from '@/business/user'

const componentMap = new Map<string, unknown>([
    ['edit', defineAsyncComponent(() => import('./components/edit/index.vue'))],
])

const props = defineProps({
    // 是否显示单选按钮
    showRadio: {
        type: Boolean,
        default: false
    },
    checked: {
        type: Object as PropType<Model.UserReceiveInfoRsp>,
    }
})

const emit = defineEmits(['change'])
const { beforeRouteLeave } = useNavigation()
const { dataList, getUserAddressList } = useAddress()
const { componentRef, componentId, openComponent, closeComponent, closeComponentEach } = useComponent(getUserAddressList)
const selectedRow = shallowRef(props.checked)

// 选择地址
const onChange = (item: Model.UserReceiveInfoRsp) => {
    selectedRow.value = item
    emit('change', item)
}

const onEdit = (item?: Model.UserReceiveInfoRsp) => {
    selectedRow.value = item
    openComponent('edit')
}

getUserAddressList()
beforeRouteLeave(() => closeComponentEach())
</script>

<style lang="less">
@import './index.less';
</style>