<?php

namespace App\Http\Controllers\Api\Excel;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Exports\Maintanance\allSheet as MaintananceSheets;
use App\Exports\RoadDanger\allSheet as RoadDangerSheets;
use App\Exports\RoadUpgrade\allSheet as RoadUpgradeSheets;
use App\Exports\Technical\allSheet as TechnicalSheets;
use App\Exports\Tollhouse\allSheet as TollhouseSheets;
use App\Exports\Village\allSheet as VillageSheets;
use App\Exports\AxisReport\AllSheet as AxisReportsSheets;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;
use App\Exports\Activities\One\AllSheets as OneSheet;
use App\Exports\Activities\Two\AllSheets as TwoSheet;
use App\Exports\Activities\Three\Activity as ActivitySheet;

use App\Exports\Activities\Two\Alaem\AllSheets as AlaemSheet;
use App\Exports\Activities\Two\Paksazi\AllSheets as PaksaziSheet;
use App\Exports\Activities\Two\Hefaz\AllSheets as HefazSheet;
use App\Exports\Activities\Two\Roshanaei\AllSheets as RoshanaeiSheet;
use App\Exports\Activities\Two\Khatkeshi\AllSheets as KhatkeshiSheet;
use App\Exports\Activities\Two\Zemestani\AllSheets as ZemestaniSheet;
use App\Exports\Activities\Two\Rangamizi\AllSheets as RangamiziSheet;
use App\Exports\Activities\Two\Shosteshoo\AllSheets as ShosteshooSheet;
use App\Exports\Activities\Two\Imensazi\AllSheets as ImensaziSheet;
use App\Exports\Activities\Two\Harim\AllSheets as HarimSheet;
use App\Exports\Activities\Two\Pol\AllSheets as PolSheet;
use App\Exports\Activities\Two\Toonel\AllSheets as ToonelSheet;
use App\Exports\Activities\Two\Mashinalat\AllSheets as MashinalatSheet;
use App\Exports\Activities\Two\Rahdar\AllSheets as RahdarSheet;
use App\Exports\Activities\Two\Ezterari\AllSheets as EzterariSheet;
use App\Exports\Activities\Two\Bazrasi\AllSheets as BazrasiSheet;
use App\Exports\Receipts\AllSheets as ReceiptSheet;
use App\Exports\User\AllSheets as UserList;
use App\Exports\UserActivity\AllSheet as UserActivity;
use App\Exports\DailyAccident\AllSheets as DailyAccident;
use App\Exports\RoadItems\AllSheets as RoadItems;
use App\Exports\RoadPatrol\AllSheets as RoadPatrol;
use App\Exports\SafetyAndPrivacy\AllSheets as SafetyAndPrivacy;

use App\Exports\ContractsExport;
use App\Exports\LawmakersExport;
use App\Exports\ProposalExport;


use App\Exports\FastNewReactPendingExport;
use App\Exports\FastNewReactDoneExport;

use Carbon\Carbon;

class ReportController extends Controller
{
    public function downloadContracts(Request $request)
    {
        auth()->user()->addActivityComplete(1010);
        // $name = 'گزارش لیست قراردادها ' . verta(Carbon::now())->format('Y-m-d H-i') . '.xlsx';
        $name = 'گزارش لیست قراردادها ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ContractsExport(
            $request->fromDate,
            $request->toDate,
            $request->province,
            $request->status,
            $request->projectType,
            $request->status_rel
        ), $name);
    }

    public function downloadLawmakers(Request $request)
    {
        auth()->user()->addActivityComplete(1093);
        $name = 'گزارش مکاتبات نمایندگان مجلس ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new LawmakersExport(
            $request->constituency,
            $request->province,
        ), $name);
    }

    public function downloadProposal(Request $request)
    {
        auth()->user()->addActivityComplete(1094);
        $name = 'گزارش پروژه های پیشنهادی ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ProposalExport(
            $request->province,
        ), $name);
    }
    

    public function downloadMaintanance(Request $request)
    {
        auth()->user()->addActivityComplete(1087);
        $name = 'گزارش از پروژه ها - جمع بندی روکش آسفالت ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new MaintananceSheets($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadVillage(Request $request)
    {
        auth()->user()->addActivityComplete(1088);
        $name = 'گزارش از پروژه ها - جمع بندی ساخت راه های روستایی ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new VillageSheets($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadRoadDanger(Request $request)
    {
        auth()->user()->addActivityComplete(1089);
        $name = 'گزارش از پروژه ها - جمع بندی نقاط حادثه خیز ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RoadDangerSheets($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadRoadUpgrade(Request $request)
    {
        auth()->user()->addActivityComplete(1090);
        $name = 'گزارش از پروژه ها - جمع بندی ارتقای ایمنی راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RoadUpgradeSheets($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadTechnical(Request $request)
    {
        auth()->user()->addActivityComplete(1091);
        $name = 'گزارش از پروژه ها - جمع بندی ابنیه فنی ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new TechnicalSheets($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadTollhouse(Request $request)
    {
        auth()->user()->addActivityComplete(1092);
        $name = 'گزارش از پروژه ها - جمع بندی راهدارخانه ها ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new TollhouseSheets($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }


    public function downloadActivitiesOne(Request $request)
    {
        auth()->user()->addActivityComplete(1034);
        $name = 'گزارش از فعالیت روزانه و جاری - جدول 1 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new OneSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadActivitiesThree(Request $request)
    {
        auth()->user()->addActivityComplete(1035);
        $name = 'گزارش از فعالیت روزانه و جاری - جدول 3 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ActivitySheet($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }
    
    
    public function downloadFastNewReactPending(Request $request)
    {
        auth()->user()->addActivityComplete(1095);
        $name = 'گزارش از واکنش سریع ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new FastNewReactPendingExport($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadFastNewReactDone(Request $request)
    {
        auth()->user()->addActivityComplete(1095);
        $name = 'گزارش از واکنش سریع ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new FastNewReactDoneExport($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }
    
    public function downloadAxisReports(Request $request)
    {
        auth()->user()->addActivityComplete(1096);
        $patrols = $request->patrol ?? null;
        $activities = $request->activity ?? null;
        $projects = $request->project ?? null;
        $observeds = $request->observed ?? null;
        $name = 'گزارش محوری ' . verta()->now()->format('Y-m-d H-i') .'.xlsx';
        return Excel::download(new AxisReportsSheets($patrols, $activities, $projects, $observeds), $name);
    }

    public function downloadMaremat(Request $request)
    {
        auth()->user()->addActivityComplete(1097);
        $name = 'گزارش از فعالیت روزانه و جاری(مرمت رویه) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new TwoSheet($request->fromDate, $request->toDate, $request->province, $request->last_status_id), $name);
    }

    public function downloadPaksazi(Request $request)
    {
        auth()->user()->addActivityComplete(1098);
        $name = 'گزارش از فعالیت روزانه و جاری(پاکسازی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new PaksaziSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadAlaem(Request $request)
    {
        auth()->user()->addActivityComplete(1099);
        $name = 'گزارش از فعالیت روزانه و جاری(علائم) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new AlaemSheet($request->fromDate, $request->toDate, $request->province), $name);
    }


    public function downloadHefaz(Request $request)
    {
        auth()->user()->addActivityComplete(1100);
        $name = 'گزارش از فعالیت روزانه و جاری(حفاظ) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new HefazSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadRoshanaei(Request $request)
    {
        auth()->user()->addActivityComplete(1101);
        $name = 'گزارش از فعالیت روزانه و جاری(روشنایی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RoshanaeiSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadKhatkeshi(Request $request)
    {
        auth()->user()->addActivityComplete(1102);
        $name = 'گزارش از فعالیت روزانه و جاری(خطکشی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new KhatkeshiSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadZemestani(Request $request)
    {
        auth()->user()->addActivityComplete(1103);
        $name = 'گزارش از فعالیت روزانه و جاری( زمستانی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ZemestaniSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadRangamizi(Request $request)
    {
        auth()->user()->addActivityComplete(1104);
        $name = 'گزارش از فعالیت روزانه و جاری(رنگ آمیزی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RangamiziSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadShosteshoo(Request $request)
    {
        auth()->user()->addActivityComplete(1105);
        $name = 'گزارش از فعالیت روزانه و جاری(شستشو) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ShosteshooSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadImensazi(Request $request)
    {
        auth()->user()->addActivityComplete(1106);
        $name = 'گزارش از فعالیت روزانه و جاری(ایمن سازی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ImensaziSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadHarim(Request $request)
    {
        auth()->user()->addActivityComplete(1107);
        $name = 'گزارش از فعالیت روزانه و جاری(حریم) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new HarimSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadPol(Request $request)
    {
        auth()->user()->addActivityComplete(1108);
        $name = 'گزارش از فعالیت روزانه و جاری(پل) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new PolSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadToonel(Request $request)
    {
        auth()->user()->addActivityComplete(1109);
        $name = 'گزارش از فعالیت روزانه و جاری(تونل) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ToonelSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadMashinalat(Request $request)
    {
        auth()->user()->addActivityComplete(1110);
        $name = 'گزارش از فعالیت روزانه و جاری(ماشین آلات) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new MashinalatSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadRahdar(Request $request)
    {
        auth()->user()->addActivityComplete(1111);
        $name = 'گزارش از فعالیت روزانه و جاری(راهدارخانه) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RahdarSheet($request->fromDate, $request->toDate, $request->province), $name);
    }
    public function downloadEzterari(Request $request)
    {
        auth()->user()->addActivityComplete(1112);
        $name = 'گزارش از فعالیت روزانه و جاری(اظطراری) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new EzterariSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadbazrasi(Request $request)
    {
        auth()->user()->addActivityComplete(1113);
        $name = 'گزارش از فعالیت روزانه و جاری(بازرسی) - جدول 2 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new BazrasiSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadUserActivityReport(Request $request)
    {
        auth()->user()->addActivityComplete(1116);
        $name = 'گزارش از فعالیت کاربران ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new UserActivity($request->group_id, $request->fromDate, $request->toDate), $name);
    }

    public function downloadReceiptReport(Request $request)
    {
        auth()->user()->addActivityComplete(1126);
        $name = 'گزارش از  خسارات وارده به ابنیه فنی و تاسیسات راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ReceiptSheet($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadUserReport(Request $request)
    {
        auth()->user()->addActivityComplete(1127);
        $name = 'گزارش از  کاربران سایت ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new UserList($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadDailyAccidentReport(Request $request)
    {
        $name = 'گزارش از  تصادفات روزانه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new DailyAccident($request->fromDate, $request->toDate, $request->province), $name);
    }
    
    public function downloadRoadItemsReport(Request $request)
    {
        $name = 'گزارش از  فعالیت روزانه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RoadItems($request->fromDate, $request->toDate, $request->province), $name);
    }
    

    public function downloadRoadPatrolsReport(Request $request)
    {
        $name = 'گزارش از گشت راهداری روزانه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new RoadPatrol($request->fromDate, $request->toDate, $request->province), $name);
    }

    public function downloadSafetyAndPrivacyReport(Request $request)
    {
        $name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        // return Excel::download(new SafetyAndPrivacy($request->fromDate, $request->toDate, $request->province), $name);
        return Excel::download(new SafetyAndPrivacy($request->id, $request->info_id, $request->step, $request->fromDate, 
                                                    $request->toDate, $request->province_id), $name);
        
    }

    public function redirect(Request $request)
    {
        if ($request->province) {
            $url_correction = "&province={$request->province}";
        }else {
            $url_correction = "";
        }

        switch ($request->type) {
            case 1:
                return redirect("/reports/two/maremat?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
                    
            case 2:
                return redirect("/reports/two/paksazi?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;

            case 3:
                return redirect("/reports/two/alaem?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
            
            case 4:
                return redirect("/reports/two/hefaz?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;

            case 5:
                return redirect("/reports/two/roshanaei?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;

            case 6:
                return redirect("/reports/two/khatkeshi?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
                
            case 7:
                return redirect("/reports/two/rangamizi?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
            
            case 8:
                return redirect("/reports/two/shosteshoo?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
    
            case 9:
                return redirect("/reports/two/imensazi?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
                
    
            case 10:
                return redirect("/reports/two/harim?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
                
    
            case 11:
                return redirect("/reports/two/pol?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
    
            case 12:
                return redirect("/reports/two/toonel?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
        
            case 13:
                return redirect("/reports/two/zemestani?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
        
            case 14:
                return redirect("/reports/two/mashinalat?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
        
            case 15:
                return redirect("/reports/two/rahdar?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;

            case 16:
                return redirect("/reports/two/ezterari?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;

            case 20:
                return redirect("/reports/two/bazrasi?fromDate={$request->fromDate}&toDate={$request->toDate}".$url_correction);
                break;
    
            default:
                return response()->json([
                    'error' => '404'
                ], 404);
                break;
    
        }
    }

}