<?php

namespace Modules\Category\Http\Controllers;

use App\Functions\Upload;
use App\Http\Controllers\BasicController;
use Illuminate\Http\Request;
use Modules\Category\Entities\Model;
use Modules\Product\Entities\Model as Product;
use Yajra\DataTables\DataTables;

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

    public function datatable(Request $request)
    {
        $query = Model::query()
            ->when($request->parent_id, function ($query) use ($request) {
                return $query->where('parent_id', $request->parent_id);
            })
            ->when(! $request->parent_id, function ($query) {
                return $query->where(function ($query) {
                    $query->whereNull('parent_id')->orWhere('parent_id', 0);
                });
            });

        if (request()->sort_column && request()->sort_direction) {
            $query->orderBy(request()->sort_column, request()->sort_direction);
        }

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

    public function create()
    {
        $Products = Product::get();

        return view('category::create', compact('Products'));
    }

    public function store(Request $request)
    {
        $Model = Model::create($request->only(['parent_id', 'title_ar', 'title_en', 'status']));
        $Model->Products()->sync($request->products);
        if ($request->hasFile('file')) {
            $Model->image = Upload::UploadFile($request->file, 'Categories');
        }
        $Model->save();
        alert()->success(__('trans.addedSuccessfully'));

        return redirect()->back();
    }

    public function show($id)
    {
        $Model = Model::where('id', $id)->firstorfail();

        $Models = $Model->children;

        return view('category::show', compact('Models', 'Model'));
    }

    public function edit($id)
    {
        $Model = Model::with(['Products:id'])->findOrFail($id);
        $Products = Product::get();

        return view('category::edit', compact('Model','Products'));
    }

    public function update(Request $request, $id)
    {
        $Model = Model::where('id', $id)->firstorfail();
        $Model->update($request->only(['parent_id', 'title_ar', 'title_en', 'status']));
        $Model->Products()->sync($request->products);
        if ($request->hasFile('file')) {
            $Model->image = Upload::UploadFile($request->file, 'Categories');
        }
        $Model->save();
        alert()->success(__('trans.updatedSuccessfully'));

        return redirect()->back();
    }

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