<?php $title = 'Share Form: ' . htmlspecialchars($form->title); ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-share-alt mr-2"></i>Share: <?= htmlspecialchars($form->title) ?></h1>
    <a href="<?= $baseUrl ?>/forms" class="glass-btn"><i class="fas fa-arrow-left mr-1"></i>Back</a>
</div>

<div class="row">
    <div class="col-lg-7">
        <div class="glass-card mb-4">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-link mr-2"></i>Generate Share Link</h5>
            <form method="POST" action="<?= $baseUrl ?>/forms/<?= $form->id ?>/share/generate">
                <div class="row">
                    <div class="col-md-6 form-group">
                        <label style="color: rgba(255,255,255,0.7);">Expires in</label>
                        <select name="expires_in" class="form-control glass-input">
                            <option value="">Never</option>
                            <option value="+1 day">1 Day</option>
                            <option value="+7 days">7 Days</option>
                            <option value="+30 days">30 Days</option>
                            <option value="+90 days">90 Days</option>
                        </select>
                    </div>
                    <div class="col-md-6 form-group">
                        <label style="color: rgba(255,255,255,0.7);">Share with member (optional)</label>
                        <select name="shared_with" class="form-control glass-input">
                            <option value="">Anyone with link</option>
                            <?php if (!empty($members)): ?>
                                <?php foreach ($members as $m): ?>
                                <option value="<?= $m->id ?>"><?= htmlspecialchars($m->name) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label style="color: rgba(255,255,255,0.7);">Message (optional)</label>
                    <textarea name="message" class="form-control glass-input" rows="2" placeholder="Include a personal message..."></textarea>
                </div>
                <button type="submit" class="glass-btn glass-btn-primary"><i class="fas fa-link mr-1"></i>Generate Link</button>
            </form>
        </div>

        <?php if (!empty($shares)): ?>
        <div class="glass-card">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-history mr-2"></i>Generated Links</h5>
            <?php foreach ($shares as $share): ?>
            <div class="mb-3 p-3" style="background: rgba(255,255,255,0.03); border-radius: 8px; border: 1px solid rgba(255,255,255,0.06);">
                <div class="d-flex justify-content-between align-items-start">
                    <div>
                        <code style="color: #1cc88a; word-break: break-all;"><?= $baseUrl ?>/forms/shared/<?= htmlspecialchars($share->token) ?></code>
                        <div style="font-size: 0.8rem; color: rgba(255,255,255,0.4); margin-top: 4px;">
                            <i class="fas fa-user mr-1"></i><?= htmlspecialchars($share->shared_by_name ?? 'Unknown') ?>
                            <?php if ($share->expires_at): ?>
                                · <i class="fas fa-clock mr-1"></i>Expires <?= date('M d, Y', strtotime($share->expires_at)) ?>
                            <?php endif; ?>
                            <?php if ($share->shared_with): ?>
                                · <i class="fas fa-user-tag mr-1"></i>Specific member
                            <?php endif; ?>
                            · <?= date('M d, Y H:i', strtotime($share->created_at)) ?>
                        </div>
                        <button class="glass-btn btn-sm mt-1" style="font-size: 0.75rem;" onclick="navigator.clipboard.writeText('<?= $baseUrl ?>/forms/shared/<?= htmlspecialchars($share->token) ?>');$(this).text('Copied!');setTimeout(()=>$(this).html('<i class=\'fas fa-copy mr-1\'></i>Copy'),2000);"><i class="fas fa-copy mr-1"></i>Copy</button>
                        <button class="glass-btn btn-sm mt-1" style="font-size: 0.75rem;" data-toggle="modal" data-target="#sendShareModal<?= $share->id ?>"><i class="fas fa-envelope mr-1"></i>Send</button>
                    </div>
                    <form method="POST" action="<?= $baseUrl ?>/forms/share/<?= $share->id ?>/revoke" style="display:inline;" onsubmit="return confirm('Revoke this share link?');">
                        <button type="submit" class="btn btn-sm" style="color: #e74a3b;" title="Revoke"><i class="fas fa-ban"></i></button>
                    </form>
                </div>
            </div>

            <!-- Send Share Modal -->
            <div class="modal fade" id="sendShareModal<?= $share->id ?>" tabindex="-1">
                <div class="modal-dialog"><div class="modal-content glass-modal-content">
                    <form method="POST" action="<?= $baseUrl ?>/forms/<?= $form->id ?>/share/<?= $share->token ?>/send">
                        <div class="modal-header"><h5 class="modal-title">Send Share Link</h5><button type="button" class="close" data-dismiss="modal">&times;</button></div>
                        <div class="modal-body">
                            <p style="color: rgba(255,255,255,0.7);">Notify members about this shared form.</p>
                            <div class="form-group">
                                <label style="color: rgba(255,255,255,0.7);">Recipients</label>
                                <select name="recipients[]" class="form-control glass-input" multiple size="5">
                                    <?php if (!empty($members)): ?>
                                        <?php foreach ($members as $m): ?>
                                        <option value="<?= $m->id ?>"><?= htmlspecialchars($m->name) ?> (<?= htmlspecialchars($m->email) ?>)</option>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </select>
                                <small style="color: rgba(255,255,255,0.4);">Hold Ctrl to select multiple</small>
                            </div>
                            <div class="form-group">
                                <label style="color: rgba(255,255,255,0.7);">Personal Message</label>
                                <textarea name="personal_message" class="form-control glass-input" rows="2" placeholder="Optional message"></textarea>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="glass-btn" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="glass-btn glass-btn-primary">Send</button>
                        </div>
                    </form>
                </div></div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-lg-5">
        <div class="glass-card">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-info-circle mr-2"></i>About Sharing</h5>
            <ul style="color: rgba(255,255,255,0.7); font-size: 0.9rem; padding-left: 1.2rem;">
                <li class="mb-2">Share links allow anyone with the link to access and submit the form.</li>
                <li class="mb-2">You can set an expiry date for the link.</li>
                <li class="mb-2">Optionally restrict to a specific member.</li>
                <li class="mb-2">Send the link directly to members via the internal messaging system.</li>
                <li class="mb-2">Revoke a link at any time to disable it.</li>
            </ul>
        </div>
    </div>
</div>
