<style>
    .checkbox{
        padding-left: 0px !important;
    }

    .radio input[type="radio"], .checkbox input[type="checkbox"] {
        float: right;
        margin-right: -20px;
    }
</style>
<?php
/* @var $this TidController */
/* @var $model Tid */
/* @var $form CActiveForm */

echo $form->dropDownListControlGroup($model, 'ti_type_id', TiType::getNamesForDropDown(), array(
    'empty' => Yii::t('default', 'Select TI Type'),
    'ajax' => array(
	'type' => 'POST',
	'data' => 'js:{ti_type_id:$(this).val(),contract_id: $("#Tid_contract_id").val(),status_contract_id: $("#TidStatus_contract_id").val(),ajax:true}',
	'url' => CController::createUrl('tid/processTiType'),
	'success' => 'js:function(data) {
                    if(data == "Permanent"){
                        $("#ti_expiry_date").hide();
                        $("label[for=Tid_ti_expiry_date]").hide();
			$("label[for=TidStatus_ti_expiry_date]").hide();
                     }
                    else{
		    $("#ti_expiry_date").show();
                        $("label[for=Tid_ti_expiry_date]").show();
			$("label[for=TidStatus_ti_expiry_date]").show();
                        $("#ti_expiry_date").val(data);
			}
		    
            }',
    )
));

echo $form->labelEx($model, 'ti_issue_date');

$this->widget('yiiwheels.widgets.datepicker.WhDatePicker', array(
    'model' => $model,
    'id' => 'ti_issue_date',
    'attribute' => 'ti_issue_date',
    'pluginOptions' => array(
	'format' => 'yyyy-mm-dd'
    )
));

echo $form->labelEx($model, 'ti_expiry_date');

$this->widget('yiiwheels.widgets.datepicker.WhDatePicker', array(
    'model' => $model,
    'id' => 'ti_expiry_date',
    'attribute' => 'ti_expiry_date',
    'pluginOptions' => array(
	'format' => 'yyyy-mm-dd'
    )
));


echo $form->dropDownListControlGroup($model, 'contract_id', Contract::getNamesForDropDown(), array(
    'empty' => Yii::t('default', 'Select Contract'),
    'ajax' => array(
	'type' => 'POST',
	'data' => 'js:{ti_type_id:$("#Tid_ti_type_id").val(),status_ti_type_id:$("#TidStatus_ti_type_id").val(),contract_id: $(this).val(),ajax:true}',
	'url' => CController::createUrl('tid/processTiType'),
	'success' => 'js:function(data) {
                    $("#ti_expiry_date").val(data);
		    
            }',
    )
));

echo $form->dropDownListControlGroup($model, 'cca_id', Cca::getNamesForDropDown(), array('empty' => Yii::t('default', 'Select CCA')));

echo $form->textAreaControlGroup($model, 'comments', array('size' => 50, 'maxlength' => 50, 'style' => 'height:100px !important;'));

if (!$model->isNewRecord && $from != 'renew') {

    if ($model->under_processing == 'Yes')
	$model->under_processing = 1;
    else
	$model->under_processing = 0;



    echo $form->checkBoxControlGroup($model, 'under_processing');

    echo $form->dropDownListControlGroup($model, 'renewal_stage_id', RenewalStage::getNamesForDropDown(),
	    array(
		'empty' => Yii::t('default', 'Select Processing Stage'),
		'id' => 'renewal_stage_id',
		'labelOptions' => array('label' => false),
		'onchange' => 'processRenewalStage();'
//		'ajax' => array(
//		    'type' => 'POST',
//		    'data' => 'js:{renewal_stage_id: $(this).val(),ajax:true}',
//		    'url' => CController::createUrl('tid/processRenewalStage'),
//		    'success' => 'js:function(data) {
//			    if(data == "true")
//
//		     }',
//		)
    ));

    echo $form->dropDownListControlGroup($model, 'transferred_to_contract_id', Contract::getNamesForDropDown(), array(
	'empty' => Yii::t('default', 'Select Contract'),
	'id' => 'transferred_to_contract_id',
    ));
}
?>
<script type="text/javascript">

    $(document).ready(function() {
        var permanent_id = '<?php echo TiType::getPermanentTiType(); ?>';
        var view_renewal = '<?php echo ($model->under_processing) ? 'true' : 'false' ?>';
	
        if (view_renewal != 'true') {
            $('#renewal_stage_id').toggle();
	    processRenewalStage();
        }

        if ($("#Tid_ti_type_id").val() == permanent_id && permanent_id != '') {
            $("#ti_expiry_date").hide();
            $("label[for=Tid_ti_expiry_date]").hide();
        }
        else
        {
            if ($("#TidStatus_ti_type_id").val() == permanent_id && permanent_id != '') {
                $("#ti_expiry_date").hide();
                $("label[for=TidStatus_ti_expiry_date]").hide();
            } else {
                $("#ti_expiry_date").show();
                $("label[for=Tid_ti_expiry_date]").show();
            }
        }

	processRenewalStage();

    });

    $('#Tid_under_processing').change(function() {
        $('#renewal_stage_id').toggle();
	processRenewalStage();
	
    });

    //    $('#renewal_stage_id').change(function() {
    //        processRenewalStage();
    //    });

    $('#TidStatus_under_processing').change(function() {	
        $('#renewal_stage_id').toggle();
	processRenewalStage();
	
    });


    function processRenewalStage(){
	var under_transfer_of_privilege = '<?php echo Setting::getUnderTransferOfPrivilege() ?>';
	
	if($("#renewal_stage_id").css('display') != 'none' && $('#renewal_stage_id').val() != null && $('#renewal_stage_id').val() != '' && $('#renewal_stage_id').val() == under_transfer_of_privilege){
	    $("#transferred_to_contract_id").show();
	    $("label[for=Tid_transferred_to_contract_id]").show();
	    $("label[for=TidStatus_transferred_to_contract_id]").show();
	}
	else{
	    $("#transferred_to_contract_id").hide();
	    $("label[for=Tid_transferred_to_contract_id]").hide();
	    $("label[for=TidStatus_transferred_to_contract_id]").hide();
	}
    }
</script>