<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\DetailView;
use app\components\Utilities;
use yii\bootstrap\Modal;

$this->title = Yii::t('app', 'View');

$script =<<< JS
    $(document).ready(function(){
        $('.update-file-field').click(function(e){
            e.preventDefault();
            $('#update-file-field-modal').modal('show')
                .find('#update-file-field-modal-content')
                .load($(this).attr('value'));
        });

        $('.upload-file-field').click(function(e){
            e.preventDefault();
            $('#upload-file-field-modal').modal('show')
                .find('#upload-file-field-modal-content')
                .load($(this).attr('value'));
        });
    });
JS;
$this->registerJs($script);

?>

<div class="forms-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <?= Utilities::getSessionAlertContainer('could_not_update_file', 'danger') ?>
    <?= Utilities::getSessionAlertContainer('file_updated', 'success') ?>
    <?= Utilities::getSessionAlertContainer('could_not_delete_file', 'danger') ?>
    <?= Utilities::getSessionAlertContainer('file_deleted', 'success') ?>
    <?= Utilities::getSessionAlertContainer('could_not_upload_file', 'danger') ?>
    <?= Utilities::getSessionAlertContainer('file_uploaded', 'success') ?>

    <?php if(isset($record['isCompleted']) && ! $record['isCompleted']): ?>
        <div class="alert alert-warning">
            <span class="glyphicon glyphicon-info-sign"></span> <?= 'This record hasn\'t completed yet, so the shown fields aren\'t the full fields of record.' ?> 
            <?php if(Yii::$app->user->can('Admin') || Utilities::checkPermissionForForm($form_id, 'complete-data')): ?>
                <?= Html::a('(Complete It)', ['/campaigns/complete-record', 'campaign_id' => (string)$record['campaignId'], 'document_id' => $document_id], ['class' => 'btn-link', 'style' => 'color: #607D8B;']) ?>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <p>
        <?php if(Yii::$app->user->can('Admin') || Utilities::checkPermissionForForm($form_id, 'update')): ?>
            <?= Html::a('Update', ['update-record', 'form_id' => $form_id, 'document_id' => $document_id], ['class' => 'btn btn-primary btn-expanded']) ?>
        <?php endif; ?>
    </p>

    <?= DetailView::widget([
        'template' => '<tr><th style="width: 25%;"{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>',
        'model' => $data,
        'attributes' => $data,
    ]) ?>

    <?php if($files): ?>
        <hr>
        <div class="files-section">
            <h3><?= 'Files' ?></h3>

            <table class="table table-bordered table-striped">
                <thead>
                    <th style="width: 30%;"><?= 'Field' ?></th>
                    <th style="width: 20%;"><?= 'Type' ?></th>
                    <th style="width: 20%;"><?= 'Extension' ?></th>
                    <th style="width: 20%;"><?= 'Size' ?></th>
                    <th style="width: 10%;"><?= '' ?></th>
                </thead>

                <tbody>
                
                    <?php foreach($files as $field_label => $file): ?>
                        <tr>
                            <?php $file_record = $file['file_record']; ?>
                            <td><?= $field_label ?></td>
                            <?php if(! $file_record): ?>
                                <td colspan="4" style="text-align: center;">
                                    <span class="not-set"><?= 'This field is not submitted' ?></span>&nbsp;
                                    <?= Html::a('(Upload File)', [''], ['class' => 'upload-file-field', 'value' => Url::to(['upload-file', 'form_id' => (string)$form_id, 'document_id' => (string)$document_id, 'file_field_name' => $file['field_name']])]) ?>
                                </td>
                            <?php else: ?>
                                <td><?= '<i class="fa-lg ' . Utilities::getIconOfFileType($file_record->type) . '"></i> &nbsp;' . $file_record->type ?></td>
                                <td><?= $file_record->extension ?></td>
                                <td><?= number_format($file_record->size / 1024 / 1024, 2) . ' MB' ?></td>
                                <td>
                                    <div style="display: flex; justify-content: space-evenly;">
                                        <?= Html::a('<i class="fa-lg fa fa-download"></i>', ['download-file', 'id' => (string)$file_record->_id]) ?>
                                        <!-- Update and delete file actions must be according to update-record permission -->
                                        <?php if(Yii::$app->user->can('Admin') || Utilities::checkPermissionForForm($form_id, 'update')): ?>
                                            <?= Html::a('<i class="fa-lg fa fa-edit"></i>', [''], ['class' => 'update-file-field', 'value' => Url::to(['update-file', 'form_id' => (string)$form_id, 'document_id' => (string)$document_id, 'id' => (string)$file_record->_id])]) ?>
                                            <!-- If field isn't required, we can delete it -->
                                            <?php if(! $file['is_required']): ?>
                                                <?= Html::a('<i class="fa-lg fa fa-remove" style="color: red;"></i>', ['delete-file', 'form_id' => (string)$form_id, 'document_id' => (string)$document_id, 'id' => (string)$file_record->_id], [
                                                    'data' => [
                                                        'method' => 'POST',
                                                        'confirm' => 'Are you sure you want to delete this file? If you confirm, the file will be deleted permanently and there is no way to rollback'
                                                    ],
                                                ]) ?>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            <?php endif; ?>
                        </tr>
                    <?php endforeach; ?>

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

    <div class="children-data">

        <?php if(count($children_data) > 0): ?>
            <?php $counter = 0; ?>
            <hr>
            <h1><?= 'Children Forms' ?></h1>
            <?php foreach($children_data as $form_name => $child_data): ?>
                <?php if(Yii::$app->user->can('Admin') || Utilities::checkPermissionForForm($children_forms_IDs[$counter]['form_id'], 'read')): ?>
                    <h4><?= $form_name . ' Data' ?></h4>
                    <?php if(count($child_data) > 0): ?>
                        <p>
                            <!-- The user can add child record if he/she has submit permission to child form -->
                            <?php if(Yii::$app->user->can('Admin') || Utilities::checkPermissionForForm($children_forms_IDs[$counter]['form_id'], 'submit')): ?>
                            <?= 
                                Html::a('Add Child Record', 
                                    [
                                        'render-form', 
                                            'id' => $children_forms_IDs[$counter]['form_id'],
                                            'lookup_name' => $children_forms_IDs[$counter]['lookup_field'],
                                            'lookup_value' => $children_forms_IDs[$counter]['value'],
                                    ], 
                                    ['class' => 'btn btn-success btn-expanded']) ?>
                            <?php endif; ?>
                        </p>

                        <?php
                            $keys = array_keys($child_data);
                            $values = array_values($child_data);
                        ?>

                        <?php if(count($keys) > 0): ?> 
                            <table class='table table-striped table-bordered'>
                                <thead>
                                    <?php for($i = 0; $i < count($keys); $i++): ?>
                                        <?php if($i == 0): ?>
                                            <th style='width: 5%'></th>
                                        <?php else: ?>
                                            <th style='width: <?= 95 / (count($keys) - 1) ?>%;'><?= $keys[$i] ?></th>
                                        <?php endif; ?>
                                    <?php endfor; ?>
                                </thead>

                                <tbody>
                                    <?php for($j = 0; $j < count($values[0]); $j++): ?>
                                        <tr>
                                            <?php for($k = 0; $k < count($keys); $k++): ?>
                                                <?php if($k == 0): ?>
                                                    <td style='text-align: center; width: 5%;'>
                                                        <?= Html::a('<span class="glyphicon glyphicon-eye-open"></span>', 
                                                            ['view-record', 'form_id' => $children_forms_IDs[$counter]['form_id'], 'document_id' => $values[$k][$j]]) ?>
                                                    </td>
                                                <?php else: ?>
                                                    <td><?= $values[$k][$j] ?? '<span class="not-set">(not set)</span>' ?></td>
                                                <?php endif; ?>
                                            <?php endfor; ?>
                                        </tr>
                                    <?php endfor; ?>
                                </tbody>
                            </table>

                            <p>
                                <?= Html::a('View All &nbsp; <i class="fa fa-angle-double-right"></i>', 
                                    [
                                        'view-all-child-records',
                                            'form_id' => $children_forms_IDs[$counter]['form_id'],
                                            'field_name' => $children_forms_IDs[$counter]['lookup_field'],
                                            'value' => $children_forms_IDs[$counter]['value'],
                                            'parent_form_id' => $form_id
                                    ],
                                    ['class' => 'btn btn-purple btn-expanded']) ?>
                            </p>
                        <?php else: ?>
                            <table class='table table-striped table-bordered'>
                                <thead>
                                    <th style='text-align: center;'><?= 'No data found' ?></th>
                                </thead>
                            </table>
                        <?php endif; ?>
                    <?php else: ?>
                        <!-- Child form fields haven't managed yet -->
                        <?php if(Yii::$app->user->can('Admin')): ?>
                            <?php if(Utilities::checkIfChildFormFieldsVisibilityHasManaged($form_id, (string)$children_forms_IDs[$counter]['form_id'])): ?>
                                <p class='text-info'><span class='glyphicon glyphicon-info-sign'></span> <?= 'There is no data to show' ?></p>
                            <?php else: ?>
                                <p class='text-info'><span class='glyphicon glyphicon-info-sign'></span> <?= 'You haven\'t managed the visibility of this form fields yet' ?></p>
                                <?= Html::a('Manage Now', ['manage-children-forms-visible-fields', 'id' => $form_id], ['class' => 'btn btn-expanded btn-primary']) ?>
                            <?php endif; ?>
                        <?php else: ?>
                            <p class='text-info'><span class='glyphicon glyphicon-info-sign'></span> <?= 'The visibility of this form fields haven\'t managed yet or there is no data to show' ?></p>
                        <?php endif; ?>
                    <?php endif; ?>
                    
                    <?php $counter++; ?>
                    <hr>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

</div>

<?php 
    Modal::begin([
        'header' => '<h3>' . 'Update File' . '</h3>',
        'id' => 'update-file-field-modal',
        'size' => 'modal-md',
        'closeButton' => false
    ]);

    echo '<div id="update-file-field-modal-content"></div>';

    Modal::end();
?>

<?php 
    Modal::begin([
        'header' => '<h3>' . 'Upload File' . '</h3>',
        'id' => 'upload-file-field-modal',
        'size' => 'modal-md',
        'closeButton' => false
    ]);

    echo '<div id="upload-file-field-modal-content"></div>';

    Modal::end();
?>