<?php
$cs = Yii::app()->getClientScript();
$baseUrl = Yii::app()->baseUrl;

       
    $cs->registerScript("makeCovertable",  "function makeCovertable(id){
        var r = confirm('Are you sure?');
            if(r==true)
            {
                jQuery.ajax({
                'type'  : 'post',
                'data'  : 'id=' + id,
                'url'  : $('#liveSite').val()+'index.php?r=purchasingorder/Makeconvert',
                'cache': false,
                'success':function(data){
                    if(data.status=='fail'){
                        alert('Failed!');
                    }else {
                        reloadTable('#tbl_po_movement_analysis_details');
                        t8.success('Done!');
                    }
                }
                });
            }
    };", CClientScript::POS_HEAD);
    
  ?>
  <script>
     $(document).ready(function() {
         var table_id ="#tbl_po_movement_analysis_details";
         var table = $(table_id).on("preXhr.dt", function (e, settings, data) {
             return data;
         }).DataTable({
             scrollY         : "645px",
             pageLength      : 50,
             processing: true,
             order: [[ 0, "desc" ]],
             columns: [
                       {className: ""},
                       {className: ""},      
                       {className: "nw sl col-max-6"},      
                       {className: "" },
                       {className: ""},
                       {className: ""},      
                       
                   ],
                   columnDefs: [
                           {
                             
                           }
                   ],
                   rowCallback: function ( row, data ){ 
                       if ( data[6] == "0" ){
                           $("td", row).css("background-color", "#ff8080");

                       }
                   	  if ( data[7] == "Y" ){
                                       $("td", row).css("background-color", "#D6FCDA");
                                   }
                   		/*else if ( data[14] == "Y" ){
                           $("td", row).css("background-color", "#D6FCDA");
                       }*/
                       
                   },
       	
             ajax: {
                 url: "<?php  echo Yii::app()->createUrl('Purchasingorder/GetNewMovementAnalysisList') ?>",
                 type: "POST",
                 cache : false,
             },
             serverSide:     true,
             fixedColumns:   true,
             processing: true,
		 	  "fnCreatedRow"  : function( nRow, aData, iDataIndex ){
         		$(nRow).addClass("context-menu");
     		},
     });
    $('#tbl_po_movement_analysis_details tbody').on('mouseover', 'tr', function () {
    $.contextMenu('destroy');
    });
         $('#tbl_po_movement_analysis_details tbody').on('click', 'tr', function () {
        	 $.contextMenu( 'destroy' );
             var data = table.row( this ).data();
			 var is_approved		    =   data[6];
        	 var is_converted	        =   data[7];

             $.contextMenu({
            	    selector: '.context-menu',
            	    callback: function(key, options) {
                     	var action_id = key;
        					var data 			= 	table.row( this ).data();
        					var id				=	data[0];
        					var is_approved		=   data[6];
        					var is_converted	=   data[7];
        					if(action_id == "edit"){
        					    location.href=$('#liveSite').val()+"/index.php?r=purchasingorder/createmovementanalysis&type=edit&id="+id;
        					}
        					else if(action_id == "view"){
        						   location.href=$('#liveSite').val()+"/index.php?r=purchasingorder/createmovementanalysis&type=view&id="+id;
        					}
        					else if(action_id == "convert"){
        						    loadPopupModal("enquiry/create&analysis_id="+id,"Convert to Enquiry","xl");
        					}
        					else if(action_id == "approve"){
        						location.href=$('#liveSite').val()+"/index.php?r=purchasingorder/createmovementanalysis&type=approve&id="+id;
        					}
        					else if(action_id == "print"){
        						window.open($('#liveSite').val()+"/index.php?r=purchasingorder/PrintMovementAnalysisPDF&frm=1&id="+id,"_blank");
        					}

        					
                 },
            	    items: {
            	    	<?php 
                	    if(Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["po_new_order_movement_analysis_edit"])){?>
                	    	"edit": {name: "Edit", icon: "edit", disabled: function(key, opt) { 
    		                    // this references the trigger element
    		                   if(is_approved > 0){
    		                    	return !this.data('editDisabled'); 
    		                    }
    		                    }},
                	    <?php } ?>
                            <?php 
                	    //if(Yii::app()->user->authorised_user == 1){?>
                	    	"view": {name: "View", icon: "fa-eye", disabled: function(key, opt) { 
    		                    // this references the trigger element
    		                   if(is_approved == 0){
    		                    	return !this.data('viewDisabled'); 
    		                    }
    		                    }},
                	    <?php //} ?>
                	    <?php  if(Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["po_new_order_movement_analysis_convert"])){?>
             				"convert": {name: "Convert", icon: "fa-retweet", disabled: function(key, opt) { 
    		                     if(is_approved == 0 || is_converted=='Y'){
    		                    	return !this.data('convertDisabled'); 
    		                    }
             				}},
	                 	<?php } ?> 
                	    <?php  if(Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["po_new_order_movement_analysis_approve"])){?>
         					"approve": {name: "Approve", icon: "fa-question", disabled: function(key, opt) { 
    		                    // this references the trigger element
    		                    if(is_approved > 0){
    		                    	return !this.data('approveDisabled'); 
    		                    }
             				}
         				},
                 		<?php } ?> 
                 		
                 		<?php if((Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["po_new_order_movement_analysis_pdf"]))){ ?>
		                 		"print": {name: "Print", icon: "fa-print", disabled: function(key, opt) { 
			                    // this references the trigger element
			                     
			                    }},
			                    
		                        <?php } ?>
                 		 
                 		
                 		
},
            	});


         });
          
           
  		
     });
        </script>
    <section class="panel" style="background-color:#F6F3EE;">
        <header class="panel-heading clearfix panel-theame">
            <i class="material-icons">list</i> New Order Movement Analysis
            <div class="pull-right">
                <?php 
                     if(isset(Yii::app()->user->havePermissionOn['po_new_order_movement_analysis_add']) || Yii::app()->user->authorised_user == 1){?>
                    <button class="btn btn-sm btn-theme" id="add_popup" onclick="addNewMovementAnalysis();"><i class="fa fa-plus"></i> ADD</button>
                    <?php } ?>
                    
            </div>
        </header>
        <div class="panel-body">
            <div class="custom-data-table">
                <table class="table table-hover table-bordered personal-task" id="tbl_po_movement_analysis_details" style="font-size:13px;">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Date</th>
                            <th>Generic</th>
                            <th>Approved By</th>
                            <th>Approved Date</th>
                            <th>Created By</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </section>
    
    <?php
    
     $cs = Yii::app()->getClientScript();

       
    $cs->registerScript('addNewMovementAnalysis',  'function addNewMovementAnalysis(){
     location.href="index.php?r=purchasingorder/createmovementanalysis";
    };', CClientScript::POS_HEAD);
    
    ?>
