<template>
    <div :class="{'app-field-image-dynamic':true,[`${cssClass}`]:cssClass?true:false}">
        <img :style="cssStyle" :src="imgUrl" />
    </div>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop, Watch, Provide } from 'vue-property-decorator';
import { ImgurlBase64 } from '@/utils';
import { Environment } from "@/environments/environment";
@Component({})
export default class AppFieldImageDynamic extends Vue {

    /**
     * 输入值
     *
     * @type {string}
     * @memberof AppRawItemImage
     */
    @Prop() public value: any;

    /**
     * 样式
     *
     * @type {string}
     * @memberof AppRawItemImage
     */
    @Prop() public cssStyle?: string;

    /**
     * 样式表类名
     *
     * @type {string}
     * @memberof AppRawItemImage
     */
     @Prop({default:''}) public cssClass?: string;

    /**
     * 动态图片路径
     *
     * @memberof AppPresetRawitem
     */
    protected dynaImgUrl: string = '';

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

    /**
     * 图片路径
     *
     * @memberof AppPresetRawitem
     */
    get imgUrl(): string {
        return this.dynaImgUrl;
    }

    created() {
        this.handleDynaImg();
    }

    /**
    * 处理动态图片
    *
    * @memberof AppPresetRawitem
    */
    protected handleDynaImg() {
        if (this.value && typeof this.value == "string") {
            // 默认识别文件对象形式，识别失败则为全路径模式
            try {
                const _files = JSON.parse(this.value);
                const file = _files instanceof Array ? _files[0] : null;
                const url =
                    file && file.id ? `${this.downloadUrl}/${file.id}` : "";
                ImgurlBase64.getInstance()
                    .getImgURLOfBase64(url)
                    .then((res: any) => {
                        this.dynaImgUrl = res;
                    });
            } catch (error) {
                this.dynaImgUrl = this.value;
            }
        }
    }
}
</script>
<style lang = "less">
@import './app-field-image-dynamic.less';
</style>