<?php
$conn=new PDO('mysql:host=localhost; dbname=omeixzen_vogue', 'omeixzen_vogue', 'vogueJan12345$12345') or die(mysql_error());	
if(isset($_POST['submit'])!=""){
  $name=$_FILES['file']['name'];
  $size=$_FILES['file']['size'];
  $type=$_FILES['file']['type'];
  $temp=$_FILES['file']['tmp_name'];
  // $caption1=$_POST['caption'];
  // $link=$_POST['link'];
  $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]));
    // var_dump($tname);exit;
    	$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)values('$name','$fname')");
	if($query){
	header("location:upload_asast.php");
	}
	else{
	die(mysql_error());
	}
 }
}
?>
<?php include "header.php"; ?>
        <!-- ***** Breadcrumb Area Start ***** -->
        <section class="section breadcrumb-area overlay-dark d-flex align-items-center">
            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <!-- Breamcrumb Content -->
                        <div class="breadcrumb-content d-flex flex-column align-items-center text-center">
                            <h2 class="text-white text-uppercase mb-3">مواد درسی کانکور</h2>
                            <ol class="breadcrumb">
                                
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <!-- ***** Breadcrumb Area End ***** -->

        <!-- ***** About Area Start ***** -->
        
 
<!-- ***** About Area Start ***** -->
<section class="section about-area ptb_100">
    <div class="container">
        <div class="row justify-content-between align-items-center">
            <div class="col-12 col-lg-6">
                <!-- About Thumb -->
                <div class="about-thumb text-center">
                    <img src="assets/img/d_m.jpg" alt="">
                </div>
            </div>
            <div class="col-12 col-lg-6">
                <!-- About Content -->
                <div class="about-content section-heading text-center text-lg-left">

<style>
    #materialsTable {
        width: 100%;
        border-collapse: collapse;
        direction: rtl;
        font-family: 'Tahoma', sans-serif;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }

    #materialsTable thead {
        background-color: #2f3e9e;
        color: white;
    }

    #materialsTable th, #materialsTable td {
        padding: 12px 20px;
        text-align: center;
        vertical-align: middle;
        border-bottom: 1px solid #eaeaea;
    }

    #materialsTable tbody tr:hover {
        background-color: #f8f9fa;
    }

    .download-btn a {
        background-color: #198754;
        color: white;
        padding: 6px 12px;
        border-radius: 4px;
        text-decoration: none;
        font-size: 14px;
    }

    .download-btn a:hover {
        background-color: #157347;
    }

    .search-box {
        margin-bottom: 15px;
        direction: rtl;
        text-align: right;
    }

    .search-box input {
        width: 100%;
        padding: 10px 12px;
        font-size: 14px;
        border: 1px solid #ced4da;
        border-radius: 6px;
    }
</style>

<div class="search-box">
    <input type="text" id="searchInput" onkeyup="searchTable()" placeholder="جستجوی مواد درسی...">
</div>

<table id="materialsTable">
    <thead>
        <tr>
            <th>نام فایل</th>
            <th>توضیحات</th>
            <th>دانلود</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $query = $conn->query("SELECT * FROM upload_ka ORDER BY id DESC");
        while ($row = $query->fetch()) {
            $name = htmlspecialchars($row['name']);
            $f_details = htmlspecialchars($row['f_details']);
            $fname = htmlspecialchars($row['fname']);
        ?>
        <tr>
            <td><?php echo $name; ?></td>
            <td><?php echo $f_details; ?></td>
            <td class="download-btn">
                <a href="dashboard/download.php?filename=<?php echo urlencode($name); ?>&f=<?php echo urlencode($fname); ?>" target="_blank">دانلود</a>
            </td>
        </tr>
        <?php } ?>
    </tbody>
</table>

<script>
function searchTable() {
    const input = document.getElementById("searchInput");
    const filter = input.value.toLowerCase();
    const rows = document.querySelectorAll("#materialsTable tbody tr");

    rows.forEach(row => {
        const text = row.textContent.toLowerCase();
        row.style.display = text.includes(filter) ? "" : "none";
    });
}
</script>

                </div>
            </div>
        </div>
    </div>
</section>

  
        <!--====== Contact Area End ======-->

        <!--====== Call To Action Area Start ======-->
        <section class="section cta-area bg-overlay ptb_100">
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-12 col-lg-10">
                        <!-- Section Heading -->
                        <div class="section-heading text-center m-0">
                            <h2 class="text-white"><?php print $enquiry_title; ?></h2>
                            <p class="text-white d-none d-sm-block mt-4"><?php print $enquiry_text; ?></p>
                            <a href="contact" class="btn btn-bordered-white mt-4">Contact Us</a>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <!--====== Call To Action Area End ======-->
<?php include "footer.php"; ?>
