<template>
    <div :class="['page', cardInfo.applyStatus == 1 ? 'page' : 'page']">
        <div class="card-swiper">
            <div class="swiper-container">
                <div class="card-wrapper">
                    <div class="card-info" @click.stop="(e: MouseEvent) => toggleCardNo(card.id, e)" :class="{ flipping: isFlipping[cardInfo.id] }">
                        <img src="@/assets/images/logo.png" alt="" srcset="" />
                        <div class="number">
                            {{ cardInfo?.cardNo?.replace(/(\d{4})\d+(\d{4})/, '$1 **** **** $2') }}
                        </div>
                        <div class="card-b">
                            <div class="valid">
                                <span class="lable">{{ t('cards.p13') }}</span>
                                <span>{{ cardInfo.firstName }} {{ cardInfo.lastName }}</span>
                            </div>
                            <div class="valid">
                                <span class="lable">{{ t('cards.p14') }}</span>
                                <span>{{ cardInfo.expire }}</span>
                            </div>
                        </div>
                        <img
                            v-if="cardInfo.status != 1"
                            src="https://upload.wikimedia.org/wikipedia/commons/a/a4/Flag_of_the_United_States.svg"
                            class="flags"
                            @click.stop="ucardActivate(cardInfo.id)"
                        />
                    </div>
                </div>
            </div>
        </div>

        <div class="balance-content">{{ t('apply-record-detail.p1') }}</div>
        <div class="g">
            <img src="../assets/images/apply-record-1.png" alt="" />
            <div class="g-l">
                <div class="g-item">
                    <div class="label a1">{{ cardInfo.cardName }}</div>
                </div>
                <div class="g-item">
                    <div class="label">{{ t('apply-record-detail.p2') }}</div>
                    <div :class="statusClass(cardInfo.kycStatus)">{{ statusMap[cardInfo.kycStatus] }}</div>
                </div>
                <div class="g-item" v-if="cardInfo.kycStatus == 2 &&(cardInfo.applyStatus === 0 || cardInfo.applyStatus === 1)">
                    <div class="label">{{ t('apply-record-detail.p7') }}</div>
                    <div :class="statusClass1(cardInfo.applyStatus)">{{ applyStatusMap[cardInfo.applyStatus] }}</div>
                </div>
            </div>
        </div>

        <div class="card-info1" v-if="cardInfo.cardName && !props.type">
            <div class="card-title">{{ cardInfo.cardName }}</div>
            <div class="card-content">{{ cardInfo.cardDesc }}</div>
            <ul class="card-list">
                <li>
                    <span class="label">{{ t(`select-card.kycRequire`) }}</span>
                    <span>{{ cardInfo.kycRequire ? t('select-card.kycRequiredDesc') : t('select-card.kycRequiredDesc2') }}</span>
                </li>
                <li>
                    <span class="label">{{ t('select-card.currency') }}</span>
                    <span>{{ cardInfo?.rechargeCurrencyInfoList[0]?.currency }}</span>
                </li>
                <li>
                    <span class="label">{{ t('select-card.needPhotoForActiveCard') }}</span>
                    <span>{{
                        cardInfo.needPhotoForActiveCard ? t('select-card.needPhotoForActiveCardDesc') : t('select-card.needPhotoForActiveCardDesc2')
                    }}</span>
                </li>
                <li>
                    <span class="label">{{ t('select-card.needPhotoForOperateCard') }}</span>
                    <span>{{ cardInfo.kycRequire ? t('select-card.kycRequiredDesc') : t('select-card.kycRequiredDesc2') }}</span>
                </li>
                <li>
                    <span class="label">{{ t('select-card.status') }}</span>
                    <span>{{ cardInfo.status ? t('select-card.statusDesc') : t('select-card.statusDesc2') }}</span>
                </li>
                <li class="one">
                    <span class="label">{{ t('select-card.price') }}</span>
                    <span>{{ t('select-card.free') }}</span>
                </li>
            </ul>
        </div>

        <div class="fixed-btn" v-if="cardInfo.kycStatus === 2 && cardInfo.applyStatus === null">
            <div class="cwg-button">
                <van-button type="primary" block @click="handleApply(3, cardInfo)">{{ t('cards.p7') }}</van-button>
            </div>
        </div>

        <div class="fixed-btn" v-if="[null, -1, 1, 3].includes(cardInfo.kycStatus)">
            <div class="cwg-button">
                <van-button type="primary" block @click="handleApply(3, cardInfo)">{{ t('cards.p7') }}</van-button>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import useUserStore from '@/stores/use-user-store'
const userStore = useUserStore()
const cardList = computed(() => userStore.userCard)
const { t } = useI18n()
const cardInfo = ref({})
const showCardNo = ref<{ [key: string]: boolean }>({})
const isFlipping = ref<{ [key: string]: boolean }>({})
const isShowBtn = ref(false)
const dateRange = ref<[string, string] | undefined>(undefined)
dateRange.value = ['', '']
const props = defineProps({
    cardTypeId: String,
    type: String,
})
async function getKycList() {
    let b = cardList.value.filter((i) => {
        return i.kycStatus == null
    })
    if (b.length != 0) {
        isShowBtn.value = true
    } else {
        isShowBtn.value = false
    }
    let filtered = cardList.value
    if (props.cardTypeId) {
        filtered = cardList.value.filter((item) => item.cardTypeId == props.cardTypeId)
    }
    cardInfo.value = filtered[0]
}
const statusMap: Record<string, string> = {
    null: t('kyc.statusDesc'),
    '-1': t('kyc.statusDesc'),
    '1': t('kyc.statusDesc2'),
    '2': t('kyc.statusDesc3'),
    '3': t('kyc.statusDesc4'),
}
const applyStatusMap: Record<string, string> = {
    '0': t('apply-record-detail.p8'),
    '1': t('apply-record-detail.p6'),
}
function statusClass(status: number) {
    switch (status) {
        case 2:
            return 'status-default status-success'
        case 3:
            return 'status-default status-error'
        default:
            return 'status-default'
    }
}
function statusClass1(status: number) {
    switch (status) {
        case 1:
            return 'status-default status-success'
        default:
            return 'status-default'
    }
}

const toggleCardNo = (cardId: string, event: MouseEvent) => {
    event.stopPropagation()
    isFlipping.value[cardId] = !isFlipping.value[cardId]
    showCardNo.value[cardId] = !showCardNo.value[cardId]
}

onMounted(async () => {
    cardList.value.forEach((card) => {
        showCardNo.value[card.id] = false
        isFlipping.value[card.id] = false
    })
    getKycList()
})
</script>

<style scoped lang="scss">
.page {
    padding: 16px 16px 150px 16px;
    box-sizing: border-box;
    min-height: 100vh;
    background: var(--main-bg);
}
.page1 {
    padding: 16px;
}
.card-wrapper {
    position: absolute;
    width: 100%;
    height: 100%;
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform;
}

.card-info {
    background: url(/src/assets/images/visa.png) no-repeat center center;
    background-size: cover;
    border-radius: 0.426667rem;
    padding: 0.64rem 0.533333rem 0 0.533333rem;
    color: var(--main-yellow);
    box-shadow: 0 0.106667rem 0.533333rem rgba(214, 255, 0, 0.1);
    border: 1px solid rgba(214, 255, 0, 0.2);
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: baseline;
    flex-wrap: wrap;
    transform-style: preserve-3d;
    transition: transform 0.6s ease;
    flex-direction: column;

    &.flipping {
        transform: rotateY(180deg);
    }

    .card-b {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        span {
            display: block;
        }
        .valid {
            font-size: var(--font-size-14);
            font-weight: 500;
            color: var(--black);
            text-shadow: 0 0 8px rgba(214, 255, 0, 0.2);
            gap: 8px;
            padding-right: 30px;
            line-height: 20px;
        }
        .lable {
            font-size: var(--font-size-10);
            font-weight: 400;
        }
    }
}

.card-front,
.card-back {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding: 24px 20px 16px 20px;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.card-back {
    transform: rotateY(180deg);
}

.owner {
    font-size: var(--font-size-14);
    line-height: 2;
    margin-bottom: 8px;
    text-shadow: 0 0 10px rgba(214, 255, 0, 0.3);
    display: flex;
    align-items: center;
    gap: 8px;

    i {
        font-size: var(--font-size-18);
        color: var(--main-yellow);
    }
}

.number {
    color: var(--black);
    font-size: var(--font-size-18);
    font-weight: 500;
    line-height: 3;
    letter-spacing: 2px;
    margin: 24px 0;
    text-shadow: 0 0 15px rgba(214, 255, 0, 0.4);
    display: flex;
    align-items: center;
    gap: 8px;
}

.actions {
    display: flex;
    justify-content: space-between;
    margin: 20px 0 16px 0;
}

.action-btn {
    color: var(--white);
    border: none;
    border-radius: 12px;
    padding: 10px 2px;
    font-size: var(--font-size-14);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    transition: all 0.3s ease;
    text-align: center;
    color: #1a1a1a;

    .btn-icon {
        width: 46px;
        height: 46px;
        border-radius: 50%;
        background: #ff4766;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    i {
        display: inline-block;
        line-height: 46px;
        width: 22px;
        height: 22px;
        color: var(--black);
        margin-bottom: 4px;
    }
}

.balance-wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 30px 0;
    font-size: var(--font-size-14);
}

.balance-content {
    font-size: var(--font-size-20);
    color: var(--white);
    font-weight: bold;
    margin-bottom: 24px;
}

.balance-title {
    font-size: var(--font-size-12);
    color: var(--white);
}

.currency {
    display: flex;
    align-items: center;
    font-size: var(--font-size-14);
    margin-right: 12px;
    color: var(--white);
}

.flag {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-right: 6px;
}

.balance {
    font-size: var(--font-size-14);
    font-weight: bold;
    color: var(--white);
}

.transactions {
    border-radius: 16px;
    margin-bottom: 16px;
    padding: 16px 0;
}
.trans-icon {
    width: 40px;
    height: 40px;
    display: flex;
    background: var(--main-bg);
    box-shadow: 0 4px 12px rgba(214, 255, 0, 0.1);
    border-radius: 8px;
    margin-right: 12px;
    align-items: center;
    justify-content: center;
    .trans-icon-inner {
        width: 36px;
        height: 36px;
        background: rgba(212, 206, 206, 0.24);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    i {
        color: #000;
        width: 18px;
        height: 18px;
        font-size: var(--font-size-16);
        border-radius: 50%;
    }
}

.trans-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    color: var(--white);
    i {
        font-size: var(--font-size-20);
        cursor: pointer;
    }
}
::v-deep .van-calendar {
    background: var(--action-bg);
}
::v-deep .van-calendar__month-mark {
    display: none;
}
::v-deep .van-calendar__header-subtitle {
    color: var(--white);
}
::v-deep .van-calendar__header-title {
    color: var(--white);
}
::v-deep .van-calendar__month-title {
    color: var(--main-yellow);
}

.trans-title {
    font-size: var(--font-size-20);
    color: var(--white);
    font-weight: bold;
}

.date-field {
    width: 200px;

    :deep(.van-field__control) {
        color: var(--white);
    }

    :deep(.van-field__placeholder) {
        color: var(--gray);
    }
}

:deep(.van-popup) {
    background: var(--action-bg);
}

:deep(.van-picker__toolbar) {
    background: var(--action-bg);
    border-bottom: 1px solid var(--border);
}

:deep(.van-picker__title) {
    color: var(--white);
}

:deep(.van-picker__confirm) {
    color: var(--main-yellow);
}

:deep(.van-picker__cancel) {
    color: var(--gray);
}

:deep(.van-picker-column) {
    color: var(--white);
}

:deep(.van-picker-column__item) {
    color: var(--white);
}

:deep(.van-picker-column__item--selected) {
    color: var(--main-yellow);
}

.transaction {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    /* border-bottom: 1px solid var(--border); */
    font-size: var(--font-size-16);
}

.transaction:last-child {
    border-bottom: none;
}

.trans-left {
    width: 200px;
}

.trans-right {
    width: 100px;

    div {
        text-align: right;
    }
}

.trans-type {
    color: var(--white);
    font-size: var(--font-size-14);
    line-height: 2;
}

.trans-desc {
    font-size: var(--font-size-12);
    color: var(--gray);
}

.trans-amount {
    font-size: var(--font-size-14);
    color: var(--white);
    line-height: 2;
}

.trans-date {
    color: var(--gray);
    font-size: var(--font-size-12);
}

.card-swiper {
    position: relative;
    overflow: hidden;
    margin-bottom: 20px;
    padding-bottom: 5px;
}

.swiper-container {
    position: relative;
    width: 100%;
    height: 209px;
    touch-action: pan-y pinch-zoom;
    user-select: none;
}

.card-info {
    position: absolute;
    width: 100%;
    height: 100%;
    transition: transform 0.3s ease;
    will-change: transform;
}

.swiper-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 16px;
    gap: 16px;
}

.swiper-btn {
    background: var(--action-bg);
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--main-yellow);

    &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }

    i {
        font-size: var(--font-size-20);
    }
}

.swiper-dots {
    display: flex;
    gap: 8px;
}

.dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--action-bg);
    cursor: pointer;
    transition: all 0.3s ease;

    &.active {
        background: var(--main-yellow);
        transform: scale(1.2);
    }
}

.swiper-indicators {
    display: flex;
    justify-content: center;
    margin-top: 16px;
    gap: 8px;
}

.indicator-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--action-bg);
    cursor: pointer;
    transition: all 0.3s ease;

    &.active {
        background: var(--main-yellow);
        transform: scale(1.2);
    }
}
.flags {
    width: 20px;
    height: 20px;
    cursor: pointer;
    position: absolute;
    top: 10px;
    right: 10px;
}

.cwg-btn {
    margin: 36px 0 80px 0;
}

.card-info1 {
    width: 100%;
}

.card-chip {
    position: absolute;
    right: 12px;
    top: 16px;
    width: 18px;
    height: 12px;
    background: linear-gradient(145deg, #f5f5f5, #eaeaea);
    border-radius: 3px;
    border: 1px solid #bbb;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.card-title {
    margin: 24px 0 12px 0;
    font-weight: 600;
    font-size: 22px;
    line-height: 28px;
    text-align: left;
    color: #1a1a1a;
}

.card-list {
    list-style: none;
    padding: 12px 0;
    margin: 0;
    width: 100%;
    flex: 1;
    overflow-y: auto;
    box-shadow: 0 4px 20px rgba(140, 145, 143, 0.194);
    border-radius: 10px;
}

.card-list li {
    display: flex;
    justify-content: space-between;
    font-size: var(--font-size-12);
    color: #1a1a1a;
    padding: 12px 24px;
    transition: all 0.3s ease;
    position: relative;
    font-family: 'Roboto';
    font-style: normal;
    font-weight: 600;
    font-size: 14px;
    line-height: 20px;
}

.label {
    font-weight: 600;
    font-size: 14px;
    line-height: 20px;
    color: #8e8a8a;
}

.card-list li:hover {
    background: rgba(73, 247, 166, 0.1);
    border-radius: 6px;
}

.card-list li:last-child {
    margin-bottom: 0;
}

.card-list li span:first-child {
    color: #bbb;
}

.card-content {
    font-size: var(--font-size-16);
    color: #474747;
    margin: 0 0 26px 0;
    text-align: left;
    line-height: 24px;
}
.a1 {
    flex: 1;
}
.card-list {
    .one {
        margin: 12px 24px;
        padding: 12px 0;
        border-top: 1px dashed #beb6b6;
        color: #329644;
        font-family: Roboto;
        font-size: 28px;
        font-style: normal;
        font-weight: 700;
        line-height: 36px;
        align-items: flex-end;
        .label {
            color: #343434;
            font-family: Roboto;
            font-size: 16px;
            font-style: normal;
            font-weight: 600;
            line-height: 24px;
        }
    }
}
</style>
