<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Cms\Classes\Controller;
use BackendMenu;

use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;
use Tps\Tps\Models\Contacts;
class ContactController extends Controller
{
	protected $Contacts;

    protected $helpers;

    public function __construct(Contacts $Contacts, Helpers $helpers)
    {
        parent::__construct();
        $this->Contacts    = $Contacts;
        $this->helpers          = $helpers;
    }

    public function index(){
	//
        $data = $this->Contacts->all()->toArray();

        return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
    }

    public function show($id){

        $data = $this->Contacts::find($id);

        if ($data){
            return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
        } else {
            $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
        }

    }

    public function store(Request $request){

        $arr = $request->all();
        unset($arr['/api/v1/contact']);
    
        while ($data = current($arr)) {
            $this->Contacts->{key($arr)} = $data;
            next($arr);
        }
    
        $validation = Validator::make($request->all(), $this->Contacts->rules);
        
        if($validation->passes()){
            $this->Contacts->save();
    
            $vars = [
                'name' => $request->input('name'),
                'email' => $request->input('email'),
                'messsage' => $request->input('message'),
                'phone' => $request->input('phone'),
            ];
    
            \Mail::send('ahmadfatoni.apigenerator::mail.contactForm', $vars, function($message) use ($request) {
    
                $message->to('gerchekgerchek1@gmail.com', 'Admin Person');
                $message->subject('Menzil Mekan');
    
                if($request->hasFile('file')){
                    $file = $request->file('file');
                    $message->attach($file->getRealPath(), [
                        'as' => $file->getClientOriginalName(),
                        'mime' => $file->getClientMimeType(),
                    ]);
                }
            });
    
            return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Contacts->id]);
        } else {
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors());
        }
    }
    
    
    

    public function update($id, Request $request){

        $status = $this->Contacts->where('id',$id)->update($data);
    
        if( $status ){
            
            return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.');

        }else{

            return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.');

        }
    }

    public function delete($id){

        $this->Contacts->where('id',$id)->delete();

        return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
    }

    public function destroy($id){

        $this->Contacts->where('id',$id)->delete();

        return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
    }


    public static function getAfterFilters() {return [];}
    public static function getBeforeFilters() {return [];}
    public static function getMiddleware() {return [];}
    public function callAction($method, $parameters=false) {
        return call_user_func_array(array($this, $method), $parameters);
    }
    
}
