<?php

use common\widgets\GridView;
use yii\widgets\Pjax;

$item = Yii::$app->controller->id;
$title = 'Branches';
?>
<?php Pjax::begin(['id' => "$title-Grid"]);?>
  <?=GridView::widget(
      [
        'controller' => 'settings',
        'responsive' => true,
        'dataProvider' => $branchProvider,
        'rowOptions' => function ($model, $key, $index, $grid) {
            return ['data-id' => $model['id']];
        },
        'tableOptions' => ['class' => 'table table-striped table-bordered'],
        'id' => 'branches',
        'filterSelector' => '#branchespagesize',
        'columns' => [
            [
                'name' => 'related',
                'class' => '\yii\grid\CheckboxColumn',
                'checkboxOptions' => function ($model, $key, $index, $column) {
                    $id = $model['id'];
                    $name = $model['name'];
                    return [
                        'checked' => $model->related,
                        'value' => $model['id'],
                        'onclick' => "relatedb('$id')",
                    ];
                },
            ],
            'nickname:raw:Branch Nickname',
            [
                'header' => 'Default Branch',
                'name' => 'primaryrecord',
                'class' => '\yii\grid\RadioButtonColumn',
                'radioOptions' => function ($model, $key, $index, $column) {
                    $id = $model->id;
                    return [
                        'checked' => $model->primaryrecord,
                        'value' => $model['id'],
                        'onclick' => "setPrimary('$id')",
                    ];
                },
            ],
            [
                'format' => 'raw',
                'label' => 'Customer Account Code',
                'attribute' => 'account_code',
                'value' => function ($model) {
                    $id = $model->id;
                    return \yii\helpers\Html::TextInput(
                        'account_code',
                        $model->account_code,
                        [
                            'class' => 'form-control',
                            'onChange' => "updateRecord('$id', 'data', $(this).val());"
                        ]
                    );
                }
            ],
            [
                'class' => common\widgets\ActionColumn::class,
                'template' => '{view} {update}',
                'controller' => 'settings',
                'urlCreator' => function ($action, $model, $key, $index) {
                    if ($action === 'view') {
                        $url = '/NGauge/settings/' . $action . '?id=' . $model->id . "&language=" . $model->language;
                        return $url;
                    }
                    if ($action === 'update') {
                        $url = '/NGauge/settings/' . $action . '?id=' . $model->id . "&language=" . $model->language;
                        return $url;
                    }
                    if ($action === 'delete') {
                        $url = '/NGauge/settings/' . $action . '?id=' . $model->id . "&language=" . $model->language;
                        return $url;
                    }
                }
            ],
            ],
      ]
);
?>

<script>
    pageid = "<?= $model->id ?>";
    modelclass = "Accounts";
    related = 'Settings';
    function relatedb(rid) {
      $.ajax({
            type: "POST",
            url: "/NGauge/system/related",
            data: {sid: pageid, source: modelclass, related: related, rid: rid, },
          })
    }

    function setPrimary(id) {
        $.ajax({
            type: "POST",
            url: "/NGauge/system/setprimaryrecord",
            data: {
                id: id,
                modelid: pageid,
                source: modelclass,
                related: related,
            },
            success: function(result, b, c) {
                $('#message').html(result.message);
                $('#flashmessage').delay(500).fadeIn('normal', function() {
                    $(this).delay(2500).fadeOut();
                });
            }
        })
    }

    function updateRecord(id, field, value) {
        $.ajax({
            type: "POST",
            url: "/NGauge/system/updaterecord",
            data: {
                id: id,
                modelid: pageid,
                source: modelclass,
                related: related,
                field: field,
                value: value,
            },
            success: function(result, b, c) {
                $('#message').html(result.message);
                $('#flashmessage').delay(500).fadeIn('normal', function() {
                    $(this).delay(2500).fadeOut();
                });
            }
        })
    }
  </script>
    <?php Pjax::end();?>