import React from "react";
import ReactDOM from "react-dom";
import DashboardContainer from "./containers/DashboardContainer/index.jsx";
import Dashboard2Container from "./containers/Dashboard2Container/index.jsx";

const dashboardDiv = document.getElementById("app-dashboard");


const dashboardContainer = dashboardDiv ? ReactDOM.render(<DashboardContainer />, dashboardDiv) : false;


netuno.addNavigationLoad(() => {
    const navigation = $('[netuno-navigation]');

    navigation.children().first().after(`
    <li>
        <a netuno-navigation-dashboard2 href="#netuno_dashboard2">
            <i class="fa fa-fw fa-dashboard"></i> Dashboard2
        </a>
    </li>`);

    const containers = $('[netuno-containers]');

    containers.children().first().after(`
    <div netuno-container-dashboard2 id="netuno_dashboard2" style="display:none;">Novo Dashboard
    </div>`);

    const dashboard2Div = document.getElementById("netuno_dashboard2");
    const dashboard2Container = dashboard2Div ? ReactDOM.render(<Dashboard2Container />, dashboard2Div) : false;

    $('[netuno-navigation]').find('a').on('netuno:click', (e)=> {
        const link = $(e.target);
        if (dashboardContainer && link.is('[netuno-navigation-dashboard]')) {
            // Menu > Dashboard > Clicked!
        }
        if (dashboard2Container && link.is('[netuno-navigation-dashboard2]')) {
            dashboard2Container.navigationClick();
        }
    });
});

netuno.addContentLoad((container) => {
    // When any content is loaded dinamically this is executed...
    if (container.is('[netuno-form-search="YOUR_FORM_NAME"]')) {
        // When search page is loaded...
    } else if (container.is('[netuno-form-edit="YOUR_FORM_NAME"]')) {
        // When form edit is loaded...
    }
});

netuno.addPageLoad(() => {
    // When page is loaded...
    let modal = $('#app-dashboard-modal-form');
    modal.on('hidden.bs.modal', ()=> {
        modal.find('[netuno-form-edit]').empty();
    });
    $('#app-dashboard-modal-form-button').on('click', ()=> {
        modal.modal('show');
        netuno.loadFormEdit(modal.find('[netuno-form]'));
    });
    modal.find('[netuno-form]').on('netuno:save', ()=> {
        modal.modal('hide');
    });
});


