<template>
  <div class="app-upload-file-info">
      <a class="file-item" v-for="file in files" @click="() => onDownload(file)"><i class="el-icon-document"></i>{{file.name}}</a>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator';
import { Environment } from '@/environments/environment';
import { CreateElement } from 'vue';
import { Subject, Unsubscribable } from 'rxjs';
import EntityService from '@/service/entity-service';
import { safeRedirect } from '@/utils';

@Component({
})
export default class AppUploadFileInfo extends Vue {

    /**
     * 初始化值
     *
     * @type {*}
     * @memberof AppUploadFileInfo
     */
    @Prop() public value?: any;

    /**
     * 数据值变化
     *
     * @param {*} newval
     * @param {*} val
     * @memberof AppUploadFileInfo
     */
    @Watch('value')
    onValueChange(newval: any, val: any) {
        this.dataProcess();
    }

    /**
     * 所属表单项名称
     *
     * @type {string}
     * @memberof AppUploadFileInfo
     */
    @Prop() public name!: string;

    /**
     * 应用上下文
     *
     * @type {*}
     * @memberof AppUploadFileInfo
     */
    @Prop() public context!: any;

    /**
     * 实体服务
     *
     * @type {EntityService}
     * @memberof AppUploadFileInfo
     */
    @Prop() public appEntityService!: EntityService;

    /**
     * 默认为当前实体主键id，有指定则按表单参数
     *
     * @type {string}
     * @memberof AppUploadFileInfo
     */
    @Prop() public ownerid!: string;

    /**
     * 上传文件路径
     *
     * @memberof AppUploadFileInfo
     */
    public uploadUrl = Environment.BaseUrl + Environment.UploadFile;

    /**
     * 下载文件路径
     *
     * @memberof AppUploadFileInfo
     */
    public downloadUrl = Environment.BaseUrl + Environment.ExportFile;

    /**
     * 文件列表
     *
     * @memberof AppUploadFileInfo
     */
    public files = [];

    /**
     * 数据处理
     *
     * @private
     * @memberof AppUploadFileInfo
     */
    private dataProcess(): void {
        if(this.value){
            let files = JSON.parse(this.value);
            if(files.length){
                files.forEach((file: any) => {
                    let url = `${this.downloadUrl}/${file.id}`;
                    const entityName = this.appEntityService.APPDENAME;
                    const base64 = `${file.id}|${entityName}|${this.ownerid}|${this.context.srfpersonid || this.context.srfuserid}`;
                    const downloadUrl =`${url}?key=${window.btoa(base64)}${Math.floor(
                        1000 + Math.random() * 9000,
                    )}`
                    file.url = downloadUrl;
                });
            }else{
              files = []
            }
            this.files =files;
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof AppUploadFileInfo
     */
    public created() {
        this.dataProcess();
    }


    /**
     * 下载文件
     *
     * @param {*} file
     * @memberof AppUploadFileInfo
     */
    public onDownload(file: any) {
        safeRedirect(file.url, { type: '_blank'});
    }

}
</script>

<style lang='less'>
@import './app-upload-file-info.less';
</style>