<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use App\Country;
use Illuminate\Support\Facades\Validator;

class CountriesController extends AdminController
{
    public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/countries";
        $this->breadcrumbs[1]['name'] = "Countries";
    }
    
    public function index() {

        $model = Country::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "Country";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/countries/index', $data);
    }
    
    public function create() {
        $data["pageHeader"] = false;
        $data["model"] = new Country();
        $data["title"] = "Country";
        $data["subTitle"] = "Create";

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/countries/create', $data);
    }
    
    public function save(Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
             'name' => 'required|max:255',
             'code' => 'required|max:255',
             'iso_code' => 'required|max:255',
             'dialing_code' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            unset($input["_token"]);
           
            $model = new Country();
            $input["created_at"] = date("Y-m-d H:i:s");
            $id = $model->insertGetId($input);


            \Session::flash('success', 'Country added successfully!');
            return redirect('admin/countries');
        }
    }
    
    public function show($id) {

        $q = new Country();
        $data['model'] = $q->find($id);
        $data["title"] = "Country";

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->title;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/countries/show', $data);
    }
    
    public function edit($id) {
        $data["model"] = Country::find($id);
        $data["pageHeader"] = false;
        $data["title"] = "Country";
        $data["subTitle"] = "Edit";
        $data["id"] = $id;
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/countries/edit', $data);
    }
    
    public function update($id, Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
            'name' => 'required|max:255',
             'code' => 'required|max:255',
             'iso_code' => 'required|max:255',
             'dialing_code' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            unset($input["_token"]);


            $input["updated_at"] = date("Y-m-d H:i:s");
            Country::where('id', $id)->update($input);
            \Session::flash('success', 'Country updated successfully!');
            return redirect('admin/countries');
        }
    }
    
      public function delete($id) {


        Country::where('id', $id)->delete();
        \Session::flash('error', 'Country deleted successfully!');
        return redirect('admin/countries');
    }
}
