@include('Admin.Shop.Articles.partials.prices.block_price_new')
		
<div id="append_price" class="row">
	@include('Admin.Shop.Articles.partials.prices.list-prices')
</div>

<button type="button" class="btn btn-sm btn-primary add-new-price">Ajouter un tarif <i class="fa fa-plus"></i></button>

@push('js')
	<script>

		function append_price() {
			$('.select2').select2();
			handle_change_package();
			load_unities($('.unities'), $('.packages').val());
		}

		$("#append_price").appender({
			rowSection: '.row-new-price',
			type: '.row-price',
			addBtn: '.add-new-price',
			appendEffect: 'slide',
			addClass: 'animated bounceInLeft',
			rowNumber: '.row-price-number',
			deleteBtn: '.delete-new-price-btn',
			callback: append_price,
			rowNumberStart: 2,
			hideSection: true
		});

		function handle_change_package() {
			$('.packages').change( function() {
				var package_id = $(this).val();
				var $package = $(this);
				var $parent = $package.parent().parent();
				var $selector = $parent.find('.unities');
				load_unities($selector, package_id);	
			});
		}

		function init_unities() {
			$('.packages').each( function() {
				var package_id = $(this).val();
				var $package = $(this);
				var $parent = $package.parent().parent();
				var $selector = $parent.find('.unities');
				load_unities($selector, package_id);
				value_id = $selector.data('id');
				$selector.val(value_id).trigger('change');	
			});
		}

		function load_unities($selector, package_id) {
			$.ajax({
				url : "{{ route('Admin.Shop.Unities.getOptionsByPackage') }}",
				method : 'POST',
				data: {	package_id: package_id },
				success : function(data) {
					setOptions($selector, data);
					// $selector.empty().select2({data: data});
				}
			});
		}

		function handle_delete_price() {
			$('.delete-price-btn').click(function() {
				var $selector = $(this).parents('.row-price');
				var id = $selector.find('.price_id').val();

				confirm_delete(id, laroute.route('Admin.Shop.ArticlePrices.destroy', {id : id}), function() {
					$selector.remove();
				});
			});
		}

		function handle_prices() {
			$('.price-item').change(function() {
				tax_selected = $(this).parent().prev().find('select option:selected').text();
				price_taxed = $(this).val() * (1 + (tax_selected / 100));
				$(this).parent().parent().find('.price-taxed-item').val(price_taxed);
			})
		}

		function handle_prices_taxed() {
			$('.price-taxed-item').change(function() {
				tax_selected = $(this).parent().prev().find('select option:selected').text();
				console.log($(this).parent().prev());
				price = $(this).val() / (1 + (tax_selected / 100));
				$(this).parent().parent().find('.price-item').val(price);
			})
		}

		$(function() {
			handle_change_package();
		});

	</script>
@endpush
