<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<?php echo $this->form->render($this) ?>

<div class="waiting_msg_box" style="display:none;">
	<div id="waiting_msg_box_cont"  class="waiting_msg_box_cont" style="height:auto;">
    <p><?php echo $this->translate("Please wait.. It might take some time to translate the text. You will be notified here when the process is complete, do not close this pop up until the process ends."); ?></p>
    <div class="waiting_loader_wrap">
      <img src="<?php echo $this->layout()->staticBaseUrl ?>application/modules/Core/externals/images/loading.gif" alt="<?php echo $this->translate('Loading') ?>" class="waiting_loader_img" />
    </div>
    <div id="completelangs" class="w-100"></div>
    <div id="startlangs" class="w-100"></div>
  </div>
</div>

<script type="application/javascript">

	function hideShow(value) {
		if(value == 'all') {
			scriptJquery("#main_languagefiles-wrapper").show();
		} else {
			scriptJquery("#main_languagefiles-wrapper").hide();
		}
		parent.Smoothbox.instance.doAutoResize();
	}

  en4.core.runonce.add(function() {
    scriptJquery('.global_form_translate').submit(function(e) {
      e.preventDefault();
			
			var phrases_translate = scriptJquery("#phrases_translate").val();

			if(phrases_translate == 'all') {
				var elem = scriptJquery('.checkBoxClass');
				var languages = [];
				for(var i=0;i < elem.length; i++) {
					var id = scriptJquery(elem[i]).attr('id').replace('main_languagefiles-','');
					if(scriptJquery(elem[i]).prop('checked')) { 
						languages.push(scriptJquery("#"+scriptJquery(elem[i]).attr('id')).val());
					}
				}
				
				if(languages.length <= 0) {
					alert('Please select at least one csv file.');
					return;
				}
				
				var allLangChoose = scriptJquery(scriptJquery('#main_languagefiles-all')).prop('checked');
				if(allLangChoose == true) {
					convertlanguagses(allLangChoose, languages, 1);
				} else {
					convertlanguagses(allLangChoose, languages, 0);
				}
			} else {
				generatelang();
			}
    });
  });

  function convertlanguagses(allLangChoose, languages, counter) {

    var langs = languages[counter];
    var convert_languages = scriptJquery("#convert_languages").val();
		var phrases_translate = scriptJquery("#phrases_translate").val();
    scriptJquery('.waiting_msg_box').show();

    scriptJquery('#startlangs').html('<div class="core_success_message"><span>' + langs + en4.core.language.translate(' file is in translation process...') +'</span></div>');
    
    scriptJquery.ajax({
      dataType: 'html',
      method: 'post',
      'url': en4.core.baseUrl + 'admin/language/addnewpack',
      'data': {
        format: 'html',
        langs: langs,
				phrases_translate: phrases_translate,
        convert_languages: convert_languages,
      },
      success: function(responseHTML) {
      
        var response = scriptJquery.parseJSON( responseHTML );
        if(response.status == 1) {
          counter = counter+ 1;
          if(counter > languages.length) {
            scriptJquery('#waiting_msg_box_cont').html('');
            scriptJquery('#waiting_msg_box_cont').html('<div class="core_success_message"><span>'+ en4.core.language.translate('Selected files translation has been completed successfully.') + '</span></div>');
						setTimeout(() => {
							parent.location.reload();
						}, 2000);
          } else {
            scriptJquery('#startlangs').html('');
            scriptJquery('#completelangs').show();
            scriptJquery('#completelangs').html('<div class="core_success_message"><span>'+langs+ en4.core.language.translate(' file is successfully translated.') + '</span></div>').fadeOut(15000,'',function(){ scriptJquery(this).hide();});
            convertlanguagses(allLangChoose, languages, counter);
          }
        }
      }
    });
    return false;
  }

  function generatelang() {
        
    scriptJquery('.waiting_msg_box').show();
    scriptJquery('#waiting_msg_box_cont').html('<div class="core_success_message waiting_process_message"><img src="' + en4.core.staticBaseUrl + 'application/modules/Core/externals/images/loading.gif" alt="" class="waiting_loader_img" /><span>' + en4.core.language.translate('Please wait.. It might take some time to translate the text. You will be notified here when the process is complete, do not close this pop up until the process ends.') + '</span></div>');
    convertlang = scriptJquery.ajax({
      dataType: 'html',
      method: 'post',
      'url': en4.core.baseUrl + 'admin/language/convertmisssinglang',
      'data': {
        format: 'html',
        langs: '<?php echo $this->locale ?>',
        phrases_translate: scriptJquery('#phrases_translate').val(),
      },
      success: function(responseHTML) {
      
        var response = scriptJquery.parseJSON( responseHTML );
        if(response.status == 1) {
          scriptJquery('#waiting_msg_box_cont').html('<div class="core_success_message"><span>' + en4.core.language.translate('All the missing phrases have been successfully translated.') + '</span></div>');
          setTimeout(() => {
						parent.location.reload();
					}, 2000);
        }
      }
    });
    return false;
  }

  en4.core.runonce.add(function () {
    scriptJquery("#main_languagefiles-all").click(function () {
        scriptJquery(".checkBoxClass").prop('checked', scriptJquery(this).prop('checked'));
    });
  });
  
  en4.core.runonce.add(function () {
    scriptJquery("#convert_languages-all").click(function () {
  		scriptJquery(".checkBoxClassconvert").prop('checked', scriptJquery(this).prop('checked'));
    });
  });

</script>
<style>
.core_success_message{
  border-radius: 10px;
  background-color:#c8e5bc;
  font-size:14px;
  padding:10px;
  text-align:center;
}
#startlangs .core_success_message{
  background-color: #f5f3b4;
  color: #000;
}
.waiting_msg_box_cont{
  border-width:0;
  box-shadow:none;
  border-radius:0;
  font-size:14px;
  height:auto;
  width:100%;
  margin:0;
  padding:10px;
  height:100%;
  inset:0;
}
.waiting_loader_wrap{
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0;
}
.waiting_loader_img{
  width: 24px;
  height: 24px;
}
.waiting_process_message{
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.waiting_msg_box_cont a.close{
  position:initial;
  margin:15px 0 0;
  padding:5px 15px;
  transform:initial;
}
.form-options-wrapper{
	max-height:150px;
	overflow:auto !IMPORTANT;
	border-width:1px;
	padding:10px;
}
</style>