<template>
    <div>
        <div style="margin:100px auto 10px auto;text-align: center">
            <img src="../../../assets/file-down.png" height="100">
            <p style="margin:10px auto">{{fileName}}</p>
            <p style="color:#aaa;"> {{ctime}}</p>
        </div>
        <div class="bottom-btn" style="padding: 20px">
            <van-button block type="info" @click="_view">预览</van-button>

        </div>
    </div>
</template>

<script>

    export default {
        data() {
            return {
                fileName: '',
                id: '',
                ctime: '',
            }
        },
        mounted() {
            this._getQuery();
        },
        methods: {
            _getQuery() {
                this.fileName = this.$route.query.fileName;
                this.id = this.$route.query.id;
                this.ctime = this.$dateformat(this.$route.query.ctime,'yyyy/mm/dd');
            },
            //下载
            _ok() {
                this.$toast.loading({
                    message: '下载中...',
                    forbidClick: true,
                });
                var dtask = plus.downloader.createDownload(
                    this.$global.baseURL + '/drs/v1/sampling_plan_attachment/down?id=' + this.id,
                    {}, (d, status) => {
                        // 下载完成
                        if (status == 200) {
                            this.$toast.clear();
                            this.$toast.success({message: '下载到' + d.filename, duration: 4000});
                            this.$router.go(-1);
                        } else {
                            this.$toast.clear();
                            this.$toast.error('下载失败');
                        }
                    });
                dtask.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                dtask.setRequestHeader('accessToken', localStorage.getItem('accessToken'));
                dtask.start();
            },
            //下载并预览文档
            _view() {
                //调用第三放程序打开指定文件（必须是本地路径）
                this.$toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                });
                var dtask = plus.downloader.createDownload(
                    this.$global.baseURL + '/drs/v1/sampling_plan_attachment/down?id=' + this.id, {}, (d, status) => {
                        if (status == 200) {
                            plus.runtime.openFile(d.filename);//cn.wps.moffice_eng-----wps的包名
                        }
                        this.$toast.clear();
                    });
                dtask.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                dtask.setRequestHeader('accessToken', localStorage.getItem('accessToken'));
                dtask.start();
            },
        }
    }
</script>
