<script>
    $(document).ready(function () {
        var id = $('#MasUserGroup_id').val();
        if (id > 0) {
            var hid_models = $('#hid_model').val();
            if (hid_models != "") {
                var hid = JSON.parse(hid_models);
                $.each(hid, function (key, val) {
                    $("#usergroup-form #MasUserGroup_" + key).val(val);
                });
                $("#usergroup-form input:checkbox").prop("checked", false);
                if (hid != "")
                    $.each((hid), function (iteration, item) {
                        $(".permissions input[value=\'" + iteration + "\']").prop("checked", true);
                    });
            }
        }
    });
</script>    
<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'usergroup-form',
    'enableAjaxValidation' => false,
    'enableClientValidation' => false,
        ));
?>
<div class="permissions vertical-tab">
    <ul class="nav nav-tabs nav-vertical nav-tab-theme" style="margin-top: 0">
          
        <li><a href="#menu_tab" data-toggle="tab">Menu</a></li>
        <li><a href="#operations_tab" data-toggle="tab">Operations</a></li>
        <li><a href="#master_tab" data-toggle="tab">Masters</a></li>
        <li><a href="#admin_tab" data-toggle="tab">Admin</a></li>
    </ul>
    <div class="tab-content tab-vertical">
        <ul class="material clearfix form-ul">
            <li class="col-sm-6">
                <input type="hidden" name="hid_model" id="hid_model" value=<?php echo $model->permissions; ?>>
                <?php echo $form->hiddenField($model, 'id');
                echo $form->textField($model, 'group_name', array('data-label' => 'User Group','autocomplete'=>'off','onpaste'=>"return false",'onkeypress'=>'checkCharacters(this.id);'));  ?>
            </li>            
            <li class="col-sm-6">
                <?php echo $form->checkBoxList($model, 'permissions[checkall]', array('checkall' => ''), array('class' => 'main_check', 'data-label' => 'Select All', 'onclick' => ' $("#usergroup-form input:checkbox").prop("checked", $(this).prop("checked"));')); ?>
            </li>            
        </ul>
        <hr class="theme-divider">
 
            <div class="tab-pane active" id="menu_tab">
            <div class="table-responsive">
                <table class="table table-hover table-bordered table-theme table-permission">
                    <thead>
                        <tr>
                            <th class="text-right"><?php echo $form->checkBoxList($model, 'permissions[menu_check_all]', array('menu_check_all' => ''), array('data-label' => 'Check All', 'onclick' => ' $("#menu_tab input:checkbox").prop("checked", $(this).prop("checked"));')); ?> select all</th>
                            <th class="text-center">View</th>

                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="text-bold">Orders</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_orders]', array('menu_orders' => ''), array('title' => 'Orders')); ?></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Consolidated Orders</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_consolidated_orders]', array('menu_consolidated_orders' => ''), array('title' => 'Consolidated Orders')); ?></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Inventory</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_inventory]', array('menu_inventory' => ''), array('title' => 'Inventory')); ?></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Goods In</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_goods_in]', array('menu_goods_in' => ''), array('title' => 'Operations Goods In')); ?></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Unshipped</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_unshipped_sales]', array('menu_unshipped_sales' => ''), array('title' => 'Consolidated Supply Confirmation')); ?></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Backlog</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_backlog]', array('menu_backlog' => ''), array('title' => 'Backlog')); ?></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Supply Confirmation</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_supplyconfirmation]', array('menu_supplyconfirmation' => ''), array('title' => 'Supply Confirmation')); ?></td>
                        </tr> 
                        
                         <tr>
                            <td class="text-bold">Sales</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_sales]', array('menu_sales' => ''), array('title' => 'Sales')); ?></td>
                        </tr> 
                         <tr>
                            <td class="text-bold">Consolidated Supply Confirmation</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_consolidated_supplyconfirmation]', array('menu_consolidated_supplyconfirmation' => ''), array('title' => 'Consolidated Supply Confirmation')); ?></td>
                        </tr> 
                       	<tr>
                            <td class="text-bold">Consolidated Order Report</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[menu_report_consolidated_order]', array('menu_report_consolidated_order' => ''), array('title' => 'Consolidated Order Report')); ?></td>
                        </tr>  
                                                 
                    </tbody>
                </table>
            </div>
        </div>
       <div class="tab-pane" id="operations_tab">
            <div class="table-responsive">
                <table class="table table-hover table-bordered table-theme table-permission">
                    <thead>
                        <tr>
                            <th width="60%" class="text-right"><?php echo $form->checkBoxList($model, 'permissions[operations_check_all]', array('operations_check_all' => ''), array('data-label' => 'Check All', 'onclick' => ' $("#operations_tab input:checkbox").prop("checked", $(this).prop("checked"));')); ?> select all </th>
                            <th class="text-center">View</th>             
                            <th class="text-center">Edit</th>
                            <th class="text-center">Add/Upload</th>
                            <th class="text-center">Download</th>
                            <th class="text-center">Delete</th>
                           
                          
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td width="60%" class="text-bold">Orders</td>
                            <td class="text-center">
                                <?php echo $form->checkBoxList($model, 'permissions[orders_view]', array('orders_view' => ''), array('title' => 'View')); ?> 
                                <?php //echo $form->checkBoxList($model, 'permissions[stock_view_expiry]', array('stock_view_expiry' => ''), array('title' => 'View Expiry')); ?>
                            </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[orders_edit]', array('orders_edit' => ''), array('title' => 'Edit')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[orders_upload]', array('orders_upload' => ''), array('title' => 'Upload')); ?></td>
                            
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[orders_download]', array('orders_download' => ''), array('title' => 'Download')); ?></td>
                            
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[orders_delete]', array('orders_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">Consolidated Orders</td>
                            <td></td>
                            <td></td>
                            <td class="text-center"> <?php echo $form->checkBoxList($model, 'permissions[consolidatedorders_add]', array('consolidatedorders_add' => ''), array('title' => 'Add Consolidated Order')); ?>
                            <?php // echo $form->checkBoxList($model, 'permissions[consolidatedorders_upload]', array('consolidatedorders_upload' => ''), array('title' => 'Upload')); ?></td>
                            <td class="text-center"> <?php echo $form->checkBoxList($model, 'permissions[consolidatedorders_download]', array('consolidatedorders_download' => ''), array('title' => 'Download')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[consolidatedorders_delete]', array('consolidatedorders_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">Inventory</td>
                            <td class="text-center">
                                <?php echo $form->checkBoxList($model, 'permissions[inventory_view]', array('inventory_view' => ''), array('title' => 'View')); ?>                   
                            </td>
                            <td> </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[inventory_upload]', array('inventory_upload' => ''), array('title' => 'Upload')); ?> </td>
                
                            <td></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[inventory_delete]', array('inventory_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                            
                            

                        </tr>
                       <tr>
                            <td width="60%" class="text-bold">GoodsIn</td>
                            <td class="text-center">
                                <?php echo $form->checkBoxList($model, 'permissions[goodsin_view]', array('goodsin_view' => ''), array('title' => 'View')); ?>                   
                            </td>
                            <td> </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[goodsin_upload]', array('goodsin_upload' => ''), array('title' => 'Upload')); ?> </td>
                
                            <td></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[goodsin_delete]', array('goodsin_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                            

                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">Unshipped</td>
                            <td class="text-center">
                                <?php echo $form->checkBoxList($model, 'permissions[unshipped_view]', array('unshipped_view' => ''), array('title' => 'View')); ?>                   
                            </td>
                            <td> </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[unshipped_upload]', array('unshipped_upload' => ''), array('title' => 'Upload')); ?> </td>
                
                            <td class="text-center"><?php //echo $form->checkBoxList($model, 'permissions[unshipped_download]', array('unshipped_download' => ''), array('title' => 'Download')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[unshipped_delete]', array('unshipped_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                            

                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">Backlog</td>
                            <td class="text-center">
                                <?php echo $form->checkBoxList($model, 'permissions[backlog_view]', array('backlog_view' => ''), array('title' => 'View')); ?>                   
                            </td>
                            <td> </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[backlog_upload]', array('backlog_upload' => ''), array('title' => 'Upload')); ?> </td>
                
                            <td class="text-center"><?php //echo $form->checkBoxList($model, 'permissions[backlog_download]', array('backlog_download' => ''), array('title' => 'Download')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[backlog_delete]', array('backlog_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                            

                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">Supply Confirmation</td>
                            <td class="text-center">
                                <?php //echo $form->checkBoxList($model, 'permissions[supply_view]', array('supply_view' => ''), array('title' => 'View')); ?>                   
                            </td>
                            <td> </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[supply_upload]', array('supply_upload' => ''), array('title' => 'Upload')); ?> 
                            <?php echo $form->checkBoxList($model, 'permissions[supply_salesupload]', array('supply_salesupload' => ''), array('title' => 'Sales Upload')); ?></td>
                
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[supply_download]', array('supply_download' => ''), array('title' => 'Download')); ?>
                            
                            </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[supply_delete]', array('supply_delete' => ''), array('title' => 'Delete')); ?></td>
                           

                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">Sales</td>
                            <td class="text-center">
                                <?php echo $form->checkBoxList($model, 'permissions[sales_view]', array('sales_view' => ''), array('title' => 'View')); ?>                   
                            </td>
                            <td> </td>
                            <td></td>
                
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[sales_download]', array('sales_download' => ''), array('title' => 'Download')); ?>
                            
                            </td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[sales_delete]', array('sales_delete' => ''), array('title' => 'Delete')); ?></td>
                           
                            

                        </tr>
                        
                    </tbody>
                </table>
            </div>

        </div>
              <div class="tab-pane" id="master_tab">
            <div class="table-responsive">
                <table class="table table-hover table-bordered table-theme table-permission">
                    <thead>
                        <tr>
                            <th class="text-right"><?php echo $form->checkBoxList($model, 'permissions[master_check_all]', array('master_check_all' => ''), array('data-label' => 'Check All', 'onclick' => ' $("#master_tab input:checkbox").prop("checked", $(this).prop("checked"));')); ?> select all</th>
                            <th class="text-center">Menu</th>
                            <th class="text-center">Add</th>
                            <th class="text-center">Edit</th> 
                            
                            <th class="text-center">Upload</th>
                            <th class="text-center">Continue</th>
                            <th class="text-center">Discontinue</th>
                             <th class="text-center">View</th>
                             <th class="text-center">Save</th>

                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="text-bold">GMC</td>
                             <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_view]', array('master_gmc_view' => ''), array('title' => 'View')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_add]', array('master_gmc_add' => ''), array('title' => 'Add')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_edit]', array('master_gmc_edit' => ''), array('title' => 'Edit')); ?></td>
                           <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_upload]', array('master_gmc_upload' => ''), array('title' => 'Upload')); ?>
                            <?php echo $form->checkBoxList($model, 'permissions[master_gmc_uploadprice]', array('master_gmc_uploadprice' => ''), array('title' => 'Uploadprice')); ?>
                            <?php echo $form->checkBoxList($model, 'permissions[master_gmc_uploadgoodsin]', array('master_gmc_uploadgoodsin' => ''), array('title' => 'Uploadgoodsin')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_continue]', array('master_gmc_continue' => ''), array('title' => ' Continue')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_discontinue]', array('master_gmc_discontinue' => ''), array('title' => 'Discontinue')); ?></td> 
                             <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_gmc_view_inventory]', array('master_gmc_view_inventory' => ''), array('title' => 'view')); ?></td> 
                         <td></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Distributers</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_distributer_view]', array('master_distributer_view' => ''), array('title' => 'View')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_distributer_add]', array('master_distributer_add' => ''), array('title' => 'Add')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_distributer_edit]', array('master_distributer_edit' => ''), array('title' => 'Edit')); ?></td>
                             <td> </td>
                             <td> </td>
                            <td></td>
                             <td></td>
                              <td></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Product Category</td>
                             <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_product_view]', array('master_product_view' => ''), array('title' => 'View')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_product_add]', array('master_product_add' => ''), array('title' => 'Add')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_product_edit]', array('master_product_edit' => ''), array('title' => 'Edit')); ?></td>
                             <td> </td>
                             <td> </td>
                            <td></td>
                             <td></td>
                              <td></td>
                        </tr> 
                        <tr>
                            <td class="text-bold">Region</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_region_view]', array('master_region_view' => ''), array('title' => 'View')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_region_add]', array('master_region_add' => ''), array('title' => 'Add')); ?></td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_region_edit]', array('master_region_edit' => ''), array('title' => 'Edit')); ?></td>
                             <td> </td>
                             <td> </td>
                              <td></td>
                            <td></td>
                             <td></td>
                        </tr>
                         <tr>
                            <td class="text-bold">Settings</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_setting_view]', array('master_setting_view' => ''), array('title' => 'View')); ?></td>
                            <td class="text-center"><?php //echo $form->checkBoxList($model, 'permissions[master_setting_add]', array('master_setting_add' => ''), array('title' => 'Add')); ?></td>
                            <td class="text-center"><?php //echo $form->checkBoxList($model, 'permissions[master_setting_edit]', array('master_setting_edit' => ''), array('title' => 'Edit')); ?></td>
                             <td> </td>
                             <td> </td>
                            <td></td>
                             <td></td>
                             <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[master_setting_save_view]', array('master_setting_save_view' => ''), array('title' => 'Save View')); ?></td>
                         </tr>
                        
                    </tbody>
                </table>
            </div>
        </div> 
        <div class="tab-pane" id="admin_tab">
            <div class="table-responsive">
                <table class="table table-hover table-bordered table-theme table-permission">
                    <thead>
                        <tr>
                            <th width="60%" class="text-right"><?php echo $form->checkBoxList($model, 'permissions[admin_check_all]', array('admin_check_all' => ''), array('title' => 'Check All', 'onclick' => ' $("#admin_tab input:checkbox").prop("checked", $(this).prop("checked"));')); ?> select all</th>
                            <th class="text-center">Add/Edit</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td width="60%" class="text-bold">Users</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[admin_users_add_edit]', array('admin_users_add_edit' => ''), array('title' => 'View')); ?></td>
                        </tr>
                        <tr>
                            <td width="60%" class="text-bold">User Group</td>
                            <td class="text-center"><?php echo $form->checkBoxList($model, 'permissions[admin_usergroup_add]', array('admin_usergroup_add' => ''), array('title' => 'View')); ?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>        
    </div>
</div>

<div class="clearfix">
    <div class="modal-footer">
        <button type="button" class="btn btn-danger" id="btn_usergroup_close" data-dismiss="modal">Close</button>
        <?php
        $url = Yii::app()->createUrl('usergroup/SaveUserGroup');

        echo CHtml::ajaxSubmitButton('Save', $url, array(
            'dataType' => 'json',
            'type' => 'post',
            'success' => 'function(data) {
                $("#loader").hide();
                var user_group_id= data.id;
                if(data.status=="success"){  
                     t8.success("User Group Details Saved Successfully!");
                     $("#btn_usergroup_close").click();
                     reloadTable("#tbl_usergroup_details");
                } else { 
                    $.each(data, function(key, val) {
                        $("#usergroup-form #"+key+"_em_").text(val).show(); 
                        t8.error(""+val+""); 
                    }); 
                } 
            }',
            'beforeSend' => 'function(){
                $("#loader").show();
                $("#usergroup-form .usergroup-save-btn").prop("disabled", true);
                $("#usergroup-form .usergroup-save-btn").addClass("btn_loding").removeClass("btn btn-primary"); 
                $(".errorMessage").hide(); 
            }',
            'complete' => 'function(){
                $("#usergroup-form .usergroup-save-btn").prop("disabled", false);
                $("#usergroup-form .usergroup-save-btn").addClass("btn btn-primary").removeClass("btn_loding"); 
            }'
        ), array('id' => 'mybtn' . rand(), 'class' => 'usergroup-save-btn btn btn-theme'));
        ?>
    </div>
</div>
        <?php $this->endWidget(); ?>
        
        <script>

function checkCharacters(filed_id){
    var flag=0;
     $("#"+filed_id).keypress(function (e) {
        var key = e.keyCode || e.which;       
        //Regular Expression
       
        var reg_exp = /^[A-Za-z0-9 ]+$/;
        //Validate Text Field value against the Regex.
        var is_valid = reg_exp.test(String.fromCharCode(key));
        if (!is_valid) {
          t8.error("No special characters Please!");
        }
        return is_valid;
      });
      
      
}
</script>
