<template>
    <div @click="runEvent" @dblclick="runEvent">
        <img :src="imgSrc" :height="height" :width="width"
             v-if="imgSrc" :style="'cursor:' + (configData.download == '1' ? 'pointer' : 'default')"
             @click="download(imgSrc)" />
        <span v-else>{{noImgMsg}}</span>
    </div>
</template>

<script>
    import EventParentRender from "../../eventParentRender";
    export default {
        name: "show-img-render",
        extends:EventParentRender,
        data(){
            return {
                imgSrc:"",
                noImgMsg:'暂无图片',
                height:'80px',
                width:'80px',
            }
        },
        created(){
            this.initData();
        },
        watch:{
            value(){
                this.initData();
            }
        },
        methods:{
            initData(){
                let code = this.configData.imgCode || "";
                let width = this.configData.width || "";
                let height = this.configData.height || "";
                let text = this.configData.text || "";
                if(width){
                    if(width == 'auto'){
                        this.width = '';
                    }else{
                        this.width = width;
                    }
                }
                if(height){
                    if(height == 'auto'){
                        this.height = '';
                    }else{
                        this.height = height;
                    }
                }
                if(text){
                    this.noImgMsg = text;
                }
                let id = "";
                Object.keys(this.rowData).forEach(key => {
                    if(id){
                        return;
                    }
                    if(key.startsWith("ID_")){
                        id = this.rowData[key];
                    }
                })
                this.axios.post(this.api.upload.viewFile,{fkId: id + "_" + code}).then(res => {
                    if((res.data || []).length > 0){
                        this.imgSrc = this.api.baseUploadUrl+'downloadFile?id='+ (res.data[0]).id;
                    }
                })
            },
            download(url){
                if(this.configData.download != '1'){
                    return;
                }
                let link = document.createElement('a');
                link.setAttribute('href', url);
                link.click();
                link = null;
            }
        }
    }
</script>

<style scoped>

</style>
