<?php echo $header; ?>
<div id="content">
    <div class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb) { ?>
            <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
        <?php } ?>
    </div>
    <?php if ($error_warning) { ?>
        <div class="warning"><?php echo $error_warning; ?></div>
    <?php } ?>
    <div class="box">
        <div class="heading">
            <h1><img src="view/image/module.png" alt="" /> <?php echo $heading_title; ?></h1>
            <div class="buttons"><a onclick="$('#form').submit();" class="button"><span><?php echo $button_save; ?></span></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><span><?php echo $button_cancel; ?></span></a></div>
        </div>
        <div class="content">
            <div id="tabs" class="htabs"><a href="#tab-general"><?php echo $tab_general; ?></a><a href="#tab-statistics"><?php echo $tab_statistics; ?></a></div>
            <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
                <div id="tab-general">
                <table class="form">
                    <tr>
                        <td><?php echo $entry_status; ?></td>
                        <td><select style="min-width: 100px;" name="notify_when_arrives_status">
                                <?php if ($notify_when_arrives_status) { ?>
                                    <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                                    <option value="0"><?php echo $text_disabled; ?></option>
                                <?php } else { ?>
                                    <option value="1"><?php echo $text_enabled; ?></option>
                                    <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><?php echo $entry_show_mode; ?></td>
                        <td>
                                <?php if ($notify_when_arrives_show_mode) { ?>
                                    <input type="radio" checked="checked" name="notify_when_arrives_show_mode" value="1"/><?php echo $text_show_module; ?>
                                    <input type="radio"  name="notify_when_arrives_show_mode" value="0"/><?php echo $text_show_product; ?>
                                <?php } else { ?>
                                    <input type="radio"  name="notify_when_arrives_show_mode" value="1"/><?php echo $text_show_module; ?>
                                    <input type="radio" checked="checked" name="notify_when_arrives_show_mode" value="0"/><?php echo $text_show_product; ?>
                                <?php } ?>
                            </td>
                    </tr>
                </table>                                      <?php echo $text_tip_layout;?>
                <table id="module" class="list">
                    <thead>
                        <tr>
                            <td class="left"><?php echo $entry_layout; ?></td>
                            <td class="left"><?php echo $entry_position; ?></td>
                            <td class="left"><?php echo $entry_status; ?></td>
                            <td class="right"><?php echo $entry_sort_order; ?></td>
                            <td></td>
                        </tr>
                    </thead>
                    <?php $module_row = 0; ?>
                    <?php foreach ($modules as $module) { ?>
                        <tbody id="module-row<?php echo $module_row; ?>">
                            <tr>
                                <td class="left"><select name="notify_when_arrives_module[<?php echo $module_row; ?>][layout_id]">
                                        <?php foreach ($layouts as $layout) { ?>
                                            <?php if ($layout['layout_id'] == $module['layout_id']) { ?>
                                                <option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
                                            <?php } else { ?>
                                                <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
                                            <?php } ?>
                                        <?php } ?>
                                    </select></td>
                                <td class="left"><select name="notify_when_arrives_module[<?php echo $module_row; ?>][position]">
                                        <?php if ($module['position'] == 'content_top') { ?>
                                            <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
                                        <?php } else { ?>
                                            <option value="content_top"><?php echo $text_content_top; ?></option>
                                        <?php } ?>  
                                        <?php if ($module['position'] == 'content_bottom') { ?>
                                            <option value="content_bottom" selected="selected"><?php echo $text_content_bottom; ?></option>
                                        <?php } else { ?>
                                            <option value="content_bottom"><?php echo $text_content_bottom; ?></option>
                                        <?php } ?>     
                                        <?php if ($module['position'] == 'column_left') { ?>
                                            <option value="column_left" selected="selected"><?php echo $text_column_left; ?></option>
                                        <?php } else { ?>
                                            <option value="column_left"><?php echo $text_column_left; ?></option>
                                        <?php } ?>
                                        <?php if ($module['position'] == 'column_right') { ?>
                                            <option value="column_right" selected="selected"><?php echo $text_column_right; ?></option>
                                        <?php } else { ?>
                                            <option value="column_right"><?php echo $text_column_right; ?></option>
                                        <?php } ?>
                                    </select></td>
                                <td class="left"><select name="notify_when_arrives_module[<?php echo $module_row; ?>][status]">
                                        <?php if ($module['status']) { ?>
                                            <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                                            <option value="0"><?php echo $text_disabled; ?></option>
                                        <?php } else { ?>
                                            <option value="1"><?php echo $text_enabled; ?></option>
                                            <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                                        <?php } ?>
                                    </select></td>
                                <td class="right"><input type="text" name="notify_when_arrives_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" /></td>
                                <td class="left"><a onclick="$('#module-row<?php echo $module_row; ?>').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>
                            </tr>
                        </tbody>
                        <?php $module_row++; ?>
                    <?php } ?>
                    <tfoot>
                        <tr>
                            <td colspan="4"></td>
                            <td class="left"><a onclick="addModule();" class="button"><span><?php echo $button_add_module; ?></span></a></td>
                        </tr>
                    </tfoot>
                </table>
                </div>
                <div id="tab-statistics">
                    <?php if (empty($product_statistics)){?>
                        <?php echo $text_no_data;?>
                    <?php }else{?>
                    <div style="clear:both;">
                        <?php if ($error_extension_curl){ ?>
                       <div style="margin-bottom: 5px;margin-left: 15px;color:red;float: left;" id="notify_button"><?php echo $error_extension_curl;?></div>          
                        <?php }else{?>
                       <div style="margin-bottom: 5px;float: left;" id="notify_button"><a class="button" onclick="notifyNow();"><span><?php echo $button_notify; ?> </span></a></div>          
                        <?php }?>
                    </div>
                   <table class="list">
                       <thead>
                           <tr>
                               <td><?php echo $text_product_name;?></td>
                                <td><?php echo $text_product_requested;?></td>
                                 <td><?php echo $text_product_notified;?></td>
                                 <td width="300"><?php echo $text_product_emails;?></td>
                                 <td><?php echo $text_delete_statistic;?></td>
                            </tr>
                       </thead>
                       <tbody>
                           <?php foreach ($product_statistics as $product_id => $product){?>
                           <tr id="statistics_<?php echo $product_id;?>">
                               <td style="padding:5px;"><?php echo $product['name'];?></td>
                               <td id="statistics_<?php echo $product_id;?>_requested"><?php echo $product['requested'];?></td>
                               <td id="statistics_<?php echo $product_id;?>_notified"><?php echo $product['notified'];?></td>
                               <td>
                                   [ <a class="view_mails">
                                       <span><?php echo $text_view_mails;?></span>
                                       <span style="display:none;"><?php echo $text_hide_mails;?></span>
                                     </a> ]
                                   <div style="display:none;"><?php echo $product['mails'];?></div>
                               </td>
                               <td>[ <a onclick="deleteStatistic(<?php echo $product_id;?>)"><span><?php echo $button_remove;?></span> ]</td>
                           </tr>
                           <?php } ?>
                       </tbody>
                   </table>
                     <?php }?>
                </div>
              </div>      
            </form>
    </div>
</div>
<script type="text/javascript"><!--

    $('#tabs a').tabs(); 
            //--></script>

<script type="text/javascript"><!--
    $('.view_mails').live('click',function(){
        
        $(this).parent().children('div').toggle('slow');
        
        $(this).children('span').toggle();
    });
    
            //--></script>

<script type="text/javascript"><!--
    
    
    function deleteStatistic(product_id){
        
        if (window.confirm('<?php echo $text_confirm_delete;?>')){
            
        $.ajax({
		type: 'POST',
		url: 'index.php?route=module/notify_when_arrives/delete&token=<?php echo $token; ?>',
		dataType: 'json',
		data: 'product_id=' + product_id,
		beforeSend: function() {
			$(this).after('<img style="vertical-align:center" src="view/image/loading.gif" alt="" id="loading" />');
		},
		complete: function() {
  
			$('#loading').remove();
		},
		success: function(json) {
 
			if (json['error']) {
			
                             alert('<?php echo $text_delete_error;?>');
			}
			
			if (json['success']) {
                            
                            $('#statistics_'+ product_id).remove();
         
                             alert('<?php echo $text_delete_ok;?>');
		 
			}
                        
                        
		}
               
                
	});
        
        }
    }
    
    function notifyNow(){
          
        $.ajax({
		type: 'POST',
		url: 'index.php?route=module/notify_when_arrives/notify&token=<?php echo $token; ?>',
		dataType: 'json',
		data: '',
		beforeSend: function() {
			$('#notify_button').after('<img style="vertical-align:center" src="view/image/loading.gif" alt="" id="loading" />');
		},
		complete: function() {
	 
			 $('#loading').remove();
		},
		success: function(json) {
                    var  html = '';
			if (json['statistics']){
                            
                           var statistics = json['statistics'];
                           var  statistic  = null;
                            
                           for(var i in statistics){

                                  statistic = statistics[i];
           
                               html+='<tr id="statistics_' + statistic['product_id'] + '">';
                                    html+='<td style="padding:5px;">' + statistic['name'] + '</td>';
                                    html+='<td id="statistics_' + statistic['product_id'] + '_requested">' + statistic['requested'] + '</td>';
                                    html+='<td id="statistics_' + statistic['product_id'] + '_notified">' + statistic['notified'] + '</td>';
                                    html+='<td>';
                                        html+='[ <a class="view_mails">';
                                           html+=' <span><?php echo $text_view_mails;?></span>';
                                           html+=' <span style="display:none;"><?php echo $text_hide_mails;?></span>';
                                         html+=' </a> ]';
                                       html+=' <div style="display:none;"><br/>' + statistic['mails'] + '</div>';
                                  html+='  </td>';
                                   html+=' <td>[ <a onclick="deleteStatistic(' + statistic['product_id'] + ')"><span><?php echo $button_remove;?></span> ]</td>';
                               html+=' </tr>';
  
                           }
      
                           $('.list tbody').html(html);
                           
                        }
                        
                        if ($('#msg_update').length ==0){
                        
                         $('#notify_button').after('<span id="msg_update" style="color:green;margin-left:10px;"><?php echo $text_updated;?></span>');
       
                        }
		}
	});
        
}

    
    
            //--></script>
<script type="text/javascript"><!--
    var module_row = <?php echo $module_row; ?>;

    function addModule() {	
        html  = '<tbody id="module-row' + module_row + '">';
        html += '  <tr>';
        html += '    <td class="left"><select name="notify_when_arrives_module[' + module_row + '][layout_id]">';
<?php foreach ($layouts as $layout) { ?>
                    html += '      <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>';
<?php } ?>
                html += '    </select></td>';
                html += '    <td class="left"><select name="notify_when_arrives_module[' + module_row + '][position]">';
                html += '      <option value="content_top"><?php echo $text_content_top; ?></option>';
                html += '      <option value="content_bottom"><?php echo $text_content_bottom; ?></option>';
                html += '      <option value="column_left"><?php echo $text_column_left; ?></option>';
                html += '      <option value="column_right"><?php echo $text_column_right; ?></option>';
                html += '    </select></td>';
                html += '    <td class="left"><select name="notify_when_arrives_module[' + module_row + '][status]">';
                html += '      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
                html += '      <option value="0"><?php echo $text_disabled; ?></option>';
                html += '    </select></td>';
                html += '    <td class="right"><input type="text" name="notify_when_arrives_module[' + module_row + '][sort_order]" value="" size="3" /></td>';
                html += '    <td class="left"><a onclick="$(\'#module-row' + module_row + '\').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>';
                html += '  </tr>';
                html += '</tbody>';
	
                $('#module tfoot').before(html);
	
                module_row++;
            }
            //--></script>
<?php echo $footer; ?> 