<?php
$conn=new PDO('mysql:host=localhost; dbname=omeixzen_vogue', 'omeixzen_vogue', 'vogueJan12345$12345') or die(mysql_error());

// Handle Deletion
if (isset($_GET['delete_id'])) {
    $id = intval($_GET['delete_id']);
    $stmt = $conn->prepare("SELECT fname FROM upload_ka WHERE id = ?");
    $stmt->execute([$id]);
    $row = $stmt->fetch();
    if ($row) {
        $file = "upload/" . $row['fname'];
        if (file_exists($file)) {
            unlink($file);
        }
        $conn->prepare("DELETE FROM upload_ka WHERE id = ?")->execute([$id]);
        header("Location: upload_asast.php");
        exit();
    }
}

// Handle Upload
if (isset($_POST['submit']) != "") {
    $name = $_FILES['file']['name'];
    $size = $_FILES['file']['size'];
    $type = $_FILES['file']['type'];
    $temp = $_FILES['file']['tmp_name'];
    $f_details = $_POST['file_details'];
    $fname = date("YmdHis") . '_' . $name;

    $chk = $conn->query("SELECT * FROM upload_ka WHERE name = '$name'")->rowCount();
    if ($chk) {
        $i = 1;
        $c = 0;
        while ($c == 0) {
            $i++;
            $reversedParts = explode('.', strrev($name), 2);
            $tname = (strrev($reversedParts[1])) . "_" . ($i) . '.' . (strrev($reversedParts[0]));
            $chk2 = $conn->query("SELECT * FROM upload_ka WHERE name = '$tname'")->rowCount();
            if ($chk2 == 0) {
                $c = 1;
                $name = $tname;
            }
        }
    }

    $move = move_uploaded_file($temp, "upload/" . $fname);
    if ($move) {
        $query = $conn->query("INSERT INTO upload_ka (name, fname, f_details) VALUES ('$name', '$fname', '$f_details')");
        if ($query) {
            header("Location: upload_kankor.php");
        } else {
            die(mysql_error());
        }
    }
}
?>

<?php include"header.php";?>
<?php include"sidebar.php";?>

<!-- ============================================================== -->
<!-- Start right Content here -->
<!-- ============================================================== -->
<div class="main-content">
 <div class="page-content">
       <div class="container-fluid">

                    <!-- start page title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                

                                <div class="page-title-right">
                                    
                                </div>

                            </div>
                        </div>
                    </div>
                    <!-- end page title -->


                    <div class="row">

                        <!--end col-->
                        <div class="col-xxl-9">
                            <div class="card mt-xxl-n5">
                                <div class="card-header">
                                    <ul class="nav nav-tabs-custom rounded card-header-tabs border-bottom-0" role="tablist">
                                        <li class="nav-item">
                                            <a class="nav-link active" data-bs-toggle="tab" href="#personalDetails" role="tab" aria-selected="false">
                                                <i class="fas fa-home"></i> اپلود مواد درسی کانکور
                                            </a>
                                        </li>


                                    </ul>
                                </div>





                                <div class="card-body p-4">
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="personalDetails" role="tabpanel">
                                     
              <form action="" method="post" enctype="multipart/form-data">
    <div class="row">

        <div class="col-lg-6">
            <div class="mb-3">
                <label>انتخاب فایل</label>
                <input type="file" class="form-control" name="file" id="file" required />
            </div>
        </div>

        <div class="col-lg-6">
            <div class="mb-3">
                <label>جزئیات فایل</label>
                <input type="text" class="form-control" name="file_details" placeholder="توضیحاتی در مورد فایل" required />
            </div>
        </div>

        <div class="col-12">
            <input type="submit" class="btn btn-primary" name="submit" value="آپلود" />
        </div>
    </div>
</form>

                                                        </div>
                                                    </div>
												<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered mt-4" id="example">
    <thead>
        <tr class="text-center">
            <th>نام فایل</th>
            <th>جزئیات</th>
            <th>دانلود</th>
            <th>حذف</th>
        </tr>
    </thead>
    <tbody class="text-center">
        <?php
        $query = $conn->query("SELECT * FROM upload_ka ORDER BY id DESC");
        while ($row = $query->fetch()) {
            $name = htmlspecialchars($row['name']);
            $details = htmlspecialchars($row['f_details']);
            $fname = $row['fname'];
            $id = $row['id'];
        ?>
        <tr>
            <td><?php echo $name; ?></td>
            <td><?php echo $details; ?></td>
            <td>
                <a class="btn btn-success btn-sm" href="download.php?filename=<?php echo urlencode($name); ?>&f=<?php echo urlencode($fname); ?>">دانلود</a>
            </td>
            <td>
                <a class="btn btn-danger btn-sm" href="?delete_id=<?php echo $id; ?>" onclick="return confirm('آیا مطمئن هستید که می‌خواهید حذف کنید؟');">حذف</a>
            </td>
        </tr>
        <?php } ?>
    </tbody>
</table>

                                                    <!--end col-->
                                                    
                                                    </div>
                                                    <!--end col-->
                                                </div>
                                                <!--end row-->
                                            </form>
                                        </div>
                                        <!--end tab-pane-->

                                        <!--end tab-pane-->

                                        <!--end tab-pane-->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--end col-->
                    </div>


                </div>
                <!-- container-fluid -->
            </div>
            <!-- End Page-content -->

            <?php include"footer.php";?>
