/* Admin JS */
(function($) {
	$(document).ready(function() {
		$('.swp_form_fields .handlediv').click(function(){
			$(this).parent().toggleClass('closed');
		})

	    $(document).on('click','#sendy-tabs ul.tabs li a',function(e){
	    	e.preventDefault();
	    	$('#sendy-tabs ul.tabs li a').parent().removeClass('active')
	    	$(this).parent().addClass('active')
	    	var id = $(this).attr('href');
	    	$target = $(id).show();
	    	$('.tab-content').not($target).hide();
	    })

	    if(window.location.hash) {
	    	var tab = window.location.hash.substr(1);
	    	$('#sendy-tabs ul.tabs li a[href="#'+tab+'"]').trigger('click');
		}

		$('.add-new-list-button').click(function() {
			$('#row-add-new-list').slideDown('100');
		})
		$('#cancel_options').click(function() {
				$('#row-add-new-list').slideUp('100');
			})
			/*Sendy Shortcode Form*/
		$('#sendy-shortcode-form input').click(function(event) {
			var shortCode = '';
			var listId = '';
			var inputs = $('#sendy-shortcode-form ').serializeArray();
			jQuery.each(inputs, function(i, field) {
				switch (field.name) {
					case 'listid[]':
						listId += field.value + ','
						break;
					case 'showname':
						shortCode += ' name="true"'
						break;
					case 'showcaptcha':
						shortCode += ' captcha="true"'
						break;
					case 'showterms':
						shortCode += ' terms="true" '
						break;
					case 'showsubscribers':
						shortCode += ' subscribers="true"'
						break;
				}
			});
			if (listId != '') {
				list = ' id="' + listId.slice(0, -1) + '"';
			} else {
				list = '';
			}
			final_shortcode = '[subscribe-sendy-form' + list + shortCode + ']';
			$('#shortcode_box').val(final_shortcode);
		});
		/**/
		$('#sendy-unsubscribe-shortcode-form input').click(function(event) {
			var listId = '';
			var inputs = $('#sendy-unsubscribe-shortcode-form ').serializeArray();
			jQuery.each(inputs, function(i, field) {
				switch (field.name) {
					case 'listid[]':
						listId += field.value + ','
						break;
				}
			});
			if (listId != '') {
				list = ' id="' + listId.slice(0, -1) + '"';
			} else {
				list = '';
			}
			final_shortcode = '[unsubscribe-sendy-form' + list + ']';
			$('#unshortcode_box').val(final_shortcode);
		});
		/*Show register page list on checkbox click*/
		$('.sendy_on_register').click(function(event) {
					$('#register_lists_row').slideDown()
			})

		 $('.sendy_on_register_disable').click(function(event) {
					$('#register_lists_row').slideUp()
			})
			/*Show Woocoomerce page list on checkbox click*/
		$('#sendy_woo_checkout_check').click(function(event) {
				if ($(this).prop("checked")) {
					$('#woo_lists_row').slideDown()
				} else {
					$('#woo_lists_row').slideUp()
				}
			})
			/*Show edd page list on checkbox click*/
		$('#sendy_edd_checkout_check').click(function(event) {
			if ($(this).prop("checked")) {
				$('#edd_lists_row').slideDown()
			} else {
				$('#edd_lists_row').slideUp()
			}
		})

		$('#sendy_buddypress_check').click(function(event) {
			if ($(this).prop("checked")) {
				$('#buddypress_lists_row').slideDown()
			} else {
				$('#buddypress_lists_row').slideUp()
			}
		})




		$('.custom-field-modal-trigger').click(function(){
			 $('#custom-field-modal').slideDown('slow');
		})

		 $('#custom-field-modal-close').click(function(){
			 $('#custom-field-modal').slideUp('slow');
		})

		 $('#custom_field_type').change(function(event) {

		 });

		  /** Add custom field form */

			$(document).on('submit','#add-custom-field-form',function(){
				// Field Label
				var field_label = $('#custom_field_label').val();
				if( field_label == '' ){
				 Materialize.toast('Please enter a field label', 4000,'red')
				 return false;
				}
				// Field Name
				var field_name = $('#custom_field_name').val();
				if( field_name == '' ){
				 Materialize.toast('Please enter a field name', 4000,'red')
				 return false;
				}
				// Field Type
				var field_type = $('#custom_field_type').val();
				if( field_type == null ){
				 Materialize.toast('Please select a field type', 4000,'red')
				 return false;
				}

			})

			$('#custom_field_type').change(function(){
				var sel_val = $(this).val();
				if(sel_val == 'dropdown' || sel_val == 'checkbox' ){
					$('.custom_field_options_cont').fadeIn()
					$('.hidden_field_value_cont').hide()
				}else if(sel_val == 'hidden'){
					$('.hidden_field_value_cont').fadeIn()
					$('.custom_field_options_cont').hide()
				}else{
					$('.hidden_field_value_cont').hide()
					$('.custom_field_options_cont').hide()
				}

			})

	})
})(jQuery);