<?php

namespace App\Http\Controllers;

use App\Models\City;
use App\Models\Province;
use App\Models\InfoItem;
use Carbon\Carbon;

use Illuminate\Http\Request;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Facades\DB;

class BiController extends Controller
{
    public function index(Request $request){
        $project_years = [];
        $v = verta();
        $y = $v->format('Y');
        for($i=1395; $i<=$y; $i++){
            $project_years[$i] = 0;
        }
        $axis_type = ['1' => 0, '2' => 0, '3' => 0, '4' => 0];
        $project_status = [0, 0, 0, 0, 0, 0, 0, 0, 0];
        $province = [
            '1' =>0,
            '2' =>0,
            '3' =>0,
            '4' =>0,
            '5' =>0,
            '6' =>0,
            '7' =>0,
            '8' =>0,
            '9' =>0,
            '10' =>0,
            '11' =>0,
            '12' =>0,
            '13' =>0,
            '14' =>0,
            '15' =>0,
            '16' =>0,
            '17' =>0,
            '18' =>0,
            '19' =>0,
            '20' =>0,
            '21' =>0,
            '22' =>0,
            '23' =>0,
            '24' =>0,
            '25' =>0,
            '26' =>0,
            '27' =>0,
            '28' =>0,
            '29' =>0,
            '30' =>0,
            '31' =>0,
            '32' =>0,
            '33' =>0,
            '34' =>0,
            '35' =>0,
        ];
        $upgrade_safety_count = 0; 
        $technical_building_count = 0; 
        $construction_rural_count = 0;
        $tollhouse_management_count = 0;
        $maintenance_project_count = 0;
        $danger_prevention_count = 0;
        $tollhouse_management = null;
        $danger_prevention = null;
        $technical_building = null;
        $maintenance_project = null;
        $upgrade_safety = null;
        $city = null;
        $pt = $request->input('pt') ?? null;
        $ps = $request->input('ps') ?? null;
        $at = $request->input('at') ?? null;
        $py = $request->input('py') ?? null;
        $pp = $request->input('pp') ?? null;
        $pc = $request->input('pc') ?? null;
        $maintenance_project = DB::table('road_maintenance_projects')
                                ->when(!empty($pp), function ($query) use ($pp) {
                                    return $query->where('province_id', $pp);
                                })
                                ->when(!empty($py), function ($query) use ($py) {
                                    if(1395 == $py){
                                        return $query->where('contract_date', '<=', 1395);
                                    }
                                    return $query->where('contract_date', $py);
                                })
                                ->when(!empty($pc), function ($query) use ($pc) {
                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                })
                                ->where('status', 2)
                                ->select('road_maintenance_projects.id as i', 
                                DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_maintenance_projects.start_latlng, \"$[0]\")) AS l" ),
                                DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_maintenance_projects.start_latlng, \"$[1]\")) AS g" ), 
                                'road_maintenance_projects.province_fa as p', 'road_maintenance_projects.axis_type',
                                'road_maintenance_projects.city_1_fa as c1', 'road_maintenance_projects.city_2_fa as c2', 
                                'road_maintenance_projects.province_id', 'road_maintenance_projects.project_status', 
                                'road_maintenance_projects.city_3_fa as c3', 'road_maintenance_projects.axis_type_fa as a',
                                    'road_maintenance_projects.project_name as pn', 'road_maintenance_projects.contract_date as d', 
                                    'road_maintenance_projects.axis_name as an', 'road_maintenance_projects.project_status_fa as ps')
                                ->get();
        $maintenance_project_count = $maintenance_project->count();
        $danger_prevention = DB::table('road_danger_preventions')
                                ->when(!empty($pp), function ($query) use ($pp) {
                                    return $query->where('province_id', $pp);
                                })
                                ->when(!empty($py), function ($query) use ($py) {
                                    if(1395 == $py){
                                        return $query->where('contract_date', '<=', 1395);
                                    }
                                    return $query->where('contract_date', $py);
                                })
                                ->when(!empty($pc), function ($query) use ($pc) {
                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                })
                                ->where('status', 2)
                                ->select('road_danger_preventions.id as i', 
                                DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_danger_preventions.start_latlng, \"$[0]\")) AS l" ),
                                DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_danger_preventions.start_latlng, \"$[1]\")) AS g" ), 
                                'road_danger_preventions.province_fa as p', 'road_danger_preventions.axis_type',
                                'road_danger_preventions.city_1_fa as c1', 'road_danger_preventions.city_2_fa as c2', 
                                'road_danger_preventions.province_id', 'road_danger_preventions.project_status', 
                                'road_danger_preventions.city_3_fa as c3', 'road_danger_preventions.axis_type_fa as a',
                                'road_danger_preventions.project_name as pn', 'road_danger_preventions.contract_date as d', 
                                'road_danger_preventions.axis_name as an', 'road_danger_preventions.project_status_fa as ps')
                                ->get();
        $danger_prevention_count = $danger_prevention->count();
        $upgrade_safety = DB::table('road_upgrade_safeties')
                            ->when(!empty($pp), function ($query) use ($pp) {
                                return $query->where('province_id', $pp);
                            })
                            ->when(!empty($py), function ($query) use ($py) {
                                if(1395 == $py){
                                    return $query->where('contract_date', '<=', 1395);
                                }
                                return $query->where('contract_date', $py);
                            })
                            ->when(!empty($pc), function ($query) use ($pc) {
                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                            })
                            ->where('status', 2)
                            ->select('road_upgrade_safeties.id as i', 
                            DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_upgrade_safeties.start_latlng, \"$[0]\")) AS l" ),
                            DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_upgrade_safeties.start_latlng, \"$[1]\")) AS g" ), 
                            'road_upgrade_safeties.province_fa as p', 'road_upgrade_safeties.axis_type',
                            'road_upgrade_safeties.city_1_fa as c1', 'road_upgrade_safeties.city_2_fa as c2', 
                            'road_upgrade_safeties.province_id', 'road_upgrade_safeties.project_status', 
                            'road_upgrade_safeties.city_3_fa as c3', 'road_upgrade_safeties.axis_type_fa as a',
                            'road_upgrade_safeties.project_name as pn', 'road_upgrade_safeties.contract_date as d', 
                            'road_upgrade_safeties.axis_name as an', 'road_upgrade_safeties.project_status_fa as ps')
                            ->get();
        $upgrade_safety_count = $upgrade_safety->count();
        $technical_building = DB::table('road_technical_buildings')
                                ->when(!empty($pp), function ($query) use ($pp) {
                                    return $query->where('province_id', $pp);
                                })
                                ->when(!empty($py), function ($query) use ($py) {
                                    if(1395 == $py){
                                        return $query->where('contract_date', '<=', 1395);
                                    }
                                    return $query->where('contract_date', $py);
                                })
                                ->when(!empty($pc), function ($query) use ($pc) {
                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                })
                                ->where('status', 2)
                                ->select('road_technical_buildings.id as i', 
                                DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_technical_buildings.start_latlng, \"$[0]\")) AS l" ),
                                DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_technical_buildings.start_latlng, \"$[1]\")) AS g" ), 
                                'road_technical_buildings.province_fa as p', 'road_technical_buildings.axis_type',
                                'road_technical_buildings.city_1_fa as c1', 'road_technical_buildings.city_2_fa as c2', 
                                'road_technical_buildings.province_id', 'road_technical_buildings.project_status', 
                                'road_technical_buildings.city_3_fa as c3', 'road_technical_buildings.axis_type_fa as a',
                                'road_technical_buildings.project_name as pn', 'road_technical_buildings.contract_date as d', 
                                'road_technical_buildings.axis_name as an', 'road_technical_buildings.project_status_fa as ps')
                                ->get();
        $technical_building_count = $technical_building->count();
        $tollhouse_management = DB::table('road_tollhouse_managements')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($py), function ($query) use ($py) {
                                        if(1395 == $py){
                                            return $query->where('contract_date', '<=', 1395);
                                        }
                                        return $query->where('contract_date', $py);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                    })
                                    ->where('status', 2)
                                    ->select('road_tollhouse_managements.id as i', 
                                    DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_tollhouse_managements.start_latlng, \"$[0]\")) AS l" ),
                                    DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_tollhouse_managements.start_latlng, \"$[1]\")) AS g" ), 
                                    'road_tollhouse_managements.province_fa as p', 'road_tollhouse_managements.axis_type',
                                    'road_tollhouse_managements.city_1_fa as c1', 'road_tollhouse_managements.city_2_fa as c2', 
                                    'road_tollhouse_managements.province_id', 'road_tollhouse_managements.project_status', 
                                    'road_tollhouse_managements.city_3_fa as c3', 'road_tollhouse_managements.axis_type_fa as a',
                                    'road_tollhouse_managements.project_name as pn', 'road_tollhouse_managements.contract_date as d', 
                                    'road_tollhouse_managements.axis_name as an', 'road_tollhouse_managements.project_status_fa as ps')
                                    ->get();
        $tollhouse_management_count = $tollhouse_management->count();
    
        $count_all = $upgrade_safety_count + $technical_building_count
            + $tollhouse_management_count + $maintenance_project_count 
            + $danger_prevention_count;
        $all_count = array( "upgrade_safety_count" => $upgrade_safety_count,
                            "danger_prevention_count" => $danger_prevention_count,
                            "technical_building_count" => $technical_building_count,
                            "maintenance_project_count" => $maintenance_project_count,
                            "tollhouse_management_count" => $tollhouse_management_count
            );
        $all = array( "tollhouse_management" => $tollhouse_management,
                    "danger_prevention" => $danger_prevention,
                    "technical_building" => $technical_building,
                    "maintenance_project" => $maintenance_project,
                    "upgrade_safety" => $upgrade_safety
            );
            // return $all;
        foreach($all as $key => $value)
            if($value != null)
                foreach($value as $key1 => $data){
                    switch($data->axis_type){
                        case 1: 
                            $axis_type['1']++;
                        break;
                        case 2: 
                            $axis_type['2']++;
                        break;
                        case 3: 
                            $axis_type['3']++;
                        break;
                        case 4: 
                            $axis_type['4']++;
                        break;
                    }
                    switch($data->province_id){                        
                        case 1: 
                            $province['1']++;
                        break;                        
                        case 2: 
                            $province['2']++;
                        break;                        
                        case 3: 
                            $province['3']++;
                        break;                        
                        case 4: 
                            $province['4']++;
                        break;                        
                        case 5: 
                            $province['5']++;
                        break;                        
                        case 6: 
                            $province['6']++;
                        break;                        
                        case 7: 
                            $province['7']++;
                        break;                        
                        case 8: 
                            $province['8']++;
                        break;                        
                        case 9: 
                            $province['9']++;
                        break;                        
                        case 10: 
                            $province['10']++;
                        break;                        
                        case 11: 
                            $province['11']++;
                        break;                        
                        case 12: 
                            $province['12']++;
                        break;                        
                        case 13: 
                            $province['13']++;
                        break;                        
                        case 14: 
                            $province['14']++;
                        break;                        
                        case 15: 
                            $province['15']++;
                        break;                        
                        case 16: 
                            $province['16']++;
                        break;                        
                        case 17: 
                            $province['17']++;
                        break;                        
                        case 18: 
                            $province['18']++;
                        break;                        
                        case 19: 
                            $province['19']++;
                        break;                        
                        case 20: 
                            $province['20']++;
                        break;                        
                        case 21: 
                            $province['21']++;
                        break;                        
                        case 22: 
                            $province['22']++;
                        break;                        
                        case 23: 
                            $province['23']++;
                        break;                        
                        case 24: 
                            $province['24']++;
                        break;                        
                        case 25: 
                            $province['25']++;
                        break;                        
                        case 26: 
                            $province['26']++;
                        break;                        
                        case 27: 
                            $province['27']++;
                        break;                        
                        case 28: 
                            $province['28']++;
                        break;                        
                        case 29: 
                            $province['29']++;
                        break;                        
                        case 30: 
                            $province['30']++;
                        break;                        
                        case 31: 
                            $province['31']++;
                        break;                        
                        case 32: 
                            $province['32']++;
                        break;                        
                        case 33: 
                            $province['33']++;
                        break;                        
                        case 34: 
                            $province['34']++;
                        break;                        
                        case 35: 
                            $province['35']++;
                        break;                        
                    }
                    ($data->d <= 1395)?$project_years['1395']++:$project_years[$data->d]++;
                    $project_status[$data->project_status]++;
                }
        if($py != null){//// avg progress physic
            if($py == 1395){
                $avg = DB::select(DB::raw(
                "SELECT AVG(i) AS ag FROM(
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\"))
                            ELSE 0
                        END AS i
                    FROM road_maintenance_projects
                    WHERE project_distance_update IS NOT NULL and contract_date <= 1395)q
                UNION 
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                    FROM road_danger_preventions
                    WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qq
                UNION 
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                    FROM road_upgrade_safeties
                    WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qqq
                UNION
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                    FROM road_technical_buildings
                    WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qqqq
                UNION
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                    FROM road_tollhouse_managements
                    WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qqqqq)z")); 
            }
            else
            {
                $avg = DB::select(DB::raw("SELECT AVG(i) AS ag FROM(SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                CASE
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\"))
                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\"))
                ELSE 0
                END AS i
                FROM road_maintenance_projects
                WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                UNION 
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                            CASE
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                ELSE 0
                            END AS i
                            FROM road_danger_preventions
                            WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                UNION 
                SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                            CASE
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                ELSE 0
                            END AS i
                            FROM road_upgrade_safeties
                            WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                UNION
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                            CASE
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                ELSE 0
                            END AS i
                            FROM road_technical_buildings
                            WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                UNION
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                            CASE
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                ELSE 0
                            END AS i
                            FROM road_tollhouse_managements
                            WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q)z"));  
            }
            }
        else{
            $avg = DB::select(DB::raw("SELECT AVG(i) AS ag FROM(SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
            CASE
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\"))
                WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\"))
            ELSE 0
            END AS i
            FROM road_maintenance_projects
            WHERE project_distance_update IS NOT NULL )q
            UNION 
                        SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                        FROM road_danger_preventions
                        WHERE project_distance_update IS NOT NULL )q
            UNION 
            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                        FROM road_upgrade_safeties
                        WHERE project_distance_update IS NOT NULL )q
            UNION
                SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                        FROM road_technical_buildings
                        WHERE project_distance_update IS NOT NULL )q
            UNION
                SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                            ELSE 0
                        END AS i
                        FROM road_tollhouse_managements
                        WHERE project_distance_update IS NOT NULL )q)z"));
            }
        if($pp != null){/// pahne shahrestan
            if($py != null){
                if($py == 1395){
                    $city = DB::select(DB::raw("SELECT city_fa,city_id,sum(cnt) as cnt FROM (
                        SELECT 1 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_maintenance_projects WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 2 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_danger_preventions WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 4 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_technical_buildings WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 3 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_upgrade_safeties WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 5 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_tollhouse_managements WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa)q 
                    GROUP BY city_fa"));
                }
                else {
                    $city = DB::select(DB::raw("SELECT city_fa,city_id,sum(cnt) as cnt FROM (
                        SELECT 1 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_maintenance_projects WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 2 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_danger_preventions WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 4 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_technical_buildings WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 3 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_upgrade_safeties WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                        UNION
                        SELECT 5 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                        COUNT(*) AS cnt FROM road_tollhouse_managements WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa)q 
                    GROUP BY city_fa"));
                }
            }
            else {
                $city = DB::select(DB::raw("SELECT city_fa,city_id,sum(cnt) as cnt FROM (
                    SELECT 1 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                    COUNT(*) AS cnt FROM road_maintenance_projects WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                    UNION
                    SELECT 2 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                    COUNT(*) AS cnt FROM road_danger_preventions WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                    UNION
                    SELECT 4 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                    COUNT(*) AS cnt FROM road_technical_buildings WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                    UNION
                    SELECT 3 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                    COUNT(*) AS cnt FROM road_upgrade_safeties WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                    UNION
                    SELECT 5 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                    COUNT(*) AS cnt FROM road_tollhouse_managements WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa)q 
                GROUP BY city_fa"));
            }
            }
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'rahdari' => [
                'p_ostani' => ($city)?$city:null,
                'avg' => $avg,    
                'counts' => $all_count,
                'count_all' => $count_all,
                'axis_type_count' => $axis_type,
                'p_keshvari' => $province,
                'project_status' => $project_status,
                'project_years' => $project_years
            ]
        ], 200);
    }
    public function index_n(Request $request){
        $pt = $request->input('pt') ?? null;
        $ps = $request->input('ps') ?? null;
        $at = $request->input('at') ?? null;
        $py = $request->input('y') ?? null;
        $pp = $request->input('p') ?? null;
        $pc = $request->input('c') ?? null;
        $i = $request->i ?? null;
        switch ($i) {
            case 2:
                $maintenance_project_count = DB::table('road_maintenance_projects')
                                                ->when(!empty($pp), function ($query) use ($pp) {
                                                    return $query->where('province_id', $pp);
                                                })
                                                ->when(!empty($py), function ($query) use ($py) {
                                                    if(1395 == $py){
                                                        return $query->where('contract_date', '<=', 1395);
                                                    }
                                                    return $query->where('contract_date', $py);
                                                })
                                                ->when(!empty($pc), function ($query) use ($pc) {
                                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                                })
                                                ->where('status', 2)
                                                ->get()->count();
                $danger_prevention_count = DB::table('road_danger_preventions')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $upgrade_safety_count = DB::table('road_upgrade_safeties')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $technical_building_count = DB::table('road_technical_buildings')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $tollhouse_management_count = DB::table('road_tollhouse_managements')
                                            ->when(!empty($pp), function ($query) use ($pp) {
                                                return $query->where('province_id', $pp);
                                            })
                                            ->when(!empty($py), function ($query) use ($py) {
                                                if(1395 == $py){
                                                    return $query->where('contract_date', '<=', 1395);
                                                }
                                                return $query->where('contract_date', $py);
                                            })
                                            ->when(!empty($pc), function ($query) use ($pc) {
                                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                            })
                                            ->where('status', 2)
                                            ->get()->count();            
                $count_all_rahradi = $upgrade_safety_count + $technical_building_count
                                    + $tollhouse_management_count + $maintenance_project_count 
                                    + $danger_prevention_count;
                ///// faaliat rozane jari
                $road_items = DB::table('road_items_projects')
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->select('city_id', DB::raw('count(*) as total'))
                                            ->where('province_id', $pp)->groupBy('city_id');
                                        })
                                        ->when(empty($pp), function ($query) use ($pp) {
                                            return $query->select('province_id', DB::raw('count(*) as total'))
                                                         ->groupBy('province_id');
                                        })
                                        ->get();
                $road_items_chart = DB::table('road_items_projects')
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->select('item', DB::raw('count(*) as total'))
                                            ->where('province_id', $pp);
                                        })
                                        ->when(empty($pp), function ($query) use ($pp) {
                                            return $query->select('road_items_projects.item', DB::raw('count(*) as total'));
                                        })
                                        // ->join('info_items', 'road_items_projects.item','=', 'info_items.item')
                                        ->groupBy('road_items_projects.item')
                                        ->get();
                $road_items_count = DB::table('road_items_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $road_patrols_count = DB::table('road_patrol_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $observeds_count_ok = DB::table('road_observeds')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('rms_province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('rms_city_id', $pc);
                                        })
                                        ->where('rms_status', '<>', 0)
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $observeds_count_nok = DB::table('road_observeds')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('rms_province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('rms_city_id', $pc);
                                        })
                                        ->where('rms_status', '=', 0)
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $points_counts = DB::table('accident_points')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->select( DB::raw('count(*) as total'))
                                            ->where('province_id', $pp);
                                        })
                                        ->when(empty($pp), function ($query) use ($pp) {
                                            return $query->select(DB::raw('count(*) as total'));
                                        })
                                        ->get();
                $items_fa = InfoItem::groupBy('item')->get();
                foreach($road_items_chart as $value){
                    foreach($items_fa as $val){
                        if($value->item == $val->item)
                        $value->item_fa = $val->item_str;
                    }
                }
                if(!empty($pp)){
                    $province = City::where('province_id', $pp)
                                    ->get();
                    foreach($road_items as $value){
                        foreach($province as $val){
                            if($value->city_id == $val->id)
                        $value->city_fa = $val->name_fa;
                        }
                    }
                }
                return response()->json([
                    'status' => 'succeed',
                    'message' => '',
                    'items' => [
                        // 'p_ostani' => ($city)?$city:null,
                        // 'avg' => $avg,    
                        'item' => $road_items_chart,
                        // 'count_all' => $count_all,
                        // 'axis_type_count' => $axis_type,
                        // 'p_keshvari' => $province,
                        // 'project_status' => $project_status,
                        // 'project_years' => $project_years
                    ],
                    'pahne' => $road_items,
                    'counts' => [
                        'rahdari' => $count_all_rahradi,
                        'items'   => $road_items_count[0]->total,
                        'patrols'   => $road_patrols_count[0]->total,
                        'observds' => [
                            'nok' => $observeds_count_nok[0]->total,
                            'ok' => $observeds_count_ok[0]->total,
                            'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total
                        ],
                        'acc_points' => $points_counts[0]->total,
                    ]
                ], 200);
                ///// end faaliat rozane jari
                break;
            case 3:
                //// start patrol
                $road_patrol = DB::table('road_patrol_projects')
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('city_id', $pc);
                                    })
                                    // ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->select('city_id', DB::raw('count(*) as total'))
                                        ->where('province_id', $pp)->groupBy('city_id');
                                    })
                                    ->when(empty($pp), function ($query) use ($pp) {
                                        return $query->select('province_id', DB::raw('count(*) as total'))
                                                     ->groupBy('province_id');
                                    })
                                    ->get();
                $road_patrols_count = DB::table('road_patrol_projects')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('city_id', $pc);
                                    })
                                    // ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                /// end patrol
                $road_items_count = DB::table('road_items_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $maintenance_project_count = DB::table('road_maintenance_projects')
                                                ->when(!empty($pp), function ($query) use ($pp) {
                                                    return $query->where('province_id', $pp);
                                                })
                                                ->when(!empty($py), function ($query) use ($py) {
                                                    if(1395 == $py){
                                                        return $query->where('contract_date', '<=', 1395);
                                                    }
                                                    return $query->where('contract_date', $py);
                                                })
                                                ->when(!empty($pc), function ($query) use ($pc) {
                                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                                })
                                                ->where('status', 2)
                                                ->get()->count();
                $danger_prevention_count = DB::table('road_danger_preventions')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $upgrade_safety_count = DB::table('road_upgrade_safeties')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $technical_building_count = DB::table('road_technical_buildings')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $tollhouse_management_count = DB::table('road_tollhouse_managements')
                                            ->when(!empty($pp), function ($query) use ($pp) {
                                                return $query->where('province_id', $pp);
                                            })
                                            ->when(!empty($py), function ($query) use ($py) {
                                                if(1395 == $py){
                                                    return $query->where('contract_date', '<=', 1395);
                                                }
                                                return $query->where('contract_date', $py);
                                            })
                                            ->when(!empty($pc), function ($query) use ($pc) {
                                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                            })
                                            ->where('status', 2)
                                            ->get()->count();            
                $count_all_rahradi = $upgrade_safety_count + $technical_building_count
                                    + $tollhouse_management_count + $maintenance_project_count 
                                    + $danger_prevention_count;
                $observeds_count_ok = DB::table('road_observeds')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('rms_province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('rms_city_id', $pc);
                                    })
                                    ->where('rms_status', '<>', 0)
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $observeds_count_nok = DB::table('road_observeds')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('rms_province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('rms_city_id', $pc);
                                    })
                                    ->where('rms_status', '=', 0)
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $points_counts = DB::table('accident_points')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->select( DB::raw('count(*) as total'))
                                        ->where('province_id', $pp);
                                    })
                                    ->when(empty($pp), function ($query) use ($pp) {
                                        return $query->select(DB::raw('count(*) as total'));
                                    })
                                    ->get();
                if(!empty($pp)){
                    $province = City::where('province_id', $pp)
                                    ->get();
                    foreach($road_patrol as $value){
                        foreach($province as $val){
                            if($value->city_id == $val->id)
                        $value->city_fa = $val->name_fa;
                        }
                    }
                }
                return response()->json([
                    'status' => 'succeed',
                    'message' => '',
                    'patrols' => [
                    ],
                    'pahne' => $road_patrol,
                    'counts' => [
                        'rahdari' => $count_all_rahradi,
                        'items'   => $road_items_count[0]->total,
                        'patrols'   => $road_patrols_count[0]->total,
                        'observds' => [
                            'nok' => $observeds_count_nok[0]->total,
                            'ok' => $observeds_count_ok[0]->total,
                            'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total
                        ],
                        'acc_points' => $points_counts[0]->total,
                    ]
                ], 200);
                break;
            case 4:
                //// shekayat barresi shode
                if($pp != null){
                    if($pc !=null){
                        $sql = "SELECT city_id,ROUND(cnt/sall* 100) AS total FROM (
                            SELECT *,SUM(cnt) AS sall FROM (
                                SELECT * FROM (SELECT 1 AS t,rms_city_id AS city_id
                                    ,COUNT(*) AS cnt FROM road_observeds WHERE rms_status <> 0 AND rms_province_id = ".$pp." AND rms_city_id = ".$pc.")done
                                UNION 
                                SELECT * FROM (SELECT 2 AS t,rms_city_id AS city_id,COUNT(*) AS cnt
                                 FROM road_observeds WHERE rms_status = 0 AND rms_province_id = ".$pp." AND rms_city_id = ".$pc.")done
                            )summ GROUP BY city_id)final WHERE t =1";
                        $observds = DB::select(DB::raw($sql));
                        $sql = "SELECT city_id,ROUND(cnt/sall* 100) AS ag FROM (
                            SELECT *,SUM(cnt) AS sall FROM (
                                SELECT * FROM (SELECT 1 AS t,rms_city_id AS city_id
                                    ,COUNT(*) AS cnt FROM road_observeds WHERE rms_status <> 0 AND rms_province_id = ".$pp." AND rms_city_id = ".$pc.")done
                                UNION 
                                SELECT * FROM (SELECT 2 AS t,rms_city_id AS city_id,COUNT(*) AS cnt
                                 FROM road_observeds WHERE rms_status = 0 AND rms_province_id = ".$pp." AND rms_city_id = ".$pc.")done
                            )summ GROUP BY city_id)final WHERE t =1";
                        $avg = DB::select(DB::raw($sql));
                    }
                    else {
                        $sql = "SELECT city_id,ROUND(cnt/sall* 100) AS total FROM (
                            SELECT *,SUM(cnt) AS sall FROM (
                                SELECT * FROM (SELECT 1 AS t,rms_city_id AS city_id
                                    ,COUNT(*) AS cnt FROM road_observeds WHERE rms_status <> 0 AND rms_province_id = ".$pp." GROUP BY rms_city_id)done
                                UNION 
                                SELECT * FROM (SELECT 2 AS t,rms_city_id AS city_id,COUNT(*) AS cnt
                                 FROM road_observeds WHERE rms_status = 0 AND rms_province_id = ".$pp." GROUP BY rms_city_id)done
                            )summ GROUP BY city_id)final WHERE t =1";
                        $observds = DB::select(DB::raw($sql));
                        $sql = "SELECT ROUND(AVG(percent)) AS ag FROM(SELECT city_id,ROUND(cnt/sall* 100) AS percent FROM (
                            SELECT *,SUM(cnt) AS sall FROM (
                                SELECT * FROM (SELECT 1 AS t,rms_city_id AS city_id
                                    ,COUNT(*) AS cnt FROM road_observeds WHERE rms_status <> 0 AND rms_province_id = ".$pp." GROUP BY rms_city_id)done
                                UNION 
                                SELECT * FROM (SELECT 2 AS t,rms_city_id AS city_id,COUNT(*) AS cnt FROM road_observeds
                                 WHERE rms_status = 0 AND rms_province_id = ".$pp." GROUP BY rms_city_id)done
                            )summ GROUP BY city_id)final WHERE t =1)alla";
                        $avg = DB::select(DB::raw($sql));
                    }
                }
                else {
                    $sql = "SELECT province_id,ROUND(cnt/sall* 100) AS total FROM (
                        SELECT *,SUM(cnt) AS sall FROM (
                            SELECT * FROM (SELECT 1 AS t,rms_province_id AS province_id
                                ,COUNT(*) AS cnt FROM road_observeds WHERE rms_status <> 0 GROUP BY rms_province_id)done
                            UNION 
                            SELECT * FROM (SELECT 2 AS t,rms_province_id AS province_id,COUNT(*) AS cnt FROM road_observeds WHERE rms_status = 0 GROUP BY rms_province_id)done
                        )summ GROUP BY province_id)final WHERE t =1";
                    $observds = DB::select(DB::raw($sql));
                    $sql = "SELECT ROUND(AVG(percent)) AS ag FROM(SELECT province_id,ROUND(cnt/sall* 100) AS percent FROM (
                        SELECT *,SUM(cnt) AS sall FROM (
                            SELECT * FROM (SELECT 1 AS t,rms_province_id AS province_id
                                ,COUNT(*) AS cnt FROM road_observeds WHERE rms_status <> 0 GROUP BY rms_province_id)done
                            UNION 
                            SELECT * FROM (SELECT 2 AS t,rms_province_id AS province_id,COUNT(*) AS cnt FROM road_observeds WHERE rms_status = 0 GROUP BY rms_province_id)done
                        )summ GROUP BY province_id)final WHERE t =1)alla";
                    $avg = DB::select(DB::raw($sql));
                }
        
                $observeds_count_ok = DB::table('road_observeds')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('rms_province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('rms_city_id', $pc);
                                    })
                                    ->where('rms_status', '<>', 0)
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $observeds_count_nok = DB::table('road_observeds')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('rms_province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('rms_city_id', $pc);
                                    })
                                    ->where('rms_status', '=', 0)
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $big_chart = "SELECT FeatureTypeTitle,fk_FeatureType,COUNT(*) AS cnt,
                CASE WHEN rms_status = 0 THEN 0 ELSE 1 END AS res
                 FROM road_observeds GROUP BY fk_FeatureType,res                ";
                $observds_chart_big = DB::table('road_observeds')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('rms_province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('rms_city_id', $pc);
                                        })
                                        ->groupBy('fk_FeatureType', 'res')
                                        ->select(DB::raw('FeatureTypeTitle,fk_FeatureType
                                        ,COUNT(*) AS cnt,
                                        CASE WHEN rms_status = 0 THEN 0 ELSE 1 END AS res'))
                                        ->get();
                //// end shekayat
                $road_items_count = DB::table('road_items_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $maintenance_project_count = DB::table('road_maintenance_projects')
                                                ->when(!empty($pp), function ($query) use ($pp) {
                                                    return $query->where('province_id', $pp);
                                                })
                                                ->when(!empty($py), function ($query) use ($py) {
                                                    if(1395 == $py){
                                                        return $query->where('contract_date', '<=', 1395);
                                                    }
                                                    return $query->where('contract_date', $py);
                                                })
                                                ->when(!empty($pc), function ($query) use ($pc) {
                                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                                })
                                                ->where('status', 2)
                                                ->get()->count();
                $danger_prevention_count = DB::table('road_danger_preventions')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $upgrade_safety_count = DB::table('road_upgrade_safeties')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $technical_building_count = DB::table('road_technical_buildings')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $tollhouse_management_count = DB::table('road_tollhouse_managements')
                                            ->when(!empty($pp), function ($query) use ($pp) {
                                                return $query->where('province_id', $pp);
                                            })
                                            ->when(!empty($py), function ($query) use ($py) {
                                                if(1395 == $py){
                                                    return $query->where('contract_date', '<=', 1395);
                                                }
                                                return $query->where('contract_date', $py);
                                            })
                                            ->when(!empty($pc), function ($query) use ($pc) {
                                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                            })
                                            ->where('status', 2)
                                            ->get()->count();            
                $count_all_rahradi = $upgrade_safety_count + $technical_building_count
                                    + $tollhouse_management_count + $maintenance_project_count 
                                    + $danger_prevention_count;
                $road_patrols_count = DB::table('road_patrol_projects')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('city_id', $pc);
                                    })
                                    ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
               
                $points_counts = DB::table('accident_points')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->select( DB::raw('count(*) as total'))
                                        ->where('province_id', $pp);
                                    })
                                    ->when(empty($pp), function ($query) use ($pp) {
                                        return $query->select(DB::raw('count(*) as total'));
                                    })
                                    ->get();
                if(!empty($pp)){
                    $province = City::where('province_id', $pp)
                                    ->get();
                    foreach($observds as $value){
                        foreach($province as $val){
                            if($value->city_id == $val->id)
                        $value->city_fa = $val->name_fa;
                        }
                    }
                }
                return response()->json([
                                        'status' => 'succeed',
                    'message' => '',
                    'observds' => [
                        'avg' => $avg,
                        'chart_2_3' => $observds_chart_big,
                        'chart_4'  => [
                            'nok' => $observeds_count_nok[0]->total,
                            'ok' => $observeds_count_ok[0]->total,
                            'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total
                        ],
                    ],
                    'pahne' => $observds,
                    'counts' => [
                        'rahdari'  => $count_all_rahradi,
                        'items'    => $road_items_count[0]->total,
                        'patrols'  => $road_patrols_count[0]->total,
                        'observds' => [
                            'nok' => $observeds_count_nok[0]->total,
                            'ok' => $observeds_count_ok[0]->total,
                            'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total
                        ],
                        'acc_points' => $points_counts[0]->total,          
                    ]
                ], 200);
                break;
            case 5:
                $points = DB::table('accident_points')
                            ->when(!empty($pp), function ($query) use ($pp) {
                                return $query->select('province_id','province as province_fa', DB::raw('count(*) as total'))
                                ->where('province_id', $pp);
                            })
                            ->when(empty($pp), function ($query) use ($pp) {
                                return $query->select('province_id','province as province_fa', DB::raw('count(*) as total'))
                                            ->groupBy('province_id');
                            })
                            ->get();
                $points_counts = DB::table('accident_points')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->select( DB::raw('count(*) as total'))
                                        ->where('province_id', $pp);
                                    })
                                    ->when(empty($pp), function ($query) use ($pp) {
                                        return $query->select(DB::raw('count(*) as total'));
                                    })
                                    ->get();
                if($pp != null){
                    $sql = "SELECT SUM(cnt) AS ag FROM(SELECT id,progress,province,COUNT(*) AS cnt,
                        CASE WHEN progress = 100 THEN 1 ELSE 0 END AS pro FROM accident_points 
                        WHERE province_id=".$pp."
                        GROUP BY province,pro ) q WHERE pro =0";
                    $avg_not_ok = DB::select(DB::raw($sql));
                    $sql = "SELECT SUM(cnt) AS ag FROM(SELECT id,progress,province,COUNT(*) AS cnt,
                        CASE WHEN progress = 100 THEN 1 ELSE 0 END AS pro FROM accident_points 
                        WHERE province_id=".$pp."
                        GROUP BY province,pro ) q WHERE pro =1";
                    $avg_ok = DB::select(DB::raw($sql));
                    $sql = "SELECT COUNT(*) AS cnt,geometric_desc FROM accident_points
                    where province_id= ".$pp." GROUP BY geometric_desc";
                    $chart2_3 = DB::select(DB::raw($sql));
                }
                else {
                    $sql = "SELECT SUM(cnt) AS ag FROM(SELECT id,progress,province,COUNT(*) AS cnt,
                        CASE WHEN progress = 100 THEN 1 ELSE 0 END AS pro FROM accident_points 
                        GROUP BY province,pro ) q WHERE pro =0";
                    $avg_not_ok = DB::select(DB::raw($sql));
                    $sql = "SELECT SUM(cnt) AS ag FROM(SELECT id,progress,province,COUNT(*) AS cnt,
                        CASE WHEN progress = 100 THEN 1 ELSE 0 END AS pro FROM accident_points 
                        GROUP BY province,pro ) q WHERE pro =1";
                    $avg_ok = DB::select(DB::raw($sql));
                    $sql = "SELECT COUNT(*) AS cnt,geometric_desc FROM accident_points GROUP BY geometric_desc";
                    $chart2_3 = DB::select(DB::raw($sql));

                }
                $road_items_count = DB::table('road_items_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
                $maintenance_project_count = DB::table('road_maintenance_projects')
                                                ->when(!empty($pp), function ($query) use ($pp) {
                                                    return $query->where('province_id', $pp);
                                                })
                                                ->when(!empty($py), function ($query) use ($py) {
                                                    if(1395 == $py){
                                                        return $query->where('contract_date', '<=', 1395);
                                                    }
                                                    return $query->where('contract_date', $py);
                                                })
                                                ->when(!empty($pc), function ($query) use ($pc) {
                                                    return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                                })
                                                ->where('status', 2)
                                                ->get()->count();
                $danger_prevention_count = DB::table('road_danger_preventions')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $upgrade_safety_count = DB::table('road_upgrade_safeties')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $technical_building_count = DB::table('road_technical_buildings')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();
                $tollhouse_management_count = DB::table('road_tollhouse_managements')
                                            ->when(!empty($pp), function ($query) use ($pp) {
                                                return $query->where('province_id', $pp);
                                            })
                                            ->when(!empty($py), function ($query) use ($py) {
                                                if(1395 == $py){
                                                    return $query->where('contract_date', '<=', 1395);
                                                }
                                                return $query->where('contract_date', $py);
                                            })
                                            ->when(!empty($pc), function ($query) use ($pc) {
                                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                            })
                                            ->where('status', 2)
                                            ->get()->count();            
                $count_all_rahradi = $upgrade_safety_count + $technical_building_count
                                    + $tollhouse_management_count + $maintenance_project_count 
                                    + $danger_prevention_count;
                $road_patrols_count = DB::table('road_patrol_projects')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('city_id', $pc);
                                    })
                                    ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $observeds_count_ok = DB::table('road_observeds')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('rms_province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('rms_city_id', $pc);
                                    })
                                    ->where('rms_status', '<>', 0)
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $observeds_count_nok = DB::table('road_observeds')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('rms_province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('rms_city_id', $pc);
                                    })
                                    ->where('rms_status', '=', 0)
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                return response()->json([
                        'status' => 'succeed',
                        'message' => '',
                        'acc_points' => [ 
                            'avg' => ((float)$avg_ok[0]->ag)?round(100*(float)$avg_ok[0]->ag/((float)$avg_ok[0]->ag+(float)$avg_not_ok[0]->ag)):0,    
                            'chart2_3' => $chart2_3,
                            // 'count_all' => $count_all,
                            // 'axis_type_count' => $axis_type,
                            // 'project_status' => $project_status,
                            // 'project_years' => $project_years
                        ],
                        'pahne' => $points,
                        'counts' => [
                            'rahdari' => $count_all_rahradi,
                            'items'   => $road_items_count[0]->total,
                            'patrols'   => $road_patrols_count[0]->total,
                            'observds' => [
                                'nok' => $observeds_count_nok[0]->total,
                                'ok' => $observeds_count_ok[0]->total,
                                'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total
                            ],
                            'acc_points' => $points_counts[0]->total,
                        ]
                    ], 200);
                break;
            case 7:
                ///start camp
                // $sql = "SELECT blockage_cause,blockage_cause_fa,COUNT(*) AS cnt,SUM(blockage_length) 
                // AS lenght FROM camps WHERE is_open = 2 GROUP BY blockage_cause";
                // $pies = DB::select(DB::raw($sql));
                $sql = "SELECT axis_type, axis_type_fa, blockage_cause, blockage_cause_fa, 
                COUNT(*) AS cnt, SUM(blockage_length) AS lenght FROM camps 
                GROUP BY blockage_cause,axis_type";
                $chart_2_3_4 = DB::select(DB::raw($sql));
                $sql = "SELECT 
                bl_len FROM(
                    SELECT 
                    SUM(blockage_length) AS bl_len
                    FROM
                    camps
                    )q";
                $chart_1 = DB::select(DB::raw($sql));
                return response()->json([
                    'charts' => [
                    // 'pies' => $pies,
                    'chart_2_3_4' => $chart_2_3_4,
                    'staticks' =>round($chart_1[0]->bl_len)
                    ]
                ]);
                ///end camp
                //// counts
                $points_counts = DB::table('accident_points')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->select( DB::raw('count(*) as total'))
                                        ->where('province_id', $pp);
                                    })
                                    ->when(empty($pp), function ($query) use ($pp) {
                                        return $query->select(DB::raw('count(*) as total'));
                                    })
                                    ->get();
                $road_items_count = DB::table('road_items_projects')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('city_id', $pc);
                                    })
                                    ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                    ->select(DB::raw('count(*) as total'))
                                    ->get();
                $maintenance_project_count = DB::table('road_maintenance_projects')
                                            ->when(!empty($pp), function ($query) use ($pp) {
                                                return $query->where('province_id', $pp);
                                            })
                                            ->when(!empty($py), function ($query) use ($py) {
                                                if(1395 == $py){
                                                    return $query->where('contract_date', '<=', 1395);
                                                }
                                                return $query->where('contract_date', $py);
                                            })
                                            ->when(!empty($pc), function ($query) use ($pc) {
                                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                            })
                                            ->where('status', 2)
                                            ->get()->count();
                $danger_prevention_count = DB::table('road_danger_preventions')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($py), function ($query) use ($py) {
                                        if(1395 == $py){
                                            return $query->where('contract_date', '<=', 1395);
                                        }
                                        return $query->where('contract_date', $py);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                    })
                                    ->where('status', 2)
                                    ->get()->count();
                $upgrade_safety_count = DB::table('road_upgrade_safeties')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($py), function ($query) use ($py) {
                                        if(1395 == $py){
                                            return $query->where('contract_date', '<=', 1395);
                                        }
                                        return $query->where('contract_date', $py);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                    })
                                    ->where('status', 2)
                                    ->get()->count();
                $technical_building_count = DB::table('road_technical_buildings')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($py), function ($query) use ($py) {
                                        if(1395 == $py){
                                            return $query->where('contract_date', '<=', 1395);
                                        }
                                        return $query->where('contract_date', $py);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                    })
                                    ->where('status', 2)
                                    ->get()->count();
                $tollhouse_management_count = DB::table('road_tollhouse_managements')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->get()->count();            
                $count_all_rahradi = $upgrade_safety_count + $technical_building_count
                                + $tollhouse_management_count + $maintenance_project_count 
                                + $danger_prevention_count;
                $road_patrols_count = DB::table('road_patrol_projects')
                                ->when(!empty($pp), function ($query) use ($pp) {
                                    return $query->where('province_id', $pp);
                                })
                                ->when(!empty($pc), function ($query) use ($pc) {
                                    return $query->where('city_id', $pc);
                                })
                                ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                ->select(DB::raw('count(*) as total'))
                                ->get();
                $observeds_count_ok = DB::table('road_observeds')
                                ->when(!empty($pp), function ($query) use ($pp) {
                                    return $query->where('rms_province_id', $pp);
                                })
                                ->when(!empty($pc), function ($query) use ($pc) {
                                    return $query->where('rms_city_id', $pc);
                                })
                                ->where('rms_status', '<>', 0)
                                ->select(DB::raw('count(*) as total'))
                                ->get();
                $observeds_count_nok = DB::table('road_observeds')
                                ->when(!empty($pp), function ($query) use ($pp) {
                                    return $query->where('rms_province_id', $pp);
                                })
                                ->when(!empty($pc), function ($query) use ($pc) {
                                    return $query->where('rms_city_id', $pc);
                                })
                                ->where('rms_status', '=', 0)
                                ->select(DB::raw('count(*) as total'))
                                ->get();
            
                return response()->json([
                    'status' => 'succeed',
                    'message' => '',
                    'acc_points' => [ 
                        // 'avg' => $tol,    
                        // 'chart2_3_4' => $chart2_3_4,
                    ],
                    // 'pahne' => $points,
                    'counts' => [
                        'rahdari' => $count_all_rahradi,
                        'items'   => $road_items_count[0]->total,
                        'patrols'   => $road_patrols_count[0]->total,
                        'observds' => [
                            'nok' => $observeds_count_nok[0]->total,
                            'ok' => $observeds_count_ok[0]->total,
                            'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total
                        ],
                        'acc_points' => $points_counts[0]->total,
                    ]
                ], 200);
                break;
            default:////// proje haye rahdari
                $project_years = [];
                $v = verta();
                $y = $v->format('Y');
                for($i=1395; $i<=$y; $i++){
                    $project_years[$i] = 0;
                }
                $axis_type = ['1' => 0, '2' => 0, '3' => 0, '4' => 0];
                $project_status = [0, 0, 0, 0, 0, 0, 0, 0, 0];
                $province = [
                    '1' =>0,
                    '2' =>0,
                    '3' =>0,
                    '4' =>0,
                    '5' =>0,
                    '6' =>0,
                    '7' =>0,
                    '8' =>0,
                    '9' =>0,
                    '10' =>0,
                    '11' =>0,
                    '12' =>0,
                    '13' =>0,
                    '14' =>0,
                    '15' =>0,
                    '16' =>0,
                    '17' =>0,
                    '18' =>0,
                    '19' =>0,
                    '20' =>0,
                    '21' =>0,
                    '22' =>0,
                    '23' =>0,
                    '24' =>0,
                    '25' =>0,
                    '26' =>0,
                    '27' =>0,
                    '28' =>0,
                    '29' =>0,
                    '30' =>0,
                    '31' =>0,
                    '32' =>0,
                    '33' =>0,
                    '34' =>0,
                    '35' =>0,
                ];
                $upgrade_safety_count = 0; 
                $technical_building_count = 0; 
                $construction_rural_count = 0;
                $tollhouse_management_count = 0;
                $maintenance_project_count = 0;
                $danger_prevention_count = 0;
                $tollhouse_management = null;
                $danger_prevention = null;
                $technical_building = null;
                $maintenance_project = null;
                $upgrade_safety = null;
                $city = null;
                $maintenance_project = DB::table('road_maintenance_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->select('road_maintenance_projects.id as i', 
                                        DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_maintenance_projects.start_latlng, \"$[0]\")) AS l" ),
                                        DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_maintenance_projects.start_latlng, \"$[1]\")) AS g" ), 
                                        'road_maintenance_projects.province_fa as p', 'road_maintenance_projects.axis_type',
                                        'road_maintenance_projects.city_1_fa as c1', 'road_maintenance_projects.city_2_fa as c2', 
                                        'road_maintenance_projects.province_id', 'road_maintenance_projects.project_status', 
                                        'road_maintenance_projects.city_3_fa as c3', 'road_maintenance_projects.axis_type_fa as a',
                                            'road_maintenance_projects.project_name as pn', 'road_maintenance_projects.contract_date as d', 
                                            'road_maintenance_projects.axis_name as an', 'road_maintenance_projects.project_status_fa as ps')
                                        ->get();
                $maintenance_project_count = $maintenance_project->count();
                $danger_prevention = DB::table('road_danger_preventions')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->select('road_danger_preventions.id as i', 
                                        DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_danger_preventions.start_latlng, \"$[0]\")) AS l" ),
                                        DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_danger_preventions.start_latlng, \"$[1]\")) AS g" ), 
                                        'road_danger_preventions.province_fa as p', 'road_danger_preventions.axis_type',
                                        'road_danger_preventions.city_1_fa as c1', 'road_danger_preventions.city_2_fa as c2', 
                                        'road_danger_preventions.province_id', 'road_danger_preventions.project_status', 
                                        'road_danger_preventions.city_3_fa as c3', 'road_danger_preventions.axis_type_fa as a',
                                        'road_danger_preventions.project_name as pn', 'road_danger_preventions.contract_date as d', 
                                        'road_danger_preventions.axis_name as an', 'road_danger_preventions.project_status_fa as ps')
                                        ->get();
                $danger_prevention_count = $danger_prevention->count();
                $upgrade_safety = DB::table('road_upgrade_safeties')
                                    ->when(!empty($pp), function ($query) use ($pp) {
                                        return $query->where('province_id', $pp);
                                    })
                                    ->when(!empty($py), function ($query) use ($py) {
                                        if(1395 == $py){
                                            return $query->where('contract_date', '<=', 1395);
                                        }
                                        return $query->where('contract_date', $py);
                                    })
                                    ->when(!empty($pc), function ($query) use ($pc) {
                                        return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                    })
                                    ->where('status', 2)
                                    ->select('road_upgrade_safeties.id as i', 
                                    DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_upgrade_safeties.start_latlng, \"$[0]\")) AS l" ),
                                    DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_upgrade_safeties.start_latlng, \"$[1]\")) AS g" ), 
                                    'road_upgrade_safeties.province_fa as p', 'road_upgrade_safeties.axis_type',
                                    'road_upgrade_safeties.city_1_fa as c1', 'road_upgrade_safeties.city_2_fa as c2', 
                                    'road_upgrade_safeties.province_id', 'road_upgrade_safeties.project_status', 
                                    'road_upgrade_safeties.city_3_fa as c3', 'road_upgrade_safeties.axis_type_fa as a',
                                    'road_upgrade_safeties.project_name as pn', 'road_upgrade_safeties.contract_date as d', 
                                    'road_upgrade_safeties.axis_name as an', 'road_upgrade_safeties.project_status_fa as ps')
                                    ->get();
                $upgrade_safety_count = $upgrade_safety->count();
                $technical_building = DB::table('road_technical_buildings')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($py), function ($query) use ($py) {
                                            if(1395 == $py){
                                                return $query->where('contract_date', '<=', 1395);
                                            }
                                            return $query->where('contract_date', $py);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                        })
                                        ->where('status', 2)
                                        ->select('road_technical_buildings.id as i', 
                                        DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_technical_buildings.start_latlng, \"$[0]\")) AS l" ),
                                        DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_technical_buildings.start_latlng, \"$[1]\")) AS g" ), 
                                        'road_technical_buildings.province_fa as p', 'road_technical_buildings.axis_type',
                                        'road_technical_buildings.city_1_fa as c1', 'road_technical_buildings.city_2_fa as c2', 
                                        'road_technical_buildings.province_id', 'road_technical_buildings.project_status', 
                                        'road_technical_buildings.city_3_fa as c3', 'road_technical_buildings.axis_type_fa as a',
                                        'road_technical_buildings.project_name as pn', 'road_technical_buildings.contract_date as d', 
                                        'road_technical_buildings.axis_name as an', 'road_technical_buildings.project_status_fa as ps')
                                        ->get();
                $technical_building_count = $technical_building->count();
                $tollhouse_management = DB::table('road_tollhouse_managements')
                                            ->when(!empty($pp), function ($query) use ($pp) {
                                                return $query->where('province_id', $pp);
                                            })
                                            ->when(!empty($py), function ($query) use ($py) {
                                                if(1395 == $py){
                                                    return $query->where('contract_date', '<=', 1395);
                                                }
                                                return $query->where('contract_date', $py);
                                            })
                                            ->when(!empty($pc), function ($query) use ($pc) {
                                                return $query->where('cities_id', 'like', '%"'.$pc.'":%');
                                            })
                                            ->where('status', 2)
                                            ->select('road_tollhouse_managements.id as i', 
                                            DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_tollhouse_managements.start_latlng, \"$[0]\")) AS l" ),
                                            DB::raw("JSON_UNQUOTE(JSON_EXTRACT(road_tollhouse_managements.start_latlng, \"$[1]\")) AS g" ), 
                                            'road_tollhouse_managements.province_fa as p', 'road_tollhouse_managements.axis_type',
                                            'road_tollhouse_managements.city_1_fa as c1', 'road_tollhouse_managements.city_2_fa as c2', 
                                            'road_tollhouse_managements.province_id', 'road_tollhouse_managements.project_status', 
                                            'road_tollhouse_managements.city_3_fa as c3', 'road_tollhouse_managements.axis_type_fa as a',
                                            'road_tollhouse_managements.project_name as pn', 'road_tollhouse_managements.contract_date as d', 
                                            'road_tollhouse_managements.axis_name as an', 'road_tollhouse_managements.project_status_fa as ps')
                                            ->get();
                $tollhouse_management_count = $tollhouse_management->count();
            
                $count_all = $upgrade_safety_count + $technical_building_count
                    + $tollhouse_management_count + $maintenance_project_count 
                    + $danger_prevention_count;
                $all_count = array( "upgrade_safety_count" => $upgrade_safety_count,
                                    "danger_prevention_count" => $danger_prevention_count,
                                    "technical_building_count" => $technical_building_count,
                                    "maintenance_project_count" => $maintenance_project_count,
                                    "tollhouse_management_count" => $tollhouse_management_count
                    );
                $all = array( "tollhouse_management" => $tollhouse_management,
                            "danger_prevention" => $danger_prevention,
                            "technical_building" => $technical_building,
                            "maintenance_project" => $maintenance_project,
                            "upgrade_safety" => $upgrade_safety
                    );
                    // return $all;
                foreach($all as $key => $value)
                    if($value != null)
                        foreach($value as $key1 => $data){
                            switch($data->axis_type){
                                case 1: 
                                    $axis_type['1']++;
                                break;
                                case 2: 
                                    $axis_type['2']++;
                                break;
                                case 3: 
                                    $axis_type['3']++;
                                break;
                                case 4: 
                                    $axis_type['4']++;
                                break;
                            }
                            switch($data->province_id){                        
                                case 1: 
                                    $province['1']++;
                                break;                        
                                case 2: 
                                    $province['2']++;
                                break;                        
                                case 3: 
                                    $province['3']++;
                                break;                        
                                case 4: 
                                    $province['4']++;
                                break;                        
                                case 5: 
                                    $province['5']++;
                                break;                        
                                case 6: 
                                    $province['6']++;
                                break;                        
                                case 7: 
                                    $province['7']++;
                                break;                        
                                case 8: 
                                    $province['8']++;
                                break;                        
                                case 9: 
                                    $province['9']++;
                                break;                        
                                case 10: 
                                    $province['10']++;
                                break;                        
                                case 11: 
                                    $province['11']++;
                                break;                        
                                case 12: 
                                    $province['12']++;
                                break;                        
                                case 13: 
                                    $province['13']++;
                                break;                        
                                case 14: 
                                    $province['14']++;
                                break;                        
                                case 15: 
                                    $province['15']++;
                                break;                        
                                case 16: 
                                    $province['16']++;
                                break;                        
                                case 17: 
                                    $province['17']++;
                                break;                        
                                case 18: 
                                    $province['18']++;
                                break;                        
                                case 19: 
                                    $province['19']++;
                                break;                        
                                case 20: 
                                    $province['20']++;
                                break;                        
                                case 21: 
                                    $province['21']++;
                                break;                        
                                case 22: 
                                    $province['22']++;
                                break;                        
                                case 23: 
                                    $province['23']++;
                                break;                        
                                case 24: 
                                    $province['24']++;
                                break;                        
                                case 25: 
                                    $province['25']++;
                                break;                        
                                case 26: 
                                    $province['26']++;
                                break;                        
                                case 27: 
                                    $province['27']++;
                                break;                        
                                case 28: 
                                    $province['28']++;
                                break;                        
                                case 29: 
                                    $province['29']++;
                                break;                        
                                case 30: 
                                    $province['30']++;
                                break;                        
                                case 31: 
                                    $province['31']++;
                                break;                        
                                case 32: 
                                    $province['32']++;
                                break;                        
                                case 33: 
                                    $province['33']++;
                                break;                        
                                case 34: 
                                    $province['34']++;
                                break;                        
                                case 35: 
                                    $province['35']++;
                                break;                        
                            }
                            ($data->d <= 1395)?$project_years['1395']++:$project_years[$data->d]++;
                            $project_status[$data->project_status]++;
                        }
                if($py != null){//// avg progress physic
                    if($py == 1395){
                        $avg = DB::select(DB::raw(
                        "SELECT AVG(i) AS ag FROM(
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\"))
                                    ELSE 0
                                END AS i
                            FROM road_maintenance_projects
                            WHERE project_distance_update IS NOT NULL and contract_date <= 1395)q
                        UNION 
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                            FROM road_danger_preventions
                            WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qq
                        UNION 
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                            FROM road_upgrade_safeties
                            WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qqq
                        UNION
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                            FROM road_technical_buildings
                            WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qqqq
                        UNION
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                            FROM road_tollhouse_managements
                            WHERE project_distance_update IS NOT NULL and contract_date <= 1395)qqqqq)z")); 
                    }
                    else
                    {
                        $avg = DB::select(DB::raw("SELECT AVG(i) AS ag FROM(SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                        CASE
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\"))
                            WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\"))
                        ELSE 0
                        END AS i
                        FROM road_maintenance_projects
                        WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                        UNION 
                                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                    CASE
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                        ELSE 0
                                    END AS i
                                    FROM road_danger_preventions
                                    WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                        UNION 
                        SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                    CASE
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                        ELSE 0
                                    END AS i
                                    FROM road_upgrade_safeties
                                    WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                        UNION
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                    CASE
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                        ELSE 0
                                    END AS i
                                    FROM road_technical_buildings
                                    WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q
                        UNION
                            SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                    CASE
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                        ELSE 0
                                    END AS i
                                    FROM road_tollhouse_managements
                                    WHERE project_distance_update IS NOT NULL and contract_date = ".$py.")q)z"));  
                    }
                    }
                else{
                    $avg = DB::select(DB::raw("SELECT AVG(i) AS ag FROM(SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                    CASE
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.10\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.11\"),\"$[2]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.12\"),\"$[3]\"))
                        WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.13\"),\"$[1]\"))
                    ELSE 0
                    END AS i
                    FROM road_maintenance_projects
                    WHERE project_distance_update IS NOT NULL )q
                    UNION 
                                SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                                FROM road_danger_preventions
                                WHERE project_distance_update IS NOT NULL )q
                    UNION 
                    SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.8\"),\"$[1]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.9\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                                FROM road_upgrade_safeties
                                WHERE project_distance_update IS NOT NULL )q
                    UNION
                        SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.7\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                                FROM road_technical_buildings
                                WHERE project_distance_update IS NOT NULL )q
                    UNION
                        SELECT p,CAST(i AS UNSIGNED) AS i FROM (SELECT  project_distance_update AS p,
                                CASE
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.1\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.2\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.3\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.4\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.5\"),\"$[0]\"))
                                    WHEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\")) >= 0 THEN JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(project_distance_update,\"$.6\"),\"$[0]\"))
                                    ELSE 0
                                END AS i
                                FROM road_tollhouse_managements
                                WHERE project_distance_update IS NOT NULL )q)z"));
                    }
                if($pp != null){/// pahne shahrestan
                    if($py != null){
                        if($py == 1395){
                            $city = DB::select(DB::raw("SELECT city_fa,city_id,sum(cnt) as cnt FROM (
                                SELECT 1 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_maintenance_projects WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 2 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_danger_preventions WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 4 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_technical_buildings WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 3 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_upgrade_safeties WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 5 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_tollhouse_managements WHERE province_id = 1 AND contract_date <= 1395 and status=2 GROUP BY city_1_fa)q 
                            GROUP BY city_fa"));
                        }
                        else {
                            $city = DB::select(DB::raw("SELECT city_fa,city_id,sum(cnt) as cnt FROM (
                                SELECT 1 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_maintenance_projects WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 2 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_danger_preventions WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 4 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_technical_buildings WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 3 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_upgrade_safeties WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa
                                UNION
                                SELECT 5 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                                COUNT(*) AS cnt FROM road_tollhouse_managements WHERE province_id = 1 AND contract_date = ".$py." and status=2 GROUP BY city_1_fa)q 
                            GROUP BY city_fa"));
                        }
                    }
                    else {
                        $city = DB::select(DB::raw("SELECT city_fa,city_id,sum(cnt) as cnt FROM (
                            SELECT 1 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                            COUNT(*) AS cnt FROM road_maintenance_projects WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                            UNION
                            SELECT 2 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                            COUNT(*) AS cnt FROM road_danger_preventions WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                            UNION
                            SELECT 4 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                            COUNT(*) AS cnt FROM road_technical_buildings WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                            UNION
                            SELECT 3 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                            COUNT(*) AS cnt FROM road_upgrade_safeties WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa
                            UNION
                            SELECT 5 AS t,JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(cities_id),\"$[0]\")) AS city_id,city_1_fa AS city_fa,
                            COUNT(*) AS cnt FROM road_tollhouse_managements WHERE province_id = ".$pp." and status=2 GROUP BY city_1_fa)q 
                        GROUP BY city_fa"));
                    }
                    }
            //// end prorje haye rahdari
            break;
        }
        $todayDate = Carbon::now()->format('Y-m-d');

        $road_items_count = DB::table('road_items_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
        $road_patrols_count = DB::table('road_patrol_projects')
                                        ->when(!empty($pp), function ($query) use ($pp) {
                                            return $query->where('province_id', $pp);
                                        })
                                        ->when(!empty($pc), function ($query) use ($pc) {
                                            return $query->where('city_id', $pc);
                                        })
                                        // ->leftJoin('users', 'users.id', '=', 'road_patrol_projects.user_id')
                                        ->select(DB::raw('count(*) as total'))
                                        ->get();
        $observeds_count_ok = DB::table('road_observeds')
                            ->when(!empty($pp), function ($query) use ($pp) {
                                return $query->where('rms_province_id', $pp);
                            })
                            ->when(!empty($pc), function ($query) use ($pc) {
                                return $query->where('rms_city_id', $pc);
                            })
                            ->where('rms_status', '<>', 0)
                            ->select(DB::raw('count(*) as total'))
                            ->get();
        $observeds_count_nok = DB::table('road_observeds')
                            ->when(!empty($pp), function ($query) use ($pp) {
                                return $query->where('rms_province_id', $pp);
                            })
                            ->when(!empty($pc), function ($query) use ($pc) {
                                return $query->where('rms_city_id', $pc);
                            })
                            ->where('rms_status', '=', 0)
                            ->select(DB::raw('count(*) as total'))
                            ->get();
        $points_counts = DB::table('accident_points')
                            ->when(!empty($pp), function ($query) use ($pp) {
                                return $query->select( DB::raw('count(*) as total'))
                                ->where('province_id', $pp);
                            })
                            ->when(empty($pp), function ($query) use ($pp) {
                                return $query->select(DB::raw('count(*) as total'));
                            })
                            ->get();
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'rahdari' => [ 
                'avg' => $avg,    
                'counts' => $all_count,
                'count_all' => $count_all,
                'axis_type_count' => $axis_type,
                'project_status' => $project_status,
                'project_years' => $project_years
            ],
            'pahne' => ($pp)?$city:$province,
            'counts' => [
                'rahdari' => $count_all,
                'items'   => $road_items_count[0]->total,
                'patrols'   => $road_patrols_count[0]->total,
                'observds' => [
                    'nok' => $observeds_count_nok[0]->total,
                    'ok' => $observeds_count_ok[0]->total,
                    'all' => $observeds_count_nok[0]->total + $observeds_count_ok[0]->total,
                ],
                'acc_points' => $points_counts[0]->total,
            ]
        ], 200);
    }
}