<?php

namespace App\Http\Controllers\Exports;

use App\Classes\Modules\Exports\Services\ExportsCompanyModuleSummary;
use App\Classes\Modules\Exports\Services\ExportsOrderSummaryByMarking;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;

class ExportCompanyModuleSummaryController
{
    /**
     * ExportCompanyModuleSummaryController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $token = Auth::fromUser(User::find(1));
        $request->headers->set('Authorization', 'Bearer '.$token);
    }

    public function export(Request $request) {
        $exportsCompanyModuleSummary = new ExportsCompanyModuleSummary($request);
        $exportsCompanyModuleSummary->setParameters($request->route('company_module_id'), $request->input('containerNumber'));
        $response = $exportsCompanyModuleSummary->download('customerid-markingno.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        ob_end_clean();
        return $response;
    }

    public function exportOrderSummaryByMarking(Request $request) {
        $exportsOrderSummaryByMarking = new ExportsOrderSummaryByMarking($request);
        $customer_marking = $request->route('marking');
        $exportsOrderSummaryByMarking->setParameters($customer_marking, $request->input('dateFrom'), $request->input('dateTo'));
        $response = $exportsOrderSummaryByMarking->download($customer_marking . '_order_summary.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        ob_end_clean();
        return $response;
    }
}
