<style>
.ucItem{
    border: 1px solid #ddd;
    display:table-row;
}

#ucItems{
    display: table;
    border-collapse: collapse;
    width: 100%;
}

.ucItem > div{
    display: table-cell;
    border-right: 1px solid #ddd;
}

.ucType{
    width: 40px;
}

.ucAvatar{
    width: 50px;
}

.ucSummary{
    width: 200px;
}

.ucDuration{
    width: 80px;
}

.ucContent{
    
}

.ucActions{
    width: 100px;
}
</style>

<div style="display:table;width:100%">
    <?php $this->widget('ext.DashboardItem', array('item' => $data)); ?>
</div>


<?php
/*
    enum CallState {
        Unkown = 0,
        Ringing,
        Answered,
        Hangup,
        Forwarded,
        Blindtransfered,
        Attendedtransfered,
        Missed,
        Success,
        Picked
    };
*/


$script = '
var $mydiv = $("#ucNavbar"),
    origTop = $("#ucNavbar").position().top;
    
var docked = false;
var menu = $("#ucNavbar");

window.onscroll = function (e) {
    
    if((window.pageYOffset > 200) && !docked){
        docked = true;
        $("#ucNavbar").addClass("navbar-fixed-top");
        $("#messageList").css("padding-top", "43px");
    }
    else if((window.pageYOffset < 200) && docked){
        $("#ucNavbar").removeClass("navbar-fixed-top");
        $("#messageList").css("padding-top", "0");
        docked = false;
    }

};

';

Yii::app()->getClientScript()->registerScript('ucBar', $script);
//dump($data);

$msgStateClass = '';
switch ($data->state) {
    case '0':
        //$msgStateClass = 'missedMsg';
        break;
    case '1':
        $msgStateClass = 'activeMsg';
        break;
    case '2':
        $msgStateClass = 'activeMsg';
        break;
    case '3':
        $msgStateClass = '';
        break;
    case '4':
        $msgStateClass = 'missedMsg';
        break;
    case '5':
        $msgStateClass = 'missedMsg';
        break;
    case '6':
        $msgStateClass = 'missedMsg';
        break;
    case '7':
        $msgStateClass = 'missedMsg';
        break;
    case '8':
        $msgStateClass = 'successMsg';
        break;
    case '9':
        $msgStateClass = 'missedMsg';
        break;
}


$callStateClass = '';
switch ($data->state) {
    case '0':
        //$msgStateClass = 'missedMsg';
        break;
    case '1':
        $callStateClass = 'activeCall';
        break;
    case '2':
        $callStateClass = 'activeCall';
        break;
    case '3':
        $callStateClass = '';
        break;
    case '4':
        $callStateClass = 'missedCall';
        break;
    case '5':
        $callStateClass = 'missedCall';
        break;
    case '6':
        $callStateClass = 'missedCall';
        break;
    case '7':
        $callStateClass = 'missedCall';
        break;
    case '8':
        $callStateClass = 'successCall';
        break;
    case '9':
        $callStateClass = 'missedCall';
        break;
}
?>


<style>
.ucCallItem{
    height: 20px;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
}

.callDirectionBox{
    display: inline-block;
    height: 20px;
    text-align: center;
    line-height: 20px;
    border-radius: 3px 0 0 3px;
    width:44px;
/*     background:green; */
    border-right: 1px solid #ddd;
}

.missedCall{
    color: #51a351;
}

.successCall{
    color: #da4138;
}
</style>

<div class="ucMessage <?php echo $msgStateClass ?>" id="<?php echo $data->id;?>">
    <div class="messageSummary nav" style="position: relative;margin-bottom:0;height:40px;">
        <div class="messageType pull-left" style="border-bottom:none;background: #eee;">
            
            <?php 
                switch ($data->direction) {
                    case '0':
                        echo '<i class="icon-arrow-up direction"></i>';
                        break;                    
                    case '1':
                        echo '<i class="icon-arrow-down direction"></i>';
                        break;
                }
            
            
                switch ($data->typ) {
                    case 'fax':
                        echo '<i class="icon-print"></i>';
                        break;
                    case 'call':
                        echo '<i class="icon-phone"></i>';
                        break;
                    case 'email':
                        echo '<i class="icon-envelope"></i>';
                        break;
                }
            ?>           
        </div>
        <div class="messageTarget pull-left" style="border-bottom:none;border-radius:0;">
           <img src="<?php echo $data->getAvatarUrl();?>" width="34" height="34" />
        </div>
        <div class="messageTarget pull-left" style="border-bottom:none;border-radius:0;width:200px;">
           <!--  <div class="dateX"><?php echo $data->timestamp; ?></div> -->
            <div>
                <i class="icon-user"></i> 
                <?php 
                    echo CHtml::link(
                            $data->remote_name,
                            '',
                            array(
                                'data-title'=> '<i class="icon-user"></i> aaa' . $data->remote_name,
                                //'data-trigger'=>'hover',
                                'data-content'=>'
                                    <i class="icon-phone"></i> <a href="#">Call user ('.$data->remote_number.')</a><br />
                                    <i class="icon-comments"></i> <a href="#">Jabber message</a><br />
                                    <i class="icon-envelope"></i> <a href="#">New mail</a><br />',
                                'rel'=>'popover',
                            )
                        );
                ?>
                <!-- <i class="icon-phone"></i> -->
                <b style="font-size:10px; font-weight:normal;text-decoration:none" class="callable" data-phone-number="<?php echo $data->remote_number;?>">
                    [ <?php echo $data->remote_number;?> ]
                </b>
            </div>
            <div>
                <i class="icon-time"></i>&nbsp;
                <span style="font-weight:normal">
                    <?php 
                        if($data->timestamp){
                        	echo  $data->timestamp; 
                        }
                    ?>
                </span>
            </div>
        </div>
        <div class="messageTarget pull-left" style="border-bottom:none;border-radius:0;background:#fafafa;">
           <!--  <div class="dateX"><?php echo $data->timestamp; ?></div> -->
           
           <?php
                $visibility = '';
                $info = '';
                $metaNumber = '';
                
               if(in_array($data->state, array(4, 9))){
                   
                   
                   switch ($data->state) {
                       case 4:
                            $info = '<span data-translation-id="HOMEUCITEM_FORWARDED_TO"> '.__('HOMEUCITEM_FORWARDED_TO').':</span>';
                            $metaNumber = '<a>'.$data->meta.'</a>';
                            break;
                       case 9:
                            $info = '<span data-translation-id="HOMEUCITEM_PICKEDUP_BY"> '.__('HOMEUCITEM_PICKEDUP_BY').':</span>';
                            $metaNumber = '<a>'.$data->meta.'</a>';
                            break;
                   }
               }
               else{
                   $visibility = 'style="visibility:hidden"';
               }
           ?>
           
            <div <?php echo $visibility;?>>
                <i class="icon-info-sign"></i>
                <?php echo $info;?>
                <?php echo $metaNumber;?>
            </div>
            <div>
                <i class="icon-tasks"></i>&nbsp;
                <a style="font-weight:normal" rel="tooltip" data-placement="right" title="<?php echo RelativeDateFormatter::durationInNumbers($data->duration)?>">
                    <?php 
                        echo RelativeDateFormatter::durationFromSeconds($data->duration);
                    ?>
                </a>
            </div>
        </div>
        <?php
            if($data->typ == 'fax'){
            
                if($data->fax_content_pdf){
                    echo CHtml::link(
                        '<i class="icon-arrow-down"></i> Download',
                        array(
                                'getFaxPdfDocument',
                                'id'=>$data->id,
                            )
                    );    
                }
                
                $faxPageUrls = $data->getFaxPngUrls();
                
                if(!empty($faxPageUrls)){
                    foreach($faxPageUrls as $pageIndex => $pageUrl){
                        echo CHtml::image($pageUrl, 'Page '.$pageUrl, array('style'=>'height:40px'));
                    }
                    
                }
                
                
                //dump($data->getFaxPngUrls());
            

                
            }        
        ?>
        <div class="messageActions pull-right">
            <div>
                <a rel="tooltip" href="#" data-original-title="Reply"><i class="icon-pencil" style="visibility:hidden"></i></a>
                <a rel="tooltip" href="#" data-original-title="Forward"><i class="icon-forward" style="visibility:hidden"></i></a>
                <a rel="tooltip" href="#" data-original-title="Tags"><i class="icon-tags" style="visibility:hidden"></i></a>
                <?php
                    echo CHtml::link(
                        '<i class="icon-remove"></i>',
                        array(
                            'delete',
                            'id'=>$data->id,
                            'ajax'=>'homeUCItem',
                        ),
                        array(
                            'class'=> 'delete',
                            'rel' => 'tooltip',
                            'id' => $data->id,
                            'data-original-title' => 'Delete',
                        )
                    );
                ?>
            </div>
            <div>
                <div class="ucItemRating">
                    <?php
                        $r1Id = "r1" . $data->id;
                        $r2Id = "r2" . $data->id;
                        $r3Id = "r3" . $data->id;
                        $r4Id = "r4" . $data->id;
                        $r5Id = "r5" . $data->id;
                    ?>
                    <input type="radio" id="<?php echo $r5Id;?>" value="5" name="<?php echo $data->id;?>" <?php echo $data->rating == 5 ? 'checked="checked"' : '' ?>/>
                    <label for="<?php echo $r5Id;?>"></label>
                    
                    <input type="radio" id="<?php echo $r4Id;?>" value="4" name="<?php echo $data->id;?>" <?php echo $data->rating == 4 ? 'checked="checked"' : '' ?>/>
                    <label for="<?php echo $r4Id;?>"></label>
                    
                    <input type="radio" id="<?php echo $r3Id;?>" value="3" name="<?php echo $data->id;?>" <?php echo $data->rating == 3 ? 'checked="checked"' : '' ?>/>
                    <label for="<?php echo $r3Id;?>"></label>
                    
                    <input type="radio" id="<?php echo $r2Id;?>" value="2" name="<?php echo $data->id;?>" <?php echo $data->rating == 2 ? 'checked="checked"' : '' ?>/>
                    <label for="<?php echo $r2Id;?>"></label>
                    
                    <input type="radio" id="<?php echo $r1Id;?>" value="1" name="<?php echo $data->id;?>" <?php echo $data->rating == 1 ? 'checked="checked"' : '' ?>/>
                    <label for="<?php echo $r1Id;?>"></label>
                </div>
            </div>
            
        </div>
    </div>
</div>
<?php //dump($data);
    $script = "
        $('.ucItemRating input').click(function() {
            ratingValue = $(this).val();
            icItemId = $(this).attr('name');
            
            $.post('".url('homeUCItem/default/rateUcItem')."',{
                rating: ratingValue,
                itemId: icItemId,
                ".Yii::app()->request->csrfTokenName."  : '" . Yii::app()->request->csrfToken."'
            },
            function(data){
                
                //$('#display').html(data.returnValue);
            }, 'json');
            
            console.log();
        });
    ";

    Yii::app()->clientScript->registerScript('ucMessageRating', $script);
?>

