<?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 App\Http\Model\SellerModel;
	use Validator;
	
	class SellerController extends Controller
	{
		public function __construct()
		{
			$this->seller= new SellerModel;
		}
		public function ViewSeller()
		{
			$data['url']='SellerView';
		    $Mid=$this->seller->QutationPermission($data);
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			$biztype1 = 'Seller';
			$biztype2 = 'Buyer & Seller';
			$status= 'Active';
			$data['cst_customer'] = DB::table('cst_customer')->join('cmp_company', 'cst_customer.cmp_id', '=', 'cmp_company.cmp_id')
			->where('cst_customer.cst_status','<>',$status)->Where('cst_customer.cst_type','=',$biztype1)->orwhere('cst_customer.cst_type','=',$biztype2)->orderby('cst_customer.cst_name', 'asc')->get();		
			return view('SellerView',compact('data'));
		}
		
		//add Seller coding
		public function LoadSeller()
		{
			return view('seller');
		}
		
		public function LoadSellerCompany()
		{
			$biztype1 = 'Seller';
			$biztype2 = 'Buyer & Seller';
			$status= 'Deleted';		
			$company = DB::table('cmp_company')->where('cmp_status','<>',$status)->Where('cmp_type','=',$biztype1)->orwhere('cmp_type','=',$biztype2)->orderby('cmp_name', 'asc')->get();
			
			/*
				$status= 'Deleted';
				$company = DB::select( DB::raw("SELECT * FROM `cmp_company` where cmp_status != '$status' AND ()") );
			*/
			return response()->json($company);
		}
		
		public function InsertSeller(Request $req)
		{			
			$compname= $req->input('compname');
			$custtype= $req->input('custtype');
			$customername= $req->input('customername');
			$address= $req->input('address');
			$city= $req->input('city');
			$state= $req->input('state');
			$country= $req->input('country');
			$contact1= $req->input('contact1');
			$contact2= $req->input('contact2');
			$emailid= $req->input('emailid');
			$cst_www = $req->input('wwwweb');
			$bankacct= $req->input('bankacct');
			$note= $req->input('note');
			$status = $req->input('status');
			
			$data = array('cmp_id'=>$compname,'cst_type'=>$custtype,'cst_name'=>$customername,'cst_address'=>$address,'cst_city'=>$city,'cst_state'=>$state,'cst_country'=>$country,'cst_contact1'=>$contact1,'cst_contact2'=>$contact2,'cst_email'=>$emailid,'cst_www'=>$cst_www,'cst_bankdetail'=>$bankacct,'cst_note'=>$note,'cst_status'=>$status);
			//	return response->json(['status' => 1 , 'message' => json_encode($data)]);
			
			$insertdetails = DB::table('cst_customer')->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..')]);
			}		
		}
		public function Editseller(Request $request)
		{
			$custId=$request->input('cst_id');
			$custData=DB::table('cst_customer')->where('cst_id',$custId)->first();
			return view('Editseller',compact('custData'));
		}
		public function customerUpdate(Request $request)
		{
			$data['cst_id']= $request->input('cst_id');
			$data['compname']= $request->input('compname');
			$data['custtype']= $request->input('custtype');
			$data['customername']= $request->input('customername');
			$data['address']= $request->input('address');
			$data['city']= $request->input('city');
			$data['state']= $request->input('state');
			$data['country']= $request->input('country');
			$data['contact1']= $request->input('contact1');
			$data['contact2']= $request->input('contact2');
			$data['emailid']= $request->input('emailid');
			$data['cst_www'] = $request->input('wwwweb');
			$data['bankacct']= $request->input('bankacct');
			$data['note']= $request->input('note');
			$data['status'] = $request->input('status');
		    return $id=$this->seller->customerUpdate($data);
		}
		
		public function DeleteCustomer(Request $request)
		{
			$custId=$request->input('cst_id');
			DB::table('cst_customer')->where('cst_id',$custId)->update(['cst_status'=>'Inactive']);
		}
	}
