<?php

namespace App\Http\Controllers\Settings;

use App\Http\Controllers\Controller;

use App\Models\TravelCountry;
use App\Models\TravelNationality;
use App\Models\TravelNationalityRequirement;
use Carbon\Carbon;
use HTMLHelper;
use Input;

class TravelCountryController extends Controller
{
    public function __construct()
    {
       $this->middleware('admin');

    }

    public function index($step = false)
    {
        $data = [
            'travel_countries' => TravelCountry::all(),
        ];
        return view('settings.travel_country.index', $data);
    }

    public function detail($id)
    {
        if($id == "new") {
            $model = new TravelCountry();
            $id = 'new';
            $model->active_backend = 1;
        }else{
            $model = TravelCountry::findOrFail($id);
            $id = $model->id;
        }
        $data = [
            'model' => $model,
            'id' => $id,
            'travel_nationalities' => TravelNationality::where('active', true)->get(),
        ];
        return view('settings.travel_country.detail', $data);
    }



      public function store(){

          $data = Input::all();
          $data['is_customer_country'] = isset($data['is_customer_country']) ? true : false;
          $data['active_frontend'] = isset($data['active_frontend']) ? true : false;
          $data['active_backend'] = isset($data['active_backend']) ? true : false;

          if($data['id'] == "new"){
              $data['crm_id'] = 0;
              $model = TravelCountry::create($data);
          }else{
              $model = TravelCountry::find($data['id']);
              $model->fill($data);
              $model->save();
          }

          //travel_nationality_requirement
          if(isset($data['travel_nationality_requirement'])){
              foreach ($data['travel_nationality_requirement'] as $travel_nationality_id => $text){
                  $model->setNationalityRequirement($travel_nationality_id, $text);
              }
          }

          //TODO for this thime
          //we need an update in the old CRM v1 system DB
          $tc = \App\Models\Sym\TravelCountry::find($model->crm_id);
          if(!$tc){
              $tc = \App\Models\Sym\TravelCountry::create($data);
              $model->crm_id = $tc->id;
              $model->save();
          }else{
              $tc->fill($data);
              $tc->save();
          }

          \Session()->flash('alert-save', '1');
          return redirect(route('admin_settings_travel_country_detail', [$model->id]));

      }

    public function delete($id){
        $model = TravelCountry::findOrFail($id);
        if( $model->travel_nationality_requirements){
            foreach($model->travel_nationality_requirements as $travel_nationality_requirement){
                $travel_nationality_requirement->delete();
            }
        }
        $tc = \App\Models\Sym\TravelCountry::find($model->crm_id);
        if($tc){
            $tc->delete();
        }

        $model->delete();
        \Session()->flash('alert-success', 'Eintrag gelöscht');
        return redirect()->back();
    }

}


