<?php

namespace App\Http\Controllers;
use Session;
use App\Models;
use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    
    public function index(){
        $user_id = \Auth::user()->id;
        // dd($user_id);
        $result = Models\Project::where('user_id', $user_id)->get();
        $data = [
            'page_title' => 'Projects List',
            'projects' => $result
        ];
        return view('projects.index', $data);
    }
    public function create() {
        // Show form to create a new post
        return view('projects.create');
    }

    public function store(Request $request) {
        // Save a new post
        $this->validate($request, [
            'name' => 'required',
            'description'=> 'required',
            'dependancy' => 'sometimes',
            'status' => 'required',
        ]);
        $project_arr = $request->except('_token');
        $result = Models\Project::create($project_arr);
        Session::flash('success_message', 'Project created successfully!');
        return redirect(url('projects'));
    }

    public function show($id) {
        // Show a specific post
        $user_id = \Auth::user()->id;
        $result = Models\Project::with('statusInfo')->where('id', $id)->firstOrFail();
        $data = [
            'page_title' => 'Projects Details',
            'project' => $result
        ];
        // dump($data);
        return view('projects.show', $data);
    }

    public function edit($id) {
        // Show form to edit a post
        $user_id = \Auth::user()->id;
        $result = Models\Project::with('statusInfo')->where('id', $id)->firstOrFail();
        $data = [
            'page_title' => 'Projects Update',
            'project' => $result
        ];
        return view('projects.edit', $data);
    }

    public function update(Request $request, $id) {
        // Update a specific post
        $this->validate($request, [
            'name' => 'required',
            'description'=> 'required',
            'dependancy' => 'sometimes',
            'status' => 'required',
        ]);

        $project = Models\Project::findOrFail($id);
        
        $project->name = $request->name;
        $project->description = $request->description;
        $project->dependancy = $request->dependancy;
        $project->status = $request->status;
        $result = $project->save();
        Session::flash('success_message', 'Project details updated successfully!');
        return redirect(url('projects'));
    }

    public function destroy($id) {
        // Delete a specific post
    }
}
