<?php
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model backend\models\Cartsbe */
?>

<div class="netsuite-integration-tab">
    <div class="row">
        <div class="col-md-12">
            <h4><?= Yii::t('app', 'NetSuite Integration Status') ?></h4>
            
            <?php 
            $ns = empty($model->netsuiteid) == false ? json_decode($model->netsuiteid, 1) : false;
            $hasNetsuiteId = !empty($model->netsuiteid);
            ?>
            
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h5><?= Yii::t('app', 'Sync Status') ?></h5>
                            <div class="form-group">
                                <label class="font-weight-bold"><?= Yii::t('app', 'NetSuite ID') ?>:</label>
                                <?php if ($hasNetsuiteId): ?>
                                    <div class="text-success">
                                        <i class="fas fa-check-circle"></i>
                                        <?php if (is_array($ns)): ?>
                                            <?php foreach ($ns as $key => $value): ?>
                                                <div>
                                                    <?= Html::a($key . ': ' . $value, "https://4465021.app.netsuite.com/app/accounting/transactions/salesord.nl?id={$value}", ['target' => '_blank', 'class' => 'text-primary']) ?>
                                                </div>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <?= Html::a($model->netsuiteid, "https://4465021.app.netsuite.com/app/accounting/transactions/salesord.nl?id={$model->netsuiteid}", ['target' => '_blank', 'class' => 'text-primary']) ?>
                                        <?php endif; ?>
                                    </div>
                                <?php else: ?>
                                    <div class="text-warning">
                                        <i class="fas fa-exclamation-triangle"></i>
                                        <?= Yii::t('app', 'Not synced to NetSuite') ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                            
                            <div class="form-group">
                                <label class="font-weight-bold"><?= Yii::t('app', 'Last Sync Attempt') ?>:</label>
                                <div><?= $model->netsuite_sync_date ?? Yii::t('app', 'Never') ?></div>
                            </div>
                            
                            <div class="form-group">
                                <label class="font-weight-bold"><?= Yii::t('app', 'Sync Status') ?>:</label>
                                <div>
                                    <?php if ($hasNetsuiteId): ?>
                                        <span class="badge badge-success"><?= Yii::t('app', 'Synced') ?></span>
                                    <?php else: ?>
                                        <span class="badge badge-warning"><?= Yii::t('app', 'Pending') ?></span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <h5><?= Yii::t('app', 'Sync Actions') ?></h5>
                            
                            <?php if ($model->billingstatus === 'paid' && (Yii::$app->user->identity->inGroup('Global Admins') || Yii::$app->user->identity->inGroup('Sales Admin'))): ?>
                                <div class="form-group">
                                    <?= Html::button(Yii::t('app', 'Send to NetSuite'), [
                                        'class' => 'btn btn-success',
                                        'id' => 'send-to-netsuite',
                                        'onclick' => 'sendToNetsuite("' . $model->id . '")',
                                        'disabled' => $hasNetsuiteId
                                    ]) ?>
                                </div>
                                
                                <div class="form-group">
                                    <?= Html::button(Yii::t('app', 'Link Existing NetSuite Order'), [
                                        'class' => 'btn btn-info',
                                        'id' => 'link-netsuite-order',
                                        'onclick' => 'linkNetsuiteOrder("' . $model->id . '")'
                                    ]) ?>
                                </div>
                                
                                <?php if ($hasNetsuiteId): ?>
                                <div class="form-group">
                                    <?= Html::button(Yii::t('app', 'Refresh NetSuite Status'), [
                                        'class' => 'btn btn-secondary',
                                        'id' => 'refresh-netsuite-status',
                                        'onclick' => 'refreshNetsuiteStatus("' . $model->id . '")'
                                    ]) ?>
                                </div>
                                <?php endif; ?>
                            <?php else: ?>
                                <div class="alert alert-warning">
                                    <?= Yii::t('app', 'Cart must be paid and you must have admin privileges to perform NetSuite actions.') ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <?php if ($hasNetsuiteId): ?>
    <div class="row mt-3">
        <div class="col-md-12">
            <h5><?= Yii::t('app', 'NetSuite Order Details') ?></h5>
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <tbody>
                                <tr>
                                    <th><?= Yii::t('app', 'Order Reference') ?>:</th>
                                    <td><?= $model->orderref ?></td>
                                </tr>
                                <tr>
                                    <th><?= Yii::t('app', 'NetSuite Status') ?>:</th>
                                    <td><span class="badge badge-success"><?= Yii::t('app', 'Synchronized') ?></span></td>
                                </tr>
                                <tr>
                                    <th><?= Yii::t('app', 'Total Value') ?>:</th>
                                    <td>£<?= number_format($model->total, 2) ?></td>
                                </tr>
                                <tr>
                                    <th><?= Yii::t('app', 'Customer Email') ?>:</th>
                                    <td><?= $model->email ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

<script>
function sendToNetsuite(cartId) {
    if (confirm('<?= Yii::t('app', 'Send this cart to NetSuite? This will create a new order.') ?>')) {
        $.ajax({
            url: '<?= Url::to(['/cart/sendtonetsuite']) ?>',
            type: 'POST',
            data: { id: cartId },
            success: function(response) {
                if (response.success) {
                    alert('<?= Yii::t('app', 'Cart sent to NetSuite successfully') ?>');
                    location.reload();
                } else {
                    alert('<?= Yii::t('app', 'Error sending to NetSuite') ?>: ' + response.message);
                }
            },
            error: function() {
                alert('<?= Yii::t('app', 'Network error occurred') ?>');
            }
        });
    }
}

function linkNetsuiteOrder(cartId) {
    var netsuiteId = prompt('<?= Yii::t('app', 'Enter the NetSuite Order ID to link:') ?>');
    if (netsuiteId) {
        $.ajax({
            url: '<?= Url::to(['/cart/linktons']) ?>',
            type: 'POST',
            data: { 
                id: cartId,
                netsuiteid: netsuiteId
            },
            success: function(response) {
                if (response.success) {
                    alert('<?= Yii::t('app', 'NetSuite order linked successfully') ?>');
                    location.reload();
                } else {
                    alert('<?= Yii::t('app', 'Error linking NetSuite order') ?>');
                }
            },
            error: function() {
                alert('<?= Yii::t('app', 'Network error occurred') ?>');
            }
        });
    }
}

function refreshNetsuiteStatus(cartId) {
    $.ajax({
        url: '<?= Url::to(['/cart/refresh-netsuite-status']) ?>',
        type: 'GET',
        data: { id: cartId },
        success: function(response) {
            if (response.success) {
                alert('<?= Yii::t('app', 'NetSuite status refreshed') ?>');
                location.reload();
            } else {
                alert('<?= Yii::t('app', 'Error refreshing status') ?>');
            }
        },
        error: function() {
            alert('<?= Yii::t('app', 'Network error occurred') ?>');
        }
    });
}
</script>