<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Paydrivers extends CI_Controller {
    function __construct()
 	{
	    parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
        $this->load->model('common');
		$this->load->library('session');
		$this->load->database();
		$this->load->library(array('form_validation','template'));
	}

	public function index()   
	{
		$data['drivers']= $this->common->readdatafromcollectionwhereclause("Users", "userRole", "=", "driver");
		$this->template->template_render('paydrivers',$data); 
	}

	public function fetchDriverData()
	{
		$res = $this->common->readdatadocument("Users", $this->input->post('id'));

		$response["email"]=$res['email'] ;
		$response["adminBalance"]= $res['adminBalance'] !="" ? $res['adminBalance'] : 0; 
		$response["yourBalance"]= $res['yourBalance'] !="" ? $res['yourBalance'] : 0;
		$response['success']=1;

		// $payouts = $this->common->readdatafromcollectionbetween("Payouts", "driverId", "=", $this->input->post('id'), "status", "=", "non-returned");
		// $amount = 0;
		// foreach ($payouts as $key => $payout) {
		// 	$amount = $amount + $payout["amountToBeReturned"];
		// }
		// $response["data"]["amount"] = $amount;
		// if($response["data"])
		// 	$response["success"] = TRUE;
		// else
		// 	$response["success"] = FALSE;
		echo(json_encode($response));
	}

		public function addPaymentNew(){
			$da['recivedamount']=floatval($this->input->post('recivedamount'));
			$da['description']=$this->input->post('description');
         	$da['driverId']	=$this->input->post('driverId');
         	$jk=time();

				$newbal=0;
			$adminBalance=floatval($this->input->post('adminBalance'));
			$yourBalance	=floatval($this->input->post('yourBalance'));
			if(floatval($this->input->post('recivedamount'))>=$adminBalance){
				$ceta['yourBalance']	=$yourBalance+(floatval($this->input->post('recivedamount'))-$adminBalance );
				$ceta['adminBalance']=0;
			$this->common->updatedatamodel("Users", $da['driverId'], $ceta);

			}else{
				$chul=$adminBalance-floatval($this->input->post('recivedamount'))-$yourBalance;
				if($chul>=0){
			$ceta['adminBalance']	=$chul;
			$ceta['yourBalance']=0;			
				}else{
			$ceta['adminBalance']	=$chul;
			$ceta['yourBalance']=$chul*-1;			

				}	

		
			$this->common->updatedatamodel("Users", $da['driverId'], $ceta);				
			}


			$this->common->adddatamodel("RecivedCash", 	$jk, $da);

			$response["success"] = TRUE;
		echo(json_encode($response));
		// print_r($this->input->post());
		exit;	



		}


	public function addPayment()
	{
		$data['orderId'] = time();
		$data['driverId'] = $this->input->post('driverId');
		$data["type"] = "order";
		
		$payouts = $this->common->readdatafromcollectionbetween("Payouts", "driverId", "=", $this->input->post('driverId'), "status", "=", "non-returned");
		foreach ($payouts as $key => $payout) {
			$d["status"] = "paid";
			$this->common->updatedatamodel("Payouts", $key, $d);
		}

		if($this->input->post('amount') > $this->input->post('pendingAmount')){
			$data["amount"] = $this->input->post('pendingAmount') - $this->input->post('amount');
			$data["status"] = "additional";
			$this->common->adddatamodel("Payouts", $data['orderId'], $data);
		} elseif ($this->input->post('amount') < $this->input->post('pendingAmount')) {
			$data["amountToBeReturned"] = $this->input->post('pendingAmount') - $this->input->post('amount');
			$data["status"] = "non-returned";
			$this->common->adddatamodel("Payouts", $data['orderId'], $data);
		}
		
		$response["data"] = $this->input->post();
		if($response["data"])
			$response["success"] = TRUE;
		else
			$response["success"] = FALSE;
		echo(json_encode($response));
		// print_r($this->input->post());
		exit;
	}
	
	
}
