<?php

namespace App\Http\Controllers;

use App\Media as mMedia;
use App\General as mGeneral;
use App\Settings as mSetting;
use App\Pagename as mPage;

use Illuminate\Http\Request;

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

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

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

    function index(Request $req){
      if( config('corepackage.locales.activate')){
        $page = $req->segment(2);
      }else{
        $page = $req->segment(1);
      }

      $section = mPage::where(['slug'=>$page])->first()->key;

    	$data['section'] = $page;
      $data['page'] = $section;
      $page_id = mPage::where('key',$section)->first()->id;
      $data['meta_tags'] = mPage::find($page_id)->meta_tags;
    	$data['banner'] = $this->getMedia($section, 'banner-header');
      $data['contents'] = mMedia::where(['media_page'=>$section,'media_section'=>'content'])->orderBy('temp_id','asc')->get();
    	return view('section')->with($data);
    }
}
