<?php
$settings_model = MasCompanySettings::model()->find('crm_company_id='.Yii::app()->user->master_user_company_id);
$modelPermissions= json_decode($settings_model->permissions);
?>
<style>
select#PurchasingOrder_terms_id{
    width: 200px;
}
 .the-legend {
        border-style: none;
        border-width: 0;
        font-size: 15px;
        line-height: 20px;
        margin-bottom: 0;
        width: auto;
        padding: 6px 17px;
        border: 1px solid #e0e0e0;
        background: linear-gradient(135deg, #2c3e50 0%, #5db5a4 100%);
        color: white;
        font-weight: 500;
        border-radius: 18px 2%;
    }

    .the-fieldset {
        border: 1px solid #e0e0e0;
        padding: 10px;
        background: #edfffc;
        border-radius: 5px;

    }
</style>
<ul class="clearfix form-ul material">
                <li class="col-sm-6">                  
                     <?php echo $form->textArea($model, 'notes',array('data-label'=>'Notes','class'=>'en-text-area')); ?>
                    <?php echo $form->error($model, 'notes'); ?>
                </li>
                <li class="col-sm-6">                  
                    <?php echo $form->textArea($model, 'remarks',array('data-label'=>'Remarks','class'=>'en-text-area')); ?>
                    <?php echo $form->error($model, 'remarks'); ?>
                </li>
                 
                <!--<li class="col-sm-6">
                    <?php /*echo $form->dropDownList($model, 'terms_id', MasTermsAndConditions::getTerms(),array('data-label'=>'Terms','prompt'=>'select')); ?>
                    <?php echo $form->error($model, 'terms_id');*/ ?>
                    <button type="button" class="btn btn-in-li" onclick="getTerms()" >Publish</button>
                </li-->
                
                <?php
                /*if($modelPermissions->master_pricingterms == '1'){
                   ?>
                    <li class="col-sm-6">
                        <?php echo $form->dropDownList($model, 'pricing_terms', MasPricingTerms::getPricingTerms(),array('data-label'=>'Pricing Terms','prompt'=>'select')); ?>
                        <?php echo $form->error($model, 'pricing_terms'); ?>
                    </li>
                   <?php 
                }
                ?>
                <?php
                if($modelPermissions->master_availabilities == '1'){
                   ?>
                    <li class="col-sm-6">
                        <?php echo $form->dropDownList($model, 'availability', MasAvailability::getAvailability(),array('data-label'=>'Availability','prompt'=>'select')); ?>
                        <?php echo $form->error($model, 'availability'); ?>
                    </li>
                   <?php 
                } */
                ?>
               <?php 
              /* $model->terms_id = MasTermsAndConditions::base_terms_purchase_id(); 
               $model->terms_and_conditions = MasTermsAndConditions::base_purchase_terms_conditions(); 
               ?>
                  <?php  if ((int) Yii::app()->user->haveLevelPermissionOn['master_level_permission_TC'] > 0 || Yii::app()->user->authorised_user == 1) {?>
                    <li class="col-sm-6">
                      <?php echo $form->textArea($model, 'terms_and_conditions', array('data-label'=>'Terms & Conditions','class'=>'en-text-area')); ?>
                </li>
                    <?php } else{
                            
                            echo $form->hiddenField($model, 'terms_id'); 
                            echo $form->hiddenField($model, 'terms_and_conditions');
                     }?>
                
                <!--<li class="col-sm-12 tinymce-div">
                    <label>Terms & Conditions  <span style="color:red;">*</span></label>
                    <?php //echo $form->textArea($model, 'terms_and_conditions', array('data-label' => 'Terms & Conditions <span style="color:red;">*</span>', 'class' => 'ckeditor go', 'id' => 'PurchasingOrder_terms_and_conditions', 'onkeyup' => 'myValue()')); ?>
                    <?php //echo $form->error($model, 'terms_and_conditions'); ?>
                </li-->
                */
                ?>
                </ul>


                <?php if ((int) Yii::app()->user->haveLevelPermissionOn['master_level_permission_TC'] > 0 || Yii::app()->user->authorised_user == 1) { ?>
    <?php
    if (empty($model->terms_id)) {
        //$model->terms_id = MasTermsAndConditions::base_terms_purchase_id();
        //$model->terms_and_conditions = MasTermsAndConditions::base_purchase_terms_conditions();
        $terms = MasTermsAndConditions::getTerms();
      $filteredTerms = array_filter($terms, function($value) {
    return $value === 'Purchase';
});
    }
    ?>
    <ul class="clearfix form-ul material">
        <fieldset class="the-fieldset">
            <legend class="the-legend">Terms & Conditions</legend>
            <li class="col-sm-4">
                 <?php //echo $form->dropDownList($model, 'terms_id', MasTermsAndConditions::getTerms(), array('data-label' => 'Terms', 'prompt' => 'select')); ?>
                <?php echo $form->dropDownList($model, 'terms_id', MasTermsAndConditions::getTerms(), array('data-label' => 'Terms', 'prompt' => 'select')); ?>
                <button type="button" class="btn btn-theme" onclick="getTerms()">Publish</button>
            </li>

            <li class="col-sm-8">
                <?php
                if ($model->terms_and_conditions) {
                    $model->terms_and_conditions = strip_tags($model->terms_and_conditions);
                }
                ?>
                <?php echo $form->textArea($model, 'terms_and_conditions', array('data-label' => 'Terms & Conditions', 'class' => '')); ?>
            </li>
        </fieldset>
    </ul>
    <?php
} else {
    $model->terms_id = MasTermsAndConditions::base_terms_purchase_id();
    echo $form->hiddenField($model, 'terms_id');
    $model->terms_and_conditions = MasTermsAndConditions::base_purchase_terms_conditions();
    echo $form->hiddenField($model, 'terms_and_conditions');
}
?>

<?php

    $cs = Yii::app()->getClientScript();
  

/*$cs->registerScript("onready", "$(document).ready(function(){

var editor = CKEDITOR.instances['PurchasingOrder_terms_and_conditions'];
    if (editor) { editor.destroy(true); }
    CKEDITOR.replace('PurchasingOrder_terms_and_conditions',
                        {
                             filebrowserImageUploadUrl: 'js/ckeditor/filemanager/connectors/php/upload.php?Type=Image',
                        });

        
});", CClientScript::POS_HEAD);



 $cs->registerScript("getTerms", " function getTerms() {
        var terms_id = $('#PurchasingOrder_terms_id').val();
        /*var pricing_terms_text='';
        var availability_text ='';
        if( $('#PurchasingOrder_pricing_terms').length>0){
            var pricing_terms_text = $('#PurchasingOrder_pricing_terms option:selected').text();  
        }
        if( $('#PurchasingOrder_availability').length>0){
            var availability_text = $('#PurchasingOrder_availability option:selected').text();  
        }*
        jQuery.ajax({
                'type': 'post',
                'url': '" . Yii::app()->createUrl('termsandcondition/getTermsFromId') . "',
                'data': {'term_id':terms_id},
                'cache': false,
                'success': function(data) {
                   /*if(pricing_terms_text!='' && pricing_terms_text != 'select'){
                       data+='Pricing Terms: '+pricing_terms_text;
                       
                   }
                   if(availability_text!='' && availability_text != 'select'){
                       data+='Availability: '+availability_text;
                   }*
                   //data.replace('<br>', '');
                   //$('#PurchasingOrder_terms_and_conditions').addClass('filled');
                   //$('#PurchasingOrder_terms_and_conditions').text(data);
                   //tinymce.activeEditor.setContent(data);
                   alert(data);
                   CKEDITOR.instances.PurchasingOrder_terms_and_conditions.setData(data);
                   for (instance in CKEDITOR.instances) 
                    {
                        CKEDITOR.instances[instance].updateElement();
                    }
                }
        });

        }", CClientScript::POS_HEAD);*/



        
/*$cs->registerScript("onready", "$(document).ready(function(){
if (tinymce.editors.length > 0) {
tinymce.remove();  
}
    tinymce.init({
            selector: '#PurchasingOrder_terms_and_conditions',
            statusbar: false,
            setup: function (editor) {
                editor.on('change', function () {
                    editor.save();
                });
            }
        });  
        
      
        
        
});", CClientScript::POS_HEAD);*/

$cs = Yii::app()->getClientScript();

$cs->registerScript("getTerms", " function getTerms() {
        var terms_id = $('#PurchasingOrder_terms_id').val();
       
        jQuery.ajax({
                'type': 'post',
                'url': '" . Yii::app()->createUrl('termsandcondition/getTermsFromId') . "',
                'data': {'term_id':terms_id},
                'cache': false,
                'success': function(data) {
                   
                   data.replace('<br>', '');
                   var cleanText = $('<div>' + data + '</div>').text();
                   $('#PurchasingOrder_terms_and_conditions').addClass('filled');
                   $('#PurchasingOrder_terms_and_conditions').text(cleanText);
                }
        });

    }", CClientScript::POS_HEAD);
    ?>

    
    
<script>
$.fn.modal.Constructor.prototype.enforceFocus = function() {
	        var $modalElement = this.$element;
	        $(document).on('focusin.modal',function(e) {
	                var $parent = $(e.target.parentNode);
	                if ($modalElement[0] !== e.target
	                                && !$modalElement.has(e.target).length
	                                && $(e.target).parentsUntil('*[role="dialog"]').length === 0) {
	                        $modalElement.focus();
	                }
	        });
	};
/*CKEDITOR.instances['PurchasingOrder_terms_and_conditions'].on("instanceReady", function(){                    
this.document.on("keyup", function(){
for (instance in CKEDITOR.instances) 
{
    CKEDITOR.instances[instance].updateElement();
}
});
});*/
</script>