"use strict";
import { appointmentSummary, todayAbsentEmployee, todayLateEmployee, todayEarlyLeaveEmployee } from './chartService.js';



// load dashboard
window.dashboardAdmin =  (e ) => {
    $('.c-dropdown-menu .dropdown-item').removeClass('active');
    if (e == "Company Dashboard") {
        $('.company_option').addClass('active');
    }else if (e == "Superadmin Dashboard") {
        $('.super_dashboard').addClass('active');
    }else{
        $('.profile_option').addClass('active');
    }
    $("#__selected_dashboard").html(e);
    $('.c-dropdown-menu').removeClass('show');
    let dashboardType = e;
    var data = {
        url : $("#profileWiseDashboard").val(),
        method: 'POST',
        data: {
            _token: _token,
            userID: $("#fire_base_authenticate").val(),
            dashboardType: dashboardType,
        }
    };
    custom_http_request(data).then(function(response){
        if (response.status == 200) { 
            $("#__MyProfileDashboardView").html(response?.data?.dashboard);
            if(response?.data?.dashboardType == 'Company Dashboard') {
                todayAbsentEmployee(response?.data?.today_absent.data,'today_absent_employee');
                todayLateEmployee(response?.data?.today_late.data,'today_late_employee');
                todayEarlyLeaveEmployee(response?.data?.today_early_leave.data,'today_early_leave_employee');
                appointmentSummary(response?.data?.appointment?.original?.data,'appointment_schedule');
                appointmentSummary(response?.data?.meeting?.original?.data,'meeting_schedule');
            }
            else if(response?.data?.dashboardType == 'Dashboard') {
                appointmentSummary(response?.data?.appointment?.original?.data,'appointment_schedule');
                appointmentSummary(response?.data?.meeting?.original?.data,'meeting_schedule');
            }
        }else {
            Toast.fire({
                icon: 'error',
                title: response?.data?.message ?? 'Something went wrong.',
            })
        }

    }).catch(function(error){
        if(error?.response?.data?.message) {
            Toast.fire({
                icon: 'error',
                title: error?.response?.data?.message,
            })
        }
    });
    
} 

$(function () {
    let user_slug = $("#user_slug").val();
    if (user_slug == "superadmin" || user_slug == "admin" || user_slug == "hr") {
        $('.super_dashboard').addClass('active');
        dashboardAdmin("Company Dashboard");
    } else {
        $('.profile_option').addClass('active');
        dashboardAdmin("My Dashboard");
    }
});