var Pager=require("./pager.js"),Table=require("./table.js"),Form=require("./form.js"),Dialog=require("./dialogPC.js"),Lister=function(a,t){var e=null;"table"in a&&(e=a.table);var i=$(e),r=null;"pager"in a&&(r=a.pager);var n=$(r),c=null;"search"in a&&(c=a.search);var s=$(c),d=this,l={page:1,pageSize:10,field:[],order:[],search:{}},h=$.extend({server:"/path/to/server",entityCheckbox:!1,listRendered:null,listRefreshing:null},t),o=new Pager(n,function(a){l.page=a,d.load(!1)}),u=new Table(i,{entityCheckbox:h.entityCheckbox});i.on("click","thead > tr > th",function(){if(!$(this).is("[data-orderAble]"))return!0;var a=$(this).attr("data-item"),t=$(this).attr("data-order");return t||(t="asc"),t="asc"==t?"desc":"asc",l.order[0]=[a,t],l.page=1,d.load(!0),!1}),s.on("click","[data-search-trigger]",function(){return d.setSearch(),d.load(!1),!1}),s.on("click","[data-search-button]",function(){return d.setSearch(),d.load(!1),!1}),s.on("click","[data-reset-search-button]",function(){return d.resetSearch(),d.load(!0),!1}),this.init=function(){var a=window.location.hash;0===a.indexOf("#")&&(a=a.substring(1));try{var t=JSON.parse(a);for(var e in t)e in l&&(l[e]=t[e])}catch(a){}},this.initSearch=function(){s.find("[data-search]").each(function(a,t){var e=$(t),i=e.attr("data-search");if(i in l.search){var r=e.data("widget");r?r.val(l.search[i]):e.val(l.search[i])}})},this.setSearch=function(){l.search=[],s.find("[data-search-field]").attr("data-lister-scanning","true"),s.find("[data-search-field]").each(function(a,t){var e=$(t);if(e.attr("data-lister-scanning")){e.attr("data-lister-scanning","");var i=e.attr("data-search-field"),r=e.attr("data-search-type"),n=null,c=e.data("widget");if(c)n=c.val();else{if((e.is("input[type=checkbox]")||e.is("input[type=radio]"))&&!e.is(":checked"))return;n=e.val()}if(""!==n&&null!==n){var d={};d[r]=n;var h=e.attr("data-search-group"),o=e.attr("data-search-exp");if(o&&(d.exp=o),h){d.group=h;s.find("[data-search-field="+i+"][data-search-group="+h+"]").each(function(a,t){var e=$(t);if(e.attr("data-lister-scanning")){e.attr("data-lister-scanning","");var i=e.attr("data-search-type"),r=e.attr("data-search-exp"),n=null,c=e.data("widget");n=c?c.val():e.is("input[type=checkbox]")||e.is("input[type=radio]")?e.is(":checked")?e.val():"":e.val(),""!==n&&(d[i]=n,r&&(d.exp=r))}})}var u={};u[i]=d,l.search.push(u)}}})},this.resetSearch=function(){l.search=[],s.find("[data-search-field]").attr("data-lister-scanning","true"),s.find("[data-search-field]").each(function(a,t){var e=$(t);if(e.attr("data-lister-scanning")){e.attr("data-lister-scanning","");var i=e.attr("data-search-field"),r=(e.attr("data-search-type"),e.data("widget"));r?r.val(null):e.is("input[type=checkbox]")||e.is("input[type=radio]")?e.is(":checked")&&e.prop("checked",!1):e.val(null);var n=e.attr("data-search-group");if(n){s.find("[data-search-field="+i+"][data-search-group="+n+"]").each(function(a,t){var e=$(t);if(e.attr("data-lister-scanning")){e.attr("data-lister-scanning","");var i=(e.attr("data-search-type"),e.attr("data-search-exp"),e.data("widget"));i?i.val(null):e.is("input[type=checkbox]")||e.is("input[type=radio]")?e.is(":checked")&&e.prop("checked",!1):e.val(null)}})}}})},this.setParam=function(a,t){a in l&&(l[a]=t)},this.setPageSize=function(a){l.pageSize=a},this.setPage=function(a){l.page=a},this.getParam=function(){return l},this.getPager=function(){return o},this.getAllIds=function(){var a=[];return i.find('[type=checkbox][name="__id[]"]').each(function(t,e){a.push($(e).val())}),a},this.getSelectedIds=function(){var a=[];return i.find('[type=checkbox][name="__id[]"]:checked').each(function(t,e){a.push($(e).val())}),a},this.setSelectedIds=function(a){i.find('[type=checkbox][name="__id[]"]').each(function(t,e){a.indexOf($(e).val())>=0&&$(e).prop("checked",!0)})},this.load=function(a){h.listRefreshing&&h.listRefreshing(),a?u.loading():Dialog.loadingOn(),$.post(h.server,l).done(function(t){window.location.replace("#"+JSON.stringify(l)),a||Dialog.loadingOff(),Form.defaultCallback(t,{success:function(a){u.render(a.data),o.render(a.data.total,a.data.pageSize,a.data.page),h.listRendered&&h.listRendered()}})}).fail(function(t){a||Dialog.loadingOff(),Form.defaultCallback(t)})},this.init(),this.initSearch(),this.load(!0)};module.exports=Lister;