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

class UpdateProfileGlobal extends MY_Controller {

	public function __construct() {
		parent::__construct();

	}


	public function UpdateProfile()
	{
		$data = $this->input->post();

		
		$newData = array();
		foreach ($data['familyMembers'] as $key => $familyMember) {
			$newEntry = new stdClass();
			$newEntry->memberName = $familyMember;
			$newEntry->nakshatra = $data['nakshtras'][$key];
			$newEntry->rashi = $data['rashi'][$key];
			$newEntry->relationship = $data['relationship'][$key];
			$newEntry->dob = $data['dob'][$key];
			$newEntry->anniversaryDate = $data['anniversaryDate'][$key];


			$newData[] = $newEntry;
		}



		if ($data['noFamily'] == 'checked') {
			$data['noFamily'] = json_encode(true);
		}else{
			$data['noFamily'] = json_encode(false);
		}

		if ($data['noSpouse'] == 'checked') {
			$data['noSpouse'] = json_encode(true);
		}else{

			$data['noSpouse'] = json_encode(false);
		}


		$jsonArray = json_encode(json_decode(json_encode($newData), true));


		$Id = $this->session->userdata('id');
		$userName = $this->session->userdata('refDataName');

		$moduleName = customerLogin()['moduleName'];

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

		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'].postAPI()['url']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "{  
			\"_id\":\"".$Id."\",    
			\"dataJson\": {        
				\"refDataName\": \"".$data['fname']." ".$data['lname']."\",
				\"gotraName\": \"".$data['gothram']."\",        
				\"Nakshtra\": \"".$data['stars']."\",  

				\"noFamily\": \"".$data['noFamily']."\",  
				\"noSpouse\": \"".$data['noSpouse']."\",  

				\"spouseName\": \"".$data['spouseName']."\",  

				\"spouseNakshtra\": \"".$data['spouseStars']."\",  
				
				\"memberDetail\":".$jsonArray."
				},
				\"componentConfig\": {\"moduleName\":\"Contacts\",        
				\"productID\": \"".ApiBaseUrl()['productID']."\",        
				\"clientID\": \"".ApiBaseUrl()['clientID']."\",        
				\"userName\": \"".$userName."\"    }}");

		$token = $this->session->userdata('token');
		$headers = array();
		$headers[] = 'Authorization: Bearer '.$token.'';
		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$result = curl_exec($ch);
		$response = json_decode($result);

		print_r($response);


	}

}