<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Asset;
use App\Http\Requests\StoreAssetRequest;
use App\Http\Requests\UpdateAssetRequest;
use Yajra\DataTables\DataTables;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\ImportAsset;
use App\Exports\ExportAsset;

class AssetController extends Controller
{

    /** construct */
    public function __construct()
    {
        $this->middleware('permission:asset-create', ['only' => ['create']]);
        $this->middleware('permission:asset-edit', ['only' => ['edit']]);
        $this->middleware('permission:asset-view', ['only' => ['view']]);
        // $this->middleware('permission:asset-delete', ['only' => ['delete']]);
    }
    /** construct */

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = Asset::orderBy('id', 'desc')->get();
            
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function ($data) {
                    $return = '<div class="btn-group">';

                    if (auth()->user()->can('asset-view')) {
                        $return .=  '<a href="'.route('asset.show', ['id' => $data->id]).'" class="btn btn-default btn-xs">
                                            <i class="fa fa-eye"></i>
                                        </a> &nbsp;';
                    }

                    if (auth()->user()->can('asset-edit')) {
                        $return .= '<a href="'.route('asset.edit', ['id' => $data->id]).'" class="btn btn-default btn-xs">
                                            <i class="fa fa-edit"></i>
                                        </a> &nbsp;';
                    }
                    $return .= '</div>';

                    return $return;
                })

                ->editColumn('item_name', function ($data) {
                        return $data->item_name;
                    
                })
                ->editColumn('amount', function ($data) {
                    return $data->amount;
                
                })
                ->rawColumns(['item_name', 'amount', 'action'])
                ->make(true);
        }
        return view('asset.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('asset.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreAssetRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAssetRequest $request)
    {
        $user = Asset::create($request->all());
        return redirect()->route('asset')->with('success', 'Record inserted successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Asset  $asset
     * @return \Illuminate\Http\Response
     */
    public function show(Asset $asset, $id)
    {
        $data = Asset::where(['id' => $id])->first();
        return view('asset.view',compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Asset  $asset
     * @return \Illuminate\Http\Response
     */
    public function edit(Asset $asset, $id)
    {
        $data = Asset::where(['id' => $id])->first();
        return view('asset.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateAssetRequest  $request
     * @param  \App\Models\Asset  $asset
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateAssetRequest $request)
    {
        $data = [
            'item_name' => $request->item_name,
            'amount' => $request->amount,
        ];
        $update = Asset::where(['id' => $request->id])->update($data);
        return redirect()->route('asset')->with('success', 'Record updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Asset  $asset
     * @return \Illuminate\Http\Response
     */
    public function destroy(Asset $asset)
    {
        //
    }

    /** Import */
        public function import(Request $request){

            $import = Excel::import(new ImportAsset(), $request->file('file'));    
            if($import){
                Excel::store(new ImportAsset(), 'Asset_'.Date('YmdHis').'.xlsx' ,'excel_import');
                return redirect()->route('asset')->with('sucess' ,'File Imported Sucessfully');
            }
        }
    /** Import */

    /** Export */
        public function export(Request $request){
            $slug = $request->slug;
            $name = 'Asset_'.Date('YmdHis').'.xlsx';
    
            try {
                return Excel::download(new ExportAsset($slug), $name);
            }catch(\Exception $e){
                return redirect()->back()->with('error', $e->getMessage());
            }
        }
    /** Export */
}
