<?php

namespace App\Http\Controllers;

use App\Scheme;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class SchemeController extends Controller
{
    public function add()
    {
        return view("schemes_add");
    }

    public function index()
    {
        $scheme = Scheme::orderby('id','desc')->get();
       if ($scheme) {
            $data['scheme'] = $scheme;
            $data['status'] = 1;
            $data['message'] = "Schemes retrieved successfully";
            return response($data, 200);
        } else {
            $data['status'] = 0;
            $data['message'] = "Schemes retrieval failed";
            return response($data, 400);
        }
    }

    public function show()
    {
        $schemes =  Scheme::orderby('id','desc')->get();
        return view("schemes_list", compact('schemes'));

    }

    public function store(Request $request)
    {
        $input = $request->all();
        $rules = [

            'title' => 'required',
            'description' => 'required',
            'attachment' => 'required',

        ];
        $messages = [

            'title.required' => 'Title required',
            'description.required' => 'Description required',
            'attachment.required' => 'Attachment required'
        ];
        $validator = Validator::make($input, $rules, $messages);

        if ($validator->passes()) {
            $scheme = new Scheme();
            $scheme->title = $input['title'];
            $scheme->description = $input['description'];
            $attachment = $request->attachment;
            if (isset($attachment) && $attachment != '' && $attachment != 'undefined') {
                $destinationUrlPath = '/uploads/schemes';
                //destination folder
                $destinationPath = public_path() . $destinationUrlPath;

                //create folder to save image
                File::makeDirectory($destinationPath, 0775, true, true);

                // changing file name
                $name = Str::slug($request->title . Carbon::now());
                $filename = $attachment->getClientOriginalName();
                $ext = $attachment->getClientOriginalExtension();
                if ($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf") {
                    $filename = $name . '-' . $filename;
                    $request->attachment->move($destinationPath, $filename);
                    $scheme->filename = $filename;
                    $scheme->filetype = $ext;
                    $inputs['attachment'] = $filename . "," . $ext;
                } else {
                    $inputs['attachment'] = "";
                }

            }
            $scheme->attachment = $filename;
            $scheme->save();
            if ($scheme) {
                $data['status'] = 1;
                $data['scheme_id'] = $scheme->id;
                $data['message'] = "Scheme added successfully";
                return response($data, 200);

            }
        } else {
            $data['message'] = $validator->errors(0);
            $data['status'] = 0;
            return response($data, 400);
        }

    }
}
