<?php

namespace App\Http\Controllers\Admin;

use App\Exports\ExportSuppliersProfitCentersExcel;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Maatwebsite\Excel\Facades\Excel;

class ExportController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {
        return view('admin.export.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(int $id)
    {
        if (\request()->get('fileName')) {
            $fileNameWithPath = \Storage::disk()->path('Export'.DIRECTORY_SEPARATOR.\request()->get('fileName'));

            return response()->file($fileNameWithPath, ['Content-Disposition' => 'attachment;filename='.\request()->get('fileName')]);
        }

        $Suppliers = \App\Models\Supplier::with(['profitCenter' => function ($query) {
            $query->orderBy('name');
        }])->orderBy('name')->get();

        $fileName = 'SuppliersProfitCenters_'.date('Ymd_His').'.xlsx';
        Excel::store(new ExportSuppliersProfitCentersExcel(['Suppliers' => $Suppliers]), 'Export/'.$fileName);

        return view('admin.export.SupplierProfitCenterExcelSuccess', ['fileName' => $fileName]);

        return response()->json(['success' => true]);

        return Excel::download(new ExportSuppliersProfitCentersExcel(['Suppliers' => $Suppliers]), 'SuppliersProfitCenters'.date('Ymd_His').'.xlsx');

        return view('admin.export.SupplierProfitCenterExcel')->with(['Suppliers' => $Suppliers]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function edit(int $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, int $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy(int $id)
    {
        //
    }
}
