<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Duplicate Tool</title>
    <!-- <META http-equiv="refresh" content="1;URL=/?"> -->
    	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <style type="text/css">
    	td {
    			padding: 5px;
    	}

		#selectsomething .item {
		    padding: 5px;
		    text-align: right;
		}
		#selectsomething .close {
		    float: right;
		    padding: 5px;
		    color: crimson;
		    font-size: 105%;
		    font-weight: bolder;
		}
		#selectheader {
		    font-size: 150%;
		    text-align: center;
		}
		#selectsomethinginner {
		    padding: 20px;
		}
		#selectsomething {
		    z-index: 1000;
		    position: fixed;
		    height: auto;
		    max-height: 800px;
		    overflow: auto;
		    width: auto;
		    top: 50%;
		    left: 50%;
		    transform: translate(-50%, -50%);
		    
		    background-color: #FFF;
		    border-style: solid;
		    border-width: unset;
		    border-color: #000;
		    min-width: 300px;
		}

		a {
			color:crimson;
		}
		#selectCloseBackground {
		    z-index: 1;
		    height: 100%;
		    width: 100%;
		    /*background: aliceblue;*/
		    position: fixed;
		}
    </style>
</head>

<body>




<?
	include('_functions2.php');


	$showLinks = true;
	if (isset($_GET['echo'])) {
		$shouldEcho = true;
	} else {
		$shouldEcho = false;
	}
	if (isset($_GET['test'])) {
		$test = (int)$_GET['test'];
	} else {
		$test = 99;
	}

	if (isset($_GET['cid'])) {
		$query = " SELECT * FROM _reph_certificates WHERE id=".$_GET['cid']." ";
		$record = getRecord( $query, $shouldEcho );
		printRecordHTML($record);
		$showLinks = false;
	}


	if (isset($_GET['gguid'])) {
		$query = " SELECT * FROM _reph_certificates_group WHERE guid='".$_GET['gguid']."' ";
		$record = getRecord( $query, $shouldEcho );
		printRecordHTML($record);
		$showLinks = false;
	}

	if (isset($_GET['email'])) {

		// echo urldecode( $_GET['email'] );

		$query = " SELECT * FROM _reph_certificates_group WHERE email LIKE '%".$_GET['email']."%' ";
		$record = getRecords( $query, $shouldEcho );
		printRecordsHTML($record);
		$showLinks = false;
	}

	if ( $showLinks ) {
		echo 'test 0: <a href="fixDuplicates.php?test=0">noSQL</a> | <a href="fixDuplicates.php?test=0&echo">SQL</a><br/>';
		echo 'test 1: <a href="fixDuplicates.php?test=1">noSQL</a> | <a href="fixDuplicates.php?test=1&echo">SQL</a><br/>';
		echo 'test 2: <a href="fixDuplicates.php?test=2">noSQL</a> | <a href="fixDuplicates.php?test=2&echo">SQL</a><br/>';
		echo '<br/><br/>';
	}


	if ( $test === 0 || $test === 2 ) {
		$query = "
					SELECT 
					    ecard_number
					FROM
					    _reph_report_cache
					WHERE del IS NULL 
					GROUP BY ecard_number
					HAVING COUNT(ecard_number) > 1;
		";
	}

	if ( $test === 1 ) {
		$query = "
					SELECT 
					    ordernumber
					FROM
					    _reph_report_cache
					WHERE del IS NULL 
					GROUP BY ecard_number
					HAVING COUNT(ecard_number) > 1;
		";
	}

	if ( $test !== 99 ) {
		$spent = array();
		$duplicates = getRecords2( $query, $shouldEcho );
		$hasTable = true;

		foreach ($duplicates as $key => $record) {
			$shouldRun = true;

			
			if ( $test === 0 ) {
				$query = " SELECT id,label,date_created,used,groupid FROM _reph_certificates WHERE del IS NULL AND label = '".$record['ecard_number']."' ORDER BY id DESC ";
				$thisDuplicate = getRecords2( $query, $shouldEcho );
			}

			if ( $test === 1 ) {

				$query = " SELECT id,orderid,date_created,email,first,last,guid FROM _reph_certificates_group WHERE del=0 AND orderid = '".$record['ordernumber']."' ORDER BY id DESC ";
				if ( in_array( $record['ordernumber'], $spent ) ) {
					$shouldRun = false;
				} else {
					array_push( $spent, $record['ordernumber'] );
					$thisDuplicate = getRecords2( $query, $shouldEcho );
				}
			}



			if ( $test === 2 ) {

				$query = " SELECT * FROM _reph_certificates WHERE del IS NULL AND label = '".$record['ecard_number']."' ORDER BY id DESC ";
				$thisDuplicate = getRecords2( $query, $shouldEcho );
				$hasTable = false;
			}



			if ( $shouldRun ) {
				if ( $hasTable )
					echo '<table border="2px">';
				$i=0;
				foreach ($thisDuplicate as $key => $dup) {
					if ( $test === 0 ) {
						echo '<tr>';
						echo '<td>'.'<a onclick="fixDuplicates.preview.load(\'cid\',\''.$dup['id'].'\');">'.$dup['id'].'</a>' .'</td><td>'. $dup['used'] .'</td><td>'. $dup['label'] .'</td><td>'. $dup['date_created'] .'</td><td>'. '<a onclick="fixDuplicates.preview.load(\'gguid\',\''.$dup['groupid'].'\');">'.$dup['groupid'].'</a>' .'</td>';
						echo '</tr>';
					}
					if ( $test === 1 ) {


						echo '<tr>';
						echo '<td>'.$dup['id'] .'</td><td>'. $dup['orderid'] .'</td><td>'. $dup['date_created'] .'</td><td>'. '<a onclick="fixDuplicates.preview.load(\'email\',\''.$dup['email'].'\',true);">'.$dup['email'].'</a>' .'</td><td>'. $dup['first'] .'</td><td>'. $dup['last'] .'</td><td>'. '<a onclick="fixDuplicates.preview.load(\'gguid\',\''.$dup['guid'].'\');">'.$dup['guid'].'</a>'.'</td>';
						echo '</tr>';
					}

					if ( $test === 2 ) {
						$query = " SELECT * FROM _reph_certificates WHERE del IS NULL AND groupid = '".$dup['groupid']."' ORDER BY id DESC ";
						$thisDuplicateGroup = getRecords2( $query, $shouldEcho );
						echo '<br/>';
						echo '<br/>';
						echo '<a onclick="fixDuplicates.preview.load(\'gguid\',\''.$dup['groupid'].'\');">'.$dup['groupid'].'</a>' . '&nbsp;&nbsp;&nbsp;' . $dup['label'];
						echo '<br/>';
						foreach ($thisDuplicateGroup as $key => $dupGroup) {
							echo '<a onclick="fixDuplicates.preview.load(\'cid\',\''.$dupGroup['id'].'\');">'.$dupGroup['id'].'</a>'.'&nbsp;&nbsp;&nbsp;'.$dupGroup['label'] . '<br/>';
						}

					}



					$i++;
				}

				if ( $hasTable )
					echo '</table><br/><br/>';

				// printRecordsHTML( $thisDuplicate );
				// echo '<hr/>';

			}


			
		}
	}

	// printRecordsHTML( $duplicates );
	// var_dump( $duplicates );



?>



<script type="text/javascript">
    fixDuplicates = {};
    fixDuplicates.v = {};
            fixDuplicates.preview = {
                    record: 0,
                    guid: '',
                    html: '<div id="selectsomething"><span class="close" onClick="$(\'#selectsomething\').remove();$(\'#selectCloseBackground\').remove();">X</span><div id="selectsomethinginner"><span id="selectheader"> Record </span><hr /><div class="items"> [HERE] </div></div></div>',
                load: function(field,data,plural) {
                            $.post(
                                'fixDuplicates.php?'+field+'='+encodeURI(data), {
                                    'id': 0
                                },
                                function(data) {
                                	$('#selectsomething').remove();
                                    $('body').append( fixDuplicates.preview.html.replace( '[HERE]', data ) );
                                    if ( typeof plural !== 'undefined' )
                                    	$('#selectheader').html(' Records ');


                                    $('body').prepend('<div id="selectCloseBackground"></div>');
                                    setTimeout(function() {
										$('#selectCloseBackground').click(function() {
											$('#selectsomething').remove();
											$('#selectCloseBackground').remove();
										});
                                    }, 200);

                                }
                            );
                            // fixDuplicates.preview.load(url);
                }, //==============================================
                two: function(data) {
                    console.log(data);
                            // fixDuplicates.form.two();
                } //==============================================
            };
</script>


</body>

</html>



