<?php

namespace App\Http\Controllers\Admin\Mission;

use App\Http\Controllers\Controller;
use App\Models\Misson;
use Illuminate\Http\Request;

class MissionController extends Controller
{
    public function index(){
        $missions=Misson::orderBy('created_at', 'DESC')->get();
        return view('admin.pages.mission.index',compact('missions'));
    }
    public function edit($id){
        $missions=Misson::find($id);

//        return view('admin.pages.about.Mission.edit',compact('missions'));
        return view('admin.pages.mission.edit',compact('missions'));
    }
    public function update(Request $request, $id)
    {
        $msg = [
            'title.required' => 'Please Enter title',
            'description.required' => 'Please Enter description',
        ];
        $this->validate($request, [
            'title' => 'required',
            'description' => 'required',
        ],$msg);

        $mission = Misson::where('id',$id)->first();

        $mission->title = $request->get('title');
        $mission->description = $request->get('description');

        $mission->save();

        return redirect()->route('admin::mission')->with('success','Mission Update Successfully');

    }
    public function status(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        if($status=='Active'){
            Misson::where('id',$id)->update([
                'status' => 'Inactive',
            ]);
            $st='Inactive';
            $html='<a href="javascript:void(0);" class="btn btn-warning btn-sm" onclick="active_inactive_mission('.$id.','.$st.')" ><span class="fa fa-ban"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
        else{
            Misson::where('id',$id)->update([
                'status' => 'Active',
            ]);
            $st='Active';
            $html = '<a href="javascript:void(0);" class="btn btn-success btn-sm" onclick="active_inactive_mission('.$id.','.$st.')"><span class="fa fa-check"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
    }
}
