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

class Forms extends MY_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	public function Volunteers()
	{
		$data['page'] = 'VOLUNTEERS FORM';
		$data['title'] = 'VOLUNTEER-FORM';
		$this->load->view('volunteers',$data);
	}


	public function AddVolunteers(){

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		$url = ApiBaseUrl()['url'] . 'api/upload/docsmany';

		$file_field_name = 'image'; 
		$file_path = $_FILES['idcard']['tmp_name'];
		$file_mime_type = $_FILES['idcard']['type'];

		$file = new CURLFile($file_path, $file_mime_type, basename($file_path));

		$post = array(
			$file_field_name => $file,
			'type' => 'profile'
		);

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

		$result = curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}

		curl_close($ch);

		$response = json_decode($result, true);

		$idcard_final = '';
		if ($response['success'] == 1) {
			$idcard = json_decode($response['result']['message']['path'], true);
			echo $idcard_final  = $idcard[0]['paths'];
		}

		$fname = $this->input->post('fname');
		$lname = $this->input->post('lname');

		$name = $fname.' '.$lname;

		$email = $this->input->post('email');
		$volunteerPhone = $this->input->post('volunteerPhone');
		$volunteerPhone = str_replace('-', '', $volunteerPhone);
		$Emrg_Contact_Name = $this->input->post('Emrg_Contact_Name');
		$Emrg_Contact_Number = $this->input->post('Emrg_Contact_Number');
		$Emrg_Contact_Number = str_replace('-', '', $Emrg_Contact_Number);
		$address = $this->input->post('address');
		$county = $this->input->post('county');
		$state = $this->input->post('state');
		$city = $this->input->post('city');
		$zipcode = $this->input->post('zipcode');
		$VolunteerArea = $this->input->post('VolunteerArea');
		$dob = $this->input->post('dob');
		$gender = $this->input->post('gender');
		$hours = $this->input->post('hours');
		$notes = $this->input->post('notes');

		$postdata = array(
			"productId" => ApiBaseUrl()['productID'],
			"clientId" => ApiBaseUrl()['clientID'],
			"date" => date("m/d/Y"),
			"time" => date("h:i A"),
			"volunteerID" => "",
			"name" => $name,
			"mobile" => $volunteerPhone,
			"email" => $email,
			"volunteerArea" => $VolunteerArea,
			"isInsert" => true,
			"emrgContactName" => $Emrg_Contact_Name,
			"emrgContactNumber" => $Emrg_Contact_Number,
			"dob" => $dob,
			"gender" => $gender,
			"workingHours" => $hours,
			"base64" => '/uploads/others/'.$idcard_final,
			"notes" => $notes,
			"country" => $county,
			"state" => $state,
			"city" => $city,
			"zip" => $zipcode,
			"address" => $address
		);

		$json_postdata = json_encode($postdata);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'].addVolunteer()['url']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $json_postdata);
		$headers = array();
		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$result = curl_exec($ch);
		$response = json_decode($result);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}
		curl_close($ch);

		// print_r($result); die();

		if ($response->statusCode == 1) {
			$this->session->set_flashdata('success','<div class="alert alert-success alert-dismissible fade show text-center">Volunteer Details Added Successfully!<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
			redirect(base_url('volunteer'));
		}
		else{
			$this->session->set_flashdata('failure','<div class="alert alert-warning alert-dismissible fade show text-center">Something Went Wrong!<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
			redirect(base_url('volunteer', 'refresh'));
		}
	}

}
?>