<?php

namespace App\Http\Controllers\Lamis;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Project;
use App\Service;
use App\Forms;
use App\ProjectForms;

class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = \Auth::guard('member')->user();
    	$projects = Project::where('member_id',$user->id)->get();
    	
    	return view('lamis.projects',['user'=>$user,'projects'=>$projects]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $project = Project::find($id);
        
        if(!$project){
            return redirect()->back();
        }
        if(\Auth::guard('member')->user()->id != $project->member_id){
             return redirect()->back();
        }
        $begin_service = Service::take(4)->get();
        $progress = 0;
        $forms_count = 0 ;
        $count = Service::count();
        if($count >= 4){
        $services = Service::skip(4)->take($count-4)->get();
        }
        else{
            $services = Service::take(0)->get();
        }
        if(\Auth::guard('member')->user()){
        $forms_all = Forms::count();
        $forms_count = ProjectForms::where('project_id',$id)->count();
        
        $progress =number_format(($forms_count / $forms_all)*100,'1');
        }
        
        $all_count = Forms::count();
        
        return view('lamis.steps',['begin_service'=>$begin_service,'project_id'=>$id,'services'=>$services,'forms_count'=>$forms_count,'all_count'=>$all_count,'progress'=>$progress]);
   
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
