<!--
 * @Description: 文档组件
 * @Author: 周洋
 * @Date: 2020-06-29 14:29:08
 * @LastEditTime: 2021-05-12 17:26:39
 * @LastEditors: Please set LastEditors
 * @FilePath: \cms-components\see-document.vue
-->
<template>
    <div
        class="see-document"
        v-if="isShow"
        :class="{ 'is-pc': !isMobile, 'is-mobile': isMobile }"
    >
        <template v-if="isMobile">
            <div
                class="document-item"
                v-for="(item, index) in componentData.group"
                :key="index"
            >
                <div class="document-item__img">
                    <base-image class="image" :src="item.coverPic"></base-image>
                </div>
                <div class="document-item__info">
                    <h2 :title="item.title">
                        {{ item.title || (isDesign ? "标题" : "") }}
                    </h2>
                    <p class="desc">
                        {{
                            item.desc ||
                            (isDesign ? "概要说明(不超过50个汉字)" : "")
                        }}
                    </p>
                    <div class="btn-group">
                        <p
                            class="btn download-btn"
                            @click="download(item.resourceUrl)"
                        >
                            下载
                        </p>
                        <span
                            class="btn read-btn"
                            v-if="isOfficeOrPDF(item.resourceUrl)"
                            @click="filePreview(item)"
                            >在线预览 ></span
                        >
                    </div>
                </div>
            </div>
        </template>
        <template v-else>
            <div
                class="control left"
                :class="{ 'not-allowed': curIndex === 0 }"
                @click="changeIndex('left')"
                v-show="
                    componentData.group &&
                    componentData.group.length > 3 &&
                    componentData.playType == 1
                "
            ></div>
            <div
                :class="{
                    'shuffling-layout': componentData.playType == 1,
                    'tile-layout': componentData.playType == 2,
                }"
            >
                <ul class="document-container" :style="translateX">
                    <li
                        class="document-item"
                        v-for="(item, index) in componentData.group"
                        :key="index"
                    >
                        <div class="document-item__img">
                            <base-image
                                class="image"
                                :src="item.coverPic"
                            ></base-image>
                        </div>
                        <h2 :title="item.title">
                            {{ item.title || (isDesign ? "标题" : "") }}
                        </h2>
                        <p class="document-item__desc" :title="item.desc">
                            {{
                                item.desc ||
                                (isDesign ? "概要说明(不超过50个字符)" : "")
                            }}
                        </p>
                        <div class="btn-group">
                            <!-- <span
                                class="btn read-btn"
                                v-if="isPdf(item.resourceUrl)"
                                @click="read(item.resourceUrl)"
                                >在线预览 ></span
                            > -->
                            <span
                                class="btn read-btn"
                                v-if="isOfficeOrPDF(item.resourceUrl)"
                                @click="filePreview(item)"
                                >在线预览 ></span
                            >
                            <p
                                class="btn download-btn"
                                @click="download(item.resourceUrl)"
                            >
                                下载 >
                            </p>
                        </div>
                    </li>
                </ul>
            </div>
            <div
                class="control right"
                :class="{
                    'not-allowed':
                        Math.abs(curIndex) === componentData.group.length - 3,
                }"
                @click="changeIndex('right')"
                v-show="
                    componentData.group &&
                    componentData.group.length > 3 &&
                    componentData.playType == 1
                "
            ></div>
        </template>
    </div>
</template>

<script>
import BaseImage from "./components/base-image";
import btnMixin from "./button-mixin";
import permissionMixin from "./permission-mixin";
export default {
    name: "see-document",
    mixins: [btnMixin, permissionMixin],
    props: {
        componentData: {
            required: false,
        },
    },
    components: { BaseImage },
    data() {
        return {
            curIndex: 0,
        };
    },
    methods: {
        read(url) {
            if (!url) return;
            this._mixJumpClick(!this.isMobile, url);
        },
        changeIndex(sign) {
            if (sign === "left") {
                if (this.curIndex === 0) return;
                else ++this.curIndex;
            } else if (sign === "right") {
                if (
                    Math.abs(this.curIndex) ===
                    this.componentData.group.length - 3
                )
                    return;
                else --this.curIndex;
            }
        },
        download(url) {
            if (!url) return;
            let downloadUrl = url + "?response-content-disposition=attachment";
            this._mixJumpClick(!this.isMobile, downloadUrl);
        },
        isPdf(resourceUrl) {
            if (!resourceUrl) return false;
            let pdfReg = /\w(\.pdf)/i;
            if (pdfReg.test(resourceUrl)) return true;
            else return false;
        },
        isOfficeOrPDF(resourceUrl) {
            let fileExtension = resourceUrl.split('.').pop().toLowerCase()
            switch (fileExtension) {
                case 'pdf':
                    return true
                case 'doc':
                    return true
                case 'docx':
                    return true
                case 'xls':
                    return true
                case 'xlsx':
                    return true
                case 'ppt':
                    return true
                case 'pptx':
                    return true
                default:
                    return false
            }
        },
        getOfficeServe() {
            const devOfficeServe = 'https://devofficepre.seeyoncloud.com'
            const testOfficeServe = 'https://testofficepre.seeyoncloud.com'
            const vpordOfficeServe = 'https://vprodofficepre.seeyoncloud.com'
            const officeServe = 'https://officepre.seeyoncloud.com'

            let host = window.location.host
            if (host == 'testcloud.seeyon.com') {
                return testOfficeServe
            } else if (host == 'devcloud.seeyon.com') {
                return devOfficeServe
            } else if (host == 'vprodcloud.seeyon.com') {
                return vpordOfficeServe
            } else if (host == 'cloud.seeyon.com') {
                return officeServe
            } else {
                return testOfficeServe
            }
        },
        filePreview(item) {
            window.open(this.getOfficeServe() + '/onlinePreview?url='+item.resourceUrl);
        }
    },
    created() {},
    mounted() {},
    computed: {
        isMobile() {
            return this.componentData.commonVariable.isMobile;
        },
        isDesign() {
            return this.componentData.commonVariable.pageType === "design";
        },
        translateX() {
            return {
                transform: `translateX(calc(((100% - 40px) / 3 + 20px) * ${this.curIndex}))`,
            };
        },
    },
    watch: {},
};
</script>

<style lang='scss' scoped>
p {
    margin: 0;
    padding: 0;
}
.is-mobile {
    .document-item {
        width: 100%;
        padding: 22px 20px;
        display: flex;
        align-items: center;
        background: #ffffff;
        border: 1px solid #ececec;
        border-radius: 3px;
        &:not(:last-child) {
            margin-bottom: 12px;
        }
        &__img {
            width: 84px;
            height: 84px;
            overflow: hidden;
            text-align: center;
            // background: #CCCCCC;
            & .image {
                width: 100%;
                height: 100%;
            }
        }
        &__info {
            margin-left: 24px;
            flex: 1;
            h2 {
                font-size: 14px;
                color: #333333;
                line-height: 20px;
                margin-bottom: 4px;
                font-weight: bold;
                width: 100%;
                overflow: hidden;
                -ms-text-overflow: ellipsis;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            .desc {
                font-size: 10px;
                color: #999999;
                line-height: 16px;
                margin-bottom: 8px;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
            }
            .btn-group {
                display: flex;
                align-items: center;
                .btn {
                    font-size: 12px;
                    border-radius: 2px;
                    cursor: pointer;
                }
                .download-btn {
                    width: 78px;
                    height: 24px;
                    text-align: center;
                    line-height: 24px;
                    background: #007aff;
                    color: #fff;
                    margin-right: 10px;
                }
                .read-btn {
                    width: 110px;
                    height: 24px;
                    color: #007aff;
                    text-align: center;
                    line-height: 22px;
                    background: #ffffff;
                    border: 1px solid #ececec;
                }
            }
        }
    }
}
.is-pc {
    position: relative;
    .control {
        width: 27px;
        height: 44px;
        background-repeat: no-repeat;
        background-size: 17px 17px;
        background-position: center;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
        z-index: 100;
    }
    .left {
        left: -30px;
        background-image: url("./assets/arrow-left.svg");
    }
    .right {
        right: -30px;
        background-image: url("./assets/arrow-right.svg");
    }
    .shuffling-layout {
        width: 100%;
        margin: 0 auto;
        overflow: hidden;
        .document-container {
            display: flex;
            flex-wrap: nowrap;
            transition: all ease-in-out 0.5s;
        }
    }
    .tile-layout {
        width: 100%;
        overflow: inherit;
        .document-container {
            display: flex;
            flex-wrap: wrap;
            transform: translateX(0) !important;
            .document-item {
                margin: 0;
                width: calc((100% - 40px) / 3);
                transition: all 0.2s;
                &:nth-child(3n-1) {
                    margin: 0 20px;
                }
                &:nth-child(n + 4) {
                    margin-top: 20px;
                }
                &:hover {
                    box-shadow: 0 10px 25px 0 #e1e5ed;
                }
            }
        }
    }
    ul,
    li {
        list-style: none;
        padding: 0;
        margin: 0;
    }
    .document-item {
        border-radius: 4px;
        // margin: 0 10px;
        box-shadow: 0 4px 12px 0 rgba(0,0,0,0.08);
        text-align: center;
        padding: 48px 20px 17px;
        flex-grow: 0;
        flex-shrink: 0;
        width: calc((100% - 40px) / 3);
        background: #ffffff;
        border: 1px solid #ececec;
        &:not(:last-child) {
            margin-right: 16px;
        }
        &__img {
            width: 120px;
            height: 120px;
            border-radius: 6px;
            margin: 0 auto 16px;
            overflow: hidden;
            text-align: center;
            // background: #CCCCCC;
            & .image {
                width: 100%;
                height: 100%;
            }
        }
        h2 {
            font-size: 18px;
            color: #333333;
            line-height: 20px;
            font-weight: bold;
            margin-bottom: 10px;
            width: 100%;
            overflow: hidden;
            -ms-text-overflow: ellipsis;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        &__desc {
            height: 44px;
            text-align: left;
            font-size: 14px;
            color: #999999;
            line-height: 22px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
        }
        .btn-group {
            margin-top: 22px;
            padding-top: 16px;
            border-top: 1px solid #dedede;
            display: flex;
            justify-content: space-between;
            & .btn {
                font-size: 12px;
                cursor: pointer;
                color: #3385ff;
                line-height: 14px;
            }
        }
    }
}
.not-allowed {
    cursor: default !important;
    opacity: 0.35;
    pointer-events: none;
}
</style>
