<?php

namespace App\Http\Controllers\Announcement;//call to announcement controller folder
use App\Http\Controllers\Controller;//call to controller class

use App\Http\Helpers\ConvertDateToTimeStamp;//call to convert datastampclass
use App\Models\Announcement;//call to model class
use Illuminate\Http\Request;//call to laravel request
use Carbon\Carbon;

class PostAnnouncementController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //validate field name in Form/view
        $this->validate($request, [
            'startdate' => 'required',
            'enddate' => 'required',
            'content' => 'required'
        ]);

        // create class objects
        $announcement = new Announcement();
        $convertdate = new ConvertDateToTimeStamp();

        //On left field name in DB and on right field name in Form/view
//        $announcement->posted_by = $request->input('name');
        $announcement->start_date = $convertdate::convertDateToTimeStamp($request->input('startdate'));
        $announcement->end_date = $convertdate::convertDateToTimeStamp($request->input('enddate'));
        $announcement->content = $request->input('content');
        $saved = $announcement->save();

        if($saved)
        {
            session()->put('success', 'Announcement Successfully Created!');
            return redirect()->back();
        }
        //the else part
        session()->put('error', 'There was an error creating this announcement!');
        return redirect()->back();

    }

}
