<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Company;
use Illuminate\Support\Facades\Storage;
use Validator;
use Auth;

class CompanyController extends Controller
{
    public function update(Request $request)
    {
       $company = Company::where("user_id", Auth::user()->id)->first();
       if (!$company)
       {
           return response()->json(['success'=> false, 'message'=>'Company not found'], 404);
       }

       $rules = [
        'company_name' => 'required',
        'registration_no' => 'required',
        'tax_no' => 'required',
        'tax_no' => 'required',
        'tel_no' => 'required',
        'fax' => 'required',
        'address' => 'required',
        'city' => 'required',
        'postcode' => 'required',
        'state' => 'required',
        'country' => 'required',
        'contact_person' => 'required'
    ];

    $validator = Validator::make($request->all(), $rules);
    if($validator->fails()) {
        return response()->json(['success'=> false, 'error'=> 'All the fields are required'], 400);
        //return response()->json(['success'=> false, 'error'=> $validator->messages()], 400);
    }

       $company->company_name=$request->input('company_name');
       $company->registration_no=$request->input('registration_no');
       $company->tax_no=$request->input('tax_no');
       $company->tel_no=$request->input('tel_no');
       $company->fax=$request->input('fax');
       $company->address=$request->input('address');
       $company->city=$request->input('city');
       $company->postcode=$request->input('postcode');
       $company->state=$request->input('state');
       $company->country=$request->input('country');
       $company->contact_person=$request->input('contact_person');

       $company->save();
       return response()->json(['success'=> true, 'message'=>'Company created successfully'],200);
    }

    public function companyProfile(Request $request)
    {
        $company = Company::where("user_id", Auth::user()->id)->first();
        if (!$company)
        {
            return response()->json(['success'=> false, 'error'=>'Company not found'], 404);
        }
        //validaating file types
        $validator = Validator::make($request->all(), [
        'company_profile'   => 'image|mimes:jpg,jpeg,bmp,png'
        ]);
        if($validator->fails())
        {
            return response()->json(['success'=> false, 'error'=>'Incorrect format'], 400);
        }

        if($file = $request->file('company_profile')) //company profile picture
        {
            $company_profile = $request->file('company_profile');
            $filename = $company_profile->getClientOriginalName(); //get the original file name
            $unique_name = 'comp_prof_' . md5($filename. time()); //generating a random file name

            $file = $request->file('company_profile');
            $ext = $file->getClientOriginalExtension();
            $input['company_profile'] = $filename;
            Storage::putFileAs(
                'company_profile',/*folder name*/ $file, $unique_name. '.' .$ext
            );
            $company->company_profile = $unique_name. '.' .$ext; //update database
        } //end if
        $company->save();
        return response()->json(['success'=> true, 'message'=>'Company profile picture uploaded successfully'],200);
    }//end of companyProfile()

    public function regCert(Request $request)
    {
        $company = Company::where("user_id", Auth::user()->id)->first();
        if (!$company)
        {
            return response()->json(['success'=> false, 'error'=>'Company not found'], 404);
        }

        //validating file types
        $validator = Validator::make($request->all(), [
            'reg_cert'   => 'mimes:jpg,jpeg,bmp,png,gif,svg,pdf'
            ]);
        if($validator->fails()){
            return response()->json(['success'=> false, 'error'=>'Incorrect format'], 400);
        }

        if($request->hasFile('reg_cert')) //Registration Certificate upload
        {
            $reg_cert = $request->file('reg_cert');
            $filename = $reg_cert->getClientOriginalName(); //original filename
            $unique_name = 'reg_cert_' . md5($filename. time()); //generating a random file name

            $file = $request->file('reg_cert');
            $ext = $file->getClientOriginalExtension();
            $input['reg_cert'] = $filename;
            Storage::putFileAs(
                'reg_cert',/*folder name*/ $file, $unique_name. '.' .$ext
            );
            $company->reg_cert = $unique_name. '.' .$ext; //this updates database
        }//end if
        $company->save();
        return response()->json(['success'=> true, 'message'=>'Registration Certificate uploaded successfully'], 200);
    }//end regCert()

}//end of class