<template>
    <div class="electronic-preview-container" ref="mainBoxRef">
        <!-- 顶部导航 -->
        <div class="nav-bar">
            <span class="close-btn">✕</span>
            <span class="nav-title">工资条详情</span>
            <span class="more-btn">···</span>
        </div>

        <!-- 卡片内容区域 -->
        <div class="content-card">
            <!-- 头部金额 -->
            <div class="salary-header">
                <div class="amount-wrap">
                    <span class="currency">￥</span>
                    <span class="amount">{{ formData.netSalary }}</span>
                </div>
                <div class="desc">实发工资</div>
            </div>

            <div class="divider"></div>

            <!-- 详情列表 -->
            <div class="detail-list">
                <div class="list-item">
                    <span class="label">姓名</span>
                    <span class="value">{{ formData.name }}</span>
                </div>
                <div class="list-item">
                    <span class="label">任职部门</span>
                    <span class="value">{{ formData.department }}</span>
                </div>
                <div class="list-item">
                    <span class="label">职务</span>
                    <span class="value">{{ formData.job }}</span>
                </div>
                <div class="list-item">
                    <span class="label">专员薪酬标准</span>
                    <span class="value">{{ formData.standardSalary }}</span>
                </div>
                <div class="list-item">
                    <span class="label">应出勤天数</span>
                    <span class="value">{{ formData.payableAttendanceDays }}</span>
                </div>
                <div class="list-item">
                    <span class="label">事假</span>
                    <span class="value">{{ formData.personalLeaveDays }}</span>
                </div>
                <div class="list-item">
                    <span class="label">病假</span>
                    <span class="value">{{ formData.sickLeaveDays }}</span>
                </div>
                <div class="list-item">
                    <span class="label">实出勤天数</span>
                    <span class="value">{{ formData.actualAttendanceDays }}</span>
                </div>
                <div class="list-item">
                    <span class="label">补贴</span>
                    <span class="value">{{ formData.allowance }}</span>
                </div>
                <div class="list-item">
                    <span class="label">绩效</span>
                    <span class="value">{{ formData.performance }}</span>
                </div>
                <div class="list-item">
                    <span class="label">应发合计</span>
                    <span class="value">{{ formData.totalPayable }}</span>
                </div>
                <div class="list-item">
                    <span class="label">个人社保代扣</span>
                    <span class="value">{{ formData.socialInsurance }}</span>
                </div>
                <div class="list-item">
                    <span class="label">个人公积金代扣</span>
                    <span class="value">{{ formData.housingFund }}</span>
                </div>
                <div class="list-item">
                    <span class="label">个人所得税代扣</span>
                    <span class="value">{{ formData.tax }}</span>
                </div>
                <div class="list-item">
                    <span class="label">应扣事假</span>
                    <span class="value">{{ formData.personalLeaveDeduction }}</span>
                </div>
                <div class="list-item">
                    <span class="label">应扣病假</span>
                    <span class="value">{{ formData.sickLeaveDeduction }}</span>
                </div>
                <div class="list-item">
                    <span class="label">其他扣款</span>
                    <span class="value">{{ formData.otherDeduction }}</span>
                </div>
                <div class="list-item">
                    <span class="label">应扣合计</span>
                    <span class="value">{{ formData.totalDeduction }}</span>
                </div>
                <div class="list-item">
                    <span class="label">实发工资</span>
                    <span class="value">{{ formData.netSalary }}</span>
                </div>
                <div class="list-item">
                    <span class="label">备注</span>
                    <span class="value">{{ formData.remark }}</span>
                </div>
            </div>

            <div class="divider"></div>

            <!-- 底部按钮 -->
            <div class="action-footer">
                <div class="btn outline-btn">对工资有疑问</div>
                <div class="btn primary-btn">签名确认</div>
            </div>
        </div>

        <div v-if="!isVip" class="watermark-overlay"></div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/stores/user'
import { useWatermarkGuard } from '@/hooks/useWatermarkGuard'

const props = defineProps({
    formData: {
        type: Object,
        required: true
    }
})

const userStore = useUserStore()
const { isVip } = storeToRefs(userStore)
const mainBoxRef = ref(null)

// VIP 强制水印
useWatermarkGuard(mainBoxRef, isVip)

// 动态背景水印（姓名+1234）
const watermarkUrl = computed(() => {
    const text = (props.formData.name || '') + '1234'
    const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="140" height="100">
        <text x="0" y="50" transform="rotate(340)" fill="rgba(46,46,46,0.25)" font-size="12" font-family="sans-serif">${text}</text>
    </svg>`
    const encoded = typeof window !== 'undefined' ? btoa(unescape(encodeURIComponent(svg))) : ''
    return `url("data:image/svg+xml;base64,${encoded}")`
})

defineExpose({
    mainBoxRef
})
</script>

<style scoped lang="scss">
.electronic-preview-container {
    width: 375px;
    // height: 667px;
    background-color: #F6F8FA;
    margin: 0 259px;
    position: relative;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    margin-bottom: 40px;
    // overflow: hidden;
    display: flex;
    flex-direction: column;

    .nav-bar {
        height: 44px;
        background-color: #EFEFEF;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 16px;
        flex-shrink: 0;

        .close-btn {
            font-size: 20px;
            color: #333;
            font-weight: 300;
        }

        .nav-title {
            font-weight: 500;
            font-size: 16px;
            color: #373737;
            line-height: 20px;
            text-shadow: 0 0 0.5px #373737;
        }

        .more-btn {
            font-size: 20px;
            color: #333;
            font-weight: bold;
            letter-spacing: 2px;
        }
    }

    .content-card {
        margin: 12px 18px 34px;
        background-color: #FFFFFF;
        border-radius: 6px;
        padding: 24px 12px 21px;
        flex: 1;
        overflow-y: auto;

        /* 隐藏滚动条 */
        &::-webkit-scrollbar {
            display: none;
        }

        -ms-overflow-style: none;
        scrollbar-width: none;

        /* 水印背景 */
        background-repeat: repeat;
        background-position: center;

        .salary-header {
            text-align: center;
            margin-bottom: 24px;

            .amount-wrap {
                font-size: 24px;
                color: #373737;
                line-height: 24px;
                font-weight: 700;

                .currency {
                    margin-right: 4px;
                }

                .amount {
                    font-size: 28px;
                }
            }

            .desc {
                font-size: 12px;
                color: #767676;
                margin-top: 8px;
                line-height: 12px;
            }
        }

        .divider {
            height: 1px;
            border-top: 2px dotted #F2F2F2;
            margin: 0;
        }

        .detail-list {
            padding: 12px 0 24px;
            display: flex;
            flex-direction: column;
            gap: 22px;

            .list-item {
                display: flex;
                justify-content: space-between;
                align-items: flex-start;
                font-size: 14px;
                line-height: 14px;
                padding: 0 12px;
                color: #373737;

                .label {
                    flex-shrink: 0;
                    margin-right: 16px;
                }

                .value {
                    text-align: right;
                    word-break: break-all;
                }
            }
        }

        .action-footer {
            padding-top: 60px;
            display: flex;
            justify-content: space-between;
            gap: 14px;

            .btn {
                flex: 1;
                height: 38px;
                border-radius: 4px;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 14px;
                line-height: 14px;
            }

            .outline-btn {
                color: #3B9BDF;
                border: 1px solid #3B9BDF;
                background-color: #FFFFFF;

            }

            .primary-btn {
                color: #FFFFFF;
                background-color: #3B9BDF;
                border: 1px solid #3B9BDF;
            }
        }
    }

    .watermark-overlay {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-image: url('@/public/common/watermaker.png');
        background-size: 125px 125px;
        background-repeat: repeat;
        pointer-events: none;
        z-index: 10;
    }
}
</style>
