<?php


require_once("includes/html-header.php");

$email = $_SESSION['alogin'];
$sql = "SELECT * from users where email = (:email);";
$query = $dbh->prepare($sql);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->execute();
$result = $query->fetch(PDO::FETCH_OBJ);
$cnt = 1;
$_SESSION['user_id']=$result->id;
$bbbb = $result->balance;

//get cost
$select = "SELECT * FROM control";
$g = mysqli_query($link,$select);
$data=mysqli_fetch_assoc($g);
$ssprice = $data['serverAndSign'];

if (isset($_POST['submit'])) {

    if ($bbbb < $ssprice) {
        $_SESSION['Error'] = "আপনার পর্যাপ্ত পরিমাণ ব্যালেন্স নাই, প্লিজ রিচার্জ করুন।";
    } else if ($bbbb >= $ssprice) {
        $name = mysqli_real_escape_string($link, $_POST['name']);
        $dob = mysqli_real_escape_string($link, $_POST['dob']);
        $nid_num = mysqli_real_escape_string($link, $_POST['nid_num']);
        $note = mysqli_real_escape_string($link, $_POST['note']);
        $csrfPost = mysqli_real_escape_string($link, $_POST['csrf']);
        if ($_SESSION['csrf'] == $csrfPost) {
            $sql = "INSERT INTO `servercopy_sub` (`name`, `nid_num`, `dob`, `order_time`, `note`, `user`)VALUES (:name, :nid_num, :dob, :order_time, :note, :user);";
            $data = [
                'name' => $name,
                'dob' => $dob,
                'nid_num' => $nid_num,
                'note' => $note,
                'user' => $_SESSION['alogin'],
                'order_time' => date("Y-m-d h:i:s A")
            ];


            $query = $dbh->prepare($sql);
            $query->execute($data);
            $sqlBalanceUpdate = "UPDATE `users` SET `balance` = balance - $ssprice WHERE `users`.`email` = (:email)";
            $queryBalanceUpdate = $dbh->prepare($sqlBalanceUpdate);
            $queryBalanceUpdate->bindParam(':email', $_SESSION['alogin'], PDO::PARAM_STR);
            $queryBalanceUpdate->execute();



            $_SESSION['Success'] = "অর্ডার সফল হয়েছে।শিগ্রই অ্যাাপ্রুভ করা হবে।";
        }
        // die();
    }
}
$csrf = md5(rand(1111, 99990));
$_SESSION['csrf'] = $csrf;

?>

<body>
    <!--  Body Wrapper -->
    <div class="page-wrapper" id="main-wrapper" data-layout="vertical" data-navbarbg="skin6" data-sidebartype="full" data-sidebar-position="fixed" data-header-position="fixed">


        <?php

        //include sidebar
        require_once("includes/sidebar.php");

        ?>

        <!--  Main wrapper -->
        <div class="body-wrapper">


            <?php

            //include header
            require_once("includes/header.php");

            ?>



            <div class="container-fluid">
            <!-- notice -->
            <marquee class="alert alert-dark text-white" onmouseover="this.stop()" onmouseout="this.start()" role="alert" style="font-size: 1rem;">
                <?php $se = "SELECT * FROM notice";
                $data = mysqli_query($link, $se);
                $d = mysqli_fetch_assoc($data);
                echo $d['notice']; ?>
            </marquee>
            <!-- notice end -->
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title fw-semibold mb-4 text-center">সার্ভার কপি অর্ডার করুন।</h5>
                        <div class="card">
                        
                            <div class="card-body">
                            <div class="alert alert-primary" role="alert">
                               সার্ভার কপির জন্য <?php echo $ssprice ?> টাকা কাটা হবে।
                            </div>
                                <form method="POST">
                                    <?php
                                        if(isset($_SESSION['Error'])){
                                        ?>
                                            <div class="alert alert-danger" role="alert">
                                                <?php 
                                                echo $_SESSION['Error'];
                                                $_SESSION['Error'] = null;
                                                
                                                ?>
                                            </div>
                                        <?php
                                        }elseif (isset($_SESSION['Success'])) {
                                        ?>
                                            <div class="alert alert-success" role="alert">
                                                <?php 
                                                echo $_SESSION['Success'];
                                                $_SESSION['Success'] = null;
                                                
                                                ?>
                                            </div>
                                        <?php
                                        }
                                    ?>
                                    <div class="mb-3 text-center">
                                        <label class="form-label">নামঃ *</label>
                                        <input type="text" name="name" class="form-control" required>
                                    </div>
                                    <div class="mb-3 text-center">
                                        <label class="form-label">জন্ম তারিখঃ</label>
                                        <input type="text" name="dob" class="form-control">
                                    </div>
                                    <div class="mb-3 text-center">
                                        <label class="form-label">আইডি/ভোটার/ফর্ম নাম্বারঃ *</label>
                                        <input type="number" name="nid_num" class="form-control" required>
                                    </div>
                                    <div class="mb-3 text-center">
                                        <label class="form-label">সার্ভার কপি সম্পর্কে বিস্তারিত লিখুনঃ(যদি কিছু বলার থাকে)</label>
                                        <textarea class="form-control" name="note" cols="30" rows="5"></textarea>
                                    </div>
                                    <input type="hidden" class="form-control" name="csrf" value="<?php echo $csrf ?>" >

                                    <div class="mb-3 text-center">
                                        <button type="submit" name="submit" class="btn btn-outline-primary m-1">অর্ডার করুন।</button>
                                    </div>

                                   
                                </form>
                            </div>
                        </div>
                        

                    </div>
                </div>
                <div class="card w-100">
                    <div class="card-body p-4">
                        <h5 class="card-title fw-semibold mb-4">পূর্ববর্তী অর্ডার</h5>
                        <div class="table-responsive">
                        <table class="table text-nowrap mb-0 align-middle">
                            <thead class="text-dark fs-4">
                            <tr>
                                <th class="border-bottom-0">
                                <h6 class="fw-semibold mb-0">আইডি</h6>
                                </th>
                                <th class="border-bottom-0">
                                <h6 class="fw-semibold mb-0">Rocket Number</h6>
                                </th>
                                <th class="border-bottom-0">
                                <h6 class="fw-semibold mb-0">অর্ডার টাইম</h6>
                                </th>
                                
                                <th class="border-bottom-0">
                                <h6 class="fw-semibold mb-0">স্ট্যাটাস</h6>
                                </th>
                                <th class="border-bottom-0">
                                <h6 class="fw-semibold mb-0">মোট ডাউনলোড</h6>
                                </th>
                                <th class="border-bottom-0">
                                <h6 class="fw-semibold mb-0">ডেলিভারি টাইম</h6>
                                </th>
                            </tr>
                            </thead>
                            <tbody>

                            <?php $sql = "SELECT * from servercopy_sub where user = (:email) AND status != 0 ORDER BY id DESC;";

                            $query = $dbh->prepare($sql);
                            $query->bindParam(':email', $_SESSION['alogin'], PDO::PARAM_STR);

                            $query->execute();
                            $results = $query->fetchAll(PDO::FETCH_OBJ);
                            if ($query->rowCount() > 0) {
                                foreach ($results as $result) {
                            ?>
                                    <tr>
                                        <td class="border-bottom-0"><h6 class="fw-semibold mb-0"><?php echo htmlentities($result->id); ?></h6></td>
                                        <td class="border-bottom-0">
                                            <h6 class="fw-semibold mb-1"><?php echo htmlentities($result->name); ?></h6>
                                            <span class="fw-normal"><?php echo htmlentities($result->nid_num); ?></span>                          
                                        </td>
                                        <td class="border-bottom-0">
                                            <p class="mb-0 fw-normal"><?php echo htmlentities($result->order_time); ?></p>
                                        </td>
                                        
                                        <td class="border-bottom-0">
                                            <h6 class="fw-semibold mb-0 fs-4">

                                                <?php
                                                    // if ($result->status == 1) {
                                                    //     echo "<p class='btn btn-warning'>Processing</p>";
                                                    // } elseif ($result->status == 0) {
                                                    //     echo "<p class='btn btn-danger'>Deleted</p>";
                                                    // } elseif ($result->status == 2) {
                                                    //     echo "<a href='lib/downloadServer.php?orderId=$result->id' class='btn btn-success'>Download</a>";
                                                    // } elseif ($result->status == 3) {
                                                    //     echo "<p class='btn btn-danger'>খুজে পাওয়া যায়নি।</p>";
                                                    // }
                                                ?>
                                                
                                                <?php 
                                                    if($result->status == 2){
        														      
        									        $file_path = 'servercopies/'.$result->sign_copy;
        								                echo '<a href="'.$file_path.'" download="'.$result->sign_copy.'"><i class="fa-solid fa-download"></i></a>&nbsp;&nbsp;';
        									        }
        									        else if($result->status == 1){
        									            echo '<p class="text-warning">Your Request is Pending</p>';
        									        }
        									        else if($result->status == 3){
        									            echo '<p class="text-danger">'.htmlentities($result->comments).'</p>';
        									        }
    									        ?>

                                            </h6>
                                        </td>
                                        <td class="border-bottom-0">
                                            <p class="mb-0 fw-normal"><?php echo htmlentities($result->total_download); ?></p>
                                        </td>
                                        <td class="border-bottom-0">
                                            <p class="mb-0 fw-normal"><?php if ($result->delivery_time == '') {
                                                    echo "ডেলিভারি করা হয়নি";
                                                } else {
                                                    echo $result->delivery_time;
                                                } ?></p>
                                        </td>
                                    </tr>
							<?php
								}
							} ?>

                             
                                                
                            </tbody>
                        </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/sidebarmenu.js"></script>
    <script src="assets/js/app.min.js"></script>
    <script src="assets/libs/simplebar/dist/simplebar.js"></script>


    
</body>

</html>