<?php
defined('_JEXEC') or die('Restricted access');
/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/


$fName = $field->db_field_name;

$icon_alias_prefix = (isset($field_from_params[$fName.'_add_icon_alias_prefix'])) ? $field_from_params[$fName.'_add_icon_alias_prefix'] : '';
if($icon_alias_prefix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_prefix_hidden', 'fa '.$icon_alias_prefix, $layout_html);
}
$icon_alias_suffix = (isset($field_from_params[$fName.'_add_icon_alias_suffix'])) ? $field_from_params[$fName.'_add_icon_alias_suffix'] : '';
if($icon_alias_suffix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_suffix_hidden', 'fa '.$icon_alias_suffix, $layout_html);
}

$required = '';
$offset_animation = get_field_offset_animation($field, $layout);
if(isset($field_from_params[$fName.'_required']) && $field_from_params[$fName.'_required']=='on')
    $required = ' required ';
$audios = array();
$db = JFactory::getDBO();
if (!empty($entityInstance->eiid)) {
    $db->setQuery("SELECT * FROM #__os_cck_audio_source ".
      " WHERE fk_eiid=" . $entityInstance->eiid . " and fk_fid=" . $field->fid . " ORDER BY 'id'");
    $audios = $db->loadObjectList();
}
if(isset($layout_params['fields']['showName_'.$field->db_field_name]) &&
  $layout_params['fields']['showName_'.$field->db_field_name] == 'on'){
  $layout_html = str_replace($field->db_field_name.'-label-hidden', '', $layout_html);
}
if(!isset($layout_params['field_styling']))
    $layout_params['field_styling'] = '';
if(!isset($layout_params['custom_class']))
    $layout_params['custom_class'] = '';

?>
<script>
  var request = null;
  var aid=1;
  function createRequest_audio<?php echo $field->fid; ?>() {
    if (request != null)
    return;
    try {
      request = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
              request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
              request = null;
            }
        }
    }
    if (request == null)
      alert(" :-( ___ Error creating request object! ");
  }

  function testInsert_audio<?php echo $field->fid; ?>(id1,upload){
    for(var i=1; i< upload_audios<?php echo $field->fid; ?>; i++){
        if(upload.id != ('<?php echo $field->fid; ?>_new_upload_audio'+i) &&
        document.getElementById('<?php echo $field->fid; ?>_new_upload_audio'+i).value == upload.value){
            return false;
      }
    }
    return true;
  }

  function refreshRandNumber_audio<?php echo $field->fid; ?>(id1,upload){
    id=id1;
    if(testInsert_audio<?php echo $field->fid; ?>(id1,upload)){
      createRequest_audio<?php echo $field->fid; ?>();
      // console.log('1111111111111', upload)
      var url = "<?php echo JUri::root().'index.php?option=com_os_cck&task=checkFile&format=raw';
      ?>&file="+upload.value+
      "&path=<?php echo str_replace('\\', '/', JUri::root()) . 'components/com_os_cck/files/audio/'?>";
       try{
        request.onreadystatechange = updateRandNumber1_audio<?php echo $field->fid; ?>;
        request.open("GET", url,true);
        request.send(null);
        }catch (e)
        {
            alert(e);
        }
    }else{
      alert( "You alredy select this audio file");
      upload.value="";
    }
  }

  function updateRandNumber1_audio<?php echo $field->fid; ?>(){
    if (request.readyState == 4) {
      document.getElementById("<?php echo $field->fid; ?>randNumAudio"+aid).innerHTML = request.responseText;
    }
  }

  var upload_audios<?php echo $field->fid; ?>=0;
  function new_audios<?php echo $field->fid; ?>(new_element, fid){
    div = jQuerOs(new_element).parent()[0];
    button = new_element;
    upload_audios<?php echo $field->fid; ?>++;
    newitem = "<div>"+
                  "<span width='15%'>"+
                       "<?php echo cck_lang_text('COM_OS_CCK_LABEL_AUDIO_UPLOAD') ?>" + upload_audios<?php echo $field->fid; ?> + ":"+
                  "</span>"+
                  "<span width='85%'>"+
                    "<input style=\"float:left; width:100%\" type=\"file\" "+
                      " onClick=\"jQuerOs('#<?php echo $field->fid; ?>_new_upload_audio_url"+upload_audios<?php echo $field->fid; ?>+"').val('');\" " +
                      " onChange=\"refreshRandNumber_audio<?php echo $field->fid; ?>("+upload_audios<?php echo $field->fid; ?>+",this);\" "+
                      " name=\"<?php echo $field->fid; ?>_new_upload_audio"+upload_audios<?php echo $field->fid; ?>+"\" "+
                      " id=\"<?php echo $field->fid; ?>_new_upload_audio"+upload_audios<?php echo $field->fid; ?>+"\" value=\"\"size=\"45\">"+
                    "<span id=\"<?php echo $field->fid; ?>randNumAudio"+upload_audios<?php echo $field->fid; ?>+"\" style=\"color:red;\"></span>"+
                  "</span>"+
                "</div>"+
                "<div><span style=\"text-align:center\"> OR </span></div>";
    newnode = document.createElement("span");
    newnode.innerHTML = newitem;
    div.insertBefore(newnode,button);

    newitem = "<div><span width='15%'>" +
                  "<?php echo cck_lang_text('COM_OS_CCK_LABEL_AUDIO_UPLOAD_URL'); ?>: </span>"+
                  "<span width='85%'>"+
                    "<input style=\" width:90%\" type=\"text\" "+
                            " name=\"<?php echo $field->fid; ?>_new_upload_audio_url"+upload_audios<?php echo $field->fid; ?>+"\" "+
                            " id=\"<?php echo $field->fid; ?>_new_upload_audio_url"+upload_audios<?php echo $field->fid; ?>+
                            "\" value=\"\"size=\"45\"></span>"+
                "</div><br><br>";
    newnode=document.createElement("span");
    newnode.innerHTML=newitem;
    div.insertBefore(newnode,button);
    var allowed_aud_files = 5;
    if(upload_audios<?php echo $field->fid; ?> + <?php echo count($audios); ?> >= allowed_aud_files) {
      button.setAttribute("style","display:none");
    }
  }
</script>
<?php
if (count($audios) > 0) {?>
  <div <?php echo $layout_params['field_styling']?> class="<?php echo $layout_params['custom_class']?>" <?php echo $offset_animation; ?> >
    <span width="185"></span>
      <span><span id="error_audio"></span></span>
  </div>
  <div>
    <span valign="top" align="right"><?php echo cck_lang_text("COM_OS_CCK_LABEL_AUDIO")?></span>
    <?php
    for ($k = 0;$k < count($audios);$k++) { ?>
      <div>
        <span align="right"><?php echo cck_lang_text("COM_OS_CCK_LABEL_AUDIO_ATTRIBUTE").($k + 1)?></span>
        <span>
          <?php
          if (isset($audios[$k]->src) && substr($audios[$k]->src, 0, 4) != "http") {?>
            <input class="audio_src" type="text" size="60" value="<?php echo JURI::root() . $audios[$k]->src?>" readonly="readonly"/>
            <?php
          }else{?>
            <input class="audio_src" type="text" size="60" value="<?php echo $audios[$k]->src?>" readonly="readonly"/>
            <?php
          }?>
        </span>
      </div>
      <div style="padding: 0 0 15px 0">
        <span align="right"><?php echo cck_lang_text("COM_OS_CCK_LABEL_AUDIO_DELETE")?></span>
        <span>
          <?php
          if(isset($audios[$k]->id)){?>
            <input type="checkbox" name="audio_option_del<?php echo $audios[$k]->id?>" value="<?php echo $audios[$k]->id?>">
            <?php
          }?>
        </span>
      </div>
      <?php
    } ?>
  </div>
  <?php
}

$span_style = get_align_styles($field, $layout);
?>

<span <?php if(isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']))
        {?>
rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$fName.'_tooltip'];?>"
    <?php } ?> <?php echo $span_style; ?>>
  <span id="a_items<?php echo $field->fid; ?>">
      <input <?php echo $layout_params['field_styling']?> class="btn-info <?php echo $layout_params['custom_class'].$required . $field->db_field_name?>"  <?php echo $offset_animation; ?> type="button" name="new_audio"
      value="<?php echo cck_lang_text('COM_OS_CCK_LABEL_ADD_NEW_AUDIO_FILE'); ?>" onClick="new_audios<?php echo $field->fid; ?>(this, <?php echo $field->fid; ?>)"
      id="a_add<?php echo $field->fid; ?>"/>
  </span>
</span>
