<template>
<el-dialog v-model="dialogVisible" width="637" class="dialog-box" v-if="data"
    align-center
    destroy-on-close
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false">
    <template #header="{ close, titleId, titleClass }">
        <div class="msg-header" >
            <svg-icon icon-class="qrCode" class="header-icon" />
            <div class="header-title">{{ t('share.downloadQrCode') }}</div>
            <div class="header-close" >
            <el-icon color="rgba(196, 196, 196, 1)" @click="close" ><Close /></el-icon>
            </div>
        </div>
    </template>
    <div class="msg-body" >
        <div style="margin: 10px;">
            <div style="width: 100%;display: flex;justify-content: space-around;flex-wrap: wrap;">
                <el-image v-for="(item, index) in data.img_url.split(',')" :key="index" class="img"
                :class="{'img-selected': selected==index}" :src="item" fit="contain" @click="changeSize(index)" />
            </div>
        </div>
        <el-button @click="downLoadImg" style="width: 100%;" size="large" type="primary">{{ t('common.download') }}</el-button>
    </div>
</el-dialog>

</template>

<script setup>
import { useLocale } from 'element-plus'
import { ref, defineModel, watch } from 'vue';

const { proxy } = getCurrentInstance();
const { t } = useLocale()
const dialogVisible = defineModel("show")
const props = defineProps({
    data: Object
})
const selected = ref(0)
const loading = ref(false)

function changeSize(value) {
    selected.value = value
}

async function downLoadImg() {
    const response = await fetch(props.data.img_url.split(',')[selected.value])
    // 将响应体转换为Blob
    const blob = await response.blob();

    // 创建隐藏的a标签来触发下载
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = props.data.name; // 自定义下载的文件名
    link.click();
    document.body.removeChild(link);
}


watch(dialogVisible, (n, o) => {
    if(!n) {
        selected.value = 0
    }
})
</script>

<style lang="scss" scoped>

.qr-bg {
    margin-top: auto;
    background: linear-gradient(90deg, rgba(16, 55, 118, 1) 0%, rgba(146, 219, 237, 1) 100%);

    .img-wrap {
        width: 100%;
        height: 100%;
        background-color: #fff;
        cursor: pointer;
    }
}

.el-radio {
    margin-right: 0;
    font-size: 16px;
    justify-content: center;
}

.img {
    padding: 4px;
    width: 200px;
    height: 200px;
    cursor: pointer;
}

.img-selected {
    border: 1px solid #5cb8e0;
}

</style>

<style lang="scss">
.dialog-box {
    border-radius: 16px;
    padding: 0;

    .el-dialog__header {
        margin-right: 0;
        padding: 0;
        overflow: hidden;
    }

    .el-dialog__body {
        box-sizing: border-box;
        padding: 16px 22px;
        min-height: 250px;
    }

    .msg-header {
        border-radius: 16px 16px 0 0;
        width: 100%;
        height: 50px;
        position: relative;
        overflow: hidden;
        background-color: #0F3675;
        display: flex;
        align-items: center;
        color: #fff;

        .header-icon {
            width: 25px;
            height: 28px;
            margin-left: 22px;
            margin-right: 14px;
        }

        .header-title {
            font-size: 26px;
            font-weight: 700;
        }

        .header-close {
            cursor: pointer;
            position: absolute;
            bottom: 0;
            right: 20px;
            font-size: 32px;
        }
    }

    .msg-body {
        background-color: #ffffff;
    }
}
</style>