<?php namespace App\Http\Controllers;

use App\Repositories\WebsiteRepository;
use Input;
use Session;


class WebsiteController extends Controller
{
    public $websiteRepository;

    public function __construct(WebsiteRepository $websiteRepository)
    {
        $this->websiteRepository = $websiteRepository;
    }

    public function getWebsites()
    {
        $response = $this->websiteRepository->getWebsites();
        $websites=$response['websites'];
        return view('websites', ['websites' => $websites]);
    }

    public function addWebsite()
    {
        $response = $this->websiteRepository->addWebsite(Input::get('website'));

        if ($response['result'] == 0) {
            return redirect()->back()->withInput(Input::all())->withErrors($response['validator']);

        }

        if ($response['result'] == 1) {

            Session::flash('success_msg', 'Website Added Successfully');

            return redirect()->back();
    }

        if ($response['result'] == 2) {
            Session::flash('error_msg', "Free membership allows you to add three websites get premium for unlimited websites");
            return redirect()->back();
        }




    }

    public function deleteWebsite($id)
    {
        if(env('WRITE_ACCESS')==false)
        {
            Session::flash('error_msg', 'Disabled In Demo');

            return redirect()->back();
        }

        $response = $this->websiteRepository->deleteWebsite($id);
        if ($response['result'] == 1) {

            Session::flash('error_msg', 'Website Deleted Successfully');

            return redirect()->back();
        }
    }



}
