<?php namespace App\Controllers\Admin;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use CodeIgniter\I18n\Time;

class Report extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\ReportModel');
    }
	
	function is_valid_daterange(){
		$post_data = $this->request->getPost();
		$start_dt = strtotime($post_data['from_date']);
		$end_dt = strtotime($post_data['to_date']);
		if($start_dt > $end_dt){
			$response = "FALSE|The To Date must be greater than From Date!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
	
	function no_activity_members(){
		$this->menu_id = 44;
		$this->validate_access('is_list');
		$date = new \DateTime(); // For today/now, don't pass an arg.
		$end_date = $date->format("Y-m-d");
		$date->modify("-7 day");
		$start_date = $date->format("Y-m-d");
		$excel_data = $this->mObj->get_no_activity_members($start_date);		
				
		$spreadsheet = new Spreadsheet();		
		set_spreadsheet_properties($spreadsheet,'no_activity_farmer_list');						
		
		$sheet = $spreadsheet->getActiveSheet();		
		$sheet->setTitle("no_activity_farmer_list");		
		
		
		// Set columns				
		$sheet->setCellValue('A1','No Activity from last week('.date('d-m-Y',strtotime($start_date)).' To '.date('d-m-Y',strtotime($end_date)).')');	
		$sheet->mergeCells('A1:J1');	
			
		$sheet->setCellValue('A2','Sr. No.');
		$sheet->setCellValue('B2','Farmer');
		$sheet->setCellValue('C2','Mobile No.');
		$sheet->setCellValue('D2','Email ID');
		$sheet->setCellValue('E2','Address');
		$sheet->setCellValue('F2','Pincode');
		$sheet->setCellValue('G2','City');
		$sheet->setCellValue('H2','Taluka');
		$sheet->setCellValue('I2','District');
		$sheet->setCellValue('J2','State');
		set_spreadsheet_heading_style($sheet,'A1:J2');

		// Set data
		if($excel_data){
			$row = 3; $i=0;
			foreach($excel_data as $rec){ $i++;
				$sheet->setCellValue('A'.$row,$i);
				$sheet->setCellValue('B'.$row,$rec['memeber_name']);
				$sheet->setCellValue('C'.$row,$rec['mobile_no']);
				$sheet->setCellValue('D'.$row,$rec['email_id']);
				$sheet->setCellValue('E'.$row,$rec['address']);
				$sheet->setCellValue('F'.$row,$rec['pincode']);
				$sheet->setCellValue('G'.$row,$rec['city']);
				$sheet->setCellValue('H'.$row,$rec['taluka']);
				$sheet->setCellValue('I'.$row,$rec['district']);
				$sheet->setCellValue('J'.$row,$rec['state']);
				$row++;
			}
		}else{			
			$sheet->getStyle('A3')
				->getAlignment()
				->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
			$sheet->setCellValue('A3','No Records!');	
			$sheet->mergeCells('A3:J3');			
		}
		foreach (range('A','J') as $col) {
		   $sheet->getColumnDimension($col)->setAutoSize(true);
		}
		$writer = new Xlsx($spreadsheet);
		output_spreadsheet($writer,'no_activity_farmer_list');
	}
}