<?php 
session_start();
if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == FALSE) {
    header("Location: " . $config_basedir."../admin/adminlogin.php");
}

require_once("header.php");





if (isset($_GET['del']) && is_numeric($_GET['del'])) {
    $delId = (int) $_GET['del']; // simple integer cast to ensure it's safe

   
        $stmt = $pdo->prepare("DELETE FROM copy WHERE id = ?");
        $stmt->execute([$delId]);

        if ($stmt->rowCount() > 0) {
            $delete = 1;
             echo "<script>
                    alert('copy Plan deleted successfully!');
                    window.location.href = '';
                </script>";
        }
    
}

?>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  <style>
    .button-group {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 10px; /* Space between buttons */
    }

    .button-group button {
      padding: 10px 20px;
      font-size: 16px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #f5f5f5;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    .button-group button:hover {
      background-color: #ddd;
    }

    .button-group button:active {
      background-color: #bbb;
    }
  </style>



<?php

if (isset($_POST['add_expert'])) {
    try {
        // Sanitize user input
        $name = htmlspecialchars($_POST['name']);
        $followers = htmlspecialchars($_POST['followers']);
        $position = htmlspecialchars($_POST['position']);
        $min_amt = htmlspecialchars($_POST['min_amt']);
        $max_amt = htmlspecialchars($_POST['max_amt']);
        $amount = htmlspecialchars($_POST['amount']);
        $tot_profit = htmlspecialchars($_POST['tot_profit']);
        $perc_profit = htmlspecialchars($_POST['perc_profit']);
        $rating = htmlspecialchars($_POST['rating']);
        $status = htmlspecialchars($_POST['status']);
        $pack_id = htmlspecialchars($_POST['pack_id']);

        // Handle image upload
        $maxsize = 104857600; // 100MB
        $target_dir = "accounts/upload/copy/";
        $target_file = $target_dir . basename($_FILES["imgfile2"]["name"]);
        $extension = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        $extensions_arr = ["jpg", "png", "jpeg", "gif", "jfif"];

        if (in_array($extension, $extensions_arr)) {
            if ($_FILES['imgfile2']['size'] < $maxsize) {
                $sourcepath = $_FILES["imgfile2"]["tmp_name"];
                $targetpath = "../accounts/upload/copy/" . $_FILES["imgfile2"]["name"];
                if (move_uploaded_file($sourcepath, $targetpath)) {
                    $Pack_location2 = "accounts/upload/copy/" . $_FILES['imgfile2']['name'];

                    // Prepare SQL query to insert expert
                    $stmt = $pdo->prepare("INSERT INTO copy (name, followers, position, min_amt, max_amt, amount, tot_profit, perc_profit, rating, Pack_location, status, pack_id)
                                           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                    $stmt->execute([$name, $followers, $position, $min_amt, $max_amt, $amount, $tot_profit, $perc_profit, $rating, $Pack_location2, $status, $pack_id]);

                    echo "<script>
                    alert('Copy Plan uploaded successfully!');
                    window.location.href = '';
                </script>";
                } else {
                    throw new Exception("File upload failed.");
                }
            } else {
                throw new Exception("File too large. File must be less than 100MB.");
            }
        } else {
            throw new Exception("Invalid file type. Only jpg, png, jpeg, gif, jfif are allowed.");
        }
    } catch (Exception $e) {
        echo "<div class='alert alert-danger'>" . $e->getMessage() . "</div>";
    }
}


if (isset($_POST['update_photo'])) {
    try {
        $id = htmlspecialchars($_POST['id']);
        $pack_id = htmlspecialchars($_POST['pack_id']);

        // Handle image upload
        $maxsize = 104857600; // 100MB
        $target_dir = "accounts/upload/copy/";
        $target_file = $target_dir . basename($_FILES["imgfile"]["name"]);
        $extension = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        $extensions_arr = ["jpg", "png", "jpeg", "gif", "jfif"];

        if (in_array($extension, $extensions_arr)) {
            if ($_FILES['imgfile']['size'] < $maxsize) {
                $sourcepath = $_FILES["imgfile"]["tmp_name"];
                $targetpath = "../accounts/upload/copy/" . $_FILES["imgfile"]["name"];
                if (move_uploaded_file($sourcepath, $targetpath)) {
                    $Pack_location = "accounts/upload/copy/" . $_FILES['imgfile']['name'];

                    // Prepare SQL query to update expert photo
                    $stmt = $pdo->prepare("UPDATE copy SET Pack_location = ? WHERE id = ?");
                    $stmt->execute([$Pack_location, $id]);

                    echo "<script>
                    alert('Copy photo updated successfully!');
                    window.location.href = '';
                </script>";
                } else {
                    throw new Exception("File upload failed.");
                }
            } else {
                throw new Exception("File too large. File must be less than 100MB.");
            }
        } else {
            throw new Exception("Invalid file type. Only jpg, png, jpeg, gif, jfif are allowed.");
        }
    } catch (Exception $e) {
        echo "<div class='alert alert-danger'>" . $e->getMessage() . "</div>";
    }
}
?>

<!-- HTML Form for Adding, Updating Expert or Photo -->
<!-- You can add the HTML forms for adding, updating expert, and updating photo here -->


   



        <div wire:id="0ho7JEz65LVMGIzD4ceZ" wire:initial-data="{&quot;fingerprint&quot;:{&quot;id&quot;:&quot;0ho7JEz65LVMGIzD4ceZ&quot;,&quot;name&quot;:&quot;admin.manage-users&quot;,&quot;locale&quot;:&quot;en&quot;,&quot;path&quot;:&quot;admin\/dashboard\/manageusers&quot;,&quot;method&quot;:&quot;GET&quot;,&quot;v&quot;:&quot;acj&quot;},&quot;effects&quot;:{&quot;listeners&quot;:[],&quot;path&quot;:&quot;https:\/\/xtbtrading.live\/trade\/admin\/dashboard\/manageusers&quot;},&quot;serverMemo&quot;:{&quot;children&quot;:[],&quot;errors&quot;:[],&quot;htmlHash&quot;:&quot;ed570425&quot;,&quot;data&quot;:{&quot;pagenum&quot;:10,&quot;searchvalue&quot;:&quot;&quot;,&quot;orderby&quot;:&quot;id&quot;,&quot;orderdirection&quot;:&quot;desc&quot;,&quot;selectPage&quot;:false,&quot;selectAll&quot;:false,&quot;checkrecord&quot;:[],&quot;selected&quot;:&quot;&quot;,&quot;action&quot;:&quot;Delete&quot;,&quot;username&quot;:null,&quot;fullname&quot;:null,&quot;email&quot;:null,&quot;password&quot;:null,&quot;message&quot;:null,&quot;subject&quot;:null,&quot;plan&quot;:null,&quot;datecreated&quot;:null,&quot;topamount&quot;:null,&quot;toptype&quot;:null,&quot;topcolumn&quot;:&quot;Bonus&quot;,&quot;page&quot;:1,&quot;paginators&quot;:{&quot;page&quot;:1}},&quot;dataMeta&quot;:[],&quot;checksum&quot;:&quot;9e82a6adabccc53607a584c57da8f89b4130352eb62c7aa9cc50f9455ca3c494&quot;}}">
    <div class="main-panel">
        <div class="content bg-light">
            <div class="page-inner">
                <div class="mt-2 mb-4">
                    <h1 class="title1 text-dark">COPY EXPERT</h1>
                </div>
                <div>
    </div>                <div>
    </div>                
               
               
               
               
                    <div class="mb-5 row">
                    <div class="col-md-12 ">
                 <!--<div class="card shadow p-4 bg-light">-->
                           
                           
                                                
                  
<?php 
if(isset($success_copy)){
    echo $success_copy;
}
?>

   
      
            <?php 
if(isset($edit_copy)){
    echo $edit_copy;
}
?>




            <?php 
if(isset($edit_photo)){
    echo $edit_photo;
}
?>
       
      
                         
                            
                            
                            
                            
                            
                            
                          


        
            
            
            
             
         
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 0;
            box-sizing: border-box;
        }

        .message {
            background-color: #f0f0f0;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 10px;
            max-width: 100%;
            word-wrap: break-word;
            position: relative;
        }

        .hidden-text {
            display: none;
        }

        .view-more {
            color: #007bff;
            cursor: pointer;
            font-size: 14px;
        }

        .view-more:hover {
            text-decoration: underline;
        }
    </style>

    
    
  <?php



// Handle form submission for updating expert info
if (isset($_POST['update_expert'])) {
    try {
        // Sanitize inputs and assign to variables
        $id = $_POST['id'];
        $name = htmlspecialchars($_POST['name']);
        $followers = htmlspecialchars($_POST['followers']);
        $position = htmlspecialchars($_POST['position']);
        $perc_profit = htmlspecialchars($_POST['perc_profit']);
        $tot_profit = htmlspecialchars($_POST['tot_profit']);
        $min_amt = htmlspecialchars($_POST['min_amt']);
        $max_amt = htmlspecialchars($_POST['max_amt']);
        $amount = htmlspecialchars($_POST['amount']);
        $pack_id = htmlspecialchars($_POST['pack_id']);
        $rating = $_POST['rating'];
        $status = $_POST['status'];

        // Update expert data in the database
        $stmt = $pdo->prepare("UPDATE copy SET name = ?, followers = ?, position = ?, perc_profit = ?, tot_profit = ?, 
                               min_amt = ?, max_amt = ?, amount = ?, pack_id = ?, rating = ?, status = ? WHERE id = ?");
        $stmt->execute([$name, $followers, $position, $perc_profit, $tot_profit, $min_amt, $max_amt, $amount, $pack_id, $rating, $status, $id]);

      echo "<script>
                    alert('Copy info updated successfully!');
                    window.location.href = '';
                </script>";

    } catch (PDOException $e) {
        echo "<div class='alert alert-danger'>" . $e->getMessage() . "</div>";
    }
}



// Fetch all expert data from the database
try {
    $stmt = $pdo->query("SELECT * FROM copy ORDER BY id ASC");
    $experts = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "<div class='alert alert-danger'>Error fetching data: " . $e->getMessage() . "</div>";
}

foreach ($experts as $row) {
    echo "
    <div class='message'>
        <span class='visible-text'>
            <div class='form-group'>
                <label class='form-control'>EXPERT NAME</label>
                <input type='text' class='form-control bg-light text-dark' name='name' value='" . $row['name'] . "' required/><br>
            </div>
        </span>

        <span class='hidden-text'>
            <form method='POST' enctype='multipart/form-data' action=''>
                <input type='hidden' class='form-control bg-light text-dark' name='id' value='" . $row['id'] . "'>
                <div class='form-group'>
                    <label class='form-control'>EXPERT NAME</label>
                    <input type='text' class='form-control bg-light text-dark' name='name' value='" . $row['name'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>FOLLOWERS</label>
                    <input type='text' class='form-control bg-light text-dark' name='followers' value='" . $row['followers'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>POSITION</label>
                    <input type='text' class='form-control bg-light text-dark' name='position' value='" . $row['position'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>PERCENTAGE PROFIT</label>
                    <input type='text' class='form-control bg-light text-dark' name='perc_profit' value='" . $row['perc_profit'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>TOTAL PROFIT</label>
                    <input type='text' class='form-control bg-light text-dark' name='tot_profit' value='" . $row['tot_profit'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>MINIMUM PRICE</label>
                    <input type='text' class='form-control bg-light text-dark' name='min_amt' value='" . $row['min_amt'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>MAXIMUM PRICE</label>
                    <input type='text' class='form-control bg-light text-dark' name='max_amt' value='" . $row['max_amt'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>STARTUP CAPITAL</label>
                    <input type='text' class='form-control bg-light text-dark' name='amount' value='" . $row['amount'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>PACK ID</label>
                    <input type='text' class='form-control bg-light text-dark' name='pack_id' value='" . $row['pack_id'] . "' required/><br>
                </div>
                <div class='form-group'>
                    <label class='form-control'>RATING</label>
                    <select name='rating' class='form-control bg-light text-dark' required>
                        <option value='" . $row['rating'] . "' selected>" . $row['rating'] . " star</option>
                        <option value='1'>1 star</option>
                        <option value='2'>2 star</option>
                        <option value='3'>3 star</option>
                        <option value='4'>4 star</option>
                        <option value='5'>5 star</option>
                    </select>
                </div>
                <div class='form-group'>
                    <label class='form-control'>STATUS</label>
                    <select name='status' class='form-control bg-light text-dark' required>
                        <option value='" . $row['status'] . "' selected>" . ($row['status'] == 1 ? "EXPERT IS ACTIVE" : "EXPERT NOT ACTIVE") . "</option>
                        <option value='1'>ACTIVATE EXPERT</option>
                        <option value='0'>DEACTIVATE EXPERT</option>
                    </select>
                </div>
                <div class='button-group small button'>
                    <a href='copy.php?del=" . $row['id'] . "' class='btn btn-danger'>DELETE EXPERT</a>
                    <button type='submit' class='btn btn-success' name='update_expert'>UPDATE INFO</button>
                </div>
            </form>

            <form method='POST' enctype='multipart/form-data' action=''>
                <div class='form-group'>
                    <input type='file' class='form-control bg-light text-dark' name='imgfile' accept='image/*' required/>
                    <input type='hidden' class='form-control bg-light text-dark' name='id' value='" . $row['id'] . "'>
                    <label class='form-control'>EXPERT PIX 
                        <img src='../" . $row['Pack_location'] . "' width='40px' height='40px' class='rounded-circle'>
                        <button type='submit' class='btn btn-info' name='update_photo'>CHANGE</button>
                    </label>
                </div>
            </form>
        </span>
        <span class='btn btn-primary' onclick='toggleViewMore(this)'>View More</span>
    </div><hr>";
}
?>











<script>
    function toggleViewMore(element) {
        const message = element.parentElement;
        const hiddenText = message.querySelector('.hidden-text');
        const visibleText = message.querySelector('.visible-text');

        if (hiddenText.style.display === 'none' || hiddenText.style.display === '') {
            hiddenText.style.display = 'inline';
            visibleText.style.display = 'none';
            element.textContent = 'View Less';
        } else {
            hiddenText.style.display = 'none';
            visibleText.style.display = 'inline';
            element.textContent = 'View More';
        }
    }
</script>

            
            
            
            
            
            
          
   
    
    
    
    
    
      <hr>
      

     
     

    
    
                      
                       <center>ADD MORE EXPERT TRADERS</center>
            
           
            
            
            
          <form method='POST'  enctype='multipart/form-data' action=''> 
          
          
           <input class="form-control border-primary" type="hidden" name="pack_id" value="<?php echo $pack_id; ?>" readonly>      
              
            
            
             <div class='form-group'>
                        <label class='form-control' >EXPERT NAME</label>
              <input type='text' class='form-control bg-light text-dark' name='name' placeholder="" required/><br>
            </div>
             
             
             <div class='form-group'>
                        <label class='form-control' >FOLLOWERS</label>
              <input type='text' class='form-control bg-light text-dark' name='followers'  placeholder="" required/><br>
              </div>
              
              
              
              <div class='form-group'>
                        <label class='form-control' >POSITION</label>
              <input type='text' class='form-control bg-light text-dark' name='position'  placeholder="" required/><br>
              </div>
              
              
              <div class='form-group'>
                        <label class='form-control' >PERCENTAGE PROFIT</label>
              <input type='text' class='form-control bg-light text-dark' name='perc_profit'  placeholder="" required/><br>
              </div>
            
            
            
            <div class='form-group'>
                        <label class='form-control' >TOTAL PROFIT</label>
              <input type='text' class='form-control bg-light text-dark' name='tot_profit'  placeholder="" required/><br>
              </div>
            
            
            
            
               <div class='form-group'>
                        <label class='form-control' >MINIMUM PRICE</label>
            <input type='text' class='form-control bg-light text-dark' name='min_amt'   placeholder=""  required/><br>
            </div>
            
            
             <div class='form-group'>
                        <label class='form-control' >MAXIMUM PRICE</label>
            <input type='text' class='form-control bg-light text-dark' name='max_amt'   placeholder=""  required/><br>
            </div>
            
            
            
             <div class='form-group'>
                        <label class='form-control' >STARTUP CAPITAL</label>
            <input type='text' class='form-control bg-light text-dark' name='amount'   placeholder=""  required/><br>
            </div>
            
           
             
            
            
            
            <div class='form-group'>
<label  class='form-control'>STATUS</label>
           <select name='status' class='form-control bg-light text-dark' required/>
                <option value=''>SELECT STATUS</option>
                <option value='1'>ACTIVATE</option>
                <option value='0'>DI-ACTIVATE</option>
            </select>
            </div>
            
            
            
            
            <div class='form-group'>
                        <label class='form-control' >RATING</label>
           <select name='rating' class='form-control bg-light text-dark' required/>
<option value='1'>1 star</option>
                <option value='2'>2 star</option>
                <option value='3'>3 star</option>
                <option value='4'>4 star</option>
                <option value='5'>5 star</option>
            </select>
            </div>
                      
     
     
    
    
    <div class='form-group'>
                        <label class='form-control' >UPLOAD EXPERT PIX </label>
             <input type='file' class='form-control bg-light text-dark' name='imgfile2' accept='image/*'  />   
            </div> 
             
     

    <button type="submit" class="btn btn-success btn-block btn-theme-colored2 text-red btn-lg btn-flat" name="add_expert">ADD EXPERT</button>

            </form>          
            
            
            
            
            

 
            
        </div></div></div>
<!--=====================================-->

</div>

 <?php
require ("footer.php")
?>

