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

class Paychecks 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()   
	{
		$this->load->model('drivers_model');
		$data["drivers"] = $this->drivers_model->getall_drivers()["user"];
		if(isset($_POST['incomeexpensereport'])) {
			//echo "<pre>";
			$newdate=strtotime("+1 days",strtotime($this->input->post('incomeexpense_to')));
			$loru= date('Y-m-d H:i:s',$newdate);
			// print_r($this->input->post('incomeexpense_to'));
			// print_r($this->input->post());
			// echo "</pre>";
			//exit;
			if (isset($_POST['driverId']) && $_POST['driverId'] != "" && !empty($_POST['driverId'])) {
				$triplist = $this->common->readdatafromcollectionbetweenwhere("Payouts", "completionTimeStamp" , ">", strtotime($this->input->post('incomeexpense_from'))*1000, "completionTimeStamp" , "<", strtotime($loru)*1000, "driverId" , "=", $_POST["driverId"]);
			} else {
				$triplist = $this->common->readdatafromcollectionbetweenwhere("Payouts", "completionTimeStamp" , ">", strtotime($this->input->post('incomeexpense_from'))*1000, "completionTimeStamp" , "<", strtotime($this->input->post('incomeexpense_to'))*1000);
			}
			// echo "<pre>";
			// print_r($triplist);
			// echo "</pre>";


			if(empty($triplist)) {
				$this->session->set_flashdata('warningmessage', 'No Data found..');
				$data['paychecks'] ="";
				$data['driverId']=$_POST['driverId'];
			} else {
				$data['paychecks'] =$triplist;
				$data['driverId']=$_POST['driverId'];
			}
		}else{
			$data['driverId']="";
		}
		

		$this->template->template_render('paychecks', $data); 
	}


	public function releasePaycheckall(){
	
		$mylove=explode(",",$this->input->post('checkid'));

		if(count($mylove)>0){
			for($i=0;$i<count($mylove);$i++){
				$data["status"] = "approve";
				$id=$mylove[$i];
				if($id !=""){
				$response = $this->common->updatedatamodel("Payouts", $id, $data);
				}
			}
			


		}
		$response['success'] = 1;
		//$response['dat']=$mylove
		$this->session->set_flashdata('successmessage', 'Successfully Paid.');
        echo json_encode($response);
	}

	public function releasePaycheck()   
	{
		// $data = json_decode(file_get_contents('php://input'), true);
		// $id = $this->inpout->post('paymentId');
		// $data['id'] = $this->input->post('paymentId');
        $id = $this->input->post('paymentId');
        $data["status"] = "approve";
		$response = $this->common->updatedatamodel("Payouts", $id, $data);
		$response['success'] = 1;
		$this->session->set_flashdata('successmessage', 'Successfully Paid.');
        echo json_encode($data);
		// redirect('paychecks');
	}
	
	
}
