<?php

namespace Modules\Asset\Http\Controllers;

use App\Helper\Files;
use App\Helper\Reply;
use App\Http\Controllers\AccountBaseController;
use App\Models\User;
use Illuminate\Http\Response;
use Modules\Asset\DataTables\AssetDataTable;
use Modules\Asset\Entities\Asset;
use Modules\Asset\Entities\AssetType;
use Modules\Asset\Http\Requests\StoreRequest;
use Modules\Asset\Http\Requests\UpdateRequest;

class AssetController extends AccountBaseController
{

    public function __construct()
    {
        parent::__construct();
        $this->pageTitle = __('asset::app.menu.asset');
    }

    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index(AssetDataTable $dataTable)
    {
        $this->viewAssetPermission = user()->permission('view_asset');

        abort_403($this->viewAssetPermission == 'none');

        $this->assetType = AssetType::all();
        $this->employees = User::allEmployees();
        $this->totalAssets = Asset::count();
        $this->status = ['lent', 'available', 'non-functional'];

        return $dataTable->render('asset::asset.index', $this->data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        $this->addPermission = user()->permission('add_asset');
        abort_403($this->addPermission !== 'all');

        $this->assets = new Asset();
        $this->assetType = AssetType::all();

        $this->view = 'asset::asset.ajax.create';

        if (request()->ajax()) {
            $html = view($this->view, $this->data)->render();

            return Reply::dataOnly(['status' => 'success', 'html' => $html, 'title' => $this->pageTitle]);
        }

        return view('asset::asset.create', $this->data);

    }

    public function store(StoreRequest $request)
    {
        $asset = new Asset();
        $this->storeUpdate($asset, $request);

        return Reply::successWithData(__('asset::app.storeSuccess'), ['redirectUrl' => route('assets.index')]);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function edit($id)
    {
        $this->editPermission = user()->permission('edit_asset');
        abort_403($this->editPermission !== 'all');

        $this->asset = Asset::findOrFail($id);
        $this->assetType = AssetType::all();

        $this->view = 'asset::asset.ajax.edit';

        if (request()->ajax()) {
            $html = view($this->view, $this->data)->render();

            return Reply::dataOnly(['status' => 'success', 'html' => $html, 'title' => $this->pageTitle]);

        }

        return view('asset::asset.create', $this->data);
    }

    public function update(UpdateRequest $request, $id)
    {
        $asset = Asset::findOrFail($id);
        $this->storeUpdate($asset, $request);

        return Reply::successWithData(__('messages.updatedSuccessfully'), ['redirectUrl' => route('assets.index')]);
    }

    private function storeUpdate($asset, $request)
    {
        $asset->name = $request->name;
        $asset->serial_number = $request->serial_number;
        $asset->asset_type_id = $request->asset_type_id;
        $asset->value = $request->value;
        $asset->location = $request->location;

        if ($request->has('description')) {
            $asset->description = $request->description;
        }

        if ($asset->status != 'lent') {
            $asset->status = $request->has('status') ? 'non-functional' : 'available';
        }

        if ($request->image_delete == 'yes') {
            Files::deleteFile($asset->image, 'avatar');
            $asset->image = null;
        }

        if ($request->hasFile('image')) {
            Files::deleteFile($asset->image, 'avatar');
            $asset->image = Files::upload($request->image, 'assets');
        }

        $asset->save();
    }

    public function show($id)
    {
        $this->viewPermission = user()->permission('view_asset');
        abort_403($this->viewPermission == 'none');

        $this->asset = Asset::with(['history' => function ($query) {
            return $query->orderBy('id', 'desc');
        }, 'assetType'])->findOrFail($id);

        $this->history = 'asset::asset.ajax.history';
        $this->view = 'asset::asset.ajax.show';

        if (request()->ajax()) {
            $html = view($this->view, $this->data)->render();

            return Reply::dataOnly(['status' => 'success', 'html' => $html, 'title' => $this->pageTitle]);
        }

        return view('asset::asset.create', $this->data);
    }

    public function destroy($id)
    {
        $deletePermission = user()->permission('delete_asset');
        abort_403(!in_array($deletePermission, ['all', 'added']));

        Asset::destroy($id);

        return Reply::success(__('messages.deleteSuccess'));

    }

}
