<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * @var $block \Magento\Backend\Block\Widget\Button
 */

?>
<?php echo $block->getBeforeHtml() ?>
<button <?php echo /* @escapeNotVerified */ $block->getAttributesHtml(), $block->getUiId() ?>>
    <span><?php echo /* @escapeNotVerified */ $block->getLabel() ?></span>
</button>

<?php if($this->getClass() == 'action-wysiwyg' && $this->getFullActionName()=='catalog_category_edit'): ?>
	<span class="copy-description-template">
		<button type="button" class="btn button" onclick="showHideCopyForm()">
			<span><?php echo __('Copy Desctiption Template') ?></span>
		</button>

		<div class="mgs-file-form">
            <table>
                <tbody><tr>
                    <td>
                        <label><?php echo __('Source:') ?></label>
                    </td>
                    <td style="white-space: nowrap;">
                        <input type="text" class="admin__control-text category_id" placeholder="Category Id">
                    </td>
                    <td>
						<button type="button" class="action-check" onclick="loadAvailableStore('category'); return false">
                            <span><?php echo __('Check') ?></span>
                        </button>

                        <button type="button" class="action-save" onclick="copyCategoryDescriptionTemplate()">
                            <span><?php echo __('Apply') ?></span>
                        </button>
                    </td>
                </tr>
            </tbody></table>
        </div>
	</span>

	<script>
		function showHideCopyForm(){
			require([
				'jquery'
			], function(jQuery){
				(function($) {
					$('.copy-description-template').toggleClass('_show');
				})(jQuery);
			});
		}
		function loadAvailableStore(type){
			require([
				'jquery'
			], function(jQuery){
				(function($) {
					var categoryId = $('.mgs-file-form .'+type+'_id').val();
					if(isNaN(categoryId) || categoryId=='' || categoryId==0 || categoryId=='0'){
						$('.mgs-file-form .'+type+'_id').addClass("invalid");
					}else{
						$('.mgs-file-form .'+type+'_id').removeClass("invalid");
						$('.mgs-file-form .action-check span').html("Checking...");
						$('.mgs-file-form .action-check').attr("disabled", "disabled");
						$.ajax({
							url:'<?php echo $this->getUrl("adminhtml/fbuilder/checkstore") ?>' + '?product_id='+categoryId+'&type='+type+'&form_key='+FORM_KEY,
							contentType: false,
							cache: false,
							processData:false,
							success: function(data){
								$('.mgs-file-form .action-check').removeAttr("disabled");
								$('.mgs-file-form .action-check span').html("Check");
								var result = JSON.parse(data);
								if(result.result=='success'){
									$(result.data).insertAfter('.mgs-file-form .'+type+'_id');
									$('.mgs-file-form .action-check').hide();
									$('.mgs-file-form .action-save').show();
									$('.mgs-file-form .'+type+'_id').attr('readonly','readonly');
								}else{
									$('.mgs-file-form .'+type+'_id').addClass("invalid");
								}
							}
						});
					}
				})(jQuery);
			});
		}

		function copyCategoryDescriptionTemplate(){
			require([
				'jquery'
			], function(jQuery){
				(function($) {
					$('.mgs-file-form .action-save').attr("disabled", "disabled");
					$('.mgs-file-form .action-save span').html("Saving...");
					var parentCategoryId = $('.mgs-file-form .category_id').val();
					var storeId = $('.mgs-file-form .store_id').val();
					var categoryId = '<?php echo $this->getRequest()->getParam("id") ?>';
					var ajaxUrl = '<?php echo $this->getUrl("adminhtml/fbuilder/savecategorydescription") ?>' + '?parent_category_id='+parentCategoryId+'&category_id='+categoryId+'&storeId='+storeId+'&form_key='+FORM_KEY;
					<?php if($storeId = $this->getRequest()->getParam('store')): ?>
						ajaxUrl += '&current_store=<?php echo $storeId ?>';
					<?php endif ?>
					$.ajax({
						url:ajaxUrl,
						contentType: false,
						cache: false,
						processData:false,
						success: function(data){
							location.reload();
						}
					});

				})(jQuery);
			});
		}
	</script>
<?php endif ?>
<?php echo $block->getAfterHtml() ?>
