<?php $__env->startSection('content'); ?>
<div class="grid gap-6 xl:grid-cols-2">
    
    <div class="trade-card">
        <h2 class="mb-2 text-xl font-bold text-white">FlatTrade API Credentials</h2>
        <p class="mb-4 text-sm text-slate-400">
            <?php if($isAdminFeed): ?>
                As admin, your saved credentials are used for the <strong class="text-white">shared WebSocket market feed</strong> (ticks, option chain, Greeks) for all users.
            <?php else: ?>
                Your credentials are used only for <strong class="text-white">your orders, balance, positions and P&L</strong>. Market data comes from the admin feed.
            <?php endif; ?>
        </p>

        <form method="POST" action="<?php echo e(route('broker.credentials')); ?>" class="space-y-4">
            <?php echo csrf_field(); ?>
            <div>
                <label class="mb-1 block text-sm text-slate-400">API Key</label>
                <input type="text" name="api_key" value="<?php echo e(old('api_key', $broker?->api_key)); ?>"
                       class="w-full rounded-xl border-trade-border bg-trade-panel px-4 py-2.5 text-white"
                       placeholder="Your FlatTrade API Key" required>
            </div>
            <div>
                <label class="mb-1 block text-sm text-slate-400">API Secret</label>
                <input type="password" name="api_secret"
                       class="w-full rounded-xl border-trade-border bg-trade-panel px-4 py-2.5 text-white"
                       placeholder="<?php echo e($broker?->api_secret ? '•••••••• (saved — enter to replace)' : 'Your FlatTrade API Secret'); ?>"
                       <?php echo e($broker?->api_secret ? '' : 'required'); ?>>
                <?php if($broker?->api_secret): ?>
                    <p class="mt-1 text-xs text-slate-500">Leave blank to keep existing secret, or enter a new one.</p>
                <?php endif; ?>
            </div>
            <button type="submit" class="trade-btn-primary">Save Credentials</button>
        </form>

        <p class="mt-4 text-xs text-slate-500">
            Redirect URL for FlatTrade API settings:
            <code class="text-emerald-300"><?php echo e(config('flattrade.redirect_uri')); ?></code>
        </p>
    </div>

    
    <div class="trade-card">
        <h2 class="mb-4 text-xl font-bold text-white">Connection &amp; Synced Account</h2>

        <div class="rounded-xl border border-trade-border bg-trade-panel p-4">
            <div class="flex items-center justify-between">
                <div>
                    <div class="text-sm text-slate-400">Status</div>
                    <div class="text-lg font-semibold <?php echo e($broker?->is_connected ? 'text-emerald-400' : 'text-rose-400'); ?>">
                        <?php echo e($broker?->is_connected ? 'Connected' : 'Not Connected'); ?>

                    </div>
                </div>
                <?php if($broker?->client_id): ?>
                    <div class="text-right text-sm text-slate-400">
                        <div>Client: <?php echo e($broker->client_id); ?></div>
                        <?php if($broker->is_market_feed_provider): ?>
                            <div class="mt-1 text-xs text-trade-accent">Market feed provider</div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
            <?php if($broker?->token_expires_at): ?>
                <div class="mt-2 text-xs text-slate-500">Token expires: <?php echo e($broker->token_expires_at->format('d M Y H:i')); ?></div>
            <?php endif; ?>
            <?php if($broker?->last_synced_at): ?>
                <div class="text-xs text-slate-500">Last sync: <?php echo e($broker->last_synced_at->diffForHumans()); ?></div>
            <?php endif; ?>
        </div>

        <?php if($broker?->is_connected): ?>
            <div class="mt-4 grid grid-cols-2 gap-3 text-sm">
                <div class="rounded-xl bg-trade-panel p-3">
                    <div class="text-slate-500">Available Balance</div>
                    <div class="text-lg font-bold text-white">₹<?php echo e(number_format($broker->available_balance, 2)); ?></div>
                </div>
                <div class="rounded-xl bg-trade-panel p-3">
                    <div class="text-slate-500">Margin Used</div>
                    <div class="text-lg font-bold text-white">₹<?php echo e(number_format($broker->margin_used, 2)); ?></div>
                </div>
                <div class="rounded-xl bg-trade-panel p-3">
                    <div class="text-slate-500">Unrealized P&L</div>
                    <div class="text-lg font-bold <?php echo e($broker->unrealized_pnl_total >= 0 ? 'pnl-positive' : 'pnl-negative'); ?>">
                        ₹<?php echo e(number_format($broker->unrealized_pnl_total, 2)); ?>

                    </div>
                </div>
                <div class="rounded-xl bg-trade-panel p-3">
                    <div class="text-slate-500">Realized P&L</div>
                    <div class="text-lg font-bold <?php echo e($broker->realized_pnl_total >= 0 ? 'pnl-positive' : 'pnl-negative'); ?>">
                        ₹<?php echo e(number_format($broker->realized_pnl_total, 2)); ?>

                    </div>
                </div>
            </div>
        <?php endif; ?>

        <div class="mt-6 flex flex-wrap gap-3">
            <?php if($canConnect): ?>
                <a href="<?php echo e(route('broker.connect')); ?>" class="trade-btn-primary">Connect OAuth</a>
            <?php else: ?>
                <button disabled class="trade-btn-primary opacity-50 cursor-not-allowed">Connect OAuth</button>
            <?php endif; ?>
            <?php if($broker?->is_connected): ?>
                <form method="POST" action="<?php echo e(route('broker.sync')); ?>"><?php echo csrf_field(); ?><button type="submit" class="trade-btn-ghost">Sync Now</button></form>
                <form method="POST" action="<?php echo e(route('broker.disconnect')); ?>"><?php echo csrf_field(); ?><button type="submit" class="trade-btn-danger">Disconnect</button></form>
            <?php endif; ?>
        </div>

        <?php if($errors->has('broker')): ?>
            <div class="mt-4 rounded-xl border border-rose-500/30 bg-rose-500/10 p-4 text-sm text-rose-300"><?php echo e($errors->first('broker')); ?></div>
        <?php endif; ?>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.trading', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Users/vishal/Desktop/AutoTrade-FlatTrade/resources/views/trading/broker.blade.php ENDPATH**/ ?>