## -*- coding: utf-8 -*-
<%inherit file="/base/base.html"/>

<%def name="title()">
    ${_('My account')} ${c.rhodecode_user.username} &middot; ${c.rhodecode_name}
</%def>

<%def name="breadcrumbs_links()">
    ${_('My Account')}
</%def>

<%def name="page_nav()">
    ${self.menu('admin')}
</%def>

<%def name="main()">

<div class="box box-left">
    <!-- box / title -->
    <div class="title">
        ${self.breadcrumbs()}
    </div>
    <!-- end box / title -->
    ${c.form|n}
</div>

<div class="box box-right">
    <!-- box / title -->
    <div class="title">
        <h5>
        <input class="q_filter_box" id="q_filter" size="15" type="text" name="filter" placeholder="${_('quick filter...')}" value="" style="display: none"/>
        </h5>
         <ul class="links" style="color:#DADADA">
           <li>
             <span><a id="show_perms" class="link-white current" href="#perms">${_('My permissions')}</a> </span>
           </li>
           <li>
             <span><a id="show_my" class="link-white" href="#my">${_('My repos')}</a> </span>
           </li>
           <li>
             <span><a id="show_pullrequests" class="link-white" href="#pullrequests">${_('My pull requests')}</a> </span>
           </li>
         </ul>
    </div>
    <!-- end box / title -->
    ## permissions overview
    <div id="perms_container">
    <%namespace name="p" file="/base/perms_summary.html"/>
    ${p.perms_summary(c.perm_user.permissions)}
    </div>

    <div id="my_container" style="display:none">
        <div class="table yui-skin-sam" id="repos_list_wrap"></div>
        <div id="user-paginator" style="padding: 0px 0px 0px 20px"></div>
    </div>
    <div id="pullrequests_container" class="table" style="display:none">
        ## loaded via AJAX
        ${_('Loading...')}
    </div>
</div>

<script type="text/javascript">
pyroutes.register('admin_settings_my_pullrequests', "${url('admin_settings_my_pullrequests')}", []);

var show_perms = function(e){
    YUD.addClass('show_perms', 'current');
    YUD.removeClass('show_my','current');
    YUD.removeClass('show_pullrequests','current');

    YUD.setStyle('my_container','display','none');
    YUD.setStyle('pullrequests_container','display','none');
    YUD.setStyle('perms_container','display','');
    YUD.setStyle('q_filter','display','none');
}
YUE.on('show_perms','click',function(e){
    show_perms();
})

var show_my = function(e){
    YUD.addClass('show_my', 'current');
    YUD.removeClass('show_perms','current');
    YUD.removeClass('show_pullrequests','current');

    YUD.setStyle('perms_container','display','none');
    YUD.setStyle('pullrequests_container','display','none');
    YUD.setStyle('my_container','display','');
    YUD.setStyle('q_filter','display','');
    if(!YUD.hasClass('show_my', 'loaded')){
        table_renderer(${c.data |n});
        YUD.addClass('show_my', 'loaded');
    }
}
YUE.on('show_my','click',function(e){
    show_my(e);
})

var show_pullrequests = function(e){
    YUD.addClass('show_pullrequests', 'current');
    YUD.removeClass('show_my','current');
    YUD.removeClass('show_perms','current');

    YUD.setStyle('my_container','display','none');
    YUD.setStyle('perms_container','display','none');
    YUD.setStyle('pullrequests_container','display','');
    YUD.setStyle('q_filter','display','none');

    var url = pyroutes.url('admin_settings_my_pullrequests');
    if(YUD.get('show_closed') && YUD.get('show_closed').checked) {
        var url = pyroutes.url('admin_settings_my_pullrequests', {'pr_show_closed': '1'});
    }
    ypjax(url, 'pullrequests_container', function(){
        YUE.on('show_closed','change',function (e) {
            show_pullrequests(e);
        });
    });
}
YUE.on('show_pullrequests','click',function(e){
    show_pullrequests(e)
})

var tabs = {
    'perms': show_perms,
    'my': show_my,
    'pullrequests': show_pullrequests
}
var url = location.href.split('#');
if (url[1]) {
    //We have a hash
    var tabHash = url[1];
    var func = tabs[tabHash]
    if (func){
        func();
    }
}

function table_renderer(data){
    var myDataSource = new YAHOO.util.DataSource(data);
    myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;

    myDataSource.responseSchema = {
        resultsList: "records",
        fields: [
            {key:"menu"},
            {key:"raw_name"},
            {key:"name"},
            {key:"last_changeset"},
            {key:"action"},
            ]
        };
    myDataSource.doBeforeCallback = function(req,raw,res,cb) {
        // This is the filter function
        var data     = res.results || [],
            filtered = [],
            i,l;

        if (req) {
            req = req.toLowerCase();
            for (i = 0; i<data.length; i++) {
                var pos = data[i].raw_name.toLowerCase().indexOf(req)
                if (pos != -1) {
                    filtered.push(data[i]);
                }
            }
            res.results = filtered;
        }
        return res;
    }

      // main table sorting
      var myColumnDefs = [
          {key:"menu",label:"",sortable:false,className:"quick_repo_menu hidden"},
          {key:"name",label:"${_('Name')}",sortable:true,
              sortOptions: { sortFunction: nameSort }},
          {key:"last_changeset",label:"${_('Tip')}",sortable:true,
              sortOptions: { sortFunction: revisionSort }},
          {key:"action",label:"${_('Action')}",sortable:false},
      ];

      var myDataTable = new YAHOO.widget.DataTable("repos_list_wrap", myColumnDefs, myDataSource,{
        sortedBy:{key:"name",dir:"asc"},
        paginator: YUI_paginator(50, ['user-paginator']),

        MSG_SORTASC:"${_('Click to sort ascending')}",
        MSG_SORTDESC:"${_('Click to sort descending')}",
        MSG_EMPTY:"${_('No records found.')}",
        MSG_ERROR:"${_('Data error.')}",
        MSG_LOADING:"${_('Loading...')}",
      }
      );
      myDataTable.subscribe('postRenderEvent',function(oArgs) {
          tooltip_activate();
          quick_repo_menu();
      });

      var filterTimeout = null;

      updateFilter = function() {
          // Reset timeout
          filterTimeout = null;

          // Reset sort
          var state = myDataTable.getState();
          state.sortedBy = {key:'name', dir:YAHOO.widget.DataTable.CLASS_ASC};

          // Get filtered data
          myDataSource.sendRequest(YUD.get('q_filter').value,{
              success : myDataTable.onDataReturnInitializeTable,
              failure : myDataTable.onDataReturnInitializeTable,
              scope   : myDataTable,
              argument: state
          });

      };
      YUE.on('q_filter','click',function(){
          if(!YUD.hasClass('q_filter', 'loaded')){
              //TODO: load here full list later to do search within groups
              YUD.addClass('q_filter', 'loaded');
          }
       });

      YUE.on('q_filter','keyup',function (e) {
          clearTimeout(filterTimeout);
          filterTimeout = setTimeout(updateFilter,600);
      });

    }
</script>
</%def>
