<template>
    <div :class="['app-field-image-dynamic', model.sysCss]" :style="containerStyle">
        <img :style="model.sysCss" :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";
import { PanelFieldModel } from '@/model/panel-detail';
@Component({})
export default class AppFieldImageDynamic extends Vue {

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

    /**
     * 模型
     *
     * @type {*}
     * @memberof AppFieldImageDynamic
     */
    @Prop() public model!: PanelFieldModel;

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

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

    /**
     * 容器样式
     *
     * @type {any}
     * @memberof AppFieldImageDynamic
     */
    public containerStyle: any = null;

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

    created() {
        if (this.model) {
            this.containerStyle = this.model.getElementStyle();
        }
        this.handleDynaImg();
    }

    /**
    * 处理动态图片
    *
    * @memberof AppFieldImageDynamic
    */
    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>