<template>
    <el-dialog title="" :visible.sync="visible" width="620px" :before-close="handleClose">
        <div class="imgBox" v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.8)">
            <div class="img">
                <template v-if="showImg">
                    <img :src="src" alt="" v-for="(src, i) in images" :key="`show_image_${i}`"
                        :class="{ hide: true, show: index === i }" @load="handleLoad(i)">
                </template>
            </div>
            <div class="message">
                <strong v-html="$t(`ImageBasedSurgicalPlanning.${data.key}.series_${data.id}`)"></strong>
                <p v-html="$t(`ImageBasedSurgicalPlanning.${data.key}.clinical_${data.id}`)"></p>
            </div>
        </div>
    </el-dialog>
</template>
<script>
export default {
    props: {
        visible: {
            type: Boolean,
            default: false
        },
        images: {
            type: Array,
            default: () => {
                return []
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        }
    },
    data() {
        return {
            index: 0,
            loading: false,
            timer: null,
            showImg: false,
            flag: []
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        handleClose() {
            this.$emit("update:visible", false)
        },
        handleLoad(i) {
            this.flag[i] = true
            let f = this.flag.every(item => item)
            if (f) {
                this.setTimer()
            }
            this.loading = false
        },
        init() {
            this.loading = true
            this.flag = []
            this.images.forEach(() => {
                this.flag.push(false)
            })
            this.showImg = true
        },
        setTimer() {
            this.clearTimer()
            this.timer = setInterval(() => {
                if (this.index < this.images.length - 1) {
                    this.index++
                } else {
                    this.index = 0
                }
            }, 500)
        },
        clearTimer() {
            if (this.timer) {
                clearInterval(this.timer)
                this.timer = null
            }
        }
    },
    destroyed() {
        this.clearTimer()
    }

}
</script>
<style lang="scss" scoped>
.hide {
    display: none;
}

.show {
    display: block;
}

::v-deep .el-dialog__header {
    border: none;
    margin: 0;
    padding: 0;
}

::v-deep .el-dialog__headerbtn {
    line-height: 30px;
    text-align: center;
    color: #333;
    top: 0px;
    right: -30px;
    width: 30px;
    height: 30px;
    background: #ccc;
    font-size: 16px;
    cursor: pointer;
}

::v-deep .el-dialog__body {
    background-color: #333;
    padding: 0;
    margin: 0;
}

.imgBox {
    display: flex;

    .img {
        width: 400px;
        height: 400px;
        border: none;
        box-sizing: border-box;
        line-height: 1;

        img {
            width: 100%;
            height: 100%;
        }
    }

    .message {
        width: 220px;
        color: #fff;

        strong {
            font-size: 14px;
            line-height: 25px;
            display: block;
            padding: 27px 10px 0 18px;
        }

        p {
            font-size: 12px;
            color: #aaa;
            line-height: 20px;
            padding: 17px 18px;
            text-align: left;
            word-break: keep-all;
        }
    }
}
</style>