<?php

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    global $app_list_strings, $mod_strings, $app_strings, $sugar_config, $current_user;

?>
<?php include('custom/customization/modules/Home/Dashboard/php/dashboard_header.php');?>
        <div id="wrapper">
		  <div class="col-md-12 p-20">

		  	<?php 
                $name = $_REQUEST['name'];
                $count = $_REQUEST['count'];
                $grouping = $_REQUEST['grouping'];
                $module_original_name = $_REQUEST['module_original_name'];
                $name_label = $_REQUEST['name_label'];
                $field_name = $_REQUEST['field_name'];
                $field_display_label = $_REQUEST['field_display_label'];
                $field_value = $_REQUEST['field_value'];
                $today_query = base64_decode($_REQUEST['q']);
                $is_user_click = $_REQUEST['is_user_click'];
                $display_label = $_REQUEST['display_label'];
                $getall = $_REQUEST['getall'];
                $user_wise_enable = $_SESSION['user_wise_enable_c'];
                $open_scorecard_response_session = json_decode($_SESSION['open_scorecard_response']);
                // $session_arr_web_dashboard_c = json_decode($_SESSION['session_arr_web_dashboard_c']);

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

                $dashboard_scoreboard_sub_arr = array();


                $dashboard_scoreboard_sub_arr['module_name'] = $module_original_name;

                if($grouping != "today"){

                    $display_label = $display_label;
                    
                    foreach ($open_scorecard_response_session as $key => $value) {
                        // echo "<br>".$key;
                        if($grouping == $key){
                            if($grouping == "secondary_grouping"){
                            // echo "in if"; 
                            // print_r($value[0]);
                                // echo "<br>".$value;
                                foreach ($value as $kv => $vv) {
                                    // echo "<br>".$vv->key;
                                    // print_r($vv->data);
                                    if($module_original_name == "Calls"){
                                        // echo "<br>".$vv->key;
                                        if($name == null){
                                            $dashboard_scoreboard_sub_arr['query'] = $vv->query;
                                        }else{
                                            if($name == $vv->key){
                                                // echo "in";
                                                // echo "<br>".$v1->query;
                                                $dashboard_scoreboard_sub_arr['query'] = $vv->query;
                                            }
                                        }
                                    }else{
                                        if($vv->key == $field_name){
                                            // echo "<br>".$vv->data;
                                           foreach ($vv->data as $k1 => $v1) {
                                                // echo "<br>".$v1->key;
                                                if($name == null){
                                                    $dashboard_scoreboard_sub_arr['query'] = $v1->query;
                                                }else{
                                                    if($name == $v1->key){
                                                        // echo "in";
                                                        // echo "<br>".$v1->query;
                                                        $dashboard_scoreboard_sub_arr['query'] = $v1->query;
                                                    }
                                                }
                                            } 
                                        }
                                    }
                                }
                                
                            }else{
                                foreach ($value as $k => $v) {
                                    // echo "<br>".$v->key." - ".$name;
                                    if($name == $v->key){
                                        // echo "<br>".$v->query;
                                        $dashboard_scoreboard_sub_arr['query'] = $v->query;
                                    }
                                }
                            }
                        }
                    }
                }else{
                    $display_label = "Today's Activity";
                    $dashboard_scoreboard_sub_arr['query'] = $today_query;
                }

                $dashboard_scoreboard_sub_arr['offset'] = 0;

                // echo "Final : ".json_encode($dashboard_scoreboard_sub_arr);

                // die;
               

                $pass_custom_query = base64_encode($dashboard_scoreboard_sub_arr['query']);
                

                if(empty($dashboard_scoreboard_sub_arr)){
                    echo "Something Went Wrong!";die();
                }

                // echo "Final : ".json_encode($dashboard_scoreboard_arr);
                if($current_user->id == "c1546326-6b16-7dc5-1314-5c2af7e4787b"){
                // echo "<pre>";
                // print_r($dashboard_scoreboard_sub_arr);
            }

                $dashboard_cls = new webDashboadClass;
                $result_data = $dashboard_cls->get_entries_count($dashboard_scoreboard_sub_arr);

                // die;
               

                $bulk_assigned_user_id = array();


                foreach ($result_data['result_count'] as $key1 => $value1) {
                    $bulk_assigned_user_id[$key1] = $value1['assigned_user_id'];
                }

                $pass_bulkid_to_crm = implode("|",$bulk_assigned_user_id);

                // echo "<pre>";
                // print_r($result_data);
                // echo implode("', '",$bulk_assigned_user_id);


                // die;
            ?>
            <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 $_SESSION['dashboard_level'];?></a></li>
                    <li class="breadcrumb-item"><a href="<?php echo $_SESSION['breadcrumb_list_link'];?>"><?php echo $display_label;?></a></li>
                    <li class="breadcrumb-item active"><?php if($field_display_label != ''){echo $field_display_label." - ".ucfirst($name_label);}else{echo ucfirst($name_label);} ?></li>
                  </ol>
                </nav>
            </div>
            <div class="module_name">
                <h4><?php echo ucfirst($name_label); ?> (<?php echo $count;?>) </h4>
            </div>
            <div class="row">
               
                <?php
                    if(!empty($result_data['result_count'])){
                ?>
    		  	<div class="col-md-12">
                    
                    <div id="module_detail_due_date" class="col-md-12 padding-23">
                        <!-- ALL -->
                        <!-- <a href="index.php?module=<?php echo $module_original_name;?>&action=index&dashboard=1&assigned_userid=<?php echo implode("', '",$bulk_assigned_user_id);?>&customquery=<?php echo $pass_custom_query;?>&is_user_click=<?php echo $is_user_click;?>&fromall=1" target="_blank"> 
                            //$module_original_name,$bulk_assigned_user_id,$pass_custom_query,$is_user_click,1-->
                        <a href="javascript:void(0)" target="_blank" onclick="goToCRMListview('<?php echo $module_original_name;?>','<?php echo $pass_bulkid_to_crm;?>','<?php echo $pass_custom_query;?>','<?php echo $is_user_click;?>',1);">
                        <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" id="all_count">1</b></h3>
                                    <p class="text-muted mb-0 open-sublist-color" align="center">All</p>
                                </div>
                            </div>
                        </div>
                        </a>
                        <!-- End -->
                        <?php
                            foreach ($result_data['result_count'] as $key => $value) {
                                $all_count += $value['result_count'];
                                
                        ?>
                        <!-- <a href="index.php?module=<?php echo $module_original_name;?>&action=index&dashboard=1&assigned_userid=<?php echo $value['assigned_user_id'];?>&customquery=<?php echo $pass_custom_query;?>&is_user_click=<?php echo $is_user_click;?>" target="_blank"> -->
                        <a href="javascript:void(0)" target="_blank" onclick="goToCRMListview('<?php echo $module_original_name;?>','<?php echo $value['assigned_user_id'];?>','<?php echo $pass_custom_query;?>','<?php echo $is_user_click;?>',0);">
                        <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['result_count'];?></b></h3>
                                    <p class="text-muted mb-0 open-sublist-color" align="center"><?php if(strlen(trim($value['user_name'])) == 0){ echo "N/A"; }else{ echo $value['user_name']; }?></p>
                                </div>
                            </div>
                        </div>
                         </a>
                         <?php } ?>
                         <input type="hidden" id="hidden_all_count" name="all_count" value="<?php echo $all_count;?>">

                    </div>
                </div>
                <?php
                    }
                ?>
                
               
            </div>

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