<?php
/**
 * Mapping Row Partial View
 *
 * @var string $ngaugeField
 * @var string $magentoField
 * @var array $ngaugeFields
 * @var array $magentoFields
 */

use yii\helpers\Html;

// Find field details
$ngaugeFieldData = null;
$magentoFieldData = null;

foreach ($ngaugeFields as $category) {
    if (isset($category['fields'][$ngaugeField])) {
        $ngaugeFieldData = $category['fields'][$ngaugeField];
        break;
    }
}

foreach ($magentoFields as $category) {
    if (isset($category['fields'][$magentoField])) {
        $magentoFieldData = $category['fields'][$magentoField];
        break;
    }
}

// Build Magento field options
$magentoOptions = [];
foreach ($magentoFields as $category) {
    foreach ($category['fields'] as $fieldCode => $field) {
        $magentoOptions[$fieldCode] = $field['label'] . ($field['required'] ?? false ? ' *' : '');
    }
}

// Determine compatibility
$compatibility = 'Unknown';
$compatibilityClass = 'secondary';

if ($ngaugeFieldData && $magentoFieldData) {
    $ngaugeType = $ngaugeFieldData['type'];
    $magentoType = $magentoFieldData['type'];
    
    $compatibilityMap = [
        'text' => ['text', 'textarea', 'select'],
        'textarea' => ['text', 'textarea'],
        'number' => ['text', 'price', 'select'],
        'price' => ['price', 'text'],
        'select' => ['select', 'text'],
        'datetime' => ['text']
    ];
    
    if (isset($compatibilityMap[$ngaugeType]) && in_array($magentoType, $compatibilityMap[$ngaugeType])) {
        $compatibility = 'Compatible';
        $compatibilityClass = 'success';
    } else {
        $compatibility = 'Warning';
        $compatibilityClass = 'warning';
    }
}
?>

<tr data-ngauge-field="<?= Html::encode($ngaugeField) ?>" data-magento-field="<?= Html::encode($magentoField) ?>">
    <td>
        <strong><?= Html::encode($ngaugeFieldData['label'] ?? $ngaugeField) ?></strong>
        <small class="text-muted d-block"><?= Html::encode($ngaugeField) ?></small>
        <?php if ($ngaugeFieldData): ?>
            <span class="badge badge-outline-secondary badge-sm"><?= Html::encode($ngaugeFieldData['type']) ?></span>
        <?php endif; ?>
    </td>
    <td>
        <?= Html::dropDownList(
            'magento_field[' . $ngaugeField . ']',
            $magentoField,
            ['' => '-- Select Magento Field --'] + $magentoOptions,
            [
                'class' => 'form-control form-control-sm magento-field-select',
                'data-ngauge' => $ngaugeField
            ]
        ) ?>
        <?php if ($magentoFieldData): ?>
            <small class="text-muted">
                <?= Html::encode($magentoFieldData['label']) ?>
                <span class="badge badge-outline-success badge-sm ml-1"><?= Html::encode($magentoFieldData['type']) ?></span>
                <?php if ($magentoFieldData['required'] ?? false): ?>
                    <span class="text-danger ml-1">Required</span>
                <?php endif; ?>
            </small>
        <?php endif; ?>
    </td>
    <td>
        <span class="badge badge-<?= $compatibilityClass ?>"><?= $compatibility ?></span>
    </td>
    <td>
        <?= Html::button('<i class="fas fa-trash"></i>', [
            'class' => 'btn btn-sm btn-outline-danger remove-mapping',
            'data-ngauge' => $ngaugeField,
            'title' => 'Remove mapping'
        ]) ?>
    </td>
</tr>