<?php


namespace App\Http\Controllers\Hr;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\AssetRequest;
use App\Asset;

class AssetController extends Controller
{
    
    public function index()
    {   
        $asset = new Asset;

        
        $asset_types = $asset->getDistinctAsset('type',['type']);
        
        
        return view('hr.asset.manage_asset',
        	[

        		'asset_list'           => $asset->getAssetList(),
        		'types'                => $asset_types,
              
        	]
    );
       
    }

    public function storeAsset(AssetRequest $request)
    {


        $asset = new Asset;

        $form_collect = $request->input();

        if($form_collect['asset_type'] === "Other")
        {
            $form_collect['type'] =  $form_collect['type'];
        }
        else
        {
            $form_collect['type'] =  $form_collect['asset_type'];
        }


        $curr_asset = $asset->storeAsset($form_collect);

        if(isset($curr_asset->id))
        {
            parent::setLogMessage('Asset "'.$curr_asset->name.' Code: '.$curr_asset->asset_code.'" has been created');

            parent::successMessage('Asset has been added.',parent::getLogMessage());
        }

        return redirect('asset');
    }


     public function updateAsset(AssetRequest $request)
    {
        try 
        {
            

            $form_collect = $request->input();

            $form_collect['asset'] = decrypt($request->asset);

            $asset = new Asset;


            $curr_asset = $asset->updateAsset($form_collect);

            if(isset($curr_asset))
            {
                parent::setLogMessage('Asset "'.$curr_asset->name.' Code: '.$curr_asset->asset_code.'" has been updated');

                parent::successMessage('Asset has been updated.',parent::getLogMessage());
            }
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect('asset');
    }

}
