<?php
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/systemDateTimeProfile/systemDateTimeProfileJSHelper.js', CClientScript::POS_HEAD);
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/plDynamicGrid/plDynamicGrid.js', CClientScript::POS_HEAD);
$source = app()->locale->aMName.'_'.app()->locale->pMName;
$search = array(
    app()->locale->aMName,
    app()->locale->pMName
);
$replace  = array('am','pm');
$result = str_replace($search,$replace ,$source);
$result = explode('_',$result);
?>
<script>
$.fn.datetimepicker.dates['en'] = {
    days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
    meridiem: [<?php echo (json_encode($result[0])); ?>, <?php echo(json_encode($result[1])); ?>],
    daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    daysMin:<?php echo(json_encode(array_values(Yii::app()->locale->getWeekDayNames('abbreviated', true))));?>,
    months:<?php echo(json_encode(array_values(Yii::app()->locale->getMonthNames('wide', true))));?>,
    monthsShort: <?php echo(json_encode(array_values(Yii::app()->locale->getMonthNames('abbreviated', true))));?>,
    today: "<?php echo  __("CALENDAR_TODAY");?>",
    suffix: ["st", "nd", "rd", "th"],
};    

var DateTimePickerHelper = new systemDateTimeProfileJSHelper({
    DTPUserDatePattern:'<?php echo DTPUserDatePattern(true) ?>',
    showMeridian: <?php echo json_encode($userTimeFormat == '12'); ?>,
    userPatternForDateFormat:"<?php echo userPatternForDateFormat() ?>",
    pMName:"<?php echo app()->locale->pMName ?>"
});

</script>

<style>
    .hideRow{
        display:none;
    }

    .addDateRule{
        background-color: #fff;
    }
    #date-time-profile-grid_Modal .hideRow .timepickerContent {
        min-width: 125px;
    }
    
    .table-bordered tfoot td{
        border-left-color: transparent;
    }
    .table-bordered tfoot td:last-child , .table-bordered tfoot td:first-child {
        border-left-color: #ddd;
    }
    .addWeekRule{
        background-color: #fff;
    }

</style>

<?php
$form=$this->beginWidget('ext.PlGrid.PlActiveForm', array(
        'id'=>'date-time-profile-form',
        'gridId'=>'date-time-profile-grid',		
        'action'=> $dateTimeProfile->isNewRecord ? array('create') : array(),
        'htmlOptions' => array('autocomplete' => 'off')
    ));


    echo $form->textFieldRow($dateTimeProfile, 'title', array(
            'class'=>'span3',
            'data-translation-id'=>'DATE_TIME_PROFILE_MODEL_NAME',
            'autocomplete' => 'on'
        ));
    echo $form->textFieldRow($dateTimeProfile, 'descr', array(
            'class'=>'span3',
            'data-translation-id'=>'DATE_TIME_PROFILE_MODEL_DESCR',
            'autocomplete' => 'on'
        ));

$this->widget('bootstrap.widgets.TbGridView', array(
    'id'=>'date-time-date-grid',
    'dataProvider'=>$dateTimeRule->search(DateTimeRuleType::dateRule,$dateTimeProfile->id),
    'filter'=>$dateTimeRule,
    'summaryText'=>false,
    'selectableRows'=>0,
    'type'=>'striped bordered condensed',
    'itemsCssClass'=>'plTable items',
    'rowCssClassExpression'=>'$data->id=="new"?"hideRow":""',
    'columns'=>array(  
             array(
                    'name'=>'date_from',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('date_from'),
                    'headerHtmlOptions'=>array('data-translation-target'=>'DATE_TIME_RULE_MODEL_DATE_FROM_TARGET'),
                    'value'=>array($this, 'renderDateTimePicker'),     
                ),
            array(
                'name'=>'date_to',
                'type'=>'raw',
                'header' => $dateTimeRule->getAttributeLabel('date_to'),
                'headerHtmlOptions'=>array(
                    'data-translation-id'=>'DATE_TIME_RULE_MODEL_DATE_TO',
                    'data-translation-target'=>'DATE_TIME_RULE_MODEL_DATE_TO_TARGET'
                ),
                'value'=>array($this, 'renderDateTimePicker'),

                'footer'=>__('TABLE_FOOTER_ADD_NEW_DATE_TIME_RULE'),
                'footerHtmlOptions'=>array(
                            'data-translation-id'=>'TABLE_FOOTER_ADD_NEW_DATE_TIME_RULE',
                            'style'=>'text-align: right;',
                            'class'=>'addDateRule',
                        )
            ),            
            array(
                'class'=>'bootstrap.widgets.TbButtonColumn',                
                'htmlOptions'=>array(
                    'class'=>'buttonColumnIcons',
                    'style'=>'width:55px;',
                    'title'=>__("GRID_DELETE_TOOLTIP"),
                    'data-translation-id'=>"GRID_DELETE_TOOLTIP"
                ),
                'template'=>'{delete}',
                'buttons'=>array(
                    'delete'=>array(
                        'url'=>'',
                        'label'=>"",
                        'visible'=>'true',//FIXME
                        'click'=>'js:function(){
                                $(this).parent().find(".tooltip-inner").remove();
                                $(this).parent().find(".tooltip-arrow").remove();
                                $(this).parent().parent().remove();
                        }',
                    ),
                ),
            'footer'=>'<div style="cursor: pointer;" class="icon-plus addDateRule" data-translation-id="DATE_TIME_RULE_ADD_RULE" data-toggle="tooltip" title="'.__("DATE_TIME_RULE_ADD_RULE").'"></div>',
            ),
        ),

    )
);
//FIXME ! just if translation mode is on ...
if (Yii::app()->user->translateMode) {
    echo "<i data-translation-target='DATE_TIME_RULE_MODEL_DATE_FROM_TARGET' data-translation-id='DATE_TIME_RULE_MODEL_DATE_FROM'></i>";
    echo "<i data-translation-target='DATE_TIME_RULE_MODEL_DATE_FROM_TARGET' data-translation-id='DATE_TIME_RULE_MODEL_DATE_PLACEHOLDER'></i>";
    // echo "<i data-translation-target='DATE_TIME_RULE_MODEL_DATE_TO_TARGET' data-translation-id='DATE_TIME_RULE_MODEL_DATE_TO'></i>";
    echo "<i data-translation-target='DATE_TIME_RULE_MODEL_DATE_TO_TARGET' data-translation-id='DATE_TIME_RULE_MODEL_DATE_PLACEHOLDER'></i>";
}

$this->widget('bootstrap.widgets.TbGridView', array(
    'id'=>'date-time-week-grid',
    'dataProvider'=>$dateTimeRule->search(DateTimeRuleType::weekRule,$dateTimeProfile->id),
    //  'filter'=>$dateTimeRule,
    'summaryText'=>false,        
    'selectableRows'=>0,
    'type'=>'striped bordered condensed',
    'itemsCssClass'=>'plTable items',
    'rowCssClassExpression'=>'$data->id=="new"?"hideRow":""',
    'columns'=>array(             
               array( 
                    'name'=>'mon',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('mon'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_MON'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                array( 
                    'name'=>'tue',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('tue'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_TUE'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                array( 
                    'name'=>'wed',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('wed'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_WED'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                array( 
                    'name'=>'thur',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('thur'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_THUR'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                array( 
                    'name'=>'fri',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('fri'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_FRI'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                array( 
                    'name'=>'sat',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('sat'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_SAT'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                array( 
                    'name'=>'sun',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('sun'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_SUN'),
                    'value'=>array($this, 'renderWeekdayCheckBox')
                ),
                 array(
                    'name'=>'time_from',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('time_from'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_TIME_FROM'),
                    'htmlOptions'=>array(  // FIX FOR line break between the append icons
                        'width'=>'135px',

                    ),
                    'value'=>array($this, 'renderTimePicker'),
                 ),
                 array(
                    'name'=>'time_to',
                    'type'=>'raw',
                    'header' => $dateTimeRule->getAttributeLabel('time_to'),
                    'headerHtmlOptions'=>array('data-translation-id'=>'DATE_TIME_RULE_MODEL_TIME_TO'),
                    'htmlOptions'=>array(  // FIX FOR line break between the append icons
                        'width'=>'135px',
                    ),

                    'value'=>array($this, 'renderTimePicker'),
                    'footer'=>__('TABLE_FOOTER_ADD_NEW_WEEK_TIME_RULE'),
                    'footerHtmlOptions'=>array(
                                'data-translation-id'=>'TABLE_FOOTER_ADD_NEW_WEEK_TIME_RULE',
                                'style'=>'text-align: right;',
                                'class'=>'addWeekRule',
                            )
                ),
            array(
                'class'=>'bootstrap.widgets.TbButtonColumn',
                'htmlOptions'=>array(
                    'class'=>'buttonColumnIcons', 
                    'style'=>'width:55px;',
                    'title'=>__("GRID_DELETE_TOOLTIP"),
                    'data-translation-id'=>"GRID_DELETE_TOOLTIP"
                ),
                'template'=>'{delete}',
                'buttons'=>array(
                    'delete'=>array(                        
                        'url'=>'',    
                        'label'=>"",
                        'visible'=>'true',
                        'click'=>'js:function(){    
                                $(this).parent().find(".tooltip-inner").remove();
                                $(this).parent().find(".tooltip-arrow").remove();
                                $(this).parent().parent().remove();
                        }',
                    ),
                ),
                'footer'=>'<div class="icon-plus addWeekRule" style="cursor: pointer;" data-translation-id="DATE_TIME_RULE_ADD_WEEK_RULE" data-toggle="tooltip" title="'.__("DATE_TIME_RULE_ADD_WEEK_RULE").'"></div>',
            ),
        ),

    )
);
$this->endWidget();