<template>
    <ion-page>
        <ion-header>
            <ion-toolbar>
                <ion-buttons slot="start">
                    <ion-back-button text="" default-href="#" @click="$router.back()"></ion-back-button>
                </ion-buttons>
                <ion-title>{{ $t('title.distribution') }}</ion-title>
            </ion-toolbar>
        </ion-header>
        <ion-content>
            <div class="head">
                <div class="title">{{ $t('title.distribution') }}</div>
                <div class="desc" v-if="superior">{{ $t('distribution.myInvitor') }}: {{ superior.nickname }}</div>
                <div class="qr-wrapper" @click="showQR = true">
                    <img src="@/assets/icon_qr.png" class="qr" />
                    <div class="desc">
                        <span class="text">{{ $t('distribution.qrCode') }}</span>
                        <ion-icon :icon="caretForwardCircleOutline"></ion-icon>
                    </div>
                </div>
            </div>
            <div class="stat">
                <div class="bg">
                    <div class="col">
                        <div class="content">
                            <div class="value profit">
                                {{ stat.commissionSum }}<span class="unit">{{ $t('balance.unit') }}</span>
                            </div>
                            <div class="title">{{ $t('distribution.myProfit') }}</div>
                            <div class="btn-detail" @click="$router.push({ name: 'commissionList' })">
                                {{ $t('distribution.viewDetail') }}
                            </div>
                        </div>
                    </div>
                    <div class="divider"></div>
                    <div class="col">
                        <div class="content">
                            <div class="value">{{ stat.inviteNum }}</div>
                            <div class="title">{{ $t('distribution.teamNum') }}</div>
                            <div class="btn-team" @click="$router.push({ name: 'myTeam' })">
                                {{ $t('distribution.viewTeam') }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="list-header">
                <div class="title">{{ $t('distribution.profitDetails') }}</div>
                <div class="more" @click="$router.push({ name: 'commissionList' })">
                    <div>{{ $t('common.more') }}</div>
                    <ion-icon :icon="chevronForwardOutline"></ion-icon>
                </div>
            </div>
            <CommissionItem v-for="item in records" :key="item.id" :detail="item"></CommissionItem>
            <ion-modal class="qr-modal dialog" :is-open="showQR" @didDismiss="showQR = false">
                <ion-content>
                    <vue-qrcode value="Hello, World!" :options="{ width: 250, margin: 2 }"></vue-qrcode>
                </ion-content>
            </ion-modal>
        </ion-content>
    </ion-page>
</template>
<script>
import { mapState } from 'pinia'
import { useUserStore } from '@/stores/user'
import { caretForwardCircleOutline, chevronForwardOutline } from 'ionicons/icons'
import CommissionItem from '@/components/CommissionItem.vue'
import VueQrcode from '@chenfengyuan/vue-qrcode'
export default {
    components: {
        CommissionItem,
        VueQrcode
    },
    data() {
        return {
            stat: {},
            show: false,
            url: '',
            caretForwardCircleOutline,
            chevronForwardOutline,
            showQR: false,
            records: [],
            superior: null
        }
    },
    computed: {
        ...mapState(useUserStore, ['user'])
    },
    created() {
        this.getStat()
        this.getLatestRecords()
        if (this.user.superiorId) {
            this.$http
                .get('/user/mySuperior')
                .then(res => {
                    this.superior = res
                })
                .catch(() => {})
        }
    },
    methods: {
        getStat() {
            this.$http.get('/commission/myStat').then(res => {
                this.stat = res
            })
        },
        getLatestRecords() {
            this.$http.get('/commission/records').then(res => {
                this.records = res.content
            })
        }
    }
}
</script>
<style lang="less" scoped>
.head {
    background: url(../assets/bg_distribution.png) no-repeat center;
    background-size: cover;
    height: 210px;
    padding: 41px 16px 0 16px;
    position: relative;
    z-index: 1;
    .title {
        font-size: 24px;
        font-weight: 600;
        color: var(--ion-color-step-0);
        line-height: 33px;
    }
    .desc {
        margin-top: 10px;
        color: rgba(var(--ion-color-dark-contrast-rgb), 0.6);
        font-size: 12px;
        font-weight: 400;
        line-height: 17px;
    }
    .qr-wrapper {
        position: absolute;
        top: 24px;
        right: 16px;
        width: 100px;
        height: 100px;
        background: #333a4f;
        border-radius: 4px;
        .f-col();
        justify-content: center;
        align-items: center;
        font-size: 12px;
        .qr {
            width: 48px;
            height: 48px;
        }
        .desc {
            color: #ffdb94;
            .f();
            margin-top: 9px;
            line-height: 16px;
            .text {
                margin-right: 2px;
            }
        }
    }
}
.stat {
    background: linear-gradient(315deg, #ffb075 0%, #ff7f1f 100%);
    margin: -66px 16px 0 16px;
    position: relative;
    z-index: 2;
    border-radius: 4px;
    overflow: hidden;
    .bg {
        background: url(../assets/bg_distribution_stat.png) no-repeat center;
        background-size: cover;
        height: 138px;
        .f();
        .col {
            flex: 1 1 0;
            .f;
            justify-content: center;
            .value {
                font-size: 24px;
                font-weight: bold;
                color: var(--ion-color-step-0);
                line-height: 28px;
            }
            .profit {
                .f();
                align-items: baseline;
                .unit {
                    font-size: 12px;
                }
            }
            .title {
                font-size: 12px;
                color: rgba(var(--ion-color-dark-contrast-rgb), 0.6);
                line-height: 17px;
            }
            .btn-detail {
                margin-top: 10px;
                width: 80px;
                height: 28px;
                background: rgba(var(--ion-color-dark-contrast-rgb), 1);
                border-radius: 4px;
                color: #fe6e5b;
                font-size: 12px;
                .f();
                justify-content: center;
            }
            .btn-team {
                margin-top: 10px;
                width: 80px;
                height: 28px;
                background: rgba(var(--ion-color-dark-contrast-rgb), 0.1);
                border: 1px solid var(--ion-color-step-0);
                border-radius: 4px;
                color: var(--ion-color-step-0);
                font-size: 12px;
                box-sizing: border-box;
                .f();
                justify-content: center;
            }
        }
        .divider {
            width: 1px;
            height: 60px;
            background-color: rgba(var(--ion-color-dark-contrast-rgb), 0.16);
        }
    }
}
.list-header {
    .f();
    height: 58px;
    padding: 0 16px;
    .title {
        flex-grow: 1;
        color: var(--ion-color-step-400);
        font-size: 14px;
    }
    .more {
        .f();
        color: var(--ion-color-step-200);
        font-size: 12px;
    }
}
.qr-modal {
    --width: 250px;
    --height: 250px;
}
</style>
