<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Role;
use App\Models\LogList;
use DB;
use Carbon\Carbon;

class UserActivityController extends Controller
{
    public $organization_group_ids_list_array = [
        62,
        98,
        99,
        100,
        101,
        102,
    ];

    public $country_group_ids_list_array = [
        63,
        64,
        65,
        66,
        67,
        68,
        69,
        70,
        71,
        72,
        73,
        74,
        75,
        76,
        77,
        78,
        79,
        80,
        81,
        82,
        83,
        84,
        85,
        86,
        87,
        88,
        89,
        90,
        91,
        92,
        93,
        94,
        95,
        96,
        97,
    ];

    public function index()
    {
        return view('rmto-preview.pages.userActivityReport');
    }

    public function printGroup(Role $role, Request $request)
    {
        $request->fromDate = $request->fromDate ? $request->fromDate . " 00:00:00" : Carbon::now()->format('Y-m-d 00:00:00') ;
        $request->toDate = $request->toDate ? $request->toDate . " 23:59:59" : Carbon::now()->format('Y-m-d 23:59:59');

        $data = [];

        $ids_list = '';
        $users = $role->users()->select("*")->get()->toArray();
        foreach ($users as $index => $item) {
            $user_activity_quantity_temp[$item['id']] = [
                "majmoe" => 0,
                "name" => $item['first_name'] ? $item['first_name']." ".$item['last_name']." (".$item['username'].")" : $item['name']." (".$item['username'].")",
            ];

            $user_activity_time_temp[$item['id']] = [
                "majmoe" => 0,
                "name" => $item['first_name'] ? $item['first_name']." ".$item['last_name']." (".$item['username'].")" : $item['name']."(".$item['username'].")",
            ];

            $user_activity_top_5_temp[$item['id']] = [
                "activity_list" => [],
                "name" => $item['first_name'] ? $item['first_name']." ".$item['last_name']." (".$item['username'].")" : $item['name']."(".$item['username'].")",
            ];


            if (!$index) {
                $ids_list = $item['id'];
            } else {
                $ids_list .= ",".$item['id'];
            }
        }
        $total_activity_time_sql = "
            SELECT sum(majmoe) AS activity FROM (SELECT *,
            COUNT(quantity) AS majmoe
            FROM (
                    SELECT user_id,
                        COUNT(*) AS quantity
                    
                            FROM user_activity_logs
                            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                        
                    GROUP BY yy,
                        mm,
                        dd,
                        hh,
                        user_id
                ) user_activity_by_hour_and_
            GROUP BY user_id) AS calculated_table;";

        $quantitative_activity_chart_sql = "
            SELECT created_at,
                COUNT(*) AS quantity
            
                        FROM user_activity_logs
                    WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
            GROUP BY 
                yy,
                mm,
                dd ;";

        $hourly_activity_chart_sql = "
            SELECT 
                COUNT(quantity) AS majmoe
            FROM (
                    SELECT yy,
                    mm,
                    dd,
                    hh,
                        COUNT(*) AS quantity
                            FROM user_activity_logs
                            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                    GROUP BY yy,
                        mm,
                        dd,
                        hh,
                        user_id
                ) user_activity_by_hour_and_
            GROUP BY 
                yy,
                mm,
                dd;";
        
        $total_activity_quantity_sql = "
            SELECT COUNT(*) AS total_activity_quantity
            FROM user_activity_logs
            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
            ";
   

        $top_10_activity_list_sql = "
                    SELECT *,
                        COUNT(*) AS quantity
                    FROM (
                            SELECT 
                                description,
                                log_unique_code
                            FROM user_activity_logs
                            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                        ) user_activity_distinct
                    GROUP BY
                        log_unique_code
                    ORDER BY  quantity desc
                    LIMIT 10";
        

        $user_activity_quantity_sql = "
            SELECT *,
                COUNT(*) AS quantity
            FROM (
                    SELECT 
                        user_id,
                        user_first_name,
                        user_last_name,
                        user_name,
                        username
                    FROM user_activity_logs
                    WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                ) user_activity_distinct
            GROUP BY user_id
            ORDER BY  quantity desc";

        $active_user_count_sql = "
            SELECT  COUNT(*) AS active_user_count
            FROM(
                SELECT 
                    user_id
                FROM user_activity_logs
                WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                GROUP BY user_id
            ) grpuped_by_user_id";

        $user_activity_time_sql = "
        select 
            *,
            COUNT(quantity) AS majmoe
        FROM (
                SELECT
                    user_first_name,
                    user_last_name,
                    user_name,
                    username,
                    user_id,
                    COUNT(*) AS quantity
                        FROM user_activity_logs
                        WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                GROUP BY yy,
                    mm,
                    dd,
                    hh,
                    user_id
            ) user_activity_by_hour_and_ 
            GROUP BY user_id
            ORDER BY majmoe desc;";

        foreach (DB::select($user_activity_quantity_sql) as $key => $value) {
            $user_activity_quantity_temp[$value->user_id] = [
                "majmoe" => $value->quantity ?? 0,
                "name" => $value->user_first_name ? $value->user_first_name." ".$value->user_last_name." (".$value->username.")" : $value->user_name." (".$value->username.")",
            ];
        }

        foreach (DB::select($user_activity_time_sql) as $key => $value) {
            $user_activity_time_temp[$value->user_id] = [
                "majmoe" => $value->majmoe ?? 0,
                "name" => $value->user_first_name ? $value->user_first_name." ".$value->user_last_name." (".$value->username.")" : $value->user_name." (".$value->username.")",
            ];
        }

        foreach ($user_activity_top_5_temp as $key => $value) {
            $user_activity_top_5_temp_sql = "
                SELECT
                    description,
                    COUNT(*) AS quantity
                FROM user_activity_logs
                WHERE user_id = {$key} AND created_at between '{$request->fromDate}' and '{$request->toDate}'
                GROUP BY log_unique_code
                ORDER BY quantity DESC
                LIMIT 5
            ";

            $activity_list = DB::select($user_activity_top_5_temp_sql);
            $sum = 0;
            foreach ($activity_list as $index => $item) {
                $sum += (int) $item->quantity;
            }
            $user_activity_top_5_temp[$key] = [
                "sum" => $sum,
                "name" => $value['name'],
                "activity_list" => $activity_list,
            ];
        }
        arsort($user_activity_top_5_temp);
        arsort($user_activity_time_temp);
        arsort($user_activity_quantity_temp);
        $data = [
            'fromDate' => $request->fromDate ? $request->fromDate : null,
            'toDate' => $request->toDate ? $request->toDate : null,
            'user_activity_time' => $ids_list ? $user_activity_time_temp : null,
            'hourly_activity_chart' => $ids_list ? DB::select($hourly_activity_chart_sql): null,
            'quantitative_activity_chart' => $ids_list ? DB::select($quantitative_activity_chart_sql): null,
            'total_activity_quantity' => $ids_list ? DB::select($total_activity_quantity_sql)[0]->total_activity_quantity : null,
            'total_activity_time' => $ids_list ? DB::select($total_activity_time_sql)[0]->activity : null,
            'top_10_activity_list' => $ids_list ? DB::select($top_10_activity_list_sql) : null,
            'user_activity_quantity' => $ids_list ? $user_activity_quantity_temp : null,
            'group_name' => $role->name_fa,
            'average' => round(($ids_list ? DB::select($total_activity_time_sql)[0]->activity : 0) / count($users), 2) ,
            'user_activity_top_5_temp' => $user_activity_top_5_temp,
            'total_user_count' => count($users),
            'active_user_count' => $ids_list ? DB::select($active_user_count_sql)[0]->active_user_count : null,
            'group_id' => $role->id
            ];
            
        return View('word.user-activity', compact('data'));
    }

    public function showUsers(Role $role)
    {
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $role->users()->get(),
        ], 200);
    }

    public function addGroup(Request $request)
    {
        if (Role::where("name_fa", $request->group_name)->first()) {
            return response()->json([
                'status' => 'error',
                'message' => 'این گروه وجود دارد.',
            ], 400);
        }
    
        $temp_name = "for_report_".time();
        Role::create(['name_fa' => $request->group_name, "for_report" => 1,'name' => $temp_name]);

        foreach (explode(',', $request->users)  as $key => $value) {
            if ($user = User::find($value)) {
                $user->assignRole($temp_name);
            }
        }
    
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => [],
        ], 200);
    }
    
    public function editGroup(Role $role, Request $request)
    {
        $role->name_fa = $request->group_name;
        $role->save();

        foreach ($role->users()->get() as $user) {
            $user->removeRole($role);
        }

        foreach (explode(',', $request->users)  as $key => $value) {
            if ($user = User::find($value)) {
                $user->assignRole($role);
            }
        }
    
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => [],
        ], 200);
    }

    public function logList()
    {
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => LogList::where("is_done", 1)->get(),
        ], 200);
    }

    public function indexShow(Request $request)
    {
        $request->fromDate = $request->fromDate ? $request->fromDate . " 00:00:00" : Carbon::now()->format('Y-m-d 00:00:00') ;
        $request->toDate = $request->toDate ? $request->toDate . " 23:59:59" : Carbon::now()->format('Y-m-d 23:59:59');

        $roles_must_show_report = Role::where("for_report", 1)->get();

        $data = [];

        $users_activity = [];

        foreach ($roles_must_show_report as $key => $value) {
            $ids_list = '';
            $users = $value->users()->select("id")->get()->toArray();
            foreach ($users as $index => $item) {
                if (!$index) {
                    $ids_list = $item['id'];
                } else {
                    $ids_list .= ",".$item['id'];
                }
            }
            $temp_sql = "
            SELECT sum(majmoe) AS activity FROM (SELECT *,
            COUNT(quantity) AS majmoe
            FROM (
                    SELECT *,
                        COUNT(*) AS quantity
                    FROM user_activity_logs
                    WHERE (created_at between '{$request->fromDate}' and '{$request->toDate}') and user_id IN(".$ids_list.")
                    GROUP BY yy,
                        mm,
                        dd,
                        hh,
                        user_id
                ) user_activity_by_hour_and_
            GROUP BY user_id) AS calculated_table;";

            $activity = DB::select($temp_sql)[0]->activity;
            $users_activity[] = [
            'activity_time' => $ids_list ? $activity : null,
            'average' => round(($ids_list ? $activity : 0) / count($users), 2) ,
            'group_name' => $value->name_fa,
            'user_count' => count($users),
            'is_static' => 0,
            'group_id' => $value->id
            ];
        }

        $country_group_ids_list = "";
        $country_group_user_quantity = 0;
        foreach (Role::whereIn('id', $this->country_group_ids_list_array)->get() as $key => $value) {
            $users = $value->users()->select("id")->get();
            $country_group_user_quantity += count($users);
            foreach ($users as $index => $item) {
                if (!$key) {
                    $country_group_ids_list = $item->id;
                } else {
                    $country_group_ids_list .= ",".$item->id;
                }
            }
        }

        $country_temp_sql = "
        SELECT sum(majmoe) AS activity FROM (SELECT *,
        COUNT(quantity) AS majmoe
        FROM (
                SELECT *,
                    COUNT(*) AS quantity
                        FROM user_activity_logs
                        WHERE (created_at between '{$request->fromDate}' and '{$request->toDate}') and user_id IN(".$country_group_ids_list.")
                GROUP BY yy,
                    mm,
                    dd,
                    hh,
                    user_id
            ) user_activity_by_hour_and_
        GROUP BY user_id) AS calculated_table;";

        $users_activity[] = [
            'activity_time' => $country_group_ids_list ? DB::select($country_temp_sql)[0]->activity : null,
            'average' => round(($country_group_ids_list ? DB::select($country_temp_sql)[0]->activity : 0) / $country_group_user_quantity, 2) ,
            'group_name' => 'کشوری',
            'user_count' => $country_group_user_quantity,
            'is_static' => 1,
            'group_id' => 3,
            ];

        $organization_group_ids_list = "";
        $organization_group_user_quantity = 0;
        foreach (Role::whereIn('id', $this->organization_group_ids_list_array)->get() as $key => $value) {
            $users = $value->users()->select("id")->get();
            $organization_group_user_quantity += count($users);
            foreach ($users as $index => $item) {
                if (!$key) {
                    $organization_group_ids_list = $item->id;
                } else {
                    $organization_group_ids_list .= ",".$item->id;
                }
            }
        }
            
        $organization_temp_sql = "
            SELECT sum(majmoe) AS activity FROM (SELECT *,
            COUNT(quantity) AS majmoe
            FROM (
                    SELECT *,
                        COUNT(*) AS quantity
                            FROM user_activity_logs
                            WHERE (created_at between '{$request->fromDate}' and '{$request->toDate}') and user_id IN(".$organization_group_ids_list.")
                        
                    GROUP BY yy,
                        mm,
                        dd,
                        hh,
                        user_id
                ) user_activity_by_hour_and_
            GROUP BY user_id) AS calculated_table;";
    
        $users_activity[] = [
                'activity_time' => $organization_group_ids_list ? DB::select($organization_temp_sql)[0]->activity : null,
                'average' => round(($organization_group_ids_list ? DB::select($organization_temp_sql)[0]->activity : 0) / $organization_group_user_quantity, 2) ,
                'group_name' => 'سازمانی',
                'user_count' => $organization_group_user_quantity,
                'is_static' => 2,
                'group_id' => 3,
                ];

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $users_activity,
        ], 200);
    }

    public function deleteGroup(Role $role)
    {
        $users = User::whereHas("roles", function ($query) use ($role) {
            $query->where("id", $role->id);
        })->get();
        
        foreach ($users as $key => $value) {
            $value->roles()->detach($role);
            $value->load('roles');
            $value->forgetCachedPermissions();
        }

        $role->delete();
    }

    public function roadItemProjectReport(Request $request)
    {
        Auth::User()->addActivity("این کاربر وارد قسمت گزارش فعالیت در صفحه فعالیت های جاری روزانه شده است.");
    }

    public function userActivityLogs(User $user)
    {
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $user->activityLogs,
        ], 200);
    }

    public function staticPrintCountryChart(Request $request)
    {
        $request->fromDate = $request->fromDate ? $request->fromDate . " 00:00:00" : Carbon::now()->format('Y-m-d 00:00:00') ;
        $request->toDate = $request->toDate ? $request->toDate . " 23:59:59" : Carbon::now()->format('Y-m-d 23:59:59');

        $data = [];

        $total_ids_list = ''; //  یوزرهای کل این سوپر گروه
        foreach ($this->country_group_ids_list_array as $key => $value) {
            $role = Role::find($value);

            $ids_list = ''; // یوزر های این گروه
            $users = $role->users()->select("id")->get()->toArray();
            foreach ($users as $index => $item) {
                if (!$index) {
                    $ids_list = $item['id'];
                } else {
                    $ids_list .= ",".$item['id'];
                }
            }

            if (!$key) {
                $total_ids_list = $ids_list;
            } else {
                $total_ids_list .= ",".$ids_list;
            }
            
            // تعداد کل فعالیت ها برای هر گروه این سوپر گروه
            $quantitative_activity_chart_sql = "
                SELECT 
                    COUNT(*) AS quantity
                            FROM user_activity_logs
                        WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                    
                GROUP BY 
                    yy,
                    mm,
                    dd ;";
                    
            // تعدا کل ساعت های برای هر گروه این سوپر گروه
            $hourly_activity_chart_sql = "
                SELECT 
                    COUNT(quantity) AS majmoe
                FROM (
                        SELECT *,
                            COUNT(*) AS quantity
                                FROM user_activity_logs
                                WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                        GROUP BY yy,
                            mm,
                            dd,
                            hh,
                            user_id
                    ) user_activity_by_hour_and_
                GROUP BY 
                    yy,
                    mm,
                    dd;";

            $data['labels'][] = $role->name_fa;
            $data['hourly_activity_chart'][] = isset(DB::select(DB::raw($hourly_activity_chart_sql))[0]->majmoe) ? DB::select(DB::raw($hourly_activity_chart_sql))[0]->majmoe: 0;
            $data['quantitative_activity_chart'][] = isset(DB::select(DB::raw($quantitative_activity_chart_sql))[0]->quantity) ? DB::select(DB::raw($quantitative_activity_chart_sql))[0]->quantity: 0;
        }

        $total_activity_quantity_sql = "
            SELECT COUNT(*) AS total_activity_quantity
            FROM user_activity_logs
            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
            ";

        $data['total_activity_quantity'] = $total_ids_list ? DB::select(DB::raw($total_activity_quantity_sql))[0]->total_activity_quantity : null;

        $data['total_user_count'] = count(explode(',', $total_ids_list));

        $active_user_count_sql = "
            SELECT  COUNT(*) AS active_user_count
            FROM(
                SELECT 
                    user_id
                FROM user_activity_logs
                WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
                GROUP BY user_id
            ) grpuped_by_user_id";

        $data['active_user_count'] = $total_ids_list ? DB::select(DB::raw($active_user_count_sql))[0]->active_user_count : null;
        
        $total_activity_time_sql = "
            SELECT sum(majmoe) AS activity FROM (SELECT *,
            COUNT(quantity) AS majmoe
            FROM (
                    SELECT *,
                        COUNT(*) AS quantity
                            FROM user_activity_logs
                            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
                    GROUP BY yy,
                        mm,
                        dd,
                        hh,
                        user_id
                ) user_activity_by_hour_and_
            GROUP BY user_id) AS calculated_table;";
        
        $data['total_activity_time'] = $total_ids_list ? DB::select(DB::raw($total_activity_time_sql))[0]->activity : null;
        
        $data['average'] = round(($total_ids_list ? DB::select(DB::raw($total_activity_time_sql))[0]->activity : 0) / count(explode(',', $total_ids_list)), 2);

        $top_10_activity_list_sql = "
            SELECT *,
                COUNT(*) AS quantity
            FROM (
                    SELECT 
                        description,
                        log_unique_code
                    FROM user_activity_logs
                    WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
                ) user_activity_distinct
            GROUP BY
                log_unique_code
            ORDER BY  quantity desc
            LIMIT 10";
        
        $data['top_10_activity_list'] = $total_ids_list ? DB::select(DB::raw($top_10_activity_list_sql)) : null;
        

        $data['fromDate'] = $request->fromDate ? $request->fromDate : null;
        $data['toDate'] = $request->toDate ? $request->toDate : null;
        $data['group_name'] = 'کشوری';
        return View('word.static-print-country-chart', compact('data'));
    }

    public function staticPrintOrganizationChart(Request $request)
    {
        $request->fromDate = $request->fromDate ? $request->fromDate . " 00:00:00" : Carbon::now()->format('Y-m-d 00:00:00') ;
        $request->toDate = $request->toDate ? $request->toDate . " 23:59:59" : Carbon::now()->format('Y-m-d 23:59:59');

        $data = [];
        $total_ids_list = ''; //  یوزرهای کل این سوپر گروه
        foreach ($this->organization_group_ids_list_array as $key => $value) {
            $role = Role::find($value);

            $ids_list = ''; // یوزر های این گروه
            $users = $role->users()->select("id")->get()->toArray();
            foreach ($users as $index => $item) {
                if (!$index) {
                    $ids_list = $item['id'];
                } else {
                    $ids_list .= ",".$item['id'];
                }
            }

            if (!$key) {
                $total_ids_list = $ids_list;
            } else {
                $total_ids_list .= ",".$ids_list;
            }

            // تعداد کل فعالیت ها برای هر گروه این سوپر گروه
            $quantitative_activity_chart_sql = "
                SELECT 
                    COUNT(*) AS quantity
                            FROM user_activity_logs
                        WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                GROUP BY 
                    yy,
                    mm,
                    dd ;";

            // تعدا کل ساعت های برای هر گروه این سوپر گروه
            $hourly_activity_chart_sql = "
                SELECT 
                    COUNT(quantity) AS majmoe
                FROM (
                        SELECT *,
                            COUNT(*) AS quantity
                                FROM user_activity_logs
                                WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$ids_list.")
                        GROUP BY yy,
                            mm,
                            dd,
                            hh,
                            user_id
                    ) user_activity_by_hour_and_
                GROUP BY 
                    yy,
                    mm,
                    dd;";

            $data['labels'][] = $role->name_fa;
            $data['hourly_activity_chart'][] = isset(DB::select($hourly_activity_chart_sql)[0]->majmoe) ? DB::select($hourly_activity_chart_sql)[0]->majmoe: 0;
            $data['quantitative_activity_chart'][] = isset(DB::select($quantitative_activity_chart_sql)[0]->quantity) ? DB::select($quantitative_activity_chart_sql)[0]->quantity: 0;
        }

        
        $total_activity_quantity_sql = "
            SELECT COUNT(*) AS total_activity_quantity
            FROM user_activity_logs
            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
            ";

        $data['total_activity_quantity'] = $total_ids_list ? DB::select($total_activity_quantity_sql)[0]->total_activity_quantity : null;

        $data['total_user_count'] = count(explode(',', $total_ids_list));

        $active_user_count_sql = "
            SELECT  COUNT(*) AS active_user_count
            FROM(
                SELECT 
                    user_id
                FROM user_activity_logs
                WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
                GROUP BY user_id
            ) grpuped_by_user_id";

        $data['active_user_count'] = $total_ids_list ? DB::select($active_user_count_sql)[0]->active_user_count : null;
        
        $total_activity_time_sql = "
            SELECT sum(majmoe) AS activity FROM (SELECT *,
            COUNT(quantity) AS majmoe
            FROM (
                    SELECT *,
                        COUNT(*) AS quantity
                            FROM user_activity_logs
                            WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
                    GROUP BY yy,
                        mm,
                        dd,
                        hh,
                        user_id
                ) user_activity_by_hour_and_
            GROUP BY user_id) AS calculated_table;";
        
        $data['total_activity_time'] = $total_ids_list ? DB::select($total_activity_time_sql)[0]->activity : null;
        
        $data['average'] = round(($total_ids_list ? DB::select($total_activity_time_sql)[0]->activity : 0) / count(explode(',', $total_ids_list)), 2);

        $top_10_activity_list_sql = "
            SELECT *,
                COUNT(*) AS quantity
            FROM (
                    SELECT 
                        description,
                        log_unique_code
                    FROM user_activity_logs
                    WHERE created_at between '{$request->fromDate}' and '{$request->toDate}' and user_id IN(".$total_ids_list.")
                ) user_activity_distinct
            GROUP BY
                log_unique_code
            ORDER BY  quantity desc
            LIMIT 10";
        
        $data['top_10_activity_list'] = $total_ids_list ? DB::select($top_10_activity_list_sql) : null;
        

        $data['fromDate'] = $request->fromDate ? $request->fromDate : null;
        $data['toDate'] = $request->toDate ? $request->toDate : null;
        $data['group_name'] = 'سازمان';
        return View('word.static-print-organization-chart', compact('data'));
    }
}
