<?php $this->view("includes/Inc_pagehead");?>
<style>
.m-form .m-form__group {
  margin-bottom: 0;
  padding-top: 0px;
  padding-bottom: 0px;
}
.treeview .item{
  font-size: 16px !important
}
.treeview i{
  font-size: 16px !important
}
</style>
<!-- Page Styles Goes Here-->
<link href="<?php echo base_url()?>template/assets/vendors/custom/treeview-checkbox/css/jquery.treeview.css" rel="stylesheet">

<?php $this->view("includes/Inc_navigation");?>
<div class="m-grid__item m-grid__item--fluid m-wrapper">
  <!-- BEGIN: Subheader -->
  <div class="m-subheader ">
    <div class="d-flex align-items-center">
      <div class="mr-auto">
        <h3 class="m-subheader__title m-subheader__title--separator">
          User Modules Management
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url()?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Home
              </span>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'user/roles_permissions' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                User Modules
              </span>
            </a>
          </li>
        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">

    <div class="m-portlet m-portlet--mobile m-portlet--info m-portlet--head-solid-bg m-portlet--bordered m-portlet--rounded">
      <div class="m-portlet__head">
        <div class="m-portlet__head-caption">
          <div class="m-portlet__head-title">
            <h3 class="m-portlet__head-text">
              User Modules
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">

        </div>
      </div>
      <form autocomplete="off" id="form_user_module_update" method="post" action="<?php echo base_url().'user/update_modules'?>" enctype="multipart/form-data"  class="m-form m-form--fit m-form--label-align-right">
        <div class="m-portlet__body">
          <div class="row">
            <div class="col-lg-2"></div>
            <div class="col-lg-8">
              <div class="form-group m-form__group">
                <label for="exampleInputEmail1">Role Name</label>
                <input value="<?php echo $role_data->role_title?>" type="text" readonly class="form-control m-input m-input--square" aria-describedby="emailHelp" >
                <span class="m-form__help"></span>
              </div>
              <?php if($modulebasedtype=='role'){ ?>
                <input type="hidden" name="user_id" value="<?php echo $role_data->role_id?>" />
                <input type="hidden" name="user_type" value="role" />
              <?php } ?>
              <?php if($modulebasedtype=='account'){ ?>
                <input type="hidden" name="user_id" value="<?php echo $selected_account?>" />
                <input type="hidden" name="role_id" value="<?php echo $role_data->role_id?>" />
                <input type="hidden" name="user_type" value="account" />
                <div class="form-group m-form__group">
                  <label for="user_name">
                    User
                  </label>
                  <div class="m-select2 m-select2--air m-select2--pill">
                    <select disabled style="width: 100%" required  class="form-control m-select2 m-select2-general" required id="user_add" name="user">
                      <option selected disabled>Select User</option>
                      <?php foreach ($user_accounts as $key => $value) { ?>
                        <option value="<?php echo $value->user_id ?>"><?php echo $value->user_alias ?></option>
                      <?php } ?>
                    </select>
                  </div>
                  <span class="m-form__help"></span>
                </div>
              <?php } ?>
              <div class="form-group m-form__group">
                <label for="exampleInputPassword1">Modules</label>
                <div class="m-section">
                  <div class="m-section__content">
                    <div class="m-demo" data-code-preview="true" data-code-html="true" data-code-js="false">
                      <div class="m-demo__preview" style="padding: 10px">
                        <div id="module_tree"></div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-lg-2"></div>
          </div>
        </div>
        <div class="m-portlet__foot m-portlet__foot--fit">
          <?php if($modulebasedtype=='role' || ($modulebasedtype=='account' && $selected_account!='')){ ?>
            <div class="m-form__actions">
              <button type="submit" style="float: right" class="btn btn-primary">Submit</button>
              <!-- <button type="reset" style="float: right" class="btn btn-secondary">Cancel</button> -->
            </div>
          <?php } ?>
        </div>
      </form>
    </div>
  </div>
</div>
</div>
<!-- end:: Body -->
<!-- begin::Footer -->

<?php $this->view("includes/Inc_footer");?>
<!-- end::Footer -->
</div>
<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>
<script src="<?php echo base_url()?>template/assets/vendors/custom/treeview-checkbox/js/jquery.treeview.js"></script>

<!--begin::Page Snippets -->
<script>
//$("#<?php //echo strtolower(str_replace(" ","_",$module_element_data->modulename))."_menu"; ?>").addClass('m-menu__item--active m-menu__item--open');
//$("#<?php //echo strtolower(str_replace(" ","_",$module_element_data->modulename))."_menu_".$module_element_data->elem_slno; ?>").addClass('m-menu__item--active');


$("#user_add").select2({
  placeholder: "Select User",
  dropdownParent: $("#form_user_module_update")
})
<?php if($modulebasedtype=='account' && $selected_account!=''){ ?>
  var selected_user = '<?php echo $selected_account?>';
  $("#user_add").val(selected_user).trigger('change');
  <?php } ?>
</script>
<script type="text/javascript">
<?php if($modulebasedtype=='role' || ($modulebasedtype=='account' && $selected_account!='')){ ?>

  var treeObject = [
    <?php foreach ($modules as $key0 => $value0) {
      foreach ($value0->menu_list as $key => $value) {
        $menuid = strtolower(str_replace(" ","_",$value->modulename))."_menu";
        if(sizeof($value->submenu)>0){ ?>
          {
            text:"<?php echo $value->modulename ?>", // Required
            checked:false, // Optional
            id:"<?php echo "module_".$value->moduleno ?>",
            opened:"<?php echo $value->opened ?>",
            otherDatas:"Other Datas", // Other Datas Optional
            children:[
              <?php foreach ($value->submenu as $key2 => $value2) {
                $user_element_data = $value2->user_element_data;
                ?>
                {
                  text:"<?php echo $value2->linkname ?>",
                  id:"<?php echo "element_".$value2->elementno ?>",
                  opened:"<?php echo $value2->opened ?>",
                  checked:false,
                  children:[
                    <?php
                    if($value2->addstatus==1){ ?>
                      {
                        text:"Add",
                        id:"<?php echo "element_".$value2->elementno."_add" ?>",
                        checked:<?php if(!empty($user_element_data) && $user_element_data->entrystatus==1){ echo "true";}else{ echo "false";} ?>
                      },
                      <?php }
                      if($value2->editstatus==1){ ?>
                        {text:"Edit",
                        id:"<?php echo "element_".$value2->elementno."_edit" ?>",
                        checked:<?php if(!empty($user_element_data) && $user_element_data->editstatus==1){ echo "true";}else{ echo "false";} ?>
                      },
                      <?php }
                      if($value2->viewstatus==1){ ?>
                        {text:"View",
                        id:"<?php echo "element_".$value2->elementno."_view" ?>",
                        checked:<?php if(!empty($user_element_data) && $user_element_data->viewstatus==1){ echo "true";}else{ echo "false";} ?>
                      },
                      <?php } ?>
                    ]
                  },
                  <?php } ?>
                ]
              },
              <?php }
            }
          }
          ?>
        ]
        var tw = new TreeView(treeObject,{
          showAlwaysCheckBox:true,
          fold:false,
          openAllFold:false
        });
        $("#module_tree").html(tw.root);
        jQuery(document).ready(function() {
          $(".item").each(function(){
            var id = $(this).data('id');
            var value = $(this).attr('check-value');
            $("#"+id).attr("check-value", value);
            $("#"+id).val(value);
          })
        })
        <?php } ?>

        </script>
