<?php  
$path_to_root="../../..";
$page_security = 'SA_OPEN';
	include_once($path_to_root . "/includes/db_pager.inc");

include_once($path_to_root . "/includes/session.inc");
page(_($help_context = "Contacts Assign To"), true);
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/modules/KvcodesCRM/includes/crm.inc" );
include_once($path_to_root . "/modules/KvcodesCRM/includes/crm_contacts_view.inc");


if(isset($_POST['update_contact'])){

	if(isset($_POST['user_id']) && $_POSt['user_id'] > 0 )
		Update('crm_persons', ['id' => $_POST['person_id']], ['user_id' => $_POST['user_id']]);
	elseif($_POST['username'] != '' && $_POST['password'] != '' ){
		 $_POST['CustomerRole'] = GetSingleValue('kv_crm_option', 'option_value', array('option_name' => 'customer_role'));
		$user_id = Insert('users', array('user_id' => $_POST['username'], 'real_name' => $_POST['name'], 'role_id' => $_POST['CustomerRole'], 'password' => md5($_POST['password']), 'phone' => $_POST['phone'], 'email' => $_POST['email'], 'startup_tab' => 'crm'));
		$update_contact['user_id'] = $user_id;
		if(check_value('send_welcome_email')){
			$to =  array('name' => $_POST['name'], 'email' => $_POST['email']);
			$contact = array('name' => $_POST['name'], 'name2' => '', 'phone' => $_POST['phone'], 'email' => $_POST['email'], 'user_name' => $_POST['username'], 'password' => md5('password'));
			//display_error(json_encode($contact));
			$template = array('slug' => 'new-client-created',  'details' => $contact );
			kv_mail($to, '', '', "html", array(), null, false, $template);
		}
	}
	
	if(isset($_POST['type'])){
		if($_POST['type'] == ST_SUPPLIER && $_POST['entity_id'] > 0){
			Insert('crm_contacts', ['type' => 'supplier', 'person_id' => $_POST['person_id'], 'action' => 'general', 'entity_id' => $_POST['entity_id']]);
		} elseif($_POST['type'] == ST_CUSTOMER && $_POST['entity_id'] > 0){
			if(isset($_POST['branch_code']) && $_POST['branch_code'] > 0 )
				Insert('crm_contacts', ['type' => 'cust_branch', 'person_id' => $_POST['person_id'], 'action' => 'general', 'entity_id' => $_POST['entity_id']]);
			else
				Insert('crm_contacts', ['type' => 'customer', 'person_id' => $_POST['person_id'], 'action' => 'general', 'entity_id' => $_POST['entity_id']]);
		}
	}
	echo '<script type="text/javascript" > window.close(); </script>';
}

if(isset($_GET['entity_id']))
	$_POST['person_id'] = $_GET['entity_id'];

if(list_updated('entity_id')){
	$Ajax->activate('branch_list');
}
$person_details = GetRow('crm_persons', array('id' => $_POST['person_id']));

start_form();

start_table(TABLESTYLE, "width='50%'");
	label_row(_("Name"), $person_details['name']);
	label_row(_("Name 2 "), $person_details['name2']);
	label_row(_("Short Name"), $person_details['ref']);
	label_row(_("Address"), nl2br($person_details['address']));
	label_row(_("Phone"), $person_details['phone']);
	label_row(_("Phone 2"), $person_details['phone2']);
	label_row(_("Email"), $person_details['email']);
	if(isset($person_details['user_id']) && $person_details['user_id'] == 0){
		kv_unassigned_users_for_contact_row(_("Select User Access:"), 'user_id',null,"No user selected");
		text_row(_("Username:"), 'username', null, 32, 30);
			password_row(_("Password:"), 'password', null);
			check_row(_("Send Login Credentials through email"), 'send_welcome_email', null);

	}
	label_row(_("Notes"), nl2br($person_details['notes']));

	//display_error(json_encode($person_details));
end_table();
br(2);
	start_outer_table(TABLESTYLE);
		table_section(1);
		 $list = array(ST_CUSTOMER => _("Customer"), ST_SUPPLIER => _("Supplier"));
		 start_row();
	    echo "<td>Type</td>\n";
		echo "<td>"; 
    	echo array_selector('type',null, $list, 
        array( 
            'select_submit'=> true,
            'async' => false,
            'spec_option'=> _('Select a type'),
            'disabled' => false ) ); // FIX?
    	echo "</td>";
    	end_row();

    	hidden('person_id', $_POST['person_id']);
    	if($_POST['type'] ==ST_CUSTOMER){
			table_section(2);
			
			start_row();
			$contact_type ='customer';
			$sql ="SELECT deb.debtor_no AS id, deb.name AS name  FROM ".TB_PREF."debtors_master as deb ";
			echo "<td>".combo_input('entity_id',null, $sql, 'id', 'name',array('order'=>false,'select_submit' =>true,'spec_option'=>_('Select Customer')))."<td>";
			div_start('branch_list');
			if($_POST['type']==ST_CUSTOMER  && isset($_POST['entity_id']) &&$_POST['entity_id'] !=0){
				 $sql = "SELECT branch_code As id, br_name AS name FROM ".TB_PREF."cust_branch
					WHERE debtor_no=" . db_escape($_POST['entity_id'])." ";
				echo "<td>".combo_input('branch_code', null, $sql, 'id', 'name',array('order'=>false,'select_submit' =>true,'spec_option'=>_('Select Branch'))) ."<td>";
			}
			div_end();
			end_row();
		}elseif($_POST['type'] ==ST_SUPPLIER){

			table_section(2);
			start_row();
			$contact_type ='supplier';
			$sql ="SELECT supp.supplier_id AS id, supp.supp_name AS name  FROM ".TB_PREF."suppliers as supp ";
			echo "<td>".combo_input('entity_id', null, $sql, 'id', 'name',array('order'=>false,'select_submit' =>true,'spec_option'=>_('Select Supplier')))."<td>";
			 end_row();
		}
		table_section(3);
		
		
		
	end_outer_table(1);

br();
	submit_center('update_contact',_('Update'));
	br(2);
	end_form();
end_page();