<?php

namespace App\Http\Controllers\Admin;

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

class ProjectController extends Controller
{
    public function index(Request $request)
    {
        $payload = $request->all();
        $filters = ['page' => isset($payload['page']) && $payload['page'] ? $payload['page'] : 1];
        $data = Helper::preparePagePayload('Project List');
        $data['projects'] = ProjectRepository::fetchItems($filters)->toArray();

        return view('admin.project.index', $data);
    }

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

        return response()->json(['status' => false]);
    }

    public function save($id = null)
    {
        $data = Helper::preparePagePayload('Save Project');
        if ($id) {
            $data['project'] = ProjectRepository::fetchProject(['id' => $id]);
            if (! $data['project']) {
                return redirect('admin/projects');
            }
        }

        return view('admin.project.save', $data);
    }

    public function saveProject(Request $request)
    {
        $payload = $request->all();
        $result = Validation::validatePayload($request->all(), [
            'title' => 'required',
            //'client' => 'required',
            //'start_date' => 'required',
            'slug' => 'required',
            //'description' => 'required',
        ]);
        if ($result['status']) {

            $image = $request->file('image');
            if (! $payload['id'] && ! $image) {
                return response()->json(['status' => false, 'message' => 'Image File required.']);
            }
            if($image){
                $extension = $image->getClientOriginalExtension();
                if (! in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
                    return response()->json([
                        'status' => false,
                        'message' => 'Invalid File.Only png, jpg, jpeg and gif file allowed to upload.',
                    ]);
                }

                $uploaded_path = public_path('images');
                if (! is_writable($uploaded_path)) {
                    return response()->json(['status' => false, 'message' => 'File directory is not writable.']);
                }
                $file_name = rand().'.'.$extension;
                $uploadSuccess = $image->move($uploaded_path, $file_name);
                if (! $uploadSuccess) {
                    return response()->json(['status' => false, 'message' => 'Unable to upload file.']);
                }
                $payload['media'] = $file_name;
            }

            $status = ProjectRepository::saveProject($payload);
            if ($status) {
                return response()->json(['status' => true]);
            }

            return response()->json(['status' => false]);
        }

        return response()->json($result);
    }
}
