<?php
    
    session_start();
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    global $current_user,$sugar_config;

?>
<?php include('custom/customization/modules/Home/Dashboard/php/dashboard_header.php');?>

        <div id="wrapper">

		  <div class="col-md-12 p-20">

		  	<?php 
                $module_name = $_REQUEST['module_name'];
                $module_count = $_REQUEST['module_count'];
                $module_original_name = $_REQUEST['module_key'];
                $user_name = $_REQUEST['name_label'];
                $user_wisecount = $_REQUEST['count'];
                $user_wise_scorecard = $_REQUEST['grouping'];
                $assigned_user_id = $_REQUEST['assigned_user_id'];
                $is_click_today_activity = $_REQUEST['today_activity'];
                $todays_total_count = $_REQUEST['todays_total_count'];
                $is_user_click = $_REQUEST['is_user_click'];
                $getall = $_REQUEST['getall'];
                $from_periodic = $_REQUEST['from_periodic'];
                $from_result = $_REQUEST['from_result'];
                $sublist = $_REQUEST['sublist'];
                $positive = $_REQUEST['positive'];
                $negative = $_REQUEST['negative'];
                $user_wise_enable = $_SESSION['user_wise_enable_c'];
                $get_grouping = $_REQUEST['grouping'];


                if($from_periodic == 1){
                    if($_SESSION['pvalue'] != null){
                      $explode_daterange_arr = explode('_', $_SESSION['pvalue']);
                    }else{
                      $explode_daterange_arr = explode('_', date("Y-m-d"));
                    }
                    $dashboard_level = "Periodic - ".$_SESSION['display_label'];
                }else if($from_result == 1){
                    if($_SESSION['pvalue_result'] != null){
                      $explode_daterange_arr = explode('_', $_SESSION['pvalue_result']);
                    }else{
                      $explode_daterange_arr = explode('_', date("Y-m-d"));
                    }
                    $dashboard_level = "Result - ".$_SESSION['display_label_result'];
                }else{
                    $explode_daterange_arr = explode('_', date("Y-m-d"));
                    $dashboard_level = "Open";
                }
                
                $_SESSION['dashboard_level'] = $dashboard_level;

                $dashboard_scoreboard_arr = array();


                // Program to display URL of current page. 

                if($get_grouping != "user_grouping"){
                    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
                    $link = "https"; 
                    else
                        $link = "http"; 
                      
                    // Here append the common URL characters. 
                    $link .= "://"; 
                      
                    // Append the host(domain name, ip) to the URL. 
                    $link .= $_SERVER['HTTP_HOST']; 
                      
                    // Append the requested resource location to the URL 
                    $link .= $_SERVER['REQUEST_URI']; 
                          
                    // Print the link 
                    // echo $link;
                    $_SESSION['breadcrumb_list_link'] = $link;
                }
  
                

                // echo "<pre>";
                // echo $_SESSION['session_arr_web_dashboard_c'];
                // 
                $arr_web_dashboard_c = json_decode($_SESSION['session_arr_web_dashboard_c']);
                // print_r($arr_web_dashboard_c);
                // {
                //   "session": "'.$session_id.'",
                //   "getall": "1",
                //   "module_name": "Leads",
                //   "date_field": "next_followup_date_c",
                //   "dom_field": "status,lead_source",
                //   "assigned_user_id": "0",
                //   "user_wise_enable": "1"
                // }

                

                if($is_click_today_activity == "1"){
                    $display_label = "Today's Activity";
                }else if($module_name == null || $get_grouping == "user_grouping"){
                    $display_label = $user_name." - ".$module_name;
                }else{
                    $display_label = str_replace("_", " ", $module_name);
                }

                if($is_click_today_activity == "1"){
                    $display_count = $todays_total_count;
                }else if($user_wise_scorecard != null){
                    $display_count = $user_wisecount."/".$module_count;
                }else{
                    $display_count = $module_count;
                }
                

                if(!empty($arr_web_dashboard_c)){
                    foreach ($arr_web_dashboard_c as $key => $value) {
                        // echo "<br>".$value->module_name;
                        if($value->module_name == $module_original_name){
                            // echo "<br>1";
                            $dashboard_scoreboard_arr['module_name'] = $module_original_name;
                            $dashboard_scoreboard_arr['date_field'] = $value->date_field;
                            $display_title = $value->date_field;
                            $dom_field_array = array();
                            foreach ($value->dropdown_field as $k => $v) {
                               $dom_field_array[$k] = $v;
                            }
                            $dashboard_scoreboard_arr['dom_field'] = implode(',', $dom_field_array);
                            if($user_wise_scorecard == "user_grouping"){
                                $dashboard_scoreboard_arr['getall'] = "0";
                                $dashboard_scoreboard_arr['assigned_user_id'] = $assigned_user_id;
                            }else{
                                if($getall != null && $getall == 0){
                                    $dashboard_scoreboard_arr['getall'] = "0";
                                }else{
                                    $dashboard_scoreboard_arr['getall'] = "1";
                                }
                                
                                $dashboard_scoreboard_arr['assigned_user_id'] = "0";
                            }
                            
                            $dashboard_scoreboard_arr['user_wise_enable'] = "1";
                            if($from_periodic == 1 || $from_result == 1){
                                if(count($explode_daterange_arr) == 1){
                                    $dashboard_scoreboard_arr['start_date'] = $explode_daterange_arr[0];
                                    $dashboard_scoreboard_arr['end_date'] = $explode_daterange_arr[0];
                                }else{
                                    $dashboard_scoreboard_arr['start_date'] = $explode_daterange_arr[0];
                                    $dashboard_scoreboard_arr['end_date'] = $explode_daterange_arr[1];
                                }
                                
                            }
                        }
                    }
                }else{
                    echo "Something Went Wrong!";die();
                }
                if($current_user->id == "c1546326-6b16-7dc5-1314-5c2af7e4787b"){
                        // echo "Final : ".json_encode($dashboard_scoreboard_arr);
                    }
                // echo "Final : ".json_encode($dashboard_scoreboard_arr);
                
                if($is_click_today_activity == 1){
                    $display_today_activity = json_decode($_SESSION['session_today_activity_arr']);
                }else{
                    $dashboard_cls = new webDashboadClass;
                    
                    if($from_periodic == 1){
                        $grouping = $dashboard_cls->periodic_scorecard_count($dashboard_scoreboard_arr);
                    }else if($from_result == 1){
                        empty($grouping);
                        $grouping = $dashboard_cls->result_scorecard_count($dashboard_scoreboard_arr,$positive,$negative);
                    }else{
                        empty($grouping);
                        $grouping = $dashboard_cls->lat_multi_scorecard_count($dashboard_scoreboard_arr);
                    }

                     // echo "<pre>";
                    if($current_user->id == "c1546326-6b16-7dc5-1314-5c2af7e4787b"){
                        // print_r($grouping);
                    }
                    // print_r($grouping);
                    // die;
                

                    $_SESSION['open_scorecard_response'] = json_encode($grouping);
                }
                // echo "<pre>";
                // print_r($display_today_activity);

                // die;


                //Get Original title for primary grouping 
                $userbean = BeanFactory::getBean($module_original_name);
                $display_title1 = str_replace(":","",$userbean->field_name_map[$display_title]['labelValue']);
                if($display_title1 == ''){
                    $display_title1 = $display_title;
                }else{
                    $display_title1 = $display_title1;
                }
                // print_r($userbean);
            ?>
            <!-- <input type="text" name="base_url" id="base_url" value="<?php echo $_SESSION['site_base_url']?>"> -->
            <div class="row breadcrumb-new">
                <nav aria-label="breadcrumb">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php?module=Home&action=dashboard&getall=<?php echo $getall;?>">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="index.php?module=Home&action=dashboard&getall=<?php echo $getall;?>"><?php echo $dashboard_level;?></a></li>
                    <?php if($get_grouping == "user_grouping"){?>
                        <li class="breadcrumb-item"><a href="<?php echo $_SESSION['breadcrumb_list_link'];?>"><?php echo $module_name;?></a></li>
                    <?php } ?>
                    <li class="breadcrumb-item active"><?php echo ucfirst($display_label); ?></li>
                  </ol>
                </nav>
            </div>
            <div class="module_name">
                <h4><?php echo ucfirst($display_label); ?> (<?php echo $display_count;?>) </h4>
            </div>
            <div class="row">

                <!-- Today Activity -->
                <?php
                if(!empty($display_today_activity)){
                ?>
                <div class="col-md-12">
                    
                    <div id="module_detail_due_date" class="col-md-12 padding-23">
                        <?php
                            foreach ($display_today_activity as $key => $value) {
                            if($getall == 0){?>
                            <!-- <a href="index.php?module=<?php echo $value->today_module_key;?>&action=index&dashboard=1&customquery=<?php echo base64_encode($value->todays_activity_query);?>&is_user_click=1">  -->
                            <a href="javascript:void(0)" target="_blank" onclick="goToCRMListview('<?php echo $value->today_module_key;?>','','<?php echo base64_encode($value->todays_activity_query);?>','1',0);">
                            <?php }else{?> 
                            <a href="index.php?module=Home&action=dashboard_module_sublist_template&grouping=today&name=<?php echo $value->today_module_key;?>&count=<?php echo $value->today_cnt;?>&module_original_name=<?php echo $value->today_module_key;?>&name_label=<?php echo $value->today_module_label;?>&q=<?php echo base64_encode($value->todays_activity_query);?>&getall=<?php echo $getall;?>">
                        <?php } ?>
                        
                        <div class="col-md-3 pull-left">
                            <div class="widget-bg-color-icon card-box fadeInDown animated set-cardbox pri_<?php echo $value->key;?>" style="background-color: <?php echo $value->color;?>;">
                                <div class="text-middle" >
                                    <h3 class="text-dark open-sublist-color" align="center"><b class="counter"><?php echo $value->today_cnt;?></b></h3>
                                    <p class="text-muted mb-0 open-sublist-color" align="center"><?php if(strlen(trim($value->today_module_label)) == 0){ echo "N/A"; }else{ echo $value->today_module_label; }?></p>
                                </div>
                            </div>
                        </div>
                        </a>
                         <?php } ?>
                         
                    </div>
                </div>
                <?php
                }
                ?>
                <!-- till here -->
                <!-- due date -->
                <?php
                    if($current_user->id == "c1546326-6b16-7dc5-1314-5c2af7e4787b"){
                        // echo "<pre>";
                        // print_r($grouping);
                    }
                    
                    if($sublist == 1 || ($from_periodic != 1 && $from_result != 1)){

                        if(!empty($grouping['primary_grouping'])){
                    ?>
        		  	<div class="col-md-12">
                        <div class="group_module_name" id="module_name_due_date" onclick="module_detail_hide(this)">
                            <?php echo $display_title1;?>
                            <i class="pull-right fa fa-caret-down padding-r-1" id="caret_due_date" aria-hidden="true"></i>
                        </div>
                        <div id="module_detail_due_date" class="col-md-12 padding-23 sub-field-data">
                            <?php
                                foreach ($grouping['primary_grouping'] as $key => $value) {
                                 
                                if($getall == 0){?>
                                <!-- <a href="index.php?module=<?php echo $module_original_name;?>&action=index&dashboard=1&customquery=<?php echo base64_encode($value['query']);?>&is_user_click=1"> -->
                                <a href="javascript:void(0)" target="_blank" onclick="goToCRMListview('<?php echo $module_original_name;?>','','<?php echo base64_encode($value['query']);?>','1',0);">
                                <?php }else{?>
                                <a href="index.php?module=Home&action=dashboard_module_sublist_template&grouping=primary_grouping&name=<?php echo $value['key'];?>&count=<?php echo $value['count'];?>&module_original_name=<?php echo $module_original_name;?>&name_label=<?php echo $value['label'];?>&is_user_click=<?php echo $is_user_click;?>&display_label=<?php echo $display_label;?>&getall=<?php echo $getall;?>">
                                <?php } ?>
                                <div class="col-md-3 pull-left">
                                    <div class="widget-bg-color-icon card-box fadeInDown animated set-cardbox pri_<?php echo $value['key'];?>" style="background-color: <?php echo $value['color'];?>;">
                                        <div class="text-middle" >
                                            <h3 class="text-dark color-white" align="center"><b class="counter"><?php echo $value['count'];?></b></h3>
                                            <p class="text-muted mb-0 color-white" align="center"><?php if(strlen(trim($value['label'])) == 0){ echo "N/A"; }else{ echo $value['label']; }?></p>
                                        </div>
                                    </div>
                                </div>
                                </a>
                             <?php } ?>
                             
                        </div>
                    </div>
                    <?php
                        }
                    }
                ?>
                <!-- userwise -->
                 <?php
                    if($getall == 1){
                        if($user_wise_enable == 1){
                            if(!empty($grouping['user_grouping'])){
                                // echo "<pre>";
                                // print_r($grouping);
                    ?>
                    <div class="col-md-12">
                        <div class="group_module_name" id="module_name_user_wise" onclick="module_detail_hide1()">
                            User Wise
                            <i class="pull-right fa fa-caret-down padding-r-1" id="caret_user_wise" aria-hidden="true"></i>
                        </div>
                        <div id="module_detail_user_wise" class="col-md-12 padding-23 sub-field-data">
                            <?php
                                foreach ($grouping['user_grouping'] as $key => $value) {
                                  
                                if($from_result == 1){?>
                                    <a href="javascript:void(0)" target="_blank" onclick="goToCRMListview('<?php echo $module_original_name;?>','<?php echo $value['assigned_user_id']?>','<?php echo base64_encode($value['query']);?>','1',0,'<?php echo $from_result?>');">
                                <?php }else{?>
                                    <a href="index.php?module=Home&action=dashboard_module_list_template&grouping=user_grouping&assigned_user_id=<?php echo $value['assigned_user_id'];?>&count=<?php echo $value['count'];?>&module_key=<?php echo $module_original_name;?>&name_label=<?php echo $value['assigned_user_name'];?>&module_count=<?php echo $module_count;?>&is_user_click=1&getall=<?php echo $getall;?>&from_periodic=<?php echo $from_periodic;?>&from_result=<?php echo $from_result;?>&sublist=1&module_name=<?php echo $module_name?>">
                                <?php }   
                                
                            ?>
                            
                            
                            
                            <div class="col-md-3 pull-left">
                                <div class="widget-bg-color-icon card-box fadeInDown animated set-cardbox">
                                    <div class="text-middle" >
                                        <h3 class="text-dark open-sublist-color" align="center"><b class="counter"><?php echo $value['count'];?></b></h3>
                                        <p class="text-muted mb-0 open-sublist-color" align="center"><?php if(strlen(trim($value['assigned_user_name'])) == 0){ echo "N/A"; }else{ echo $value['assigned_user_name']; }?></p>
                                    </div>
                                </div>
                            </div>
                             </a>
                            <?php } ?>
                             
                        </div>
                    </div>
                    <?php
                            }
                        }
                    }
                ?>
                <!-- product -->
                 <?php
                    // echo "<pre>";
                    // print_r($grouping->secondary_grouping);
                // if($sublist == 1 || $from_result != 1){
                    if(!empty($grouping['secondary_grouping'])){
                        foreach ($grouping['secondary_grouping'] as $key => $value) {
                ?>
                <div class="col-md-12">
                   
                    <div class="group_module_name" id="module_name_<?php echo $value['key'];?>" onclick="module_detail_hide3('<?php echo $value['key'];?>')">
                        <?php echo $value['label'];?>
                        <i class="pull-right fa fa-caret-down padding-r-1" id="caret_<?php echo $value['key'];?>" aria-hidden="true"></i>
                    </div>
                    <div id="module_detail_<?php echo $value['key'];?>" class="col-md-12 padding-23 sub-field-data">
                        <?php
                            foreach ($value['data'] as $k => $v) {
                            if($getall == 0){?>
                            <!-- <a href="index.php?module=<?php echo $module_original_name;?>&action=index&dashboard=1&customquery=<?php echo base64_encode($v['query']);?>&is_user_click=1"> -->
                            <a href="javascript:void(0)" target="_blank" onclick="goToCRMListview('<?php echo $module_original_name;?>','','<?php echo base64_encode($v['query']);?>','1',0);">
                            <?php }else{ ?>
                            <a href="index.php?module=Home&action=dashboard_module_sublist_template&grouping=secondary_grouping&name=<?php echo $v['key'];?>&count=<?php echo $v['count'];?>&module_original_name=<?php echo $module_original_name;?>&name_label=<?php echo $v['label'];?>&field_name=<?php echo $value['key'];?>&field_display_label=<?php echo $value['label'];?>&field_value=<?php echo $v['key'];?>&display_label=<?php echo $display_label;?>&getall=<?php echo $getall;?>">
                            <?php } ?>
                        <div class="col-md-3 pull-left">
                            <div class="widget-bg-color-icon card-box fadeInDown animated set-cardbox">
                                <div class="text-middle" >
                                    <h3 class="text-dark open-sublist-color" align="center"><b class="counter"><?php echo $v['count']?></b></h3>
                                    <p class="text-muted mb-0 open-sublist-color" align="center"><?php if(strlen(trim($v['label'])) == 0){ echo "N/A"; }else{ echo $v['label']; }?></p>
                                </div>
                            </div>
                        </div>
                        </a>
                        <?php } ?>
                    </div>
                    
                </div>
                    <?php
                        } 
                    }
                // }
                ?>
            </div>

		  </div>
        </div>
        <!-- END wrapper -->
        <script>
            var resizefunc = [];
        </script>
<?php include('custom/customization/modules/Home/Dashboard/php/dashboard_footer.php');?>