<?php
  
namespace App\Http\Controllers;
  
use App\Section;
use App\News;
use App\SectionAdv;
use Illuminate\Http\Request;
  
class SectionController extends Controller
{
    
    protected $rules = [
        'name' => ['required']
    ];

    public function index()
    {
        $sections = Section::orderBy('position')->get(); //->paginate(50);
  
        return view('sections.index',compact('sections'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('sections.create');
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["position"] = $input["position"] != '' ? $input["position"] : 0;
        $input["before"] = isset($input["before"]) ? ($input["before"] == 'on' ? true : false) : false;
        $input["after"] = isset($input["after"]) ? ($input["after"] == 'on' ? true : false) : false;
        $input["show_social"] = isset($input["show_social"]) ? ($input["show_social"] == 'on' ? true : false) : false;
        $input["show_social_twitter"] = isset($input["show_social_twitter"]) ? ($input["show_social_twitter"] == 'on' ? true : false) : false;
        Section::create($input);
   
        return redirect()->route('sections.index')
                        ->with('success','Section created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function show(Section $section)
    {
        return view('sections.show',compact('section'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function edit(Section $section)
    {
        return view('sections.edit',compact('section'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Section $section)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["position"] = $input["position"] != '' ? $input["position"] : 0;
        $input["before"] = isset($input["before"]) ? ($input["before"] == 'on' ? true : false) : false;
        $input["after"] = isset($input["after"]) ? ($input["after"] == 'on' ? true : false) : false;
        $input["show_social"] = isset($input["show_social"]) ? ($input["show_social"] == 'on' ? true : false) : false;
        $input["show_social_twitter"] = isset($input["show_social_twitter"]) ? ($input["show_social_twitter"] == 'on' ? true : false) : false;
        $section->update($input);
  
        return redirect()->route('sections.index')
                        ->with('success','Section updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function destroy(Section $section)
    {
        $news = News::where(function ($q) use ($section) {
            $q->where('section_id', '=', $section->id)->orWhere('region_1_id', '=', $section->id)->orWhere('region_2_id', '=', $section->id);
        })->get();
        foreach($news as $n)
        {
            if ($n->section_id == $section->id)
                $n->section_id = null;
            if ($n->region_1_id == $section->id)
                $n->region_1_id = null;
            if ($n->region_2_id == $section->id)
                $n->region_2_id = null;
            $n->save();
        }

        SectionAdv::where('section_id', '=', $section->id)->delete();

        $section->delete();
  
        return redirect()->route('sections.index')
                        ->with('success','Section deleted successfully');
    }
}