<template>
    <div class="app-file-upload">
      <el-row>
        <el-col v-if="rowPreview && files.length > 0" :span="12" class="upload-col">
            <el-button size='small' class="button-preview" icon='el-icon-view' :disabled="disabled" @click="()=>{this.dialogVisible = true;}">{{$t('components.appFileUpload.preview')}}<Badge :count="files.length" type="info"></Badge></el-button>
        </el-col>
        <el-col :span="(rowPreview && files.length > 0) ? 12 : 24" class="upload-col">
          <el-upload
            :class="{'has-uploadtip': !!uploadTip}"
            :disabled="disabled"
            :file-list="files"
            :action="getAction()"
            :headers="myHeaders"
            :before-upload="beforeUpload"
            :before-remove="onRemove"
            :on-success="onSuccess"
            :on-error="onError"
            :on-preview="onDownload"
            :drag="isdrag"
            :show-file-list="!rowPreview"
            >
                <el-button v-if="!isdrag" size='small' icon='el-icon-upload2' :disabled="disabled">{{this.$t('app.fileUpload.caption')}}</el-button>
                <i v-if="isdrag" class="el-icon-upload2"></i>
                <div v-if="isdrag" class="el-upload__text" v-safe-html="$t('components.appFileUpload.uploadText')"></div>
                <div class="upload-description">{{uploadTip}}</div>
            </el-upload>
        </el-col>
      </el-row>
      <modal width="80%" v-model="dialogVisible" footer-hide class-name='upload-preview-modal'>
        <ul class="">
          <li v-for="(file,index) in files" :key="index" class="preview-file-list-item">
            <div class='preview-file-list-img'>
              <el-image :src="file.url" class='' style=''>
                  <div slot='error' class='image-slot'>
                      <img src="@/assets/img/picture.png" style='width:100%;height:100%;'>
                  </div>
              </el-image>
              <div class='preview-file-list-actions' @mouseenter="()=>{showActions = true;}" @mouseleave="()=>{showActions = false;}">
                  <span v-show="showActions" class='action-download'>
                      <i class='el-icon-download' @click="onDownload(file)"></i>
                  </span>
                  <span v-show="showActions" :style="{ 'display': disabled? 'none' : 'inline-block' }" class='action-delete'>
                      <i class='el-icon-delete' @click="onRemove(file, files)"></i>
                  </span>
              </div>
            </div>
            <div class="file-name">{{file.name}}</div>
          </li>
        </ul>
      </modal>
    </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 Axios from 'axios';
import { Message } from 'element-ui';
import EntityService from '@/service/entity-service';

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

    /**
     * 表单状态
     *
     * @type {Subject<any>}
     * @memberof AppFileUpload
     */
    @Prop() public formState?: Subject<any>

    /**
     * 是否忽略表单项书香值变化
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    @Prop() public ignorefieldvaluechange?: boolean;

    /**
     * 是否支持拖拽
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    @Prop() public isdrag?: boolean;

    /**
     * 表单状态事件
     *
     * @private
     * @type {(Unsubscribable | undefined)}
     * @memberof AppFileUpload
     */
    private formStateEvent: Unsubscribable | undefined;

    /**
     * 表单数据
     *
     * @type {string}
     * @memberof AppFileUpload
     */
    @Prop() public data!: string;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppFormDRUIPart
     */
    @Prop() public viewparams!: any;

    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppAutocomplete
     */
    @Prop() public context!: any;

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

    /**
     * 数据值变化
     *
     * @param {*} newval
     * @param {*} val
     * @memberof AppFileUpload
     */
    @Watch('value')
    onValueChange(newval: any, val: any) {
        if (this.ignorefieldvaluechange) {
            return;
        }
        this.getParams();
        this.setFiles(newval);
        this.dataProcess();
    }

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

    /**
     * 是否禁用
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    @Prop() public disabled?: boolean;

    /**
     * 上传参数
     *
     * @type {string}
     * @memberof AppFileUpload
     */
    @Prop() public uploadparams?: any;

    /**
     * 下载参数
     *
     * @type {string}
     * @memberof AppFileUpload
     */
    @Prop() public exportparams?: any;

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

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

            /**
     * 默认文件最大上传大小
     */
     @Prop() public defaultSize?:number | string ;

    /**   
     * 允许上传的文件类型
     */
    @Prop({default: ''}) public whiteList?:string;

    /**   
     * 上传描述
     */
    @Prop({default:''}) public uploadDescription?:string;

    /**
     * 上传文件提示
     */
    public uploadTip:string = ''

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

    /**
     * 上传params
     *
     * @type {Array<any>}
     * @memberof AppFileUpload
     */
    public upload_params: Array<any> = [];

    /**
     * 导出params
     *
     * @type {Array<any>}
     * @memberof AppFileUpload
     */
    public export_params: Array<any> = [];

    /**
     * 自定义数组
     *
     * @type {Array<any>}
     * @memberof AppFileUpload
     */
    public custom_arr: Array<any> = [];

    /**
     * 应用参数
     *
     * @type {*}
     * @memberof AppImageUpload
     */
    public appData: any;

    /**
     * 文件上传限制类型
     *
     * @type {*}
     * @memberof DiskFileUpload
     */ 
    limitType:Array<String> = ['application/x-msdownload','text/x-sh','text/html','text/javascript']

    /**
     * 当前登陆人的token
     *
     * @type {string}
     * @memberof DiskFileUpload
     */
    public token: string = "Bearer " + localStorage.getItem('token');

    /**
     * 上传文件请求头
     *
     * @type {*}
     * @memberof DiskFileUpload
     */
    public myHeaders: any = {Authorization: this.token};

    /**
     * 拼接上传路径
     *
     * @memberof DiskFileUpload
     */
    public getAction() {
        return Environment.BaseUrl + `${(window as any).Environment.UploadFile}`+ '?ownertype=' + this.name + '&ownerid=' + JSON.parse(this.data).srfkey;
    }
    /**
     * 设置files
     *
     * @private
     * @memberof AppFileUpload
     */
    private setFiles(value:any): void {
        if(value==null){
            this.files = [];
            return;
        }
        let _files = JSON.parse(value);
        if (value && Object.prototype.toString.call(_files)=='[object Array]') {
            this.files = _files;
        } else {
            this.files = [];
        }
    }

    /**
     * 数据处理
     *
     * @private
     * @memberof AppFileUpload
     */
    private dataProcess(): void {
        let _url = `${Environment.BaseUrl}${Environment.UploadFile}`;
        if (this.upload_params.length > 0 ) {
            _url +='?';
            this.upload_params.forEach((item:any,i:any)=>{
                _url += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                if(i<this.upload_params.length-1){
                    _url += '&';
                }
            })    
        }
        this.files.forEach((file: any) => {
            let downloadUrl = `${Environment.BaseUrl}${Environment.ExportFile}/${file.id}`;
              const entityName = this.appEntityService.APPDENAME;
                const base64 = `${file.id}|${entityName}|${this.ownerid}|${this.context.srfpersonid || this.context.srfuserid}`;
                let url =`${downloadUrl}?key=${window.btoa(base64)}${Math.floor(
                    1000 + Math.random() * 9000,
                )}`


            if (this.export_params.length > 0) {
                if (!url.includes('?')) {
                    url +='?';
                }
            this.export_params.forEach((item:any,i:any)=>{
                url += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                if(i<this.export_params.length-1){
                    url += '&';
                }
            })
            }
            file.url = url;
        });
    }

      /**
       *  计算上传配置
       */
       public calcFileUploadConfig(){
        const config:{
            whiteList:string[],
            maxSize:number,
            description:string
        } = {
            whiteList:[],
            maxSize:10,
            description:''
        }
        const _whiteList = this.whiteList || Environment.fileUploadWhiteList;
        const _maxSize = this.defaultSize || Environment.fileUploadMaxSize;
        const _description = this.uploadDescription || Environment.fileUploadDescription;
        if(_whiteList){
            config.whiteList = _whiteList.split(',');
        }
        config.maxSize = Number(_maxSize);
        config.description = _description;
        if(!_description && _whiteList && _whiteList.length > 0){
            config.description = `${this.$t('components.appFileUpload.supportextends')}： ${_whiteList.split(',').map(type => `.${type?.toLowerCase()}`).join(' ')}，${this.$t('components.appFileUpload.singlefile')}${_maxSize}MB`;
        }
        return config;
      }
    

    /**
     * vue 生命周期
     *
     * @memberof AppFileUpload
     */
    public created() {
        if (this.formState) {
            this.formStateEvent = this.formState.subscribe(($event: any) => {
                // 表单加载完成
                if (Object.is($event.type, 'load')) {
                    this.getParams();
                    this.setFiles(this.value);
                    this.dataProcess();
                }
            });
        }
        const config = this.calcFileUploadConfig();
        this.uploadTip = config.description;
    }

    /**
     * vue 生命周期
     *
     * @returns
     * @memberof AppFileUpload
     */
    public mounted() {
        this.appData = this.$store.getters.getAppData();
        this.getParams();
        this.setFiles(this.value);
        this.dataProcess();
    }

    /**
     *获取上传，导出参数
    *
    *@memberof AppFileUpload
    */
    public getParams(){
        let uploadparams: any = JSON.parse(JSON.stringify(this.uploadparams));
        let exportparams: any = JSON.parse(JSON.stringify(this.exportparams));

        let upload_params: Array<string> = [];
        let export_params: Array<string> = [];

        let param:any = this.viewparams;
        let context:any = this.context;
        let _data:any = JSON.parse(this.data);

        if (this.uploadparams && !Object.is(this.uploadparams, '')) {
            upload_params = this.$util.computedNavData(_data,param,context,uploadparams);    
        }
        if (this.exportparams && !Object.is(this.exportparams, '')) {
            export_params = this.$util.computedNavData(_data,param,context,exportparams);
        }
        
        this.upload_params = [];
        this.export_params = [];

        for (const item in upload_params) {
            this.upload_params.push({
                [item]:upload_params[item]
            })
        }
        for (const item in export_params) {
            this.export_params.push({
                [item]:export_params[item]
            })
        }
    }

    /**
     * 组件销毁
     *
     * @memberof AppFileUpload
     */
    public destroyed(): void {
        if (this.formStateEvent) {
            this.formStateEvent.unsubscribe();
        }
    }

    /**
     * 上传之前
     *
     * @param {*} file
     * @memberof AppFileUpload
     */
    public beforeUpload(file: any) {
        const config = this.calcFileUploadConfig();
        const blackList = Environment.blackList;
        // 白名单
        const whiteList = config.whiteList;
        const fileName = file.name?.trim().toLowerCase();
        const blackResult = Array.isArray(blackList) && typeof fileName === 'string' && blackList.some(item => fileName.endsWith(`.${item}`));
        const whiteResult = Array.isArray(whiteList) && typeof fileName === 'string' && whiteList.some(item => fileName.endsWith(`.${item}`));

        if(whiteList.length > 0 && !whiteResult){
            Message.error(`${fileName} 文件格式不正确，请上传 ${whiteList.join('、')}类型的文件`);
            return false;
        }

        if (blackResult && !whiteResult) {
            Message.error(`不可上传 ${blackList.join('、')}类型的文件！`);
            return false;
        }
        if(file.size > config.maxSize * 1024 * 1024){
            Message.error(`${fileName}超出文件限制大小，单个文件不能超过${config.maxSize}MB`);
            return false
        }
        if(this.imageOnly){
            const imageTypes = ["image/jpeg" , "image/gif" , "image/png" , "image/bmp"];
            const isImage = imageTypes.some((type: any)=> Object.is(type, file.type));
            if (!isImage) {
            this.$Notice.error({ title: (this.$t('components.appFileUpload.fileTypeErrorTitle') as any) ,desc: (this.$t('components.appFileUpload.fileTypeErrorInfo') as any)});
            }
            return isImage;
        }
    }

    /**
     * 上传成功回调
     *
     * @param {*} response
     * @param {*} file
     * @param {*} fileList
     * @memberof AppFileUpload
     */
    public onSuccess(response: any, file: any, fileList: any) {
        if (!response) {
            return;
        }
        const data = { name: response.filename, id: response.fileid };
        let arr: Array<any> = [];
        this.files.forEach((_file:any) => {
            arr.push({name: _file.name, id: _file.id})
        });
        arr.push(data);

        let value: any = arr.length > 0 ? JSON.stringify(arr) : null;
        this.$emit('formitemvaluechange', { name: this.name, value: value });
    }

    /**
     * 上传失败回调
     *
     * @param {*} error
     * @param {*} file
     * @param {*} fileList
     * @memberof AppFileUpload
     */
    public onError(error: any, file: any, fileList: any) {
        const data = JSON.parse(error.message)
        this.$Notice.error({ title: data.message ||  (this.$t('components.appFileUpload.uploadError') as any)});
    }

    /**
     * 删除文件
     *
     * @param {*} file
     * @param {*} fileList
     * @memberof AppFileUpload
     */
    public onRemove(file: any, fileList: any) {
        let arr: Array<any> = [];
        fileList.forEach((f: any) => {
            if (f.id != file.id) {
                arr.push({ name: f.name, id: f.id });
            }
        });
        let value: any = arr.length > 0 ? JSON.stringify(arr) : null;
        if(arr.length == 0){
            this.dialogVisible = false;
        }
        this.$emit('formitemvaluechange', { name: this.name, value: value });
    }

    /**
     * 下载文件
     *
     * @param {*} file
     * @memberof AppFileUpload
     */
    public onDownload(file: any) {
        // 拼接url
        let _this: any = this;
        const id = typeof file.id == "string" ? file.id : JSON.stringify(file.id);
        const name = typeof file.name == "string" ? file.name : JSON.stringify(file.filename);
        const downloadUrl = `${(window as any).Environment.ExportFile}`+ '/' + id + '/' + name;
        const entityName = this.appEntityService.APPDENAME;
        const base64 = `${id}|${entityName}|${this.ownerid}|${this.context.srfpersonid || this.context.srfuserid}`;
        let url =`${downloadUrl}?key=${window.btoa(base64)}${Math.floor(
            1000 + Math.random() * 9000,
        )}`
        if (this.export_params.length > 0) {
              this.export_params.forEach((item:any,i:any)=>{
                  url += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                  if(i<this.export_params.length-1){
                      url += '&';
                  }
              })
            }
            // 发送get请求
            Axios.get(url, {
                responseType: 'arraybuffer',
            }).then((response: any) => {
                if (!response || response.status != 200) {
                    Message.error(_this.$t('components.diskFileUpload.downloadFile') + '!');
                    return;
                }
                // 请求成功，后台返回的是一个文件流
                if (response.data) {
                    // 获取文件名
                    const disposition = response.headers['content-disposition'];
                    const filename = disposition.split('filename=')[1];
                    const actualFileName = file.name || filename;
                    // 用blob对象获取文件流
                    let blob = new Blob([response.data], {type: response.headers['content-type']});
                    // 兼容ie语法
                    if('msSaveOrOpenBlob' in navigator){
                        window.navigator.msSaveOrOpenBlob(blob, actualFileName);
                        return;
                    }
                    // 通过文件流创建下载链接
                    var href = URL.createObjectURL(blob);
                    // 创建一个a元素并设置相关属性
                    let a = document.createElement('a');
                    a.href = href;
                    a.download = actualFileName;
                    // 添加a元素到当前网页
                    document.body.appendChild(a);
                    // 触发a元素的点击事件，实现下载
                    a.click();
                    // 从当前网页移除a元素
                    document.body.removeChild(a);
                    // 释放blob对象
                    URL.revokeObjectURL(href);
                } else {
                    Message.error(_this.$t('components.diskFileUpload.downloadFile1'));
                }
            }).catch((error: any) => {
                Message.error(_this.$t('components.diskFileUpload.downloadFile') + ':' + error);
            });
    }

    /**
     * 是否只支持图片上传
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    @Prop({default: false}) public imageOnly!: boolean;

    /**
     * 是否开启行内预览
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    @Prop({default: false}) public rowPreview!: boolean;

    /**
     * 是否开启行内预览
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    public dialogVisible: boolean = false;
    /**
     * 是否开启行内预览
     *
     * @type {boolean}
     * @memberof AppFileUpload
     */
    public showActions: boolean = false;


}
</script>

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