<?php
/**
 * testimonial_logic.php - CRUD with Optimized Admin UI
 */

if (isset($_POST['save_testimonial'])) {
    $id = $_POST['testi_id'];
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $designation = mysqli_real_escape_string($conn, $_POST['designation']);
    $comment = mysqli_real_escape_string($conn, $_POST['comment']);
    $rating = (int)$_POST['rating'];
    $status = $_POST['status'];
    $image_url = $_POST['image_url'];

    if ($id) {
        $query = "UPDATE testimonials SET name='$name', designation='$designation', comment='$comment', rating='$rating', status='$status', image='$image_url' WHERE id='$id'";
    } else {
        $query = "INSERT INTO testimonials (name, designation, comment, rating, status, image) VALUES ('$name', '$designation', '$comment', '$rating', '$status', '$image_url')";
    }
    
    if (mysqli_query($conn, $query)) { echo "<script>window.location='admin.php?view=testimonials';</script>"; }
}

if (isset($_GET['delete_testi'])) {
    $id = $_GET['delete_testi'];
    mysqli_query($conn, "DELETE FROM testimonials WHERE id='$id'");
    echo "<script>window.location='admin.php?view=testimonials';</script>";
}

$edit_data = null;
if (isset($_GET['edit_testi'])) {
    $id = $_GET['edit_testi'];
    $edit_data = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM testimonials WHERE id='$id'"));
}
?>

<div class="section-card">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px;">
        <h3 style="margin:0; color:#333;"><i class="fas fa-comment-dots" style="color:var(--primary);"></i> কাস্টমার টেস্টিনিমোনিয়াল</h3>
        <span style="font-size: 13px; color: #888;">গ্রাহকদের সুন্দর মতামতগুলো এখান থেকে ম্যানেজ করুন</span>
    </div>
    
    <div style="background: #ffffff; border: 1px solid #e0e0e0; border-radius: 12px; padding: 25px; margin-bottom: 35px; box-shadow: 0 4px 12px rgba(0,0,0,0.03);">
        <h4 style="margin-top:0; margin-bottom: 20px; font-size: 16px; border-bottom: 1px solid #eee; padding-bottom: 10px;">
            <i class="fas fa-plus-circle"></i> <?php echo $edit_data ? 'মতামত এডিট করুন' : 'নতুন মতামত যুক্ত করুন'; ?>
        </h4>
        <form method="POST">
            <input type="hidden" name="testi_id" value="<?php echo $edit_data['id'] ?? ''; ?>">
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px;">
                <div>
                    <label style="font-size:13px; font-weight:600; color:#555;">গ্রাহকের নাম</label>
                    <input type="text" name="name" value="<?php echo $edit_data['name'] ?? ''; ?>" required placeholder="যেমন: রেজা" style="width:100%; padding:10px; border:1px solid #ddd; border-radius:6px; margin-top:5px;">
                </div>
                <div>
                    <label style="font-size:13px; font-weight:600; color:#555;">পদবী / ঠিকানা</label>
                    <input type="text" name="designation" value="<?php echo $edit_data['designation'] ?? ''; ?>" placeholder="যেমন: নিউ মার্কেট" style="width:100%; padding:10px; border:1px solid #ddd; border-radius:6px; margin-top:5px;">
                </div>
                <div>
                    <label style="font-size:13px; font-weight:600; color:#555;">রেটিং</label>
                    <select name="rating" style="width:100%; padding:10px; border:1px solid #ddd; border-radius:6px; margin-top:5px;">
                        <?php for($i=5; $i>=1; $i--): ?>
                            <option value="<?php echo $i; ?>" <?php echo (isset($edit_data['rating']) && $edit_data['rating'] == $i) ? 'selected' : ''; ?>><?php echo $i; ?> Star</option>
                        <?php endfor; ?>
                    </select>
                </div>
            </div>
            
            <div style="margin-top:20px;">
                <label style="font-size:13px; font-weight:600; color:#555;">গ্রাহকের মন্তব্য</label>
                <textarea name="comment" rows="3" required placeholder="গ্রাহক কি বলছেন তা এখানে লিখুন..." style="width:100%; padding:10px; border:1px solid #ddd; border-radius:6px; margin-top:5px; font-family: inherit;"><?php echo $edit_data['comment'] ?? ''; ?></textarea>
            </div>

            <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 20px; margin-top:20px;">
                <div>
                    <label style="font-size:13px; font-weight:600; color:#555;">ছবির ইউআরএল (Media থেকে লিংক কপি করে দিন)</label>
                    <input type="text" name="image_url" value="<?php echo $edit_data['image'] ?? ''; ?>" placeholder="https://yourdomain.com/uploads/user.png" style="width:100%; padding:10px; border:1px solid #ddd; border-radius:6px; margin-top:5px;">
                </div>
                <div>
                    <label style="font-size:13px; font-weight:600; color:#555;">স্ট্যাটাস</label>
                    <select name="status" style="width:100%; padding:10px; border:1px solid #ddd; border-radius:6px; margin-top:5px;">
                        <option value="Active" <?php echo (isset($edit_data['status']) && $edit_data['status'] == 'Active') ? 'selected' : ''; ?>>Active (Visible)</option>
                        <option value="Inactive" <?php echo (isset($edit_data['status']) && $edit_data['status'] == 'Inactive') ? 'selected' : ''; ?>>Inactive (Hidden)</option>
                    </select>
                </div>
            </div>

            <div style="margin-top:25px; display: flex; gap: 10px;">
                <button type="submit" name="save_testimonial" class="btn" style="background:var(--primary); color:white; padding:12px 30px; border-radius:6px; font-weight:600;">
                    <i class="fas fa-save"></i> <?php echo $edit_data ? 'পরিবর্তন সেভ করুন' : 'মতামত পাবলিশ করুন'; ?>
                </button>
                <?php if($edit_data): ?>
                    <a href="admin.php?view=testimonials" class="btn" style="background:#eee; color:#666; padding:12px 30px; border-radius:6px; text-decoration:none; display:inline-block;">বাতিল</a>
                <?php endif; ?>
            </div>
        </form>
    </div>

    <div style="overflow-x: auto;">
        <table style="border: 1px solid #eee;">
            <thead>
                <tr>
                    <th style="width: 80px;">ছবি</th>
                    <th>নাম ও ঠিকানা</th>
                    <th>মন্তব্য</th>
                    <th>রেটিং</th>
                    <th>স্ট্যাটাস</th>
                    <th style="text-align:right;">অ্যাকশন</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $res = mysqli_query($conn, "SELECT * FROM testimonials ORDER BY id DESC");
                while($row = mysqli_fetch_assoc($res)):
                ?>
                <tr>
                    <td><img src="<?php echo $row['image'] ?: 'https://via.placeholder.com/60'; ?>" style="width:50px; height:50px; border-radius:10px; object-fit:cover; border: 1px solid #ddd;"></td>
                    <td>
                        <div style="font-weight:700; color:#333;"><?php echo $row['name']; ?></div>
                        <div style="font-size:12px; color:#888;"><?php echo $row['designation']; ?></div>
                    </td>
                    <td style="max-width:350px;">
                        <div style="font-size:13px; color:#555; line-height:1.5; font-style: italic;">"<?php echo (strlen($row['comment']) > 120) ? substr($row['comment'], 0, 120).'...' : $row['comment']; ?>"</div>
                    </td>
                    <td><span style="color:#f39c12; font-weight:bold;"><i class="fas fa-star"></i> <?php echo $row['rating']; ?></span></td>
                    <td>
                        <span class="badge" style="background:<?php echo $row['status']=='Active'?'#e8f5e9':'#ffebee'; ?>; color:<?php echo $row['status']=='Active'?'#2e7d32':'#c62828'; ?>;">
                            <?php echo $row['status']; ?>
                        </span>
                    </td>
                    <td style="text-align:right;">
                        <a href="admin.php?view=testimonials&edit_testi=<?php echo $row['id']; ?>" style="color:var(--primary); font-size:18px; margin-right:15px;"><i class="fas fa-edit"></i></a>
                        <a href="admin.php?view=testimonials&delete_testi=<?php echo $row['id']; ?>" onclick="return confirm('এটি মুছে ফেলতে চান?')" style="color:var(--red); font-size:18px;"><i class="fas fa-trash-alt"></i></a>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>