<?php

namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'project_title' => 'string',
            'project_disciplines' => 'string',   // required and email format validation
            'engineering' =>'string', // required and number field validation
            'allied' => 'string',
            'summary' => 'string|max:1000',
            'country_assignment' => 'string',
            'technical_queries' => 'string',
            'specific_experience' => 'string',
            'expert' => 'string',
            'schedule' => 'string',
            'total_inputs' => 'string',
            'contract' => 'string',
            'consultant_services' => 'string',
            'estimated_date' => 'string',
            'title_bar'=>  'required|mimes:pdf|max:25000',
            'term_condition' => 'string',
         
            'technical_proposal' => 'string',
            'technical_qualification'=> 'string',
            'upload_experts' => 'string',
            'prequalification' => 'string',
            'documents' =>   'required|mimes:pdf|max:25000',
            
            // 'pec_certificate' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
            'commercial'=> 'string',
          
            

        ]); // create the validations
        if ($validator->fails())   //check all validations are fine, if not then redirect and show error messages
        {

            return back()->withInput()->withErrors($validator);
            // validation failed redirect back to form
            if (request()->hasFile('title_bar')){
                request()->validate([
                    'title_bar' => 'required|mimes:pdf|max:25000',
                ]);
             
            }
           
            if (request()->hasFile('documents')){
                request()->validate([
                    'documents' => 'required|mimes:pdf|max:25000',
                ]);
             
            }
   

        } else {
            //validations are passed, save new user in database
            $Project = new Project;
            $Project->project_title = $request->project_title;
            $Project->project_disciplines = $request->project_disciplines;
            $Project->engineering = $request->engineering;
            $Project->allied = $request->allied;
            $Project->summary = $request->summary;
            $Project->country_assignment = $request->country_assignment;
            $Project->technical_queries = $request->technical_queries;
            $Project->specific_experience = $request->specific_experience;
            $Project->expert= $request->expert;
            $Project->schedule= $request->schedule;
            $Project->total_inputs = $request->total_inputs;
            $Project->contract = $request->contract;
            $Project->consultant_services = $request->consultant_services;
            $Project->estimated_date = $request->estimated_date;
            $Project->title_bar= $request->title_bar;
            $Project->term_condition = $request->term_condition;
            // $Project->status = $request->status;
            $Project->technical_proposal = $request->technical_proposal;
            $Project->technical_qualification= $request->technical_qualification;
            $Project->upload_experts= $request->upload_experts;
            $Project->prequalification= $request->prequalification;
            $Project->documents = $request->documents;
            $Project->commercial= $request->commercial;
            $Project->save();
  
            // return redirect("dashboard")->with('success', 'You have successfully registered, Login to access your dashboard');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Project  $project
     * @return \Illuminate\Http\Response
     */
    public function show(Project $project)
    {
        //
    }

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

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

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