<?php
  
namespace App\Http\Controllers;
  
use App\BreakingNews;
use Illuminate\Http\Request;
use File;
  
class BreakingNewsController extends Controller
{
    
    protected $rules = [
        'title' => ['required']
    ];

    public function index()
    {
        $breaking_news = BreakingNews::orderBy('date', 'DESC')->get(); //->paginate(50);
  
        return view('breaking_news.index',compact('breaking_news'))
            ->with('i', (request()->input('breaking_news', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('breaking_news.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["online"] = true; //isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;

        $final_date = null;
        if ($input["date"] != '')
        {
            list($dt, $time) = explode(" ", $input["date"]);
            list($day, $month, $year) = explode("/", $dt);
            $final_date = $year . "-" . $month . "-" . $day . " " . $time;
        }
        $input["date"] = $final_date;

        BreakingNews::create($input);
   
        return redirect()->route('breaking_news.index')
                        ->with('success','BreakingNews created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\BreakingNews  $breakingnews
     * @return \Illuminate\Http\Response
     */
    public function show(BreakingNews $breaking_news)
    {
        return view('breaking_news.show',compact('breaking_news'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\BreakingNews  $breakingnews
     * @return \Illuminate\Http\Response
     */
    public function edit(BreakingNews $breaking_news)
    {
        $final_date = null;
        if ($breaking_news->date != null)
        {
            list($dt, $time) = explode(" ", $breaking_news->date);
            list($year, $month, $day) = explode("-", $dt);
            $final_date = $day . "/" . $month . "/" . $year . " " . $time;
        }
        $breaking_news->date = $final_date;

        return view('breaking_news.edit',compact('breaking_news'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\BreakingNews  $breakingnews
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, BreakingNews $breaking_news)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;

        $final_date = null;
        if ($input["date"] != '')
        {
            list($dt, $time) = explode(" ", $input["date"]);
            list($day, $month, $year) = explode("/", $dt);
            $final_date = $year . "-" . $month . "-" . $day . " " . $time;
        }
        $input["date"] = $final_date;

        $breaking_news->update($input);
  
        return redirect()->route('breaking_news.index')
                        ->with('success','BreakingNews updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\BreakingNews  $breakingnews
     * @return \Illuminate\Http\Response
     */
    public function destroy(BreakingNews $breaking_news)
    {

        $breaking_news->delete();
  
        return redirect()->route('breaking_news.index')
                        ->with('success','BreakingNews deleted successfully');
    }
}