<?php

namespace App\Http\Controllers\V2\Dashboard;

use Illuminate\Http\Request;
use App\Models\RoadPatrolProject;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

use App\Exports\V2\RoadPatrol\RoadPatrolTable\AllSheets as RoadPatrolTable;

class RoadPatrolReportController extends Controller
{
    public function index()
    {
        return view('version2.dashboard_pages.road_patrols.by_table_report');
    }

    public function provincePatrolData(Request $request)
    {
        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').' 00:00:00';
            $date_to = Date('Y-m-d').' 23:59:59';
        }

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => RoadPatrolProject::provincePatrolData($from_date, $date_to),
        ], 200);
    }

    public function edarehPatrolData(Request $request)
    {

        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').' 00:00:00';
            $date_to = Date('Y-m-d').' 23:59:59';
        }

        $request->validate([
            "province_id" => "required",
        ]);

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => RoadPatrolProject::edarehPatrolData($from_date, $date_to, $request->province_id),
        ], 200);
    }

    public function roadPatrolsReportTable(Request $request)
    {
        $name = 'گزارش از گشت راهداری و ترابری ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RoadPatrolTable($request->from_date, $request->date_to), $name);
    }
}
