<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Settings as mSetting;
use App\Pagename as mPage;
use App\Media as mMedia;
use App\MediaLocale as mMediaLocale;
use Storage;
use Image;
use Carbon\Carbon;
use App\Http\Services\Uploader;

class BackendMentionLegales extends Controller
{
   public function index()
   {
      $data['banner'] = mMedia::where(['media_page'=>"mentionslegales",'media_section'=>'banner-header'])->first();
      $data['contents'] = mMedia::where(['media_page'=>"mentionslegales",'media_section'=>'content'])->orderBy('temp_id','asc')->get();
      if( mSetting::where(['setting_name'=>'mentionsLegales'])->first() ):
        $data['mentions_legales_data'] = explode('|',mSetting::where(['setting_name'=>'mentionsLegales'])->first()->setting_value);
      else:
        $data['mentions_legales_data'] = "";
      endif;
      $data['page'] = mPage::where('key', 'mentionslegales')->first();

      return view('backend.mentions-legales')->with($data);
   }

   public function save_banner(Request $req)
   {
     $section = $req->section_name;
    	$id = $req->id;
      $caption = $req->banner_caption;

      if(empty($id)) { # Make new model if $id is empty
        $this->validate($req, [
            'image' => 'required',
            'banner_caption' => 'required'
        ]);

        # Make new database model
        $db = new mMedia;

      } else {

        # Update existing record
        $db = mMedia::find($id);

      }

      # Get the old image name incase no image uploaded
      $imageName = $db->media_url;

      # If new image uploaded
      if($req->image) {

        # Save new image
        $imageName = Uploader::SaveImage($req->image, 'banner_', 'img/mentionslegales/');

        # Delete old image if new one uploaded
        if($db->media_url!=''){
          Storage::delete('/img/mentionslegales/'.$db->media_url);
        }
      }

      # Insert rquired data
      $db->media_url = $imageName;
      $db->media_page = "mentionslegales";
      $db->media_section = "banner-header";

      # Save model into database
      $db->save();

      # Saving translation
      foreach (config('translatable.locales') as $lang => $language) {

        # Check if translation is available
        $data = $db->locales()->where('locale',$lang)->first();
        if(empty($data)){
          # Create new translation model
          $translation = new mMediaLocale;
          $translation->media_id = $db->id;
          $translation->locale = $lang;
        }else{
          # Update existing translation model
          $translation = mMediaLocale::find($data->id);
        }

        # Insert translation data
        $translation->media_title = $caption[$lang];
        # Save changes
        $translation->save();
      }

      $req->session()->flash('message-banner', 'Updated successfully!');
  		$req->session()->flash('alert-class', 'alert-success');
  		$req->session()->flash('anchor', '#message-banner');

  		return back();
   }

   public function save_data(Request $req)
   {
     $data = $req->all();
     unset($data['_token']);

     $i = 0;
     foreach ($data as $key => $value) {
       if(empty($save)){
         $save = $value;
       }else{
         $save = $save."|".$value;
       }
       $i++;
     }

     if ( $setting = mSetting::where(['setting_name'=>'mentionsLegales'])->first() ):
       $db = mSetting::find($setting->id);
       $db->setting_value = $save;
       $db->save();
     else:
       $db = new mSetting;
       $db->setting_name = "mentionsLegales";
       $db->setting_value = $save;
       $db->setting_desc = "Mentions Legales";
       $db->save();
     endif;

     $req->session()->flash('message', __('backend.updated'));
     $req->session()->flash('alert-class', 'alert-success');
     $req->session()->flash('anchor', '#message');

     return back();
   }
}
