<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$_title         =  'Alışveriş Sonucu';


?>
<div class="container-fluid adres_cubugu">
  <div class="row">
  	<div class="container">
  		<div class="row">
  			<div class="col-md-12">
  				<ul>
  					<li><a href="index.php"><i class="fa fa-home"></i> Anasayfa</a> <span><i class="fa fa-arrow-right"></i></span></li>
  					<li><a href="#">Alışveriş Sonucu</a></li>
  				</ul>
  			</div>
  		</div>
  	</div>
  </div>
</div>

<section class="bg-dis">
	<div class="container">

        <div class="row mt-15">
          <div class="col-md-12">
            <div class="alan">

          <?php

                    $query = $db->prepare("SELECT * FROM siparis where siparis_key=:siparis_key LIMIT 1");
                    $gel = $query->execute(array(":siparis_key"=>$_GET['siparis_key']));
                    $gel = $query->fetch(PDO::FETCH_ASSOC);

                    if($gel){

                      echo '<center><img src="images/basari.png" style="width:150px"><br><br>
                      <div style="padding-bottom: 20px;font-size: 18px;color: #229f38;"><b>Siparişiniz başarı ile oluşturuldu.</b></div></li>
                      <div style="padding-bottom: 20px;font-size: 18px;"><b>Sipariş özetiniz email adresinize gönderilmiştir.</b></div>
                      <div style="padding-bottom: 20px;font-size: 18px;"><b>Siparişlerinizi <a href="siparislerim">Şiparişlerim</a> sayfasından kontrol edebilirsiniz.</b></div></center>';


                      $urun_html = '';

                      $query = $db->query("SELECT * FROM siparis_urun WHERE siparis_id = '{$gel['id']}'", PDO::FETCH_ASSOC);
                      if($query->rowCount()){
                        foreach($query as $row){

                          $urunquery = $db->prepare("SELECT * FROM urun where id=:id LIMIT 1");
                          $urun = $urunquery->execute(array(":id"=>$row['urun_id']));
                          $urun = $urunquery->fetch(PDO::FETCH_ASSOC);

                          $urunimg = $db->prepare("SELECT * FROM urun_img where urun_id=:urun_id LIMIT 1");
                          $uimg = $urunimg->execute(array(":urun_id"=>$row['urun_id']));
                          $uimg = $urunimg->fetch(PDO::FETCH_ASSOC);

                          $secenek = '';
                          if($row['secenek_id'] != 0){
                            $alt_secenek = $db->prepare("SELECT * FROM urun_secenek_alt where id=:id LIMIT 1");
                            $as = $alt_secenek->execute(array(":id"=>$row['secenek_id']));
                            $as = $alt_secenek->fetch(PDO::FETCH_ASSOC);

                            $ust_secenek = $db->prepare("SELECT * FROM urun_secenek where id=:id LIMIT 1");
                            $us = $ust_secenek->execute(array(":id"=>$as['urun_secenek_id']));
                            $us = $ust_secenek->fetch(PDO::FETCH_ASSOC);

                            $secenek = '<div class="sepet_varyant"><span>'.$us['baslik'].': </span> '.$as['baslik'].'</div>';
                          }

                          $urun_html.= '<tr>
                              <td><img src="'.$site.'upload/'.$uimg['img'].'" style="width: 100px;border:1px solid #ddd;padding: 5px;border-radius: 10px"></td>
                              <td>'.$urun['baslik'].'<br>'.$secenek.'</td>
                              <td>'.$row['adet'].' Adet</td>
                              <td>'.$urun['fiyat'].' TL</td>
                              <td>'.($urun['fiyat'] * $row['adet']).' TL</td>
                            </tr>';
                        }

                        $mailbody = '<!DOCTYPE html>
                              <html>
                              <head>
                                <title>'.$cek['title'].'</title>
                                <meta charset="utf-8">
                              </head>
                              <body style="padding: 30px">
                                <div style="width: 98%;margin:0 auto;background: #02add9;padding: 1%;display: inline-block;border-radius: 10px">
                                  <div style="width: 90%;float: left;background: #fff;padding: 10px 5% 20px 5%;">
                                    <center><img src="'.$site.'upload/'.$cek['logo'].'" style="width: 200px"></center>
                                    <table>
                                      <tr>
                                        <th>Ürün Fotoğrafı</th>
                                        <th>Ürün Bilgi</th>
                                        <th>Adet</th>
                                        <th>Fiyat</th>
                                        <th>Toplam Tutar</th>
                                      </tr>
                                      '.$urun_html.'
                                      <tr>
                                        <td colspan="3"></td>
                                        <td colspan="2" style="text-align: left;padding: 20px">
                                          <b>Sipariş ID</b>: '.$gel['id'].'<br>
                                          <b>Durum</b>: Onay Bekliyor<br>
                                          <b>Referans Kodu</b>: '.$_GET['siparis_key'].'<br>
                                          <b>Ödeme Yöntemi</b>: '.$odeme_yontemi[$gel['odeme_yontemi']].'<br>
                                          <b>Toplam Tutar</b>: '.$gel['toplam_tutar'].' TL<br>
                                        </td>
                                      </tr>
                                    </table>
                                  </div>
                                </div>
                                <style type="text/css">
                                body{font-family:arial}table{width:100%;border:1px solid #ddd}table tr{padding:0;margin:0}table tr th{border:1px solid #ddd;padding:0;margin:0;background:#02add9;color:#fff;padding:10px}table tr td{border:1px solid #ddd;padding:0;text-align:center;margin:0;border-spacing:0}
                                </style>
                              </body>
                              </html>';

                              $mail_ayar = $db->query("SELECT * FROM mail_ayar")->fetch(PDO::FETCH_ASSOC);

                              if(!empty($mail_ayar['host']) AND !empty($mail_ayar['mail']) AND !empty($mail_ayar['sifre'])){
                                require 'vendor/autoload.php';

                                $mail = new PHPMailer(true);

                                $mail->SMTPDebug = SMTP::DEBUG_SERVER;
                                $mail->isSMTP();
                                $mail->Host       = $mail_ayar['host'];
                                $mail->SMTPAuth   = true;
                                $mail->Username   = $mail_ayar['mail'];
                                $mail->Password   = $mail_ayar['sifre'];
                                $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
                                $mail->Port       = 587;
                                $mail->CharSet = 'UTF-8';
                                $mail->SMTPDebug = 0;   
                                $mail->setFrom($mail_ayar['mail'], $cek['title']);
                                $mail->addAddress($cek['siparis_email'], $cek['title']);
                                $mail->addAddress($gel['email'], $gel['ad'].' '.$gel['soyad']);
                                $mail->isHTML(true);
                                $mail->Subject = 'Yeni Sipariş';
                                $mail->Body    = $mailbody;
                                $mail->AltBody = '';
                                $mail->send();
                              }


                      }


                    }else{
                      echo '<center><img src="images/hata.png" style="width:150px"><br><br><span style="font-size:25px;color:red;font-weight:bold">Bir hata oluştu.</span></center>';
                    }

               


          

        ?>
            
            </div>
          </div>
        </div>

  </div>
</section>
