<? include ("../settings.php"); ?>
<? include ("session.php"); ?>
<?
    if (isset($_REQUEST['registrant_id']) && $_REQUEST['registrant_id']>0){
        
        // Updating replaced registrant
        $bind = array(":search1"=>$_REQUEST['registrant_id']);
        $update = array("registrant_replaced"=>1);
        $db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);

            
        // Duplicating the current record
        $insert = array(
            "registrant_type" => $_REQUEST['registrant_type'],
            "registrant_salutation" => $_REQUEST['registrant_salutation'],
            "registrant_fullname" => $_REQUEST['registrant_fullname'],
			"registrant_firstname" => $_REQUEST['registrant_firstname'],
			"registrant_lastname" => $_REQUEST['registrant_lastname'],
			"registrant_designation" => $_REQUEST['registrant_designation'],
			"registrant_organisation" => $_REQUEST['registrant_organisation'],
			"registrant_email" => $_REQUEST['registrant_email'],
			"registrant_diet" => $_REQUEST['registrant_diet'],
			"registrant_badgecolour" => $_REQUEST['registrant_badgecolour'],
			"remarks" => $_REQUEST['remarks'],
		    "registrant_paid" => $_REQUEST['registrant_paid'],
		    "registrant_payment_mode" => $_REQUEST['registrant_payment_mode'],
		    "registrant_paid_amount_total" => $_REQUEST['registrant_paid_amount_total'],
			"role_id" => $_REQUEST['role_id']
        );
  
        //print_r($insert);
        $db->insert("tbl_registrant", $insert);
        
        $results = $db->run("select last_insert_id() as lastid;");
        print_r($results);
        $replaceid = $results[0]['lastid'];
        
        
        $update = array(
            "registrant_replacing"=>$_REQUEST['registrant_id']
        );
        $bind = array(
            ":search1" => $replaceid
        );
        
        $db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);
        
        
        if ($_REQUEST['role_id']!="0"){
            $results = $db->select("tbl_registrant","registrant_id = :search1",$bind);
            $result = $results[0];
            if($result['registrant_number']==""){
                $numbers = $db->select("tbl_id");
                $registrant_number = $numbers[0]['id_itself'];
                $increase = $registrant_number + 1;
                $bindid = array(":search1" => 1);
                $updateid = array("id_itself" => $increase);
                $db->update("tbl_id", $updateid, "id = :search1", $bindid);
                $update = array ("registrant_number"=>$registrant_number);
                $db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);
            }else{
                $registrant_number = $result['registrant_number'];
            }
            
            $bindid = array(":search1" => $_REQUEST['role_id']);
            $prefixes = $db->select("tbl_role","role_id=:search1",$bindid);
            $prefix = $prefixes[0]['role_prefix'];
            $registrant_sn = $prefix.str_pad($registrant_number, 3, "0", STR_PAD_LEFT);
            $update = array ("registrant_sn"=>$registrant_sn);
            $db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);            
            
        }        
        
        echo "1";
        
    }


?>