<template>
    <tr>
        <td colspan="3" style="padding: 0px; border:0;">
            <table>
                <tbody id="cnxt_objects_folder_mysql_dbo">
                    <ItemObject v-for="(itemObject, indexObject) of objectList" :key="indexObject" :name='itemObject.name' :schema="schema" :idConnection="idConnection" @getColumns='getColumns' @selectedItemObject='selectedItemObject' :itemObject='itemObject' @getSource="getSource" :object='object' :selectedSchema='selectedSchema' :tableName='tableName'></ItemObject>
                </tbody>
            </table>
        </td>
    </tr>
</template>
<script lang="ts">
import { Attribute } from '@/domain/models/Attribute.interface';
import { Connection } from '@/domain/models/connections/Connection';
import { jsonUtilities } from '@/infrastructure/utilities/jsonUtilities'
import { ConnectionService } from '@/domain/services/Connection.service'
import  ItemObject  from '@/lib-components/component/ConnectionExplorerComponent/ItemObject.vue';

export default{
    name : 'ObjectList',
    emits : ['getColumns',  'getSource', ],
    props:{
        connection : { type:Connection, default: null },
        schema : { type: String, default: '' },
        idConnection : { type: String, default: '' },
        showItemObject : { type:Boolean, default:false},
        object : { type: String, default: '' },
        selectedSchema : { type: String, default: '' },
        tableName : { type: String, default: '' },
        showViews: { type: Boolean, default: true },
    },
    components: {
        ItemObject
    },
    data : () => {
        return {
            connectionService : {} as ConnectionService,
            showDatabaseObjects : false,
            objectList : [{name :'tables', selected : false}, {name :'views', selected : false}],
        }
    },
    methods: {
        selectedItemObject(name : String){
            for(let indexItem = 0; indexItem < this.objectList.length; indexItem++){
                this.objectList[indexItem].selected = (this.objectList[indexItem].name == name);
            }
        },
        getColumns(columnList : []){
            this.columnList = columnList;
            this.$emit("getColumns", this.columnList);
        },
        getValueAttribute(attribute : Attribute, prop : String){
            return jsonUtilities.getJsonArrayEntrybyValue<Attribute>(attribute, prop).value;
        },
        getSource(source : String){
            this.source =  source;
            this.$emit("getSource",source);
        },
        hiddenViews(){
            if(this.showViews == 'false'){
                this.objectList = this.objectList.filter((item :any) => item.name == 'tables');
            }
        },
    },
    async mounted(){
        this.hiddenViews();
        this.connectionService = new ConnectionService();
        if(this.selectedSchema.selected)
        {
            this.$emit("getColumns", this.columnList);
            for(let indexItem = 0; indexItem < this.objectList.length; indexItem++)
            {
                if(this.objectList[indexItem].name === this.object){
                    this.objectList[indexItem].selected  = true;
                }
            }
        }
    },
}
</script>
<style scoped>
td{
    border:0;
}
</style>
