<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Incident;
use App\Models\IncidentMessage;
use App\Models\StatusCategory;
use Carbon\Carbon; 

class IncidentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $ins = Incident::orderBy('id' , 'DESC')->get();

        return view('Admin.Incident.index' , compact('ins'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $status = StatusCategory::orderBy('id' , 'DESC')->get();
        return view('Admin.Incident.add' , compact('status'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request , [
            'name' => 'required',
            'impact' => 'required',
            'status' => 'required',
            'm_status' => 'required',
            'components' => 'required',
            'message' => 'required',
            'show_status' => 'required'
            ]);
            

            $date = Carbon::now();
            
            $ok = Incident::create([
                'name' => $request->name,
                'impact' => $request->impact,
                'components' => $request->components,
                'status' => $request->m_status,
                'show_status' => $request->show_status,
                'server_name' => $request->server_name
                ]);
            
            if($ok){

                foreach($request->status as $t){
                $str = explode('-' , $t);

                $id = $str[0];
                $val = $str[1];

                StatusCategory::find($id)->update([
                    'status' => $val
                ]);
                
            } 

                IncidentMessage::create([
                    'incident_id' => $ok->id,
                    'date' => $date,
                    'status' => $request->m_status,
                    'message' => $request->message
                ]);

               return redirect()->route('Incident.index')->with('success_message' , 'Your Record Successfully Created');
            }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $ins = IncidentMessage::where('incident_id' , $id)->orderBy('id' , 'DESC')->get(); 
        return view('Admin.Incident.show' , compact('ins'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $status = StatusCategory::orderBy('id' , 'DESC')->get();
        $in = Incident::find($id); 
        
        return view('Admin.Incident.update' , compact('status' , 'in'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $this->validate($request , [
            'name' => 'required',
            'impact' => 'required',
            'status' => 'required',
            'm_status' => 'required',
            'components' => 'required',
            'message' => 'required',
            'show_status' => 'required'
            ]);
            

            $date = Carbon::now();
            
            $ok = Incident::find($id)->update([
                'name' => $request->name,
                'impact' => $request->impact,
                'components' => $request->components,
                'status' => $request->m_status,
                'show_status' => $request->show_status,
                'server_name' => $request->server_name
                ]);
            
            if($ok){

                foreach($request->status as $t){
                $str = explode('-' , $t);

                $id_main = $str[0];
                $val = $str[1];

                StatusCategory::find($id_main)->update([
                    'status' => $val
                ]);
                
            } 

                IncidentMessage::create([
                    'incident_id' => $id,
                    'date' => $date,
                    'status' => $request->m_status,
                    'message' => $request->message
                ]);

                return redirect()->route('Incident.index')->with('success_message' , 'Your Record Successfully Updated');
            }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $ok = Incident::find($id)->delete();
        return redirect()->route('Incident.index')->with('success_message' , 'Your Record Successfully Deleted');
       
    }
}
