<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Libraries\Helper;
use App\Libraries\Validation;
use App\Repositories\CareerRepository;
use Illuminate\Http\Request;

class CareerController extends Controller
{
    public function index(Request $request)
    {
		$payload = $request->all();
    	$filters = ['page' => isset($payload['page']) && $payload['page'] ? $payload['page'] : 1];
        $data = Helper::preparePagePayload('Career List');
        $data['careers'] = CareerRepository::fetchItems($filters)->toArray();
        return view('admin.career.index',$data);
    }

    public function deleteCareer(Request $request)
    {
    	$payload = $request->all();
    	if(isset($payload['id']) && $payload['id']){
    		$status = CareerRepository::deleteCareer($payload);
    		if($status){
    			return response()->json(['status' => true]);
    		}
    	}
    	return response()->json(['status' => false]);
    }

    public function save($id = null)
    {
        $data = Helper::preparePagePayload('Save Career');
        if($id){
            $data['career'] = CareerRepository::fetchCareer(['id' => $id]);
            if(!$data['career']){
                return redirect('admin/careers');
            }
        }
        return view('admin.career.save',$data);

    }

    public function saveCareer(Request $request)
    {
        $payload = $request->all();
        $result = Validation::validatePayload($request->all(), ['title' => 'required','description' => 'required']);
        if($result['status']){
            $status = CareerRepository::saveCareer($payload);
            if($status){
                return response()->json(['status' => true]);
            }
            return response()->json(['status' => false]);
        }
        return response()->json($result);
    }

    public function changeStatus(Request $request)
    {
        $payload = $request->all();
        if(isset($payload['id']) && $payload['id']){
            $result = CareerRepository::changeStatus($payload);
            if($result){
                return response()->json($result);
            }
        }
        return response()->json(['status' => false,'message' => 'Unable to change status.Please try again.']);
    }
}
