<?php

namespace App\Http\Controllers;

use App\Settings as mSetting;
use App\Media as mMedia;
use App\Message as mMessage;
use App\MediaLocale as mMediaLocale;
use Storage;
use Image;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Services\Uploader; #custom class

class BackendContact extends Controller
{
    public function getMedia($section)
    {
        return mMedia::firstOrCreate(['media_page' => 'contact', 'media_section' => $section]);
    }

    public function getGeneral($section)
    {
        return mGeneral::firstOrCreate(['general_page'=>'contact','general_section'=> $section]);
    }

    public function getSetting($name, $default = '')
    {
        return mSetting::firstOrCreate(['setting_name' => $name], ['setting_value' => $default]);
    }
    public function __construct()
    {
        $this->middleware('auth');
    }

    function index(){
      $data['companyEmail'] = $this->getSetting('companyEmail');
      $data['companyPhone'] = $this->getSetting('companyPhone');
      $data['companyAddress'] = $this->getSetting('companyAddress');
      $data['companyLatLon'] = $this->getSetting('companyLatLon');

      $data['companyFacebook'] = $this->getSetting('companyFacebook');
      $data['companyInstagram'] = $this->getSetting('companyInstagram');
      $data['companyTwitter'] = $this->getSetting('companyTwitter');
      $data['companyPinterest'] = $this->getSetting('companyPinterest');
      $data['companyPath'] = $this->getSetting('companyPath');
      $data['companyGplus'] = $this->getSetting('companyGooglePlus');
      $data['companyLinkedIn'] = $this->getSetting('companyLinkedIn');
      $data['companySnapchat'] = $this->getSetting('companySnapchat');
      $data['companyWhatsapp'] = $this->getSetting('companyWhatsapp');
      $data['companyYoutube'] = $this->getSetting('companyYoutube');
      $data['companyLine'] = $this->getSetting('companyLine');


      $data['banner'] = $this->getMedia('banner-header');
      $data['messages'] = mMessage::all();

      $data['page'] = "contact";
      return view('backend.contact')->with($data);
    }

    function savecontactinfo(Request $req){
      $emailId = $req->companyEmailId;
      $phoneId = $req->companyPhoneId;
      $addressId = $req->companyAddressId;
      $latlonId = $req->companyLatLonId;
      $companyEmail = $req->companyEmail;
      $companyPhone = $req->companyPhone;
      $companyAddress = $req->companyAddress;
      $companyLatLon = $req->companyMaps;

      $this->validate($req, [
        'companyEmail' => 'email|required',
        'companyPhone' => 'required',
        'companyAddress' => 'required',
      ]);

    $save = [$emailId=>$companyEmail, $phoneId=>$companyPhone, $addressId=>$companyAddress, $latlonId=>$companyLatLon];

    foreach ($save as $key => $val) {
      $db = mSetting::find($key);
      $db->setting_value = $val;
      $db->save();
    }

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

    return back();
    }

    function savesocmed(Request $req){
      $fbId = $req->companyFacebookId;
      $igId = $req->companyInstagramId;
      $twId = $req->companyTwitterId;
      $ptId = $req->companyPinterestId;
      $paId = $req->companyPathId;
      $gpId = $req->companyGplusId;
      $liId = $req->companyLinkedInId;
      $scId = $req->companySnapchatId;
      $waId = $req->companyWhatsappId;
      $ytId = $req->companyYoutubeId;
      $lnId = $req->companyLineId;

      $companyFacebook = $req->companyFacebook;
      $companyInstagram = $req->companyInstagram;
      $companyTwitter = $req->companyTwitter;
      $companyPinterest = $req->companyPinterest;
      $companyPath = $req->companyPath;
      $companyGplus = $req->companyGplus;
      $companyLinkedIn = $req->companyLinkedIn;
      $companySnapchat = $req->companySnapchat;
      $companyWhatsapp = $req->companyWhatsapp;
      $companyYoutube = $req->companyYoutube;
      $companyLine = $req->companyLine;

      $this->validate($req, [
        'companyFacebook' => 'url|nullable',
        'companyInstagram' => 'url|nullable',
        'companyTwitter' => 'url|nullable',
        'companyPinterest' => 'url|nullable',
        'companyPath' => 'url|nullable',
        'companyGplus' => 'url|nullable',
        'companyLinkedIn' => 'url|nullable',
        'companySnapchat' => 'url|nullable',
        'companyWhatsapp' => 'nullable',
        'companyYoutube' => 'url|nullable',
        'companyLine' => 'nullable',
    ]);

    $save = [
      $fbId=>$companyFacebook,
      $igId=>$companyInstagram,
      $twId=>$companyTwitter,
      $ptId=>$companyPinterest,
      $paId=>$companyPath,
      $gpId=>$companyGplus,
      $liId=>$companyLinkedIn,
      $scId=>$companySnapchat,
      $waId=>$companyWhatsapp,
      $ytId=>$companyYoutube,
      $lnId=>$companyLine,
    ];

    foreach ($save as $key => $val) {
      $db = mSetting::find($key);
      $db->setting_value = $val;
      $db->save();
    }

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

    return back();
    }

    function save_banner(Request $req){
        $db = mMedia::find($req->id);
        $alt = $req->imagealt;

        $this->validate($req, [
            'image' => 'required',
        ]);

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

          # Save new image
          $imageName = Uploader::SaveImage($req->image, 'banner_', 'img/contact/', $alt[app()->getLocale()]);

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

        $db->media_url = $imageName;
        $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_desc = $alt[$lang];
          # Save changes
          $translation->save();
        }

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

        return back();
    }

    function read_message(Request $req){
        $id = $req->id;

        $db = mMessage::find($id);
        $db->message_status = "0";
        $db->save();
    }

    function delete_message(Request $req){
        $id = $req->id;
        mMessage::find($id)->delete();
        return back();
    }
}
