<?PHP
$auth = false;
$authID = 'EAE5972B-83F4-2760-1CCC-96C6F52BAF0D';
if (isset($_COOKIE["guid"])) {
    if ($_COOKIE["guid"] === $authID) {
        $auth = true;
    }
}
if (isset($_POST["username"]) && isset($_POST["password"])) {
    if ($_POST["username"] === 'sales' && $_POST["password"] === '7ab1f81c336ef987a09842fa12638631') {
        $auth = true;
    }
}

if (isset($_GET["AE532C4E"])) {
	$auth = true;
}


if ($auth) {
	if (isset($_GET["AE532C4E"])) {} else {
		header('Content-Type: application/csv');
	}
    echo"created,order,first,last,email,card,value,transfer,tip,emailed,gift,type,modified,used\n";
    include('_functions2.php');
    // $data = spreadsheet();

	$query = "
				SELECT * FROM _reph_report_cache

				WHERE del IS NULL

				ORDER BY modified DESC, ecard_number ASC
	";

	$data = getRecords( $query );

// printRecordsHTML( $data );

        foreach($data as $key => $item) {


            if ($item['used']) {
                $used = 'YES';
            } else {
                $used = 'NO';
            }


	        if ($item['cardtype'] === 1 || $item['cardtype'] === '1') {
	        	$cardtype = 'Gold';
	        } else if ($item['cardtype'] === 3 || $item['cardtype'] === '3') {
	        	$cardtype = 'Certificate';
	        } else {
	            $cardtype = 'Black';
	        }

            $transfer = '?';

            $xfer = wasTransferred($item['guid']);
            if ($xfer) {
                $transfer = 'Yes';
            } else {
                $transfer = 'No';
            }


            $created = $item['created'];
            $order = $item['ordernumber'];
            $first = $item['first'];
            $last = $item['last'];
            $email = $item['email'];
            $card = $item['ecard_number'];
            $dvalue = $item['dvalue'];
            
            $tip = $item['tip'];
            $emailed = $item['esend'];
            $gift = $item['gift'];

            $modified = $item['modified'];




            echo"$created,$order,$first,$last,$email,$card,\"$dvalue\",$transfer,$tip,$emailed,$gift,$cardtype,$modified,$used\n";
        }


} else {
    echo"Please login\n\n";
} 




// (n) id = 227239
// (dt) date_created = 1578007759, '2020-01-02 18:29:19'
// (dt) date_modified = 1578000559, '2020-01-02 16:29:19'
// (dt) created = 1577929501, '2020-01-01 20:45:01'
// (n) ordernumber = 22257
// (s) first = 'Trang'
// (s) last = 'Nguyen'
// (s) email = 'trangnth01290@gmail.com'
// (n) ecard_number = 2225701
// (n) esend = 1
// (n) gift = 0
// (dt) modified = 1577929501, '2020-01-01 20:45:01'
// (s) guid = '33644D89-A676-08E0-915D-1D6572791F1D'
// (s) dvalue = 'One Hour Deep Tissue Massage'
// (n) tip = 0
// (n) cardtype = 3
// (n) used = 0
// (n) cid = 2824
// (nl) del = NULL
// (nl) status = NULL

?>
