<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Http\Request;
use DB;

use Validator;

class CompanyController extends Controller
{
    public function LoadCompany()
    {
    	$status= 'Deleted';
		$data['Company']=DB::table('cmp_company')->where([["cmp_status","<>",$status]])->get();
    	return view('company',compact('data'));
    }

	public function helperCollection()
    {
        $newCollection = collect([1, 2, 3, 4, 5]);
        dd($newCollection);
    }
	
	public function InsertCompany(Request $req)
    {
		$cmp_id =  $req->input('cmp_id');
    	$companyname= $req->input('companyname');
		$companytype= $req->input('companytype');
		$description= $req->input('description');
		$status = $req->input('status');
		$data = array('cmp_type'=>$companytype,'cmp_name'=>$companyname,'cmp_description'=>$description,'cmp_status'=>$status);		
		if($cmp_id == 0)
		{
			$insertdetails = DB::table('cmp_company')->insert($data);
			if (!is_null($insertdetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Inserted Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Insertion.. Please Try again..')]);
			}
		}
		else
		{
			$updatedetails = DB::table('cmp_company')->where('cmp_id',$cmp_id)->update($data);
			if (!is_null($updatedetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Updated Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Updation.. Please Try again..')]);
			}
		}
		
    }

   public function EditCompany(Request $req)
	{
		$id = $req->id;
		$company = DB::table('cmp_company')->where(['cmp_id'=>$id])->get();
		return response()->json($company);
	}
	
	public function DeleteCompany(Request $req)
	{
		$id = $req->id;
		$status= 'Deleted';
		$data = array('cmp_status'=>$status);
	
		$UpdateDetails = DB::table('cmp_company')->where('cmp_id',$id)->update($data);
		if (!is_null($UpdateDetails))
		{
			return response()->json(['status' => '1', 'message' => trans('Record Deleted Successfully.')]);
		}
		else
		{
			return response()->json(['status' => '0', 'message' => trans('Error in Deletion.. Please Try again..')]);
		}
	}
}

?>
