<div id="div-vincular-cliente">  
    <h1>Vincular Cliente &raquo; <?php echo $nomeCliente; ?></h1>
    <div class="form">
        <div class="portlet" id="yw2">
            <div class="portlet-content">
                <ul id="yw3">
                    <li><a class="search-button" href="#">Vincular Prospecto / Cliente <?php echo FaHtml::iconSetaClose(); ?></a></li>
                </ul>
            </div>
        </div>
        <div class="search-form" style="display:block">
            <?php
                $vinculados = ClientesVinculados::model()->findAll('VIN_IDCLIENTE=:atrIDCLIENTE', array(':atrIDCLIENTE' => $model->CLI_IDCLIENTE));
                $vinculados = CHtml::listData($vinculados, 'VIN_VINCULO', 'VIN_VINCULO');

                $modelClientes = new Clientes('search');
                $modelClientes->unsetAttributes();

                if (isset($_GET['Clientes']['CLI_NOME'])) {
                    $modelClientes->CLI_NOME = $_GET['Clientes']['CLI_NOME'];
                }
                $modelClientes->CLI_IDCLIENTE = $model->CLI_IDCLIENTE;
            ?>
            <div class="wide form">
                <?php
                $form = $this->beginWidget('GxActiveForm', array(
                    'action' => Yii::app()->request->baseUrl.'/index.php?r=clientes/view',
                    'method' => 'get',
                ));
                ?>

                <div class="row">
                    <?php echo $form->label($modelClientes, 'CLI_NOME'); ?>
                    <?php
                    $this->widget('ext.Utils.widgets.CrmAutoComplete', array(
                        'model' => $modelClientes,
                        'attribute' => 'CLI_NOME',
                        'url' => 'autocomplete/clienteVincular',
                        'params' => array(
                            'IDCLIENTE: "'.$model->CLI_IDCLIENTE.'"'
                            )
                        )
                    );
                    ?>
                </div>

                <div class="row buttons">
                    <input type="hidden" name="busca" value="1" />
                    <input type="hidden" name="id" value="<?php echo $model->CLI_IDCLIENTE;?>" />
                    <input type="hidden" name="vincular-cliente" value="true" />
                    <?php echo FaHtml::searchButton(); ?>
                </div>

                <?php $this->endWidget(); ?>
            </div><!-- wide form -->
        </div><!-- search-form -->
        <script type="text/javascript">
            $(document).ready(function () {
                $('.search-button').click(function() {
                    $(".search-form").slideToggle('fast');
                });
                $("input[name=yt0]").css({
                    top: $(document).find(".search-form").offset().top + 34,
                    left: $(".search-form").find("label:first").position().left + $(document).find(".search-form").find("label:first").width() + $(document).find(".search-form").find("input:first").width() + 50
                });
                $(".search-form").css({
                    height: 95,
                    paddingTop: 5
                });
            });
        </script>
        <?php
        if (isset($_GET['busca'])) {
            $this->widget('ext.Utils.widgets.CrmGridView', array(
                'id' => 'clientes-grid',
                'dataProvider' => $modelClientes->searchVincular(),
                'selectableRows' => 1,
                'afterAjaxUpdate'=>'updated',
                'template' => "{summary}{pager}{items}\n{summary}{pager}",
                'columns' => array(
                    'CLI_IDCLIENTE',
                    'CLI_NOME',
                    'CLI_FONE1',
                    array(
                        'name' => 'CLI_CIDADE',
                        'value' => 'GxHtml::valueEx($data->Cidade)',
                    ),
                    array(
                        'name' => 'CLI_SITUACAO',
                        'value' => 'Util::getStatusCliente($data->CLI_SITUACAO)',
                    ),
                    array(
                        'class' => 'CButtonColumn',
                        'template' => '{vincular}',
                        'viewButtonOptions' => array('target' => '_blank'),
                        'updateButtonOptions' => array('target' => '_blank'),
                        'buttons' => array(
                            'vincular' => array(
                                'label' => 'Vincular',
                                'url' => 'Yii::app()->createUrl("clientesVinculados/vincular", array("CLI_VINCULAR"=>$data->CLI_IDCLIENTE, "CLI_IDCLIENTE"=>' . $model->CLI_IDCLIENTE . '))',
                                'visible' => '$data->CLI_IDCLIENTE != '.$model->CLI_IDCLIENTE
                            )
                        )
                    )
                )
            ));
        }
        ?>
        <?php
        $vinculados = count($vinculados) > 0 ? Clientes::model()->findAll('CLI_IDCLIENTE in ('.implode(',', $vinculados).') order by CLI_NOME') : array();

        if (count($vinculados) > 0) { ?>
            <div class="row clientes-vinculados">
                <h2><?php echo GxHtml::encode('Clientes Vinculados'); ?></h2>
                <ul id="clientes-vinculados">
                    <?php foreach($vinculados as $vinculado) { ?>
                        <li>
                            <?php echo $vinculado->CLI_NOME . Util::getInfoContador($vinculado); ?>
                            <?php if ($vinculado->isAllowed(false)) { ?>
                                <a href="<?php echo Yii::app()->createUrl("clientes/view", array("id"=>$vinculado->CLI_IDCLIENTE)); ?>"  class="link_option_vinculado" title="Abrir" target="_blank"><?php echo FaHtml::iconView() ?></a>
                            <?php } ?>
                            <a href="<?php echo Yii::app()->createUrl("clientesVinculados/desvincular", array("CLI_DESVINCULAR"=>$vinculado->CLI_IDCLIENTE, "CLI_IDCLIENTE"=>$model->CLI_IDCLIENTE)); ?>" title="Desvincular" onclick="return confirm('Tem certeza que deseja desvincular este prospecto/cliente?');" class="link_option_vinculado"><?php echo FaHtml::iconDelete(); ?></a>                 
                        </li>
                    <?php } ?>
                </ul>
            </div>
        <?php } ?>
        <div class="clear"></div>
    </div><!-- form -->
</div>
<style type="text/css">
    .link_option_vinculado img{
        margin-top: -7px;
    }
    .tooltip-inner > ul {
        margin: 0!important;
    }    
    .tooltip-inner > ul > li {
        display: table!important;
        margin: 0!important;
        width: 100%!important;
    }
</style>