<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Province;
use App\Models\City;
use App\Models\Accident;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use App\Facades\Sms\Sms;
use Illuminate\Support\Facades\View as ViewFacade;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\JsonResponse;
use Maatwebsite\Excel\Facades\Excel;

use App\Exports\V2\Receipt\RecieptTable\AllSheets as ReceiptTable;


class ReceiptReportController extends Controller
{

    function index() : View {

        return ViewFacade::make('version2.dashboard_pages.receipt.report');
    }

    public function provinceReceiptData(Request $request) : JsonResponse
    {
        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' => Accident::provinceReceiptData($from_date, $date_to),
        ], 200);
    }

    public function cityReceiptData(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' => Accident::cityReceiptData($from_date, $date_to, $request->province_id),
        ], 200);
    }

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