<?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\LoanSettingsModel;
use Validator;

class LoanSettingsController extends Controller
{
	public function __construct()
	{
		$this->LoanSettingsMod= new LoanSettingsModel;
	}
	public function LodLonProcesingFeeSeting()
	{
		$data['url']='loanprocessingfeesettings';
		$Mid=$this->LoanSettingsMod->BranchPermission($data);
		$data['delete']= $Mid->Delete;
		$data['edit']= $Mid->Edit;
		$data['create']= $Mid->create;
		$status= 'Deleted';
		$data['loanprocessingfee']=DB::table('lpf_loanprocessingfee')->where([["lpf_status","<>",$status]])->orderBy('lpf_id', 'asc')->get();
		return view('loanprocessingfeesettings',compact('data'));
	}
	public function LoadLoanTypes()
	{
		$data['url']='loantype';
		$Mid=$this->LoanSettingsMod->BranchPermission($data);
		$data['delete']= $Mid->Delete;
		$data['edit']= $Mid->Edit;
		$data['create']= $Mid->create;
		$status= 'Deleted';
		$data['LoanTypeRecords']=$this->LoanSettingsMod->viewloantypes();
		return view('loantype',compact('data'));
	}
    public function InsertLoanTypePost(Request $req)
    {
		$ltyp_id = $req->input('ltyp_id');
		$ltyp_loantype = $req->input('ltyp_loantype');
		$ltyp_maxmonth = $req->input('ltyp_maxmonth');
		$ltyp_interestperc = $req->input('ltyp_interestperc');
		$ltyp_status = $req->input('ltyp_status');
		
		if($ltyp_id == 0)
		{
			$qinsert = DB::table('ltyp_loantypes')->insertGetId(['ltyp_loantype' => $ltyp_loantype,'ltyp_maxmonth' => $ltyp_maxmonth,'ltyp_interestperc' => $ltyp_interestperc,'ltyp_status' => $ltyp_status]);	
			$modules=DB::table('modules')->get();
			foreach($modules as $modu)
			{
				$ModuleId=$modu->Mid;
				DB::table('permission')->insert(['UsrId' => $qinsert,'Mid' => $ModuleId]);
			}			
			return response()->json(['status' => '1', 'message' => trans('Loantype record inserted...')]);
		}
		else
		{
			$data = array('ltyp_loantype'=>$ltyp_loantype,'ltyp_maxmonth'=>$ltyp_maxmonth,'ltyp_interestperc'=>$ltyp_interestperc,'ltyp_status'=>$ltyp_status);
			$updatedetails = DB::table('ltyp_loantypes')->where('ltyp_id',$ltyp_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..')]);
			}
		}
    }
	
	//Edit User type records
	public function EditLoanType(Request $req)
	{
		$id = $req->id;
		$sqlstatement = DB::table('ltyp_loantypes')->where(['ltyp_id'=>$id])->get();
		return response()->json($sqlstatement);
	}
	
	//Delete User type records
	public function DeleteLoanType(Request $req)
	{
		$id = $req->id;
		$status= 'Deleted';
		$data = array('ltyp_status'=>$status);
		
		$UpdateDetails = DB::table('ltyp_loantypes')->where('ltyp_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..')]);
		}
	}
	public function updateMasterLoan(Request $req)
		{
			$data['loanid'] = $req->input('loanid');
			$data['subb1'] = $req->input('subb1');
			$data['subb2'] = $req->input('subb2');
			$data['subb3'] = $req->input('subb3');
			$data['subb4'] = $req->input('subb4');
			$data['subb5'] = $req->input('subb5');
			return $updateMasterLoan=$this->LoanSettingsMod->updateMasterLoan($data);
		}
}
