<?php

namespace App\Http\Controllers\V2\Dashboard;

use SoapFault;
use SoapClient;
use App\Models\InfoItem;
use Illuminate\Http\Request;
use App\Models\RoadObserved;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;


class RoadItemsReportController extends Controller
{
    public function index()
    {
        return view('version2.dashboard_pages.road_items.reports');
    }

    public function get_sub_items(Request $request)
    {
        $data = InfoItem::all();
        $items = InfoItem::groupBy('item')->get();
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $data,
            'items' => $items
        ], 200);
    }
    public function get_items(Request $request)
    {
        if ($request->from_date && $request->date_to) {
            $from_date = $request->from_date;
            $date_to = $request->date_to;
            $from_date = $from_date . ' 00:00:00';
            $date_to = $date_to . ' 23:59:59';
        } else {
            $from_date = Date('Y-m-d');
            $from_date = $from_date . ' 00:00:00';
            $date_to = Date('Y-m-d');
            $date_to = $date_to . ' 23:59:59';
        }

        $item = $request->item;
        $all = array();
        $all = DB::select(
            'SELECT sub_item AS t,COUNT(*) AS c,CAST(SUM(CASE WHEN sub_item_data>0 THEN sub_item_data ELSE ABS(sub_item_data) END) AS UNSIGNED) AS s,province_id AS p FROM road_items_projects WHERE item = ' . $item . ' AND activity_date_time BETWEEN "' . $from_date . '" AND "' . $date_to . '" and status = 1 GROUP BY p, sub_item'
        );

        $country = DB::select(
            'SELECT sub_item AS t,COUNT(*) AS c,CAST(SUM(CASE WHEN sub_item_data>0 THEN sub_item_data ELSE ABS(sub_item_data) END) AS UNSIGNED) AS s, -1 AS p FROM road_items_projects WHERE item = ' . $item . ' AND activity_date_time BETWEEN "' . $from_date . '" AND "' . $date_to . '" and status = 1 GROUP BY sub_item'
        );

        $items_fa = InfoItem::where('item', $item)->get();
        foreach ($all as $value) {
            foreach ($items_fa as $val) {
                if ($value->t == $val->sub_item) {
                    $value->u = $val->sub_item_unit;
                }
            }
        }
        return response()->json(
            [
                'status' => 'succeed',
                'message' => '',
                'data' => $all,
                'country' => $country,
                'items_fa' => $items_fa
            ],
            200
        );
    }
    public function get_items_city(Request $request)
    {
        $item = $request->item;
        if ($request->from_date && $request->date_to) {
            $from_date = $request->from_date . ' 00:00:00';
            $date_to = $request->date_to . ' 23:59:59';
        } else {
            $from_date = Date('Y-m-d');
            $from_date = $from_date . ' 00:00:00';
            $date_to = Date('Y-m-d');
            $date_to = $date_to . ' 23:59:59';
        }
        $item = $request->item;
        $province = $request->province_id;
        $all = array();
        $all = DB::select(
                'SELECT sub_item AS t,COUNT(*) AS c,CAST(SUM(CASE WHEN sub_item_data>0 THEN sub_item_data ELSE ABS(sub_item_data) END) AS UNSIGNED) AS s,province_id AS p, edarat_id AS ci FROM road_items_projects WHERE item = ' . $item . ' AND province_id = ' . $province . ' AND activity_date_time BETWEEN "' . $from_date . '" AND "' . $date_to . '" and status = 1 GROUP BY ci, sub_item'
        );
        $items_fa = InfoItem::where('item', $item)->get();
        foreach ($all as $value) {
            foreach ($items_fa as $val) {
                if ($value->t == $val->sub_item) {
                    $value->u = $val->sub_item_unit;
                }
            }
        }
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $all,
            'items_fa' => $items_fa
        ], 200);
    }
    public function excel1(Request $request)
    {
        if (auth()->user()) {
            auth()->user()->addActivityComplete(1033);
        }
        if ($request->from_date && $request->date_to) {
            $from_date = $request->from_date;
            $date_to = $request->date_to;
        } else {
            $from_date = Date('Y-m-d');
            $date_to = Date('Y-m-d');
        }

        if (Auth::user()->username == 'witel' || Auth::user()->username == 'drdanesh') {
            $sql = "SELECT COUNT(*) AS s,province_id AS p,item AS i
            FROM road_items_projects WHERE
            road_items_projects.status = 1 and road_items_projects.activity_date_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59' GROUP BY province_id,item
            UNION 
            SELECT COUNT(*) AS s,province_id AS p,77 AS i FROM road_patrols 
            WHERE
            start_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59' GROUP BY province_id
            UNION
            SELECT COUNT(*) AS s,-1 AS p,item AS i FROM road_items_projects
            WHERE road_items_projects.status = 1 and road_items_projects.activity_date_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59'
            GROUP BY item
            UNION
            SELECT COUNT(*) AS s,-1 AS p,77 AS i FROM road_patrols 
            WHERE start_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59'";
        } else {

            $province_id = Auth::user()->province_id;
            $sql = "SELECT COUNT(*) AS s,province_id AS p,item AS i
            FROM road_items_projects WHERE road_items_projects.status = 1 and road_items_projects.province_id = " . $province_id . " and 
            road_items_projects.activity_date_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59' GROUP BY province_id,item
            UNION 
            SELECT COUNT(*) AS s,province_id AS p,77 AS i FROM road_patrols 
            WHERE road_patrols.province_id = " . $province_id . " and
            road_patrols.start_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59' GROUP BY province_id
            UNION
            SELECT COUNT(*) AS s,-1 AS p,item AS i FROM road_items_projects
            WHERE road_items_projects.status = 1 and road_items_projects.province_id = " . $province_id . " and road_items_projects.activity_date_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59'
            GROUP BY item
            UNION
            SELECT COUNT(*) AS s,-1 AS p,77 AS i FROM road_patrols 
            WHERE  road_patrols.province_id = " . $province_id . " and road_patrols.start_time BETWEEN '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59'";
        }

        $data = DB::select($sql);

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $data,
        ], 200);
    }
    public function excel1_city(Request $request)
    {
        $p = $request->province_id;
        if ($request->from_date && $request->date_to) {
            $from_date = $request->from_date;
            $date_to = $request->date_to;
        } else {
            $from_date = Date('Y-m-d');
            $date_to = Date('Y-m-d');
        }

        $sql = "SELECT COUNT(*) AS s,edarat_id AS c,item AS i
        FROM road_items_projects where
        status = 1 and road_items_projects.activity_date_time between '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59'
        and province_id= '" . $p . "' GROUP BY item,c
        UNION 
        SELECT COUNT(*) AS s,edare_id AS c,77 AS i FROM road_patrols 
        where
        road_patrols.start_time between '" . $from_date . " 00:00:00' and '" . $date_to . " 23:59:59' 
        and province_id= '" . $p . "' GROUP BY c";

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

    public function map(Request $request)
    {
        $province = $request->province_id;
        $city = $request->city_id;
        $sub = $request->sub_id;

        if ($request->date_from && $request->date_to) {
            $date_from = $request->date_from.' 00:00:00';
            $date_to = $request->date_to.' 23:59:59';
        } else {
            $date_from = Date('Y-m-d');
            $date_to = new \DateTime('tomorrow');
            $date_to = $date_to->format('Y-m-d');
        }
        $datass = DB::table('road_items_projects')
                    ->where('is_new', 1)
                    ->when($province, function ($query, $province) {
                        return $query->where('road_items_projects.province_id', $province);
                    })
                    ->when($city, function ($query, $city) {
                        return $query->where('road_items_projects.city_id', $city);
                    })
                    ->when($sub, function ($query, $sub) {
                        return $query->where('sub_items', 'like', '%"'.$sub.'"%');
                    })
                    ->when($request->item, function ($query) use ($request) {
                        if ($request->item > 0) {
                            return $query->where('item', $request->item);
                        }
                        return $query;
                    })
                    ->when(!is_null($request->status), function ($query) use ($request) {
                        return $query->where('status', $request->status);
                    })
                    ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
                    ->whereBetween('road_items_projects.activity_date', [
                        $date_from,
                        $date_to,
                    ])
                    ->select('road_items_projects.id as i', 'start_lat as l', 'start_lng as g', 'status')
                    ->get();
        // dd($datass);

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