<?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 RoadObservationReportController extends Controller
{
    public function index()
    {
        return view('version2.dashboard_pages.fast_react.by_table_report');
    }

    public function reportTable(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';
        }


        $fields = [
            'fk_RegisteredEventMessage',
            'road_observeds.id',
            'Title',
            'road_observeds.created_at',
            'lat',
            'lng',
            'FeatureTypeTitle',
            'Description',
            'MobileForSendEventSms',
            'road_observeds.province_id',
            'province_fa',
            'city_fa',
            'edarate_shahri.name_fa',
            'rms_last_activity_fa',
            'rms_description'
        ];

        $aliases = ['', 'road_observeds_id', '', 'road_observeds_created_at', '', '', '', '', '', 'province_id', '', '', 'edarate_shahri_name_fa', '', ''];

        $selectRaw = makeSelectQuery($fields, $aliases);

        $query = RoadObserved::leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
            ->where('rms_status', "<>", 0)
            ->whereNotNull('edarate_shahri_id')
            ->whereBetween('road_observeds.created_at', [$from_date . ' 00:00:00', $date_to . ' 23:59:59'])
            ->selectRaw($selectRaw);

        $data = processDataTable($request, '', $fields, $query);

        return response()->json(
            $data,
            $data['status'],
        );
    }

    public function CityPatrolData(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';
        }

        $all = DB::select(DB::raw(
            'SELECT
            province_fa,
            province_id,
            city_id,
            city_fa,
            COUNT(*) tedade,
            SUM(tedad_mavarede_moshede_shode) as tedad_mavarede_moshede_shode,
            SUM(tedad_mavarede_peygiri_shode) as tedad_mavarede_peygiri_shode,
            SUM(tedad_faliyat_sabt_shode) as tedad_faliyat_sabt_shode,
            ROUND(SUM(distance)) as masafat_tey_shode
            FROM (SELECT 
            province_id,
            province_fa,
            city_id,
            city_fa,
            distance,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id) AS tedad_mavarede_moshede_shode,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NOT NULL) AS tedad_mavarede_peygiri_shode,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NULL) AS tedad_faliyat_sabt_shode
            FROM `road_patrols` rp where rp.province_id = "' . $request->province_id . '" AND rp.start_time BETWEEN "' . $from_date . '" AND "' . $date_to . '") temp 
            GROUP BY city_id
            '
        ));
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $all,
        ], 200);
    }

    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(DB::raw(
            '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(DB::raw(
            '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(DB::raw(
            '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, city_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'";
        }
        // dd($sql);
        $data = DB::select(DB::raw($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,city_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,city_id
        UNION 
        SELECT COUNT(*) AS s,city_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 city_id";

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