Advertisement
Uno-Dan

Untitled

Nov 4th, 2020
2,286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. orders = null;
  3. products = null;
  4. contacts = null;
  5. customers = null;
  6. fade_time = 500;
  7.  
  8. jQuery(document).ready( function($) {
  9.     function getFormData(form) {
  10.         var unindexed_array = form.serializeArray();
  11.         var indexed_array = {};
  12.  
  13.         $.map(unindexed_array, function(n, i){
  14.             indexed_array[n['name']] = n['value'];
  15.         });
  16.         return indexed_array;
  17.     }
  18.  
  19.     function get_zones() {
  20.         var select = $('select[name=country_id]');
  21.         var id = select.val();
  22.        
  23.         var request = {
  24.             action: 'get_zones',
  25.             data: select.find('option[value=' + id + ']').attr('data-code'),
  26.             nonce: contacts.nonce
  27.         };
  28.        
  29.         $.post(contacts.ajaxurl, request, function( response ) {
  30.             $('select[name=location_id]').find('option').remove();
  31.            
  32.             var select = $('select[name=zone_id]');
  33.             select.find('option').remove();
  34.            
  35.             if( response ) {
  36.                 select.append($('<option value="0"></option>'));
  37.                                
  38.                 for (var key in response) {
  39.                     select.append($('<option value="' +
  40.                             response[key].id + '" data-code="' +
  41.                             response[key].code + '">' +
  42.                             response[key].name + '</option>'));
  43.                 }
  44.             }
  45.         });
  46.     }
  47.  
  48.     function get_locations() {
  49.         var select = $('select[name=zone_id]');
  50.         var id = select.val();
  51.        
  52.         var request = {
  53.             action: 'get_locations',
  54.             data: select.find('option[value=' + id + ']').attr('data-code'),
  55.             nonce: contacts.nonce
  56.         };
  57.        
  58.         $.post(contacts.ajaxurl, request, function( response ) {
  59.             var select = $('select[name=location_id]');
  60.             select.find('option').remove();
  61.            
  62.             if( response ) {
  63.                 select.append($('<option value="0"></option>'));
  64.                                
  65.                 for (var key in response) {
  66.                     select.append($('<option value="' +
  67.                             response[key].id + '" data-code="' +
  68.                             response[key].code + '">' +
  69.                             response[key].name + '</option>'));
  70.                 }
  71.             }
  72.         });
  73.     }
  74.  
  75.     class Orders {
  76.         constructor( cfg ) {
  77.             this.form = null;
  78.             this.page = cfg.page;
  79.             this.form_data = null;
  80.             this.fade_time = cfg.fade_time;
  81.             this.nonce = mettatealib.nonce;
  82.             this.ajaxurl = mettatealib.ajaxurl
  83.         }
  84.  
  85.         on_change_customer() {
  86.             var customer_id = $(this).val();
  87.            
  88.             var request = {
  89.                 'action': 'orders_change_customer',
  90.                 'data': customer_id,
  91.                 'nonce': orders.nonce
  92.             };
  93.            
  94.             $.post(orders.ajaxurl, request, function( response ) {
  95.                 var select = $('.form.orders select[name=branch]');
  96.                 select.find('option').remove();
  97.                 select.append('<option value="0"></option>');
  98.                 response.forEach(function(element, i) {
  99.                     select.append( $(
  100.                         '<option value="' +  element.id + '">' +
  101.                         element.branch + '</option>') );
  102.                 });
  103.             });
  104.         }
  105.  
  106.         on_change_product() {
  107.             var product_id = $(this).val();
  108.             var request = {
  109.                 'action': 'orders_change_product',
  110.                 'data': product_id,
  111.                 'nonce': orders.nonce
  112.             };
  113.             $.post(orders.ajaxurl, request, function( response ) {
  114.                var rp = response;
  115.                form = $('.form.orders');
  116.                form.find('input[name=price]').val(rp.unit_price);
  117.                form.find('input[name=markup]').val(rp.markup);
  118.                form.find('input[name=discount]').val(rp.discount);
  119.                orders.update_total();
  120.             });
  121.         }
  122.  
  123.         update_total() {
  124.             var form_orders = $('.form.orders');
  125.  
  126.             var price = parseFloat(form_orders.find('input[name=price]').val());
  127.             var quantity = parseInt(form_orders.find('select[name=quantity]').val());
  128.             var discount = parseInt(form_orders.find('input[name=discount]').val());
  129.             var markup = parseInt(form_orders.find('input[name=markup]').val());
  130.             var freight = parseInt(form_orders.find('input[name=freight]').val());
  131.             var sales_tax = parseInt(form_orders.find('input[name=sales_tax]').val());
  132.  
  133.             price = price ? price : 0;
  134.             quantity = quantity ? quantity : 0;
  135.             discount = discount ? discount : 0;
  136.             markup = markup ? markup : 0;
  137.             freight = freight ? freight : 0;
  138.             sales_tax = sales_tax ? sales_tax : 0;
  139.  
  140.             var cost = price * quantity;
  141.             if (markup) {
  142.                 cost = cost + cost * (markup/100);
  143.             }
  144.             cost = + cost - (cost * (discount/100));
  145.  
  146.             var total = (cost * (sales_tax/100)) + freight + cost;
  147.             form_orders.find('input[name=total]').val(total.toFixed(2));
  148.         }
  149.        
  150.         add_order(event = 0) {
  151.             var parent = 0;
  152.             var form = $('#orders');
  153.             var fd = getFormData( form );
  154.            
  155.             var obj = {};
  156.             form.find('tbody tr').each(function(i) {
  157.                 obj[i] = {};
  158.                
  159.                 $(this).find('td').each(function() {
  160.                     var attr = $(this).attr('data-id');
  161.                     var key = $(this).attr('data-key');
  162.                    
  163.                     if (typeof attr !== typeof undefined && attr !== false) {
  164.                         obj[i][key] = parseInt(attr.replace(/['"]+/g, ''));
  165.                     } else {
  166.                         if ($(this).find('input').length) {
  167.                             obj[i][key] = $(this).find('input').is(':checked') ? 1 : 0;
  168.                         } else {
  169.                             obj[i][key] = $(this).text();
  170.                         }
  171.                     }
  172.                 });
  173.             });
  174.                
  175.             fd['details'] = obj;
  176.            
  177.             if (event) {
  178.                 parent = event.data.parent;
  179.                 fd['parent_id'] = event.data.parent_id;
  180.             }
  181.            
  182.             var aside = form.find('aside');
  183.             aside.slideUp().removeAttr('class').find('dd').text('');
  184.            
  185.             var timeout = null;
  186.             aside.on('click', 'i', function() {
  187.                 clearTimeout(timeout);
  188.                 aside.slideUp().removeAttr('class').find('dd').text('');
  189.             });  
  190.            
  191.             var request = {
  192.                 'action': 'add_order',
  193.                 'data': fd,
  194.                 'nonce': orders.nonce
  195.             };
  196.             console.log(request);
  197.             $.post(orders.ajaxurl, request, function( response ) {
  198.                 console.log(response)
  199.                 var tbody = $('#orders_page').find('table.orders tbody');
  200.                
  201.                 if (response.result) {
  202.                     parent.find('form section').attr('data-id', response.id);
  203.                     aside.addClass('success');
  204.                     aside.find('dd').text('Success, new order has been added.');
  205.                     aside.slideDown(function(){
  206.                         timeout = setTimeout(function () {
  207.                             aside.slideUp();
  208.                         }, 5000);
  209.                     });
  210.                 } else {
  211.                     aside.addClass('error');
  212.                     aside.find('dd').text('Error, ' + response.last_error + '.');
  213.                     aside.slideDown(function(){
  214.                         timeout = setTimeout(function () {
  215.                             aside.slideUp();
  216.                         }, 5000);
  217.                     });
  218.                 }
  219.                 response.details.forEach(function(entry) {
  220.                
  221.                     tbody.append($(`
  222.                         <tr>
  223.                             <td><input type="checkbox" /></td>
  224.                             <td>` +  entry.order_number + `</td>
  225.                             <td>` +  entry.product + `</td>
  226.                             <td>` +  entry.quantity + `</td>
  227.                             <td>` +  entry.fulfilled + `</td>
  228.                             <td>` +  entry.paid + `</td>
  229.                             <td>` +  entry.customer + `</td>
  230.                             <td>` +  entry.branch + `</td>
  231.                             <td>` +  entry.order_date + `</td>
  232.                             <td>` +  entry.ship_date + `</td>
  233.                             <td>` +  entry.bill_date + `</td>
  234.                             <td>` +  entry.freight + `</td>
  235.                             <td>` +  entry.markup + `</td>
  236.                             <td>` +  entry.discount + `</td>
  237.                             <td>` +  entry.sales_tax + `</td>
  238.                             <td>` +  entry.total + `</td>
  239.                             <td>` +  entry.notes + `</td>
  240.                         </tr>
  241.                     `));
  242.                 });
  243.                
  244.             });
  245.            
  246.             $(".actions.orders :input").attr("disabled", false);
  247.            
  248.             return this;
  249.         }
  250.          
  251.         view_customers() {
  252.            
  253.         }
  254.        
  255.         view_customer() {
  256.             var customer_id = $(this).parent().find('select').val();
  257.             if ( customer_id === '0') {
  258.                 return;
  259.             }
  260.             var request = {
  261.                 action: 'view_customer',
  262.                 data: {id: customer_id, action: 'edit'},
  263.                 nonce: orders.nonce
  264.             };
  265.             var wrapper = $('.the-content>.wrapper');
  266.             $.post(orders.ajaxurl, request, function( response ) {
  267.                
  268.                 var form = $( response );
  269.                 wrapper.append( form );
  270.                
  271.                 form.find("tbody tr:odd").css("background-color", "#c3e2ff");
  272.                 form.find("tbody tr:even").css("background-color", "#ffffff");
  273.                
  274.                 form.fadeIn(orders.fade_time, function() {
  275.                    
  276.                     form.find('button.save').on('click', customers.edit_customer);
  277.                     form.find('button.delete').on('click', customers.delete_customer);
  278.                     form.find('button.cancel').on('click', customers.cancel_customer_form);
  279.                    
  280.                     form.find('table.branches tr td:first-child a').on('click', function() {
  281.                         var branch_id = $(this).closest('tr').attr('data-id');
  282.                         customers.edit_branch_form(branch_id);
  283.                     });
  284.                 });
  285.             });
  286.         }
  287.        
  288.         view_branch() {
  289.             var branch_id = $(this).parent().find('select').val();
  290.            
  291.             if ( branch_id === '0' ) {
  292.                 return;
  293.             }
  294.             var customer_id = $(this).closest('.row').find('select[name=customers]').val();
  295.             var request = {
  296.                 action: 'view_branch',
  297.                 data: {action: 'edit', id: customer_id, branch_id: branch_id},
  298.                 nonce: orders.nonce
  299.             };
  300.            
  301.             var wrapper = $('.the-content>.wrapper');
  302.             $.post(orders.ajaxurl, request, function( response ) {
  303.                 if ( response ) {
  304.                     var form = $( response );
  305.                     wrapper.append( form );
  306.                     form.find("tbody tr:odd").css("background-color", "#c3e2ff");
  307.                     form.find("tbody tr:even").css("background-color", "#ffffff");
  308.                
  309.                     form.fadeIn(orders.fade_time, function(){
  310.                        
  311.                         form.find('button.save').on( "click", {
  312.                             customer_id: customer_id,
  313.                             branch_id: branch_id },
  314.                         customers.edit_branch );
  315.                        
  316.                         form.on('click', 'button.delete', customers.delete_branch);
  317.                         form.on('click', 'button.cancel', customers.cancel_branch_form);
  318.                        
  319.                     });
  320.                 }
  321.             });
  322.         }
  323.        
  324.         insert_product( data ) {
  325.             var fd = data;
  326.             var html = '<tr data-id="' + 0 + '">';
  327.            
  328.             var paid = fd.paid ? 'checked=" checked"' : '';
  329.             var fulfilled = fd.fulfilled ? 'checked=" checked"' : '';
  330.            
  331.             html += `
  332.                 <td data-key="delete"><i class="fa fa-times"></i></td>
  333.                 <td data-key="fulfilled"><input type="checkbox"` + fulfilled + ` name="fulfilled"></td>
  334.                 <td data-key="paid"><input type="checkbox"` + paid + ` name="paid"></td>
  335.                 <td data-key="product_id" data-id="'` + fd.product_id + `'">` + fd.product + `</td>
  336.                 <td data-key="total">` + fd.total + `</td>
  337.                 <td data-key="price">` + fd.price + `</td>
  338.                 <td data-key="quantity">` + fd.quantity + `</td>
  339.                 <td data-key="order_number">` + fd.order_number + `</td>
  340.                 <td data-key="markup">` + fd.markup + `</td>
  341.                 <td data-key="discount">` + fd.discount + `</td>
  342.                 <td data-key="customer_id" data-id="'` + fd.customer_id + `'">` + fd.customer + `</td>
  343.                 <td data-key="branch_id" data-id="'` + fd.branch_id + `'">` + fd.branch + `</td>
  344.                 <td data-key="order_date">` + fd.order_date + `</td>
  345.                 <td data-key="ship_date">` + fd.ship_date + `</td>
  346.                 <td data-key="bill_date">` + fd.bill_date + `</td>
  347.                 <td data-key="freight">` + fd.freight + `</td>
  348.                 <td data-key="sales_tax">` + fd.sales_tax + `</td>
  349.                 <td data-key="notes">` + fd.notes + `</td>
  350.             `;
  351.            
  352.             html += '</tr>';
  353.            
  354.             return html;
  355.         }
  356.        
  357.         add_product() {
  358.             var form = $('.form.orders');
  359.             var aside = form.find('form>aside');
  360.             aside.slideUp().removeAttr('class').find('dd').text('');
  361.            
  362.             var fd = getFormData( form.find('form') );
  363.            
  364.             var customer_id = parseInt(fd.customers);
  365.             var branch_id = parseInt(fd.branch);
  366.            
  367.             var timeout = null;
  368.             aside.on('click', 'i', function() {
  369.                 clearTimeout(timeout);
  370.                 aside.slideUp().removeAttr('class').find('dd').text('');
  371.             });            
  372.            
  373.             if ( ! customer_id ) {
  374.                 aside.removeAttr('class');
  375.                 aside.addClass('error');
  376.                 aside.find('dd').text('Error, you must select a customer before you can a product.');
  377.                 aside.slideDown(function(){
  378.                     timeout = setTimeout(function () {
  379.                         aside.slideUp();
  380.                     }, 5000);
  381.                 });
  382.                 return;
  383.             } else if ( ! branch_id ) {
  384.                 aside.removeAttr('class');
  385.                 aside.addClass('error');
  386.                 aside.find('dd').text('Error, you must select a branch before you can a product.');
  387.                 aside.slideDown(function(){
  388.                     timeout = setTimeout(function () {
  389.                         aside.slideUp();
  390.                     }, 5000);
  391.                 });
  392.                 return;
  393.             } else if ( ! fd.order_number ) {
  394.                 aside.removeAttr('class');
  395.                 aside.addClass('error');
  396.                 aside.find('dd').text('Error, you must enter an order number before you can a product.');
  397.                 aside.slideDown(function(){
  398.                     timeout = setTimeout(function () {
  399.                         aside.slideUp();
  400.                     }, 5000);
  401.                 });
  402.                 return;
  403.             }
  404.            
  405.             fd.customer_id = customer_id;
  406.             fd.branch_id = branch_id;
  407.             fd.product_id = parseInt(fd.products);
  408.             fd.customer = form.find('.field.customer option[value= ' +customer_id+ ']').text();
  409.             fd.branch = form.find('.field.branch option[value= ' +branch_id+ ']').text();
  410.             fd.product = form.find('.field.product option[value= ' +fd.product_id+ ']').text();
  411.            
  412.             var row = $(orders.insert_product( fd ) );
  413.             var tbody = form.find('table.products tbody');
  414.             tbody.append(row);
  415.                        
  416.             tbody.find("tr:odd").css("background-color", "#c3e2ff");
  417.             tbody.find("tr:even").css("background-color", "#ffffff");
  418.            
  419.             row.find('input[name=fulfilled]').on('change', function() {
  420.                 var fulfilled = true;
  421.                 $(this).closest('tbody').find('input[name=fulfilled]').each(function () {
  422.                    if (! $(this).is(':checked')) {
  423.                        fulfilled = false;
  424.                        return false;
  425.                    }
  426.                 });
  427.                
  428.                 if (fulfilled) {
  429.                     $('.form.orders .field.fulfilled input').prop('checked', true);
  430.                 } else {
  431.                     $('.form.orders .field.fulfilled input').prop('checked', false);
  432.                 }
  433.            });
  434.            
  435.             row.find('input[name=paid]').on('change', function() {
  436.                 var paid = true;
  437.                 $(this).closest('tbody').find('input[name=paid]').each(function () {
  438.                    if (! $(this).is(':checked')) {
  439.                        paid = false;
  440.                        return false;
  441.                    }
  442.                 });
  443.                
  444.                 if (paid) {
  445.                     $('.form.orders .field.paid input').prop('checked', true);;
  446.                 } else {
  447.                     $('.form.orders .field.paid input').prop('checked', false);;
  448.                 }
  449.            });
  450.         }
  451.        
  452.         add_order_form(event = 0) {
  453.             var form_orders = $('.form.orders');
  454.             if ( form_orders.length ) return;
  455.            
  456.             var request = {
  457.                 action: 'form_orders',
  458.                 data: {
  459.                     action: 'add',  
  460.                     product_id: 1,
  461.                     quantity: 5,
  462.                     freight: 0.00,
  463.                     tax_percent: 13
  464.                 },
  465.                 nonce: orders.nonce
  466.             };
  467.  
  468.             $.post(orders.ajaxurl, request, function( response ) {
  469.                 if ( response ) {
  470.                     $(".actions.orders :input").attr("disabled", true);
  471.                    
  472.                     var form = $( response );
  473.                     orders.form = form;
  474.                     var parent = event.data  ? event.data.parent : form;
  475.                     var parent_id = event.data  ? event.data.parent_id : 0;
  476.            
  477.                     form.find( "input.date" ).datepicker({ dateFormat: 'M dd, yy' });
  478.                     form.find( "input.date[name=order_date]" ).datepicker("setDate", new Date());
  479.                
  480.                     $('.the-content>.wrapper').append( form );
  481.                    
  482.                     form.fadeIn(orders.fade_time);  
  483.                    
  484.                     form.find('button.save').on( "click", {
  485.                         parent: parent,
  486.                         parent_id: parent_id
  487.                     }, orders.add_order );
  488.  
  489.                     form.find('button.cancel').on( "click", {
  490.                         parent: parent
  491.                     }, orders.cancel_order_form );
  492.  
  493.                     form.find('button.add_product').on( "click", orders.add_product);
  494.                     form.find('select[name=customers]').on( "change", orders.on_change_customer );
  495.                     form.find('select[name=products]').on( "change", orders.on_change_product );
  496.                    
  497.                     form.find('.field.branch i.view').on( "click", orders.view_branch );
  498.                     form.find('.field.customer i.view').on( "click", orders.view_customer );
  499.                    
  500.                    
  501.                     form.find('input[name=price]').on('change', orders.update_total);
  502.                     form.find('select[name=quantity]').on('change', orders.update_total);
  503.                     form.find('input[name=markup]').on('change', orders.update_total);
  504.                     form.find('input[name=discount]').on('change', orders.update_total);
  505.                     form.find('input[name=freight]').on('change', orders.update_total);
  506.                     form.find('input[name=sales_tax]').on('change', orders.update_total);
  507.                    
  508.                     form.find('.field.branch i.view').hover( function() {
  509.                         if ($(this).parent().find('select').val() === '0') {
  510.                             $(this).css('color', 'red');
  511.                         } else {
  512.                             $(this).css('color', 'green');
  513.                         }
  514.                     });
  515.                     form.find('.field.customer i.view').hover( function() {
  516.                         if ($(this).parent().find('select').val() === '0') {
  517.                             $(this).css('color', 'red');
  518.                         } else {
  519.                             $(this).css('color', 'green');
  520.                         }
  521.                     });
  522.                    
  523.                     form.find('.field.branch i.view').mouseout( function() {
  524.                         $(this).css('color', '#0c5971');
  525.                     });
  526.                     form.find('.field.customer i.view').mouseout( function() {
  527.                         $(this).css('color', '#0c5971');
  528.                     });
  529.                    
  530.                     form.find('img.clear').on('click', function(){
  531.                         $(this).closest('form').trigger('reset');
  532.                     });
  533.                 }
  534.             });
  535.            
  536.             return this;
  537.         }
  538.  
  539.         cancel_order_form(event) {
  540.            
  541.             var parent = event.data.parent;
  542.            
  543.             $('.form.orders').fadeOut(orders.fade_time, function() {
  544.                 if (parent) {
  545.                     parent.fadeIn(orders.fade_time);
  546.                 }
  547.                 $(this).remove();
  548.             });
  549.            
  550.             $(".actions.orders :input").attr("disabled", false);
  551.             return this;
  552.         };
  553.     }
  554.    
  555.     class Products {
  556.         constructor( cfg ) {
  557.             this.form = null;
  558.             this.page = cfg.page;
  559.             this.form_data = null;
  560.             this.fade_time = cfg.fade_time;
  561.             this.nonce = mettatealib.nonce;
  562.             this.ajaxurl = mettatealib.ajaxurl
  563.         }
  564.  
  565.         get_html_row( data ) {
  566.             return `
  567.             <tr data-id="` + data.product_id + `">
  568.                 <td><input type="checkbox" /></td>
  569.                 <td>` + data.name + `</td>
  570.                 <td>` + data.description + `</td>
  571.                 <td>` + data.unit_price + `</td>
  572.                 <td>` + data.markup + `</td>
  573.                 <td>` + data.discount + `</td>
  574.                 <td>` + data.units_in_stock + `</td>
  575.                 <td>` + data.reorder_level + `</td>
  576.                 <td>` + data.unit_type + `</td>
  577.                 <td>` + data.unit_size + `</td>
  578.                 <td>` + data.unit_weight + `</td>
  579.                 <td>` + data.quantity_per_unit + `</td>
  580.                 <td>` + data.units_on_order + `</td>
  581.                 <td>` + data.notes + `</td>
  582.             </tr>
  583.             `;
  584.         }
  585.  
  586.         add_product(event = 0) {
  587.             var parent = 0;
  588.             var form = $('#products');
  589.             var form_data = getFormData( form );
  590.            
  591.             if (event) {
  592.                 parent = event.data.parent;
  593.                 form_data['parent_id'] = event.data.parent_id;
  594.             }
  595.            
  596.             var aside = form.find('>aside');
  597.             aside.slideUp();
  598.  
  599.             var timeout = null;
  600.             aside.on('click', 'i', function() {
  601.                 clearTimeout(timeout);
  602.                 aside.slideUp(function(){
  603.                    aside.removeAttr('class');
  604.                    aside.find('dd').text('');
  605.                 });
  606.             });
  607.                        
  608.             var request = {
  609.                 'action': 'add_product',
  610.                 'data': form_data,
  611.                 'nonce': products.nonce
  612.             };
  613.             $.post(products.ajaxurl, request, function( response ) {
  614.                 console.log(response);
  615.                 if ( response.result ) {
  616.                     var tbody = $('#products_page').find('table.products tbody');
  617.                     parent.find('form section').attr('data-id', response.id);
  618.  
  619.                     tbody.append($(`
  620.                         <tr data-id="` + response.id + `">
  621.                             <td><input type="checkbox" /></td>
  622.                             <td>` +  response.name + `</td>
  623.                             <td>` +  response.description + `</td>
  624.                             <td>` +  response.unit_price + `</td>
  625.                             <td>` +  response.markup + `</td>
  626.                             <td>` +  response.discount + `</td>
  627.                             <td>` +  response.units_in_stock + `</td>
  628.                             <td>` +  response.reorder_level + `</td>
  629.                             <td>` +  response.unit_type + `</td>
  630.                             <td>` +  response.unit_size + `</td>
  631.                             <td>` +  response.unit_weight + `</td>
  632.                             <td>` +  response.quantity_per_unit + `</td>
  633.                             <td>` +  response.units_on_order + `</td>
  634.                             <td>` +  response.notes + `</td>
  635.                         </tr>
  636.                     `));
  637.                     tbody.find("tr:odd").css("background-color", "#c3e2ff");
  638.                    
  639.                     aside.removeAttr('class');
  640.                     aside.addClass('success');
  641.                     aside.find('dd').text('Success, new product has been added.');
  642.                     aside.slideDown(function(){
  643.                         timeout = setTimeout(function () {
  644.                             aside.slideUp();
  645.                         }, 5000);
  646.                     });
  647.                 } else {
  648.                     aside.removeAttr('class');
  649.                     aside.addClass('error');
  650.                     aside.find('dd').text('Error, ' + response.last_error + '.');
  651.                     aside.slideDown(function(){
  652.                         timeout = setTimeout(function () {
  653.                             aside.slideUp();
  654.                         }, 5000);
  655.                     });
  656.                 }
  657.             });
  658.            
  659.             return this;
  660.         }
  661.  
  662.         edit_product(event = 0) {
  663.             var parent = 0,
  664.                 form = $('#products'),
  665.                 data = getFormData( form ),
  666.                 products_form = $('.form.products'),
  667.                 products_page = $('#products_page'),
  668.                 product_id = products_form.attr('data-id');
  669.  
  670.             if (event) {
  671.                 parent = event.data.parent;
  672.             }
  673.                        
  674.             data['product_id'] = product_id;
  675.            
  676.             var aside = form.find('>aside');
  677.             aside.slideUp().removeAttr('class');
  678.            
  679.             var timeout = null;
  680.             aside.on('click', 'i', function() {
  681.                 clearTimeout(timeout);
  682.                 aside.slideUp(function(){
  683.                    aside.removeAttr('class');
  684.                    aside.find('dd').text('');
  685.                 });
  686.             });
  687.  
  688.             var request = {
  689.                 action: 'edit_product',
  690.                 data: data,
  691.                 nonce: products.nonce
  692.             };
  693.            
  694.             $.post(products.ajaxurl, request, function( response ) {
  695.                
  696.                 if ( response.result ) {
  697.                     response.product_id = product_id;
  698.                     var row = $ ( products.get_html_row( response ) );
  699.                     products_page.find('tbody tr[data-id="' + product_id + '"]').replaceWith(row);
  700.                     products_page.find('table.products tr:odd').css("background-color", "#ffffff");
  701.                    
  702.                     aside.addClass('success');
  703.                     aside.find('dd').text('Success, product updated.');
  704.                     aside.slideDown(function(){
  705.                         timeout = setTimeout(function () {
  706.                             aside.slideUp();
  707.                         }, 5000);
  708.                     });
  709.                 } else {
  710.                     aside.addClass('error');
  711.                     aside.find('dd').text('Nothing has changed, no update is needed.');
  712.                     aside.slideDown(function(){
  713.                         timeout = setTimeout(function () {
  714.                             aside.slideUp();
  715.                         }, 5000);
  716.                     });
  717.                 }
  718.                
  719.                 products_page.find('table.products').find("tbody tr:odd").
  720.                     css("background-color", "#c3e2ff");
  721.  
  722.                 products_page.find('table.products').find("tbody tr:even").
  723.                     css("background-color", "#ffffff");
  724.             }, 'json' );
  725.            
  726.         }
  727.  
  728.         delete_product() {
  729.             var products_form = $('.form.products');
  730.             var id = products_form.attr('data-id');
  731.  
  732.             var request = {
  733.                 'action': 'delete_product',
  734.                 'data': id,
  735.                 'nonce': products.nonce
  736.             };
  737.  
  738.             $.post(products.ajaxurl, request, function( response ) {
  739.                 if ( response ) {
  740.                     products_form.fadeOut(products.fade_time, function() {
  741.                         $(this).remove();
  742.                     });
  743.                     $('.table.wrap').find('tr[data-id='+ id +']').
  744.                     fadeOut(products.fade_time, function() {
  745.                         $(this).remove();
  746.                         products.page.find('table').find("tbody tr:odd").
  747.                             css("background-color", "#c3e2ff");
  748.  
  749.                         products.page.find('table').find("tbody tr:even").
  750.                             css("background-color", "#ffffff");
  751.                     });
  752.                 }
  753.             });
  754.            
  755.             $(".actions.products :input").attr("disabled", false);
  756.         }
  757.  
  758.         add_product_form(event = 0) {
  759.             if ( $('.form.products').length ) return;
  760.            
  761.             var args = { action: 'add' };
  762.            
  763.             var request = {
  764.                 'action': 'form_products',
  765.                 'data': args,
  766.                 'nonce': products.nonce
  767.             };
  768.  
  769.             $.post(products.ajaxurl, request, function( response ) {
  770.                 if ( response ) {
  771.                    
  772.                     $(".actions.products :input").attr("disabled", true);
  773.                    
  774.                     var form = $( response );
  775.                     products.form = form;
  776.                     var parent = event.data  ? event.data.parent : form;
  777.                     var parent_id = event.data  ? event.data.parent_id : 0;
  778.            
  779.                     $('.the-content>.wrapper').append( form );
  780.                     form.fadeIn(products.fade_time);  
  781.                    
  782.                     form.find('button.save').on( "click", {
  783.                         parent: parent,
  784.                         parent_id: parent_id
  785.                     }, products.add_product );
  786.                    
  787.                     form.find('button.cancel').on( "click", {
  788.                         parent: parent
  789.                     }, products.cancel_product_form );
  790.                    
  791.                     form.find('button.image').on('click', function() {
  792.                         alert(12);
  793.                     });
  794.                    
  795.                     form.find('img.clear').on('click', function(){
  796.                         $(this).closest('form').trigger('reset');
  797.                     });;
  798.                 }
  799.             });
  800.            
  801.             return this;
  802.         }
  803.  
  804.         edit_product_form(id = 0) {
  805.             if ( $('.form.products').length ) return;
  806.            
  807.             if( id === Object(id) ) {
  808.                 id = $(this).attr('data-id');
  809.             }
  810.            
  811.             var wrapper = $('.the-content>.wrapper');
  812.             var request = {
  813.                 action: 'form_products',
  814.                 data: { action: 'edit', id: id },
  815.                 nonce: contacts.nonce
  816.             };
  817.            
  818.             $.post(contacts.ajaxurl, request, function( response ) {
  819.                 if ( response ) {
  820.                     $(".actions.products :input").attr("disabled", true);
  821.                    
  822.                     var form = $( response );
  823.                     products.form = form;
  824.                     wrapper.append(form);
  825.                     form.fadeIn(products.fade_time);
  826.                    
  827.                     form.find('button.save').on( "click", { parent: form }, products.edit_product);
  828.                     form.find('button.cancel').on( "click", { parent: 0 }, products.cancel_product_form );
  829.                     form.find('button.delete').on('click', products.delete_product);
  830.  
  831.                     form.find('img.clear').on('click', function(){
  832.                         $(this).closest('form').trigger('reset');
  833.                     });;
  834.                 }
  835.             });
  836.            
  837.         }
  838.  
  839.         cancel_product_form(event) {
  840.             var parent = event.data.parent;
  841.            
  842.             $('.form.products').fadeOut(products.fade_time, function() {
  843.                 if (parent) {
  844.                     parent.fadeIn(products.fade_time);
  845.                 }
  846.                 $(this).remove();
  847.             });
  848.            
  849.             $(".actions.products :input").attr("disabled", false);
  850.             return this;
  851.         }
  852.  
  853.         do_bulk_action() {
  854.             var page = $('#products_page');
  855.             var action = page.find('select[name=bulk_action]').val();
  856.             var keys = [];
  857.  
  858.             $('table.products tbody tr td:first-child input').each(function() {
  859.                 if ( $(this).is(':checked') ) {
  860.                     keys.push($(this).closest('tr').attr('data-id').trim());
  861.                 }
  862.             });
  863.  
  864.             var data = { keys: keys, action: parseInt(action) };
  865.             var request = {
  866.                 'action': 'product_bluk_action',
  867.                 'data': data,
  868.                 'nonce': products.nonce
  869.             };
  870.  
  871.             $.post(products.ajaxurl, request, function( response ) {
  872.                 if ( response ) {
  873.                     var tbody = $('table.products tbody');
  874.                     tbody.find('tr td:first-child input').each(function() {
  875.                         if ( $(this).is(':checked') && data['action'] === 1 ) {
  876.                             $(this).closest('tr').remove();
  877.                         }
  878.                     });
  879.                     tbody.find("tr:odd").css("background-color", "#c3e2ff");
  880.                
  881.                     tbody.find("tr:even").css("background-color", "#ffffff");
  882.                 }
  883.             });
  884.         }
  885.     }
  886.    
  887.     class Contacts {
  888.         constructor( cfg ) {
  889.             this.form = null;
  890.             this.page = cfg.page;
  891.             this.form_data = null;
  892.             this.fade_time = cfg.fade_time;
  893.             this.nonce = mettatealib.nonce;
  894.             this.ajaxurl = mettatealib.ajaxurl
  895.         }
  896.  
  897.         get_html_row( data ) {
  898.             return `
  899.             <tr data-id="` + data['id'] + `">
  900.                 <td><input type="checkbox" /></td>
  901.                 <td>` + data['status'] + `</td>
  902.                 <td>` + data['title'] + `</td>
  903.                 <td>` + data['first_name'] + `</td>
  904.                 <td>` + data['last_name'] + `</td>
  905.                 <td>` + data['roles'] + `</td>
  906.                 <td>` + data['phone1'] + `</td>
  907.                 <td>` + data['phone2'] + `</td>
  908.                 <td>` + data['email_address'] + `</td>
  909.                 <td>` + data['country'] + `</td>
  910.                 <td>` + data['state'] + `</td>
  911.                 <td>` + data['city'] + `</td>
  912.                 <td>` + data['address'] + `</td>
  913.                 <td>` + data['unit'] + `</td>
  914.                 <td>` + data['postcode'] + `</td>
  915.                 <td>` + data['notes'] + `</td>
  916.             </tr>
  917.             `;
  918.         }
  919.        
  920.         get_form_contact_roles() {
  921.             var roles = [],            
  922.                 form = contacts.form.find('#contacts'),
  923.                 contact_roles = form.find('#contact_roles'),                
  924.                 role_options = form.find('select[name=role] option');
  925.        
  926.             form.find('select[name=role] option').each(function() {
  927.                 role_options[ $(this).text() ] = parseInt($(this).val());
  928.             });
  929.            
  930.             contact_roles.find('li').each(function() {
  931.                 var text = $(this).text().trim();
  932.                 roles.push(role_options[text]);
  933.             });
  934.             return roles;
  935.         }
  936.    
  937.         qsearch() {
  938.             var request = {
  939.                 'action': 'qsearch_contacts',
  940.                 'data': $(this).parent().find('input').val(),
  941.                 'nonce': contacts.nonce
  942.             };
  943.  
  944.             $.post(contacts.ajaxurl, request, function( response ) {
  945.                 if ( response ) {
  946.                     $(".actions.contacts :input").attr("disabled", true);
  947.                    
  948.                     var sr = $(response);
  949.                     sr.find('button.search').remove();
  950.                     sr.find('table').find("tbody tr:even").
  951.                         css("background-color", "#c3e2ff");
  952.  
  953.                     $('#content .wrapper').append(sr);
  954.  
  955.                     sr.fadeIn(contacts.fade_time);
  956.  
  957.                     sr.on('click', 'tr', function() {
  958.                         sr.find('table').find("tbody tr:even").
  959.                             css("background-color", "#c3e2ff");
  960.  
  961.                         sr.find('tr').removeClass('selected');
  962.                         sr.find('td input[type=checkbox]').prop('checked', false);
  963.                         $(this).find('td input[type=checkbox]').prop('checked', true);
  964.                         $(this).addClass('selected');
  965.                         $(this).removeAttr('style');
  966.                     });
  967.                     sr.on('click', 'button.cancel', function() {
  968.                         sr.fadeOut(contacts.fade_time, function() {
  969.                             $(this).remove();
  970.                             $(".actions.contacts :input").attr("disabled", false);
  971.                         });
  972.                     });
  973.                     sr.on('click', 'button.search', function() {
  974.                         sr.fadeOut(contacts.fade_time, function() {
  975.                             $(this).remove();
  976.                             form_search_contacts();
  977.                         });
  978.                     });
  979.                     sr.on('click', 'button.edit', function() {
  980.                         sr.fadeOut(contacts.fade_time, function() {
  981.                             var id = sr.find('input[type=checkbox]:checked').closest('tr').attr('data-id');
  982.                             sr.remove();
  983.                             contacts.edit_contact_form(id);
  984.                         });
  985.                     });
  986.                 }
  987.             });
  988.         }
  989.  
  990.         add_contact_role() {
  991.             var id, item, select, selected, role, role_id = null;
  992.             id = '#' + $(this).attr('data-id');
  993.             var ul = $(id).find('ul');
  994.  
  995.             select = $('.field.role select');
  996.             selected = select.find('option:selected');
  997.             role = selected.text();
  998.             role_id = selected.val();
  999.  
  1000.             if ( role ) {
  1001.                 item = $(`<li data-id="` + role_id + `">` + role + ` <i class="fa fa-times"></i></li>`);
  1002.                 item.hide();
  1003.  
  1004.                 if ( ! ul.find('li[data-id="' + role_id + '"]').length ) {
  1005.                     ul.append(item);
  1006.                 }
  1007.                 item.find('i').on('click', function() {
  1008.                     item.fadeOut(contacts.fade_time, function() {
  1009.                         $(this).remove();
  1010.                     });
  1011.                 });
  1012.                 item.fadeIn(contacts.fade_time);
  1013.             }
  1014.             select.val(0);
  1015.  
  1016.             return this;
  1017.         }
  1018.  
  1019.         add_contact(event = 0) {
  1020.             var parent = 0;
  1021.             var form = contacts.form.find('#contacts');
  1022.             var form_data = getFormData( form );
  1023.  
  1024.             var aside = form.find('aside');
  1025.             aside.slideUp().removeAttr('class').find('dd').text('');
  1026.            
  1027.             var timeout = null;
  1028.             aside.on('click', 'i', function() {
  1029.                 clearTimeout(timeout);
  1030.                 aside.slideUp().removeAttr('class').find('dd').text('');
  1031.             });  
  1032.            
  1033.             if (event) {
  1034.                 parent = event.data.parent;
  1035.                 form_data['parent_id'] = event.data.parent_id;
  1036.             }
  1037.  
  1038.             form_data['roles'] = contacts.get_form_contact_roles();
  1039.  
  1040.             var request = {
  1041.                 'action': 'add_contact',
  1042.                 'data': form_data,
  1043.                 'nonce': contacts.nonce
  1044.             };
  1045.  
  1046.             $.post(contacts.ajaxurl, request, function( response ) {
  1047.                 if (response.result) {
  1048.                     var row = $( contacts.get_html_row( response ) );
  1049.  
  1050.                     var tbody = $('table.contacts').find('tbody');
  1051.                     tbody.append(row);
  1052.                     tbody.find("tr:odd").css("background-color", "#c3e2ff");
  1053.                     tbody.find("tr:even").css("background-color", "#ffffff");
  1054.  
  1055.                     if (event.data.parent_id) {
  1056.                         parent.find('form section').attr('data-id', response.branch_id);
  1057.                         parent.fadeIn(contacts.fade_time);
  1058.                     }
  1059.                    
  1060.                     aside.addClass('success');
  1061.                     aside.find('dd').text('Success, new contact has been added.');
  1062.                     aside.slideDown(function(){
  1063.                         timeout = setTimeout(function () {
  1064.                             aside.slideUp();
  1065.                         }, 5000);
  1066.                     });
  1067.                 } else {
  1068.                     aside.removeAttr('class');
  1069.                     aside.addClass('error');
  1070.                     aside.find('dd').text('Error, ' + response.last_error + '.');
  1071.                     aside.slideDown(function(){
  1072.                         timeout = setTimeout(function () {
  1073.                             aside.slideUp();
  1074.                         }, 5000);
  1075.                     });
  1076.                 }
  1077.             }, 'json' );    
  1078.            
  1079.             return this;
  1080.         }
  1081.  
  1082.         edit_contact(event = 0) {
  1083.             var parent = 0,
  1084.                 form = $('#contacts'),
  1085.                 data = getFormData( form ),
  1086.                 contact_roles = $('#contact_roles'),
  1087.                 contacts_form = $('.form.contacts'),
  1088.                 contacts_page = $('#contacts_page');
  1089.  
  1090.             var aside = form.find('aside');
  1091.             aside.slideUp().removeAttr('class').find('dd').text('');
  1092.            
  1093.             var timeout = null;
  1094.             aside.on('click', 'i', function() {
  1095.                 clearTimeout(timeout);
  1096.                 aside.slideUp().removeAttr('class').find('dd').text('');
  1097.             });  
  1098.            
  1099.             if (event) {
  1100.                 parent = event.data.parent;
  1101.             }
  1102.                        
  1103.             data['roles'] =  contacts.get_form_contact_roles();
  1104.             data['contact_id'] = contacts_form.attr('data-id');
  1105.  
  1106.             var request = {
  1107.                 action: 'edit_contact',
  1108.                 data: data,
  1109.                 nonce: contacts.nonce
  1110.             };
  1111.                
  1112.             $.post(contacts.ajaxurl, request, function( response ) {
  1113.                 var role_names = [];
  1114.                 data['roles'].forEach((item, index) => {
  1115.                     var text = $('.field.role select option[value="' + item + '"]').text();
  1116.                     role_names.push( text );
  1117.                 });
  1118.                 data['roles'] = role_names.join(', ');
  1119.  
  1120.                 var country_id = (parseInt( data['country_id'] ) ) ? data['country_id'] : 0;
  1121.                 var zone_id = (parseInt( data['zone_id'] ) ) ? data['zone_id'] : '';
  1122.                 var location_id = (parseInt( data['location_id'] ) ) ? data['location_id'] : 0;
  1123.                 var status_id = (parseInt( data['status_id'] ) ) ? data['status_id'] : 0;
  1124.  
  1125.                 var country = $('.field.country select option[value="' + country_id + '"]').text();
  1126.                 var state = $('.field.state select option[value="' + zone_id + '"]').text();
  1127.                 var city = $('.field.city select option[value="' + location_id + '"]').text();
  1128.                 var status = $('.field.status select option[value="' + status_id + '"]').text();
  1129.                
  1130.                 data['country'] = country;
  1131.                 data['state'] = state;
  1132.                 data['city'] = city;
  1133.                 data['status'] = status;
  1134.                 data['id'] = data['contact_id'];
  1135.  
  1136.                 if (response.result) {
  1137.                     var table = contacts_page.find('table');
  1138.                     var row = $( contacts.get_html_row( data ) );
  1139.                     var tr = table.find('tbody tr[data-id="' + data['id'] + '"]');
  1140.                    
  1141.                     tr.replaceWith(row);
  1142.                    
  1143.                     table.find("tbody tr:odd").css("background-color", "#c3e2ff");
  1144.                     table.find("tbody tr:even").css("background-color", "#ffffff");
  1145.                
  1146.                     var style = row.css('background-color');
  1147.                     row.css('background-color', '#d3ffd0');
  1148.                     timeout = setTimeout(function () {
  1149.                         row.css('background-color', style);
  1150.                     }, 1000);
  1151.                    
  1152.                     aside.addClass('success');
  1153.                     aside.find('dd').text('Success, contact updated.');
  1154.                     aside.slideDown(function() {
  1155.                         timeout = setTimeout(function () {
  1156.                             aside.slideUp();
  1157.                         }, 5000);
  1158.                     });
  1159.                 } else {
  1160.                     var row = contacts_page.find('tbody tr[data-id=' + data['id'] +']');
  1161.                    
  1162.                     var style = row.css('background-color');
  1163.                     row.css('background-color', '#ffcecf');
  1164.                     timeout = setTimeout(function () {
  1165.                         row.css('background-color', style);
  1166.                     }, 1000);
  1167.                    
  1168.                     aside.removeAttr('class');
  1169.                     aside.addClass('error');
  1170.                     aside.find('dd').text('Nothing has changed, no update is needed.');
  1171.                     aside.slideDown(function(){
  1172.                         timeout = setTimeout(function () {
  1173.                             aside.slideUp();
  1174.                         }, 5000);
  1175.                     });
  1176.                 }
  1177.  
  1178.             }, 'json' );
  1179.            
  1180.             $(".actions.contacts :input").attr("disabled", false);
  1181.         }
  1182.  
  1183.         delete_contact() {
  1184.             var contacts_form = $('.form.contacts');
  1185.             var id = contacts_form.attr('data-id');
  1186.  
  1187.             var request = {
  1188.                 'action': 'delete_contact',
  1189.                 'data': id,
  1190.                 'nonce': contacts.nonce
  1191.             };
  1192.  
  1193.             $.post(contacts.ajaxurl, request, function( response ) {
  1194.                 if ( response ) {
  1195.                     contacts_form.fadeOut(contacts.fade_time, function() {
  1196.                         $(this).remove();
  1197.                     });
  1198.                     $('.table.wrap').find('tr[data-id='+ id +']').fadeOut(contacts.fade_time, function() {
  1199.                         $(this).remove();
  1200.                         contacts.page.find('table').find("tbody tr:even").
  1201.                             css("background-color", "#c3e2ff");
  1202.  
  1203.                         contacts.page.find('table').find("tbody tr:odd").
  1204.                             css("background-color", "#ffffff");
  1205.                     });
  1206.                 }
  1207.             });
  1208.            
  1209.             $(".actions.contacts :input").attr("disabled", false);
  1210.         }
  1211.            
  1212.         search_contacts() {
  1213.             var form = $('#contacts');
  1214.             var data = getFormData( form );
  1215.             var contact_roles = form.find('#contact_roles');
  1216.            
  1217.  
  1218.             data['roles'] = [];
  1219.             contact_roles.find('li').each(function() {
  1220.                 data['roles'].push(parseInt($(this).attr('data-id')));
  1221.             });
  1222.  
  1223.             var request = {
  1224.                 'action': 'search_contacts',
  1225.                 'data': data,
  1226.                 'nonce': contacts.nonce
  1227.             };
  1228.            
  1229.             $.post(contacts.ajaxurl, request, function( response ) {
  1230.                 if ( response ) {
  1231.                     $(".actions.contacts :input").attr("disabled", true);
  1232.                    
  1233.                     var sr = $(response);
  1234.                     if (! sr.find('table tbody tr').length ) {
  1235.                         return; // Todo add dialog box for now results
  1236.                     }
  1237.                     sr.find('table').find("tbody tr:even").
  1238.                         css("background-color", "#c3e2ff");
  1239.  
  1240.                     $('#content .wrapper').append(sr);
  1241.                     $('.form.contacts').fadeOut(contacts.fade_time, function() {
  1242.                         $(this).remove();
  1243.                         sr.fadeIn(contacts.fade_time);
  1244.  
  1245.                         sr.on('click', 'tr', function() {
  1246.                             sr.find('table').find("tbody tr:even").
  1247.                                 css("background-color", "#c3e2ff");
  1248.  
  1249.                             sr.find('tr').removeClass('selected');
  1250.                             sr.find('td input[type=checkbox]').prop('checked', false);
  1251.                             $(this).find('td input[type=checkbox]').prop('checked', true);
  1252.                             $(this).addClass('selected');
  1253.                             $(this).removeAttr('style');
  1254.                         });
  1255.                         sr.on('click', 'button.cancel', function() {
  1256.                             sr.fadeOut(contacts.fade_time, function() {
  1257.                                 $(this).remove();
  1258.                                 $(".actions.contacts :input").attr("disabled", false);
  1259.                             });
  1260.                         });
  1261.                         sr.on('click', 'button.search', function() {
  1262.                             sr.fadeOut(contacts.fade_time, function() {
  1263.                                 $(this).remove();
  1264.                                 contacts.search_contacts_form();
  1265.                             });
  1266.                         });
  1267.                         sr.on('click', 'button.edit', function() {
  1268.                             sr.fadeOut(contacts.fade_time, function() {
  1269.                                 var id = sr.find('input[type=checkbox]:checked').closest('tr').attr('data-id');
  1270.                                 $(this).remove();
  1271.                                 contacts.edit_contact_form(id);
  1272.                             });
  1273.                         });
  1274.                     });
  1275.                 }
  1276.             });
  1277.            
  1278.             $(".actions.contacts :input").attr("disabled", false);
  1279.         }
  1280.  
  1281.         add_contact_form(event = null) {
  1282.             if ( $('.form.contacts').length ) return;
  1283.            
  1284.             var request = {
  1285.                 'action': 'form_contacts',
  1286.                 'data': { action: 'add' },
  1287.                 'nonce': contacts.nonce
  1288.             };
  1289.            
  1290.             $.post(contacts.ajaxurl, request, function( response ) {
  1291.                
  1292.                 var form = $( response );
  1293.                 contacts.form = form;
  1294.                 var parent = event.data  ? event.data.parent : form;
  1295.                 var parent_id = event.data  ? event.data.parent_id : 0;
  1296.  
  1297.                 $('.the-content>.wrapper').append( form );
  1298.                 form.fadeIn(contacts.fade_time,function() {
  1299.                     form.on('change', 'select[name=zone_id]', get_locations);
  1300.  
  1301.                     form.find('button.save').on( "click", {
  1302.                         parent: parent,
  1303.                         parent_id: parent_id
  1304.                     }, contacts.add_contact );
  1305.  
  1306.                     form.find('button.cancel').on( "click", { parent: parent }, contacts.cancel_contact_form );
  1307.  
  1308.                     form.on('click', 'button.add_role', contacts.add_contact_role);
  1309.                     $(".actions.contacts :input").attr("disabled", true);
  1310.                 });  
  1311.             });
  1312.            
  1313.             return this;
  1314.         }
  1315.  
  1316.         edit_contact_form(id = 0) {
  1317.             if ( $('.form.contacts').length ) return;
  1318.             if( id === Object(id) ) {
  1319.                 id = $(this).attr('data-id');
  1320.             }
  1321.            
  1322.             var data = { action: 'edit', id: id };
  1323.             var wrapper = $('.the-content>.wrapper');
  1324.             var request = {
  1325.                 action: 'form_contacts',
  1326.                 data: data,
  1327.                 nonce: contacts.nonce
  1328.             };
  1329.            
  1330.             $.post(contacts.ajaxurl, request, function( response ) {
  1331.                 $(".actions.contacts :input").attr("disabled", true);
  1332.  
  1333.                 var form = $( response );
  1334.                 contacts.form = form;
  1335.                 wrapper.append(form);
  1336.  
  1337.                 form.on('change', 'select[name=country_id]', get_zones);
  1338.                 form.on('change', 'select[name=zone_id]', get_locations);
  1339.  
  1340.                 var ul = form.find('ul');
  1341.  
  1342.                 ul.find('li').each(function() {
  1343.                     var item = $(this);
  1344.                     item.find('i').on('click', function() {
  1345.                         item.fadeOut(contacts.fade_time, function() {
  1346.                             $(this).remove();
  1347.                         });
  1348.                     });
  1349.                 });
  1350.  
  1351.                 form.fadeIn(contacts.fade_time);
  1352.  
  1353.                 form.find('button.save').on( "click", { parent: form }, contacts.edit_contact );
  1354.                 form.find('button.cancel').on( "click", { parent: form }, contacts.cancel_contact_form );
  1355.  
  1356.                 form.find('button.delete').on('click', contacts.delete_contact);
  1357.                 form.find('button.add_role').on('click', contacts.add_contact_role);
  1358.  
  1359.             });
  1360.            
  1361.         }
  1362.  
  1363.         cancel_contact_form(event) {
  1364.            
  1365.             var parent = event.data.parent;
  1366.            
  1367.             $('.form.contacts').fadeOut(contacts.fade_time, function() {
  1368.                 if (parent) {
  1369.                     parent.fadeIn(contacts.fade_time);
  1370.                 }
  1371.                 $(this).remove();
  1372.             });
  1373.            
  1374.             $(".actions.contacts :input").attr("disabled", false);
  1375.             return this;
  1376.         };
  1377.        
  1378.         search_contacts_form() {
  1379.             if ( $('.form.contacts').length ) return;
  1380.            
  1381.             var args = {
  1382.                 action: 'search'
  1383.             };
  1384.             var wrapper = $('.the-content>.wrapper');
  1385.             var request = {
  1386.                 action: 'form_contacts',
  1387.                 data: args,
  1388.                 nonce: contacts.nonce
  1389.             };
  1390.  
  1391.             $.post(contacts.ajaxurl, request, function( response ) {
  1392.                 if ( response ) {
  1393.                     form = $( response );
  1394.                     wrapper.append(form);
  1395.                     form.fadeIn(contacts.fade_time);
  1396.                    
  1397.                     form.on('change', 'select[name=country_id]', get_zones);
  1398.                     form.on('change', 'select[name=zone_id]', get_locations);
  1399.                    
  1400.                     form.find('button.search').on('click', contacts.search_contacts);
  1401.                     form.find('button.cancel').on('click', contacts.cancel_contact_form);
  1402.                     form.find('button.add_role').on('click', contacts.add_contact_role);
  1403.                     $(".actions.contacts :input").attr("disabled", true);
  1404.                 }
  1405.             });
  1406.         }
  1407.  
  1408.         do_bulk_action() {
  1409.             var page = $('#contacts_page');
  1410.             var action = page.find('select[name=bulk_action]').val();
  1411.             var keys = [];
  1412.  
  1413.             $('table.contacts tbody tr td:first-child input').each(function() {
  1414.                 if ( $(this).is(':checked') ) {
  1415.                     keys.push($(this).closest('tr').attr('data-id').trim());
  1416.                 }
  1417.             });
  1418.  
  1419.             var data = { keys: keys, action: parseInt(action) };
  1420.             var request = {
  1421.                 'action': 'contact_bluk_action',
  1422.                 'data': data,
  1423.                 'nonce': contacts.nonce
  1424.             };
  1425.  
  1426.             $.post(contacts.ajaxurl, request, function( response ) {
  1427.                 if ( response ) {
  1428.                     $('table.contacts tbody tr td:first-child input').each(function() {
  1429.                         if ( $(this).is(':checked') && data['action'] === 1 ) {
  1430.                             $(this).closest('tr').remove();
  1431.                         }
  1432.                     });
  1433.                 }
  1434.             });
  1435.         }
  1436.     };  
  1437.    
  1438.     class Customers {
  1439.         constructor( cfg ) {
  1440.             this.form = null;
  1441.             this.page = cfg.page;
  1442.             this.form_data = null;
  1443.             this.fade_time = cfg.fade_time;
  1444.             this.nonce = mettatealib.nonce;
  1445.             this.ajaxurl = mettatealib.ajaxurl
  1446.         }
  1447.    
  1448.         get_customer_row( data ) {
  1449.            
  1450.             return $(`
  1451.                 <tr data-id="` + data['id'] + `">
  1452.                     <td><input type="checkbox" /></td>
  1453.                     <td>` + data['status'] + `</td>
  1454.                     <td>` + data['customer'] + `</td>
  1455.                     <td>` + data['type'] + `</td>
  1456.                     <td>` + data['notes'] + `</td>
  1457.                 </tr>
  1458.             `);
  1459.         }
  1460.    
  1461.         add_customer() {
  1462.             var form = $('#customers');
  1463.             var data = getFormData( form );
  1464.             var page = $('#customers_page');
  1465.  
  1466.             form.find('button.add_branch').show();
  1467.            
  1468.             var tbody = $('table.customers').find('tbody');
  1469.            
  1470.             var aside = form.find('>aside');
  1471.             aside.slideUp().removeAttr('class');  
  1472.            
  1473.             var timeout = null;
  1474.             aside.on('click', 'i', function() {
  1475.                 clearTimeout(timeout);
  1476.                 aside.slideUp(function(){
  1477.                    aside.removeAttr('class');
  1478.                    aside.find('dd').text('');
  1479.                 });
  1480.             });
  1481.                    
  1482.             var request = {
  1483.                 'action': 'add_customer',
  1484.                 'data': data,
  1485.                 'nonce': customers.nonce
  1486.             };
  1487.             $.post(customers.ajaxurl, request, function( response ) { ;
  1488.                 if ( response.result ) {
  1489.                     var row = $( customers.get_customer_row( response ) );
  1490.                     var form = $('.form.customers');
  1491.                    
  1492.                     form.attr('data-id', response['id']);
  1493.                     tbody.append(row);
  1494.                     tbody.find("tr:odd").css("background-color", "#c3e2ff");
  1495.                     tbody.find("tr:even").css("background-color", "#ffffff");
  1496.  
  1497.                     form.find('input[name=customer]').val(response['customer']);
  1498.                     form.find('select[name=status_id]').val(response['status_id']);
  1499.                     form.find('select[name=type_id]').val(response['type_id']);
  1500.                     form.find('textarea[name=notes]').val(response['notes']);
  1501.                    
  1502.                     aside.addClass('success');
  1503.                     aside.find('dd').text('Success, new customer has been added.');
  1504.                     aside.slideDown(function(){
  1505.                         timeout = setTimeout(function () {
  1506.                             aside.slideUp();
  1507.                         }, 5000);
  1508.                     });
  1509.                 } else {
  1510.                     aside.removeAttr('class');
  1511.                     aside.addClass('error');
  1512.                     aside.find('dd').text('Error, ' + response.last_error + '.');
  1513.                     aside.slideDown(function(){
  1514.                         timeout = setTimeout(function () {
  1515.                             aside.slideUp();
  1516.                         }, 5000);
  1517.                     });
  1518.                 }
  1519.             }, 'json' );  
  1520.  
  1521.         }
  1522.    
  1523.         edit_customer() {
  1524.             var form = $('#customers');
  1525.             var data = getFormData( form );
  1526.             var customers_page = $('#customers_page');
  1527.             var customers_form = $('.form.customers');
  1528.             var customer_id = customers_form.attr('data-id');
  1529.            
  1530.             data['customer_id'] = customer_id;
  1531.            
  1532.             var tbody = $('table.customers').find('tbody');
  1533.            
  1534.             var aside = form.find('>aside');
  1535.             aside.slideUp().removeAttr('class');  
  1536.            
  1537.             var timeout = null;
  1538.             aside.on('click', 'i', function() {
  1539.                 clearTimeout(timeout);
  1540.                 aside.slideUp(function(){
  1541.                    aside.removeAttr('class');
  1542.                    aside.find('dd').text('');
  1543.                 });
  1544.             });
  1545.  
  1546.             var request = {
  1547.                 'action': 'edit_customer',
  1548.                 'data': data,
  1549.                 'nonce': customers.nonce
  1550.             };
  1551.             $.post(customers.ajaxurl, request, function( response ) {
  1552.                 if ( response.result ) {
  1553.                     data['id'] = customer_id;
  1554.                     data['status'] = $('.field.status select option[value="' + data['status_id'] + '"]').text();
  1555.                     data['type'] = $('.field.type select option[value="' + data['type_id'] + '"]').text();
  1556.  
  1557.                     var row = customers.get_customer_row( data );
  1558.                    
  1559.                     customers_page.find('tbody tr[data-id="' + customer_id + '"]').replaceWith( row );
  1560.                     $("table.customers tbody tr:odd").css("background-color", "#c3e2ff");
  1561.                                        
  1562.                     aside.addClass('success');
  1563.                     aside.find('dd').text('Success, customer updated.');
  1564.                     aside.slideDown(function(){
  1565.                         timeout = setTimeout(function () {
  1566.                             aside.slideUp();
  1567.                         }, 5000);
  1568.                     });
  1569.                    
  1570.                 }  else {
  1571.                     aside.removeAttr('class');
  1572.                     aside.addClass('error');
  1573.                     aside.find('dd').text('Nothing has changed, no update is needed.');
  1574.                     aside.slideDown(function(){
  1575.                         timeout = setTimeout(function () {
  1576.                             aside.slideUp();
  1577.                         }, 5000);
  1578.                     });
  1579.                 }        
  1580.             }, 'json' );
  1581.         }
  1582.      
  1583.         delete_customer() {
  1584.             var customers_form = $('.form.customers');
  1585.             var id = customers_form.attr('data-id');
  1586.  
  1587.             var request = {
  1588.                 'action': 'delete_customer',
  1589.                 'data': id,
  1590.                 'nonce': customers.nonce
  1591.             };
  1592.  
  1593.             $.post(customers.ajaxurl, request, function( response ) {
  1594.                 if ( response ) {
  1595.                     customers_form.fadeOut(fade_time, function() {
  1596.                         $('.table.wrap').find('tr[data-id='+ id +']').remove();
  1597.                        
  1598.                         $('.table.wrap').find("tbody tr:even").
  1599.                             css("background-color", "#c3e2ff");
  1600.                        
  1601.                         $('.table.wrap').find("tbody tr:odd").
  1602.                             css("background-color", "#ffffff");
  1603.                    
  1604.                         customers_form.remove();
  1605.                     });
  1606.                 }
  1607.             });
  1608.         }
  1609.        
  1610.         add_customer_form() {
  1611.             var form_customers = $('.form.customers');
  1612.             if ( form_customers.length ) return;
  1613.            
  1614.             var args = { action: 'add' };
  1615.            
  1616.             var request = {
  1617.                 'action': 'form_customers',
  1618.                 'data': args,
  1619.                 'nonce': customers.nonce
  1620.             };
  1621.             $.post(customers.ajaxurl, request, function( response ) {
  1622.                 if ( response ) {
  1623.                     var form = $( response );
  1624.                    
  1625.                     $('.the-content>.wrapper').append( form );                    
  1626.                     form.fadeIn(customers.fade_time);
  1627.                     $(".actions.customers :input").attr("disabled", true);
  1628.                     form.find('button.save').on('click', customers.add_customer);
  1629.                     form.find('button.cancel').on('click', customers.cancel_customer_form);
  1630.                     form.find('button.add_branch').on( "click", { parent: form_customers }, customers.add_branch_form );
  1631.                 }
  1632.             });
  1633.         }
  1634.  
  1635.         edit_customer_form(id = 0) {
  1636.             var show_add_branch = false;
  1637.            
  1638.             var form_customers = $('.form.customers');
  1639.            
  1640.             if ( form_customers.length ) return;
  1641.            
  1642.             if( id === Object(id) ) {
  1643.                 id = $(this).attr('data-id');
  1644.                 show_add_branch = true;
  1645.             }
  1646.            
  1647.             var args = { id: id, action: 'edit' };
  1648.             var request = {
  1649.                 'action': 'form_customers',
  1650.                 'data': args,
  1651.                 'nonce': customers.nonce
  1652.             };
  1653.  
  1654.             $.post(customers.ajaxurl, request, function( response ) {
  1655.                 if (! response ) return;
  1656.                
  1657.                 var form = $( response );
  1658.                 if ( show_add_branch ) {
  1659.                     form.find('button.add_branch').show();
  1660. //                    var branches = customers.get_branches(id);
  1661.                 }
  1662.  
  1663.                 $('.the-content>.wrapper').append( form );
  1664.                
  1665.                 var tbody = form.find('form tbody');
  1666.                 tbody.find("tr:odd").css("background-color", "#c3e2ff");
  1667.                 tbody.find("tr:even").css("background-color", "#ffffff");
  1668.                
  1669.                 form.fadeIn(customers.fade_time);
  1670.                 form.find('button.save').on('click', customers.edit_customer);
  1671.                 form.find('button.delete').on('click', customers.delete_customer);
  1672.                 form.find('button.cancel').on('click', customers.cancel_customer_form);
  1673.                
  1674.                 form.find('button.add_branch').on( "click", { parent: form_customers }, customers.add_branch_form );
  1675.                
  1676.                 form.find('table.branches tr td:first-child a').on('click', function() {
  1677.                     var branch_id = $(this).closest('tr').attr('data-id');
  1678.                     customers.edit_branch_form(branch_id);
  1679.                 });
  1680.                
  1681.                 $("table.customers tbody tr:odd").css("background-color", "#c3e2ff");
  1682.             });
  1683.         }
  1684.  
  1685.         cancel_customer_form() {
  1686.             $('.form.customers').fadeOut(contacts.fade_time, function() {
  1687.                 $(this).remove();
  1688.             });
  1689.            
  1690.             $(".actions.customers :input").attr("disabled", false);
  1691.             return this;
  1692.         };
  1693.        
  1694.         add_customer_contact(event) {
  1695.             var form = $('.form.branches');
  1696.             var aside = form.find('form aside');
  1697.             var branch_id = parseInt(form.find('form section').attr('data-id'));
  1698.            
  1699.             if ( ! branch_id ) {
  1700.                 aside.addClass('error');
  1701.                 aside.find('dd').text('You must save this branch before adding contacts.');
  1702.                 aside.slideDown(function(){
  1703.                     $(this).delay( 5000 ).slideUp();
  1704.                 });
  1705.  
  1706.             } else {
  1707.                 form.fadeOut(customers.fade_time);
  1708.                 event.data.parent = form;
  1709.                 event.data.parent_id = form.find('form section').attr('data-id');
  1710.                 contacts.add_contact_form(event);
  1711.             }
  1712.         }
  1713.          
  1714.         get_branch_row( data ) {
  1715.             return $(`
  1716.                 <tr data-id="` + data['branch_id'] + `">
  1717.                     <td><a href="#">` + 'Edit' + `</a></td>
  1718.                     <td>` + data['status'] + `</td>
  1719.                     <td>` + data['customer'] + `</td>
  1720.                     <td>` + data['branch'] + `</td>
  1721.                     <td>` + data['agent'] + `</td>
  1722.                     <td>` + data['address'] + `</td>
  1723.                     <td>` + data['unit'] + `</td>
  1724.                     <td>` + data['postcode'] + `</td>
  1725.                     <td>` + data['country'] + `</td>
  1726.                     <td>` + data['state'] + `</td>
  1727.                     <td>` + data['city'] + `</td>
  1728.                     <td>` + data['notes'] + `</td>
  1729.                 </tr>
  1730.             `);
  1731.         }
  1732.        
  1733.         add_branch() {
  1734.             var customer_id = parseInt($('.form.customers').attr('data-id'));
  1735.             var form = $('#branches');
  1736.             var form_data = getFormData( form );
  1737.             var data = { action: 'add', customer_id: customer_id, data: form_data};
  1738.            
  1739.             var aside = form.find('>aside');
  1740.             aside.slideUp();
  1741.            
  1742.             var timeout = null;
  1743.             aside.on('click', 'i', function() {
  1744.                 clearTimeout(timeout);
  1745.                 aside.slideUp().removeAttr('class').find('dd').text('');
  1746.             });            
  1747.            
  1748.             var request = {
  1749.                 'action': 'add_branch',
  1750.                 'data': data,
  1751.                 'nonce': customers.nonce
  1752.             };
  1753.             $.post(customers.ajaxurl, request, function( response ) {
  1754.                
  1755.                 var form = $('.form.branches');
  1756.                 var section = form.find('form section');
  1757.                
  1758.                 if (response.result) {
  1759.                     section.attr('data-id', response['branch_id'] );
  1760.                
  1761.                     var table = $('.form.customers table');
  1762.                     var row = customers.get_branch_row(response);
  1763.                     table.append(row);
  1764.  
  1765.                     var tbody = table.find('tbody');
  1766.                     tbody.find("tr:odd").css("background-color", "#c3e2ff");
  1767.                     tbody.find("tr:even").css("background-color", "#ffffff");
  1768.                
  1769.                     row.find('a').on('click', function() {
  1770.                         customers.edit_branch_form(row.attr('data-id'));
  1771.                     });
  1772.                    
  1773.                     aside.removeAttr('class');
  1774.                     aside.addClass('success');
  1775.                     aside.find('dd').text('Success, new branch has been added.');
  1776.                     aside.slideDown(function(){
  1777.                         timeout = setTimeout(function () {
  1778.                             aside.slideUp();
  1779.                         }, 5000);
  1780.                     });
  1781.                 } else {
  1782.                     aside.removeAttr('class');
  1783.                     aside.addClass('error');
  1784.                     aside.find('dd').text('Error, ' + response.last_error + '.');
  1785.                     aside.slideDown(function(){
  1786.                         timeout = setTimeout(function () {
  1787.                             aside.slideUp();
  1788.                         }, 5000);
  1789.                     });
  1790.                 }
  1791.             });
  1792.         }
  1793.        
  1794.         edit_branch(args = 0) {
  1795.            
  1796.             var customer_id = parseInt($('.form.customers').attr('data-id'));
  1797.             var form = $('.form.branches form');
  1798.             var customers_form = $('.form.customers');
  1799.             var branch_id = form.find('section').attr('data-id');
  1800.                        
  1801.             if (args['data']) {
  1802.                 data = args['data'];
  1803.                 customer_id = parseInt(data.customer_id);
  1804.                 branch_id = parseInt(data.branch_id);
  1805.             }
  1806.            
  1807.            
  1808.             var aside = form.find('>aside');
  1809.             aside.slideUp();
  1810.            
  1811.             var timeout = null;
  1812.             aside.on('click', 'i', function() {
  1813.                 clearTimeout(timeout);
  1814.                 aside.slideUp().removeAttr('class').find('dd').text('');
  1815.             });  
  1816.            
  1817.             var request = {
  1818.                 'action': 'edit_branch',
  1819.                 'data': {
  1820.                     action: 'edit',
  1821.                     customer_id: customer_id,
  1822.                     branch_id: branch_id,
  1823.                     data: getFormData( form )
  1824.                 },
  1825.                 'nonce': customers.nonce
  1826.             };
  1827.             $.post(customers.ajaxurl, request, function( response ) {
  1828.                 var form = $('.form.branches');
  1829.                 var table = $('.form.customers table');
  1830.                
  1831.                 if (response.result) {
  1832.                     var row = customers.get_branch_row( response );
  1833.  
  1834.                     row.find('a').on('click', function() {
  1835.                         customers.edit_branch_form(row.attr('data-id'));
  1836.                     });
  1837.                     table.find('tr[data-id='+ branch_id +']').replaceWith( row );
  1838.                    
  1839.                     var tbody = table.find('tbody');
  1840.                     tbody.find("tr:odd").css("background-color", "#c3e2ff");
  1841.                     tbody.find("tr:even").css("background-color", "#ffffff");
  1842.                    
  1843.                     aside.addClass('success');
  1844.                     aside.find('dd').text('Success, branch updated.');
  1845.                     aside.slideDown(function(){
  1846.                         timeout = setTimeout(function () {
  1847.                             aside.slideUp();
  1848.                         }, 5000);
  1849.                     });
  1850.                 } else {
  1851.                     aside.removeAttr('class');
  1852.                     aside.addClass('error');
  1853.                     aside.find('dd').text('Nothing has changed, no update is needed.');
  1854.                     aside.slideDown(function(){
  1855.                         timeout = setTimeout(function () {
  1856.                             aside.slideUp();
  1857.                         }, 5000);
  1858.                     });
  1859.                 }
  1860.                    
  1861.             });
  1862.         }
  1863.        
  1864.         delete_branch() {
  1865.             var customer_id = parseInt($('.form.customers').attr('data-id'));
  1866.             var form_branches = $('.form.branches');
  1867.             var form_customers = $('.form.customers');
  1868.             var branch_id = form_branches.find('section').attr('data-id');
  1869.                        
  1870.             var request = {
  1871.                 'action': 'delete_branch',
  1872.                 'data': branch_id,
  1873.                 'nonce': customers.nonce
  1874.             };
  1875.            
  1876.             $.post(customers.ajaxurl, request, function( response ) {
  1877.                 if (! response) return;
  1878.                
  1879.                 var form = form_branches.find('form');
  1880.                 form_branches.fadeOut(customers.fade_time, function() {
  1881.                     form_customers.fadeIn(customers.fade_time);
  1882.                     form_customers.find('table.branches tbody tr[data-id=' + branch_id + ']').remove();
  1883.                     form_branches.remove();
  1884.                 });
  1885.                
  1886.             });
  1887.            
  1888.         }
  1889.        
  1890.         add_branch_form(event = 0) {
  1891.             var parent = event.data.parent;
  1892.            
  1893.             var customers_form = $('.form.customers');
  1894.             var wrapper = $('.the-content>.wrapper');
  1895.             var customers_id = customers_form.attr('data-id');
  1896.            
  1897.             var data = {
  1898.                 'id': customers_id,
  1899.                 'branch_id': 0,
  1900.                 'action': 'add'
  1901.             };
  1902.            
  1903.             var request = {
  1904.                 'action': 'form_branches',
  1905.                 'data': data,
  1906.                 'nonce': customers.nonce
  1907.             };
  1908.            
  1909.             $.post(customers.ajaxurl, request, function( response ) {
  1910.                
  1911.                 wrapper.append($(response));
  1912.                 var form = wrapper.find('.form.branches');
  1913.                 var aside = form.find('aside');
  1914.                
  1915.                 form.on('change', 'select[name=country_id]', get_zones);
  1916.                 form.on('change', 'select[name=zone_id]', get_locations);
  1917.                
  1918.                 customers_form.fadeOut(customers.fade_time, function() {
  1919.                     form.fadeIn(customers.fade_time);
  1920.                 });
  1921.                
  1922.                 form.on('click', 'button.save', customers.add_branch);
  1923.                 form.on('click', 'button.cancel', customers.cancel_branch_form);
  1924.                
  1925.                
  1926.                 aside.on('click', 'i', function() {
  1927.                     aside.slideUp();
  1928.                 });
  1929.                 form.find('button.add_contact').on( "click", { parent: form }, customers.add_customer_contact );
  1930.             });
  1931.  
  1932.         }
  1933.        
  1934.         edit_branch_form(id = 0) {
  1935.             if( id === Object(id) ) {
  1936.                 id = $(this).attr('data-id');
  1937.             }
  1938.            
  1939.             var customers_form = $('.form.customers');
  1940.             var wrapper = $('.the-content>.wrapper');
  1941.             var customers_id = customers_form.attr('data-id');
  1942.            
  1943.             var data = {
  1944.                 'id': customers_id,
  1945.                 'branch_id': id,
  1946.                 'action': 'edit'
  1947.             };
  1948.            
  1949.             var request = {
  1950.                 'action': 'form_branches',
  1951.                 'data': data,
  1952.                 'nonce': customers.nonce
  1953.             };
  1954.             $.post(customers.ajaxurl, request, function( response ) {
  1955.                 var form = $(response);
  1956.                 wrapper.append( form );
  1957.                
  1958.                 form.find('tbody').find("tr:odd").
  1959.                     css("background-color", "#c3e2ff");
  1960.                 form.find('tbody').find("tr:even").
  1961.                     css("background-color", "#ffffff");
  1962.            
  1963.                 form.on('change', 'select[name=country_id]', get_zones);
  1964.                 form.on('change', 'select[name=zone_id]', get_locations);
  1965.                
  1966.                 customers_form.fadeOut(customers.fade_time, function() {
  1967.                     form.fadeIn(customers.fade_time);
  1968.                 });
  1969.                
  1970.                 form.on('click', 'button.save', customers.edit_branch);
  1971.                 form.on('click', 'button.delete', customers.delete_branch);
  1972.                 form.on('click', 'button.cancel', customers.cancel_branch_form);
  1973.                
  1974.                 form.find('button.add_contact').on( "click", { parent: form }, customers.add_customer_contact );
  1975.             });
  1976.         }
  1977.  
  1978.         cancel_branch_form() {
  1979.             var customers_form = $('.form.customers');
  1980.             var wrapper = $('.the-content>.wrapper');
  1981.             var form = wrapper.find('.form.branches');
  1982.            
  1983.             form.fadeOut(customers.fade_time, function() {
  1984.                 customers_form.fadeIn(customers.fadetime);
  1985.                 $(this).remove();
  1986.             });
  1987.            
  1988.             $(".actions.customers :input").attr("disabled", false);
  1989.             return this;
  1990.         };
  1991.  
  1992.         do_bulk_action() {
  1993.             var page = $('#customers_page');
  1994.             var action = page.find('select[name=bulk_action]').val();
  1995.             var keys = [];
  1996.  
  1997.             $('table.customers tbody tr td:first-child input').each(function() {
  1998.                 if ( $(this).is(':checked') ) {
  1999.                     keys.push($(this).closest('tr').attr('data-id').trim());
  2000.                 }
  2001.             });
  2002.  
  2003.             var data = { keys: keys, action: parseInt(action) };
  2004.             var request = {
  2005.                 'action': 'customers_bluk_action',
  2006.                 'data': data,
  2007.                 'nonce': customers.nonce
  2008.             };
  2009.  
  2010.             $.post(contacts.ajaxurl, request, function( response ) {
  2011.                 if ( response ) {
  2012.                     $('table.customers tbody tr td:first-child input').each(function() {
  2013.                         if ( $(this).is(':checked') && data['action'] === 1 ) {
  2014.                             $(this).closest('tr').remove();
  2015.                         }
  2016.                     });
  2017.                 }
  2018.             });
  2019.         }
  2020.     };
  2021.  
  2022.     contacts = new Contacts( { 'fade_time': 400, page: $('#contacts_page') } );
  2023.  
  2024.     actions_contacts = $('.actions.contacts');
  2025.     actions_contacts.
  2026.         on('click', 'button.add', contacts.add_contact_form).
  2027.         on('click', 'button.apply', contacts.do_bulk_action).
  2028.         on('click', 'button.search', contacts.search_contacts_form).
  2029.         on('change', 'select.bulk', function() {
  2030.             value = $(this).val();
  2031.             actions_contacts.find('select').each(function() {
  2032.                 $(this).val(value);
  2033.             });
  2034.         });
  2035.    
  2036.     if ( ! $('#search_results').length ) {
  2037.         actions_contacts.on('click', 'button.qsearch', contacts.qsearch);
  2038.         actions_contacts.find('input[type=text]').on('keypress',function(e) {
  2039.             if( e.which === 13 ) {
  2040.                 actions_contacts.find('button.qsearch').click();
  2041.             }
  2042.         });
  2043.     }
  2044.     $('#contacts_page table.contacts').on('dblclick', 'tr', contacts.edit_contact_form);
  2045.    
  2046.     // -------------------------------------------------------------------------
  2047.    
  2048.     customers = new Customers( { 'fade_time': 400, page: $('#customers_page') } );
  2049.    
  2050.     actions_customers = $('.actions.customers');
  2051.     actions_customers.
  2052.         on('click', 'button.add', customers.add_customer_form).
  2053.         on('click', 'button.apply', customers.do_bulk_action).
  2054.         on('change', 'select.bulk', function() {
  2055.             value = $(this).val();
  2056.             actions_customers.find('select').each(function() {
  2057.                 $(this).val(value);
  2058.             });
  2059.         });
  2060.    
  2061.     $('#customers_page table.customers').on('dblclick', 'tr', customers.edit_customer_form);    
  2062.    
  2063.     // -------------------------------------------------------------------------
  2064.    
  2065.     products = new Products( { 'fade_time': 400, page: $('#products_page') } );
  2066.    
  2067.     actions_products = $('.actions.products');
  2068.     actions_products.
  2069.         on('click', 'button.apply', products.do_bulk_action).
  2070.         on('click', 'button.add', products.add_product_form).        
  2071.         on('change', 'select.bulk', function() {
  2072.             value = $(this).val();
  2073.             actions_products.find('select').each(function() {
  2074.                 $(this).val(value);
  2075.             });
  2076.         });
  2077.  
  2078.     $('#products_page table.products').on('dblclick', 'tr', products.edit_product_form);    
  2079.    
  2080.     // -------------------------------------------------------------------------
  2081.    
  2082.     orders = new Orders( { 'fade_time': 400, page: $('#orders_page') } );
  2083.    
  2084.     actions_orders = $('.actions.orders');
  2085.     actions_orders.
  2086.         on('click', 'button.add', orders.add_order_form);
  2087.      
  2088. });
  2089.      
  2090.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement