<?php

namespace Modules\Branch\Http\Controllers;

use App\Functions\Upload;
use App\Http\Controllers\BasicController;
use Illuminate\Http\Request;
use Modules\Branch\Entities\Model;
use Modules\Country\Entities\Country as Country;
use Modules\Country\Entities\Region as Region;
use Yajra\DataTables\DataTables;

class WebController extends BasicController
{
    public function index(Request $request)
    {
        return view('branch::index');
    }

    public function datatable(Request $request)
    {
        $query = Model::query()
            ->with('Country', 'Region')
            ->when(request()->sort_column && request()->sort_direction, function ($query) {
                return $query->orderBy(request()->sort_column, request()->sort_direction);
            });

        return DataTables::of($query)
            ->addColumn('actions', function ($Model) {
                return view('branch::layouts.actions', ['Model' => $Model]);
            })
            ->editColumn('title_ar', function ($Model) {
                return view('branch::layouts.title_ar', ['Model' => $Model]);
            })
            ->editColumn('title_en', function ($Model) {
                return view('branch::layouts.title_en', ['Model' => $Model]);
            })
            ->addColumn('status', function ($Model) {
                return view('branch::layouts.status', ['Model' => $Model]);
            })
            ->addColumn('country_preview', function ($Model) {
                return view('branch::layouts.country_preview', ['Model' => $Model]);
            })
            ->editColumn('created_at', function ($Model) {
                return $Model->created_at->format('F j, Y, g:i a');
            })
            ->addColumn('region', function ($Model) {
                return $Model->Region->title();
            })
            ->addIndexColumn()
            ->rawColumns(['status', 'actions'])
            ->make(true);
    }

    public function create()
    {
        $Countries = Country::get();
        $Regions = Region::get();

        return view('branch::create', compact('Countries', 'Regions'));
    }

    public function store(Request $request)
    {
        $Model = Model::create($request->except('_method', '_token', 'images'));

        foreach ((array) $request->images as $img) {
            $Model->Images()->create([
                'image' => Upload::UploadFile($img, 'Branches'),
            ]);
        }
        alert()->success(__('trans.addedSuccessfully'));

        return redirect()->back();
    }

    public function edit($id)
    {
        $Model = Model::findorfail($id);
        $Countries = Country::get();
        $Regions = Region::get();

        return view('branch::edit', compact('Model', 'Countries', 'Regions'));
    }

    public function update(Request $request, $id)
    {
        $Model = Model::find($id);
        $Model->update($request->except('_method', '_token', 'images'));

        if ($request->images && count($request->images)) {
            foreach ((array) $request->images as $img) {
                $Model->Images()->create([
                    'image' => Upload::UploadFile($img, 'Branches'),
                ]);
            }
        }

        alert()->success(__('trans.updatedSuccessfully'));

        return redirect()->back();
    }

    public function destroy($id)
    {
        $Model = Model::where('id', $id)->delete();
    }
}
