<?php

namespace Wave\Http\Controllers\CMS;

use App\Http\Controllers\Controller;
use App\Models\BookkeepingDocumentCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;

class BookkeepingCategoryController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('theme::cms.bookkeepings.categories.index');
    }

    public function table()
    {
        $categories = BookkeepingDocumentCategory::query()->select('*');

        return Datatables::of($categories)
            ->escapeColumns(['id'])
            ->addColumn('status', function ($user) {
                $status = $user->status;
                if ($status == 'active') {
                    $status = '<span class="success item-list-note">' . ucwords($status) . '</span>';
                }
                else if ($status == 'inactive') {
                    $status = '<span class="failed item-list-note">' . ucwords($status) . '</span>';
                }

                return $status;
            })
            ->addColumn('actions', function ($category) {
                $viewAction = '<a class="primary-text view-edit-category" href="#" data-action-type="view" data-action-show="' . route('cms.bookkeepings.categories.show', $category->id) . '" data-action-update="' . route('cms.bookkeepings.categories.update', $category->id) . '">' . __("View") . '</a>';
                $editAction = '<a class="primary-text view-edit-category" href="#" data-action-type="edit" data-action-show="' . route('cms.bookkeepings.categories.show', $category->id) . '" data-action-update="' . route('cms.bookkeepings.categories.update', $category->id) . '">' . __("Edit") . '</a>';
                $removeAction = '<a class="failed item-list-note remove-category" href="#" data-action="' . route('cms.bookkeepings.categories.destroy', $category->id) . '">' . __("Archive") . '</a>';
                $actions = $viewAction . '<span class="long-pipe">|</span>' . $editAction . '<span class="long-pipe">|</span>' . $removeAction;

                return $actions;
            })
            ->make(true);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:bookkeeping_document_categories',
            'status' => 'required|string',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $validator->errors()
            ], 422);
        }
        else {
            BookkeepingDocumentCategory::create([
                'name' => $request->name,
                'status' => $request->status,
            ]);
    
            return response()->json([
                'success' => true,
                'message' => __("All changes have been saved!"),
            ]);
        }
    }

    public function show(BookkeepingDocumentCategory $category)
    {
        return response()->json([
            'success' => true,
            'message' => __("Bookkeeping category details has successfully retrieved!"),
            'category' => $category,
        ]);
    }

    public function update(BookkeepingDocumentCategory $category, Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:bookkeeping_document_categories,name,' . $category->id,
            'status' => 'required|string',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $validator->errors()
            ], 422);
        }
        else {
            $category->update($request->all());
    
            return response()->json([
                'success' => true,
                'message' => __("All changes have been saved!"),
            ]);
        }
    }

    public function destroy(BookkeepingDocumentCategory $category)
    {
        $category->delete();

        return response()->json([
            'success' => true,
            'message' => __("Category has successfully removed!"),
        ]);
    }
}
