<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ScheduledMaintenance;
use App\Models\ScheduledMessage;
use App\Models\StatusCategory;
use App\Models\SubCategory;
use Carbon\Carbon;
use Session;

class ScheduledMaintenanceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $mains = ScheduledMaintenance::orderBy('id' , 'DESC')->get();
        return view('Admin.Scheduled.index' , compact('mains'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $status = StatusCategory::orderBy('id' , 'DESC')->get();
        
        return view('Admin.Scheduled.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',
            'start_date' => 'required',
            'to_date' => 'required',
            'components' => 'required',
            'message' => 'required',
            'status_show' => 'required',
            'schedule_status'=>'required'
            ]);
            

            $start_date = $request->start_date; 
            $to_date = $request->to_date;
            
            $date = Carbon::now();
            
            $ok = ScheduledMaintenance::create([
                'name' => $request->name,
                'start_date' => $start_date,
                'to_date' => $to_date,
                'components' => $request->components,
                'status' => $request->status_show,
                'server_name' => $request->server_name,
                'schedule_status'=> $request->schedule_status
                ]);
            
            if($ok){

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

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

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

                ScheduledMessage::create([
                    'scheduled_maintenance_id' => $ok->id,
                    'message' => $request->message,
                    'date' => $date
                ]);
                return redirect()->route('ScheduledMain.index')->with('success_message' , 'Your Record Successfully Created');
            }
            
            
    }

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

    /**
     * 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();
        $main = ScheduledMaintenance::find($id);
        
        return view('Admin.Scheduled.update' , compact('status', 'main'));
    }

    /**
     * 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',
            'start_date' => 'required',
            'to_date' => 'required',
            'components' => 'required',
            'message' => 'required',
            'status_show' => 'required',
            'schedule_status'=> 'required'
            ]);
            

            $start_date = $request->start_date;
            $to_date = $request->to_date;
            
            $date = Carbon::now();
            
            $ok = ScheduledMaintenance::find($id)->update([
                'name' => $request->name,
                'start_date' => $start_date,
                'to_date' => $to_date,
                'components' => $request->components,
                'status' => $request->status_show,
                'server_name' => $request->server_name,
                'schedule_status'=> $request->schedule_status
                ]);
            
            if($ok){

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

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

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

                ScheduledMessage::create([
                    'scheduled_maintenance_id' => $id,
                    'message' => $request->message,
                    'date' => $date
                ]);

                return redirect()->route('ScheduledMain.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 = ScheduledMaintenance::find($id)->delete();
        return redirect()->route('ScheduledMain.index')->with('success_message' , 'Your Record Successfully Deleted');
    }
    
    
    
    public function DeleteMessage($id){
        
        
        $ok = ScheduledMessage::find($id)->delete();
        
        return redirect()->back()->with('success_message' , 'Your Message Successfully deleted');
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
