<?php
class ControllerAccountEdit extends Controller {
	private $error = array();

	public function index() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/edit', '', true);

			$this->response->redirect($this->url->link('account/login', '', true));
		}

		$this->load->language('account/edit');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment.min.js');
		$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment-with-locales.min.js');
		$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js');
		$this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css');

		$this->load->model('account/customer');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

			$this->model_account_customer->editCustomer($this->customer->getId(),$this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->response->redirect('account');
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_account'),
			'href' => $this->url->link('account/account', '', true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_edit'),
			'href' => $this->url->link('account/edit', '', true)
		);

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['firstname'])) {
			$data['error_firstname'] = $this->error['firstname'];
		} else {
			$data['error_firstname'] = '';
		}

		if (isset($this->error['lastname'])) {
			$data['error_lastname'] = $this->error['lastname'];
		} else {
			$data['error_lastname'] = '';
		}

		if (isset($this->error['email'])) {
			$data['error_email'] = $this->error['email'];
		} else {
			$data['error_email'] = '';
		}

		if (isset($this->error['telephone'])) {
			$data['error_telephone'] = $this->error['telephone'];
		} else {
			$data['error_telephone'] = '';
		}

		if (isset($this->error['custom_field'])) {
			$data['error_custom_field'] = $this->error['custom_field'];
		} else {
			$data['error_custom_field'] = array();
		}

		$data['action'] = $this->url->link('account/edit', '', true);

		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
			$data['customer_id'] = $this->customer->getId();
		}else{
			$data['customer_id'] = '';
		}

		$data['states'] = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone WHERE country_id = 99 ORDER BY name asc")->rows;

		$data['zones'] = $this->db->query("SELECT * FROM " . DB_PREFIX . "zonal")->rows;

		if (!empty($customer_info['type'])) {

			$membership = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_type WHERE id = '" . (int)$customer_info['type'] . "'")->row;
			
			$data['membership'] = $membership['type'];
			$data['membership_currency'] = $membership['currency'];
		} else {
			$data['membership'] = '';
			$data['membership_currency'] = 'INR';
		}
		//echo "<pre>"; print_r($customer_info['balance']);exit;

		if (!empty($customer_info)) {
			$data['reciept_no'] = $customer_info['reciept_no'];
			$data['membership_fee'] = $customer_info['balance'];
		} else {
			$data['reciept_no'] = '';
			$data['membership_fee'] = '0.00';
		}

		if (!empty($customer_info)) {
			$data['reciept_date'] = $customer_info['reciept_date'];
		} else {
			$data['reciept_date'] = '';
		}

		if (!empty($customer_info)) {
			$data['customerzone'] = $customer_info['zone'];
		}	else {
			$data['customerzone'] = '';
		}

		if (!empty($customer_info)) {
			$data['paid'] = $customer_info['paid'];
		} else {
			$data['paid'] = '0.00';
		}

		if (!empty($customer_info)) {
			$data['balance'] = $customer_info['balance'];
		} else {
			$data['balance'] = '0.00';
		}

		if (isset($this->request->post['firstname'])) {
			$data['firstname'] = $this->request->post['firstname'];
		} elseif (!empty($customer_info)) {
			$data['firstname'] = $customer_info['firstname'];
		} else {
			$data['firstname'] = '';
		}

		if (isset($this->request->post['firstname'])) {
			$data['firstname'] = $this->request->post['firstname'];
		} elseif (!empty($customer_info)) {
			$data['firstname'] = $customer_info['firstname'];
		} else {
			$data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$data['lastname'] = $this->request->post['lastname'];
		} elseif (!empty($customer_info)) {
			$data['lastname'] = $customer_info['lastname'];
		} else {
			$data['lastname'] = '';
		}

		if (isset($this->request->post['designation'])) {
			$data['designation'] = $this->request->post['designation'];
		} elseif (!empty($customer_info)) {
			$data['designation'] = $customer_info['designation'];
		} else {
			$data['designation'] = '';
		}

		if (isset($this->request->post['specialization'])) {
			$data['specialization'] = $this->request->post['specialization'];
		} elseif (!empty($customer_info)) {
			$data['specialization'] = $customer_info['specialization'];
		} else {
			$data['specialization'] = '';
		}

		if (isset($this->request->post['allied'])) {
			$data['allied'] = $this->request->post['allied'];
		} elseif (!empty($customer_info)) {
			$data['allied'] = $customer_info['allied'];
		} else {
			$data['allied'] = '';
		}

		if (isset($this->request->post['anydetail'])) {
			$data['anydetail'] = $this->request->post['anydetail'];
		} elseif (!empty($customer_info)) {
			$data['anydetail'] = $customer_info['anydetail'];
		} else {
			$data['anydetail'] = '';
		}

		if (isset($this->request->post['fpsi'])) {
			$data['fpsi'] = $this->request->post['fpsi'];
		} elseif (!empty($customer_info)) {
			$data['fpsi'] = $customer_info['fpsi'];
		} else {
			$data['fpsi'] = '';
		}

		if (isset($this->request->post['dob'])) {
			$data['dob'] = $this->request->post['dob'];
		} elseif (!empty($customer_info)) {
			$data['dob'] = $customer_info['dob'];
		} else {
			$data['dob'] = '';
		}

		if (isset($this->request->post['email'])) {
			$data['email'] = $this->request->post['email'];
		} elseif (!empty($customer_info)) {
			$data['email'] = $customer_info['email'];
		} else {
			$data['email'] = '';
		}

		if (isset($this->request->post['email2'])) {
			$data['email2'] = $this->request->post['email2'];
		} elseif (!empty($customer_info)) {
			$data['email2'] = $customer_info['email2'];
		} else {
			$data['email2'] = '';
		}

		if (isset($this->request->post['telephone'])) {
			$data['telephone'] = $this->request->post['telephone'];
		} elseif (!empty($customer_info)) {
			$data['telephone'] = $customer_info['telephone'];
		} else {
			$data['telephone'] = '';
		}

		if (isset($this->request->post['mobile'])) {
			$data['mobile'] = $this->request->post['mobile'];
		} elseif (!empty($customer_info)) {
			$data['mobile'] = $customer_info['mobile'];
		} else {
			$data['mobile'] = '';
		}

		if (isset($this->request->post['add1'])) {
			$data['add1'] = $this->request->post['add1'];
		} elseif (!empty($customer_info)) {
			$data['add1'] = $customer_info['add1'];
		} else {
			$data['add1'] = '';
		}

		if (isset($this->request->post['add2'])) {
			$data['add2'] = $this->request->post['add2'];
		} elseif (!empty($customer_info)) {
			$data['add2'] = $customer_info['add2'].' '.$customer_info['add3'];
		} else {
			$data['add2'] = '';
		}

		if (isset($this->request->post['add3'])) {
			$data['add3'] = $this->request->post['add3'];
		} elseif (!empty($customer_info)) {
			$data['add3'] = $customer_info['add3'];
		} else {
			$data['add3'] = '';
		}

		if (isset($this->request->post['city'])) {
			$data['city'] = $this->request->post['city'];
		} elseif (!empty($customer_info)) {
			$data['city'] = $customer_info['city'];
		} else {
			$data['city'] = '';
		}

		if (isset($this->request->post['pincode'])) {
			$data['pincode'] = $this->request->post['pincode'];
		} elseif (!empty($customer_info)) {
			$data['pincode'] = $customer_info['pincode'];
		} else {
			$data['pincode'] = '';
		}

		if (isset($this->request->post['state'])) {
			$data['state'] = $this->request->post['state'];
		} elseif (!empty($customer_info)) {
			$data['state'] = $customer_info['state'];
		} else {
			$data['state'] = '';
		}

		// Custom Fields
		$data['custom_fields'] = array();
		
		$this->load->model('account/custom_field');

		$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));

		foreach ($custom_fields as $custom_field) {
			if ($custom_field['location'] == 'account') {
				$data['custom_fields'][] = $custom_field;
			}
		}

		if (isset($this->request->post['custom_field']['account'])) {
			$data['account_custom_field'] = $this->request->post['custom_field']['account'];
		} elseif (isset($customer_info)) {
			$data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
		} else {
			$data['account_custom_field'] = array();
		}

		$awards = $this->model_account_customer->getCustomerAwards($this->customer->getId());

		$data['submitted_awards'] = array();
		$data['pending_awards'] = array();

		if (!empty($awards)) {
			foreach ($awards as $key => $award) { 
				if ($award['submit'] == 1) {

					$awardname = $this->db->query("SELECT * FROM `" . DB_PREFIX . "awards` WHERE `id` = '" . $award['award_id'] . "'")->row;

					$data['submitted_awards'][]  = array(
						'id' => $award['id'],
						'customer_id' => $award['customer_id'],
						'name' => $award['name'],
						'award_id' => $award['award_id'],
						'year' => $award['year'],
						'jason' => json_decode($award['jason'],true),
						'awardname' => $awardname['name'],
						'href' => $this->url->link('award/award/preview', 'id=' . $award['id'], true),
						'delhref' => $this->url->link('award/award/deletenomination', 'id=' . $award['id'], true),
					);
				}else{
					$awardname = $this->db->query("SELECT * FROM `" . DB_PREFIX . "awards` WHERE `id` = '" . $award['award_id'] . "'")->row;

					$data['pending_awards'][]  = array(
						'id' => $award['id'],
						'customer_id' => $award['customer_id'],
						'name' => $award['name'],
						'award_id' => $award['award_id'],
						'year' => $award['year'],
						'jason' => json_decode($award['jason'],true),
						'awardname' => $awardname['name'],
						'href' => $this->url->link('award/award/editaward', 'id=' . $award['id'], true),
						'delhref' => $this->url->link('award/award/deletenomination', 'id=' . $award['id'], true),
					);

				}
			}
		}
		$six_digit_random_number = random_int(100000, 999999);
			 $plaintext = $six_digit_random_number."|45|".$customer_info['balance']."|".$customer_info['firstname']."|
	".$customer_info['designation']."|".$customer_info['add1']."|".$customer_info['telephone']."|".
	$customer_info['email']."|".$customer_info['balance'];

$plaintext1="0|0|0|0|0";
$plaintext2=$six_digit_random_number;
$plaintext3="45";
$plaintext5="9";
$plaintext4=$customer_info['balance'];
$uurl="https://www.entosocindia.org/thankyou";
 $cipher = "AES-128-CBC";

$key = "3403322318401004";

     $ivlen = openssl_cipher_iv_length($cipher);

    $iv = openssl_random_pseudo_bytes($ivlen);

     $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv);
     $ciphertext2 = openssl_encrypt($plaintext1, $cipher, $key, $options=0, $iv);
     $ciphertext3 = openssl_encrypt($plaintext2, $cipher, $key, $options=0, $iv);
     $ciphertext4 = openssl_encrypt($plaintext3, $cipher, $key, $options=0, $iv);
     $ciphertext5 = openssl_encrypt($plaintext4, $cipher, $key, $options=0, $iv);
     $ciphertext6 = openssl_encrypt($plaintext5, $cipher, $key, $options=0, $iv);
     $ciphertext7 = openssl_encrypt($uurl, $cipher, $key, $options=0, $iv);

	$s="https://eazypay.icicibank.com/EazyPG?merchantid=341844&mandatory 
fields=".$ciphertext."&optional fields=".$ciphertext2."&returnurl=".$ciphertext7."&Reference No=".$ciphertext3."&
submerchantid=".$ciphertext4."&transaction amount=".$ciphertext5."&paymode=".$ciphertext6;
$data['breadcrumbs1'] = array();

		$data['breadcrumbs1'] = array(
			'texturll' => $s
		);

		$data['back'] = $this->url->link('account/account', '', true);
		$data['activation_code'] = $customer_info['code'];
		$data['code_validity'] = $customer_info['code_validity'];
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		$this->response->setOutput($this->load->view('account/edit', $data));
	}

	protected function validate() {
		if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
			$this->error['firstname'] = $this->language->get('error_firstname');
		}

		if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
			$this->error['lastname'] = $this->language->get('error_lastname');
		}

		if ((utf8_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
			$this->error['email'] = $this->language->get('error_email');
		}

		if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
			$this->error['warning'] = $this->language->get('error_exists');
		}

		if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
			$this->error['telephone'] = $this->language->get('error_telephone');
		}

		// Custom field validation
		$this->load->model('account/custom_field');

		$custom_fields = $this->model_account_custom_field->getCustomFields('account', $this->config->get('config_customer_group_id'));

		foreach ($custom_fields as $custom_field) {
			if ($custom_field['location'] == 'account') {
				if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) {
					$this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
				} elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
					$this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
				}
			}
		}

		return !$this->error;
	}
}