{!! Theme::section('community.pannel.campaigns.menu',['subpage'=>'']) !!}

<script src="{{Theme::asset()->get('theme/js/jquery.js')}}"></script>
<script src="{{Theme::asset()->get('theme/js/chartinator.js')}}" ></script>
<script src="{{Theme::asset()->get('theme/js/charts.js')}}" ></script>

<div class="box">
    <div class="box-title">
    	<span class="brand-colored-texts">{{$campaign->title}}</span> report
    </div>
    
    <div class="box-content signle-campaign-report">
    	
       <?php
			$lastCampaignSent=app('App\\Repositories\\CommunityCampaignEmailsRepository')->lastCampaignSent($campaign->id);
			
			$opens=app('App\\Repositories\\CommunityCampaignEmailsSentRepository')->allUniqueOpens($campaign->id);
			
			$allUniqueRecipients=$opens;
			
			$recipients=app('App\\Repositories\\CommunityCampaignEmailsSentRepository')->allUniqueReceipients($campaign->id);
			
			$unsubscribed=app('App\\Repositories\\CommunityEmailsUnsubscribeRepository')->allUniqueUnsubscribed($campaign->id);
	  
			$opens=count($opens);
			$recipients=count($recipients);
			$unsubscribed=count($unsubscribed);
			
			$openPer=0;
			$unscrPer=0;
			if($opens>0){
				$openPer=($opens/$recipients)*100;
				$openPer=number_format($openPer, 2);
			}
			
			if($unsubscribed>0){
				$unscrPer=($unsubscribed/$recipients)*100;
				$unscrPer=number_format($unscrPer, 2);
			}
			//$allUniqueRecipients=app('App\\Repositories\\CommunityCampaignEmailsSentRepository')->allUniqueOpensList($campaign->id);	
			
			//$allUniqueRecipients=$opens;
			
			$allCountryListing=array();
			
			foreach($allUniqueRecipients as $rec){
				$lastRec=app('App\\Repositories\\CommunityCampaignEmailsSentRepository')->lastRecordByEmail($campaign->id,$rec->email);					
				
				if($lastRec){
					
					$cntr=app('App\\Repositories\\CountryListingRepository')->getCountryByCodeName($lastRec->country);
					
					if($cntr){
						array_push($allCountryListing,$cntr->iso2);
						/*
						$allCityList[$cntr->name][]=[
							'city'=>$lastRec->city,
						];*/ 
					}
				}
			}
			
			$countryUnique=array_unique($allCountryListing);
			
			$countryListData='';
	  ?>
     
      <div class="row">
      		<div class="col-sm-12">
            	<table class="multiplePieChart data-table col-table">
                    <tr>
                        <th scope="col" data-type="string">Country</th>
                        <th scope="col" data-type="number">Opened</th>
                    </tr>
                        @if($countryUnique)
                            @foreach($countryUnique as $countData)
                                <tr>
                                	<?php
										$cntr=app('App\\Repositories\\CountryListingRepository')->getCountryNameByCode($countData);
									
										$cName="Unknown";
										
										if($cntr)
										{
											$cName=$cntr->name;
										}
										
										$cntCnt=app('App\\Repositories\\CommunityCampaignEmailsSentRepository')->countCountryNames($allCountryListing,$countData);
									
										$flagImg="";
										if($countData){
											$flagImg='<img src="'.URL::to('').'/themes/home/default/img/flags/'.strtolower($countData).'.png" style="margin-right:5px">';
										}
									
										$countryListData.='<div class="row">
                       <div class="col-sm-9 col-xs-9 brand-colored-texts getcitycampaignview getreportlisting" data-id="'.$campaign->id.'" data-code="'.$countData.'" data-name="'.$cName.'" style="cursor:pointer">'.$flagImg.$cName.'
					   </div>
                       <div class="col-sm-3 col-xs-3">'.$cntCnt.'</div>
					   <div class="getcitycampaignview-'.$countData.'" style="clear: both;"></div>
					   </div>';
									?>
                                    <td>{{$cName}}</td>
                                    <td align="right">
                                       {{$cntCnt}}
                                    </td>
                                </tr>
                            @endforeach    
                        @endif
                </table>
            </div>      
       </div>
       
       @if(count($countryUnique)>0)
          <div class="campaign-report-country-wise">
             <div class="row heading">
               <div class="col-sm-9 col-xs-9">Country</div>
               <div class="col-sm-3 col-xs-3">Opened</div>
             </div>
           
             {{$countryListData}}   
          </div>
       @endif     
        
       <div class="row">
      		<div class="col-sm-12">
            	 <table class="multiBarChart barChart data-table col-table">
                    <tr>
                        <th scope="col" data-type="string">Option</th>
                        <th scope="col" data-type="number">Value</th>
                    </tr>
                    
                    <tr>
                        <td>Recipients</td>
                        <td align="right">{{$recipients}}</td>
                    </tr>
                    
                    <tr>
                        <td>Opened</td>
                        <td align="right">{{$opens}}</td>
                    </tr>
                    
                    <tr>
                        <td>Unsubscribed</td>
                        <td align="right">{{$unsubscribed}}</td>
                    </tr>
                </table>
            </div>      
      </div>
      
      <br /><br />
      
      <div class="row overall-report">
	    	<div class="col-sm-6">
	    		<div class="well">
                	<h3><span class="campaign-recipeint-percent">@if($recipients > 0) {{sprintf("%02d",$recipients)}} @else 0 @endif </span> recipients </h3>
                    
                    <h3><span class="campaign-opens-percent">@if($opens > 0) {{$openPer}} % @else 0 @endif </span>  @if($opens > 0) {{sprintf("%02d",$opens)}} @endif opened</h3>
                 </div>
	    	</div>
	    	
	    	<div class="col-sm-6">
	    		<div class="well">
                	<h3><span class="campaign-unsubscribed-percent">@if($unsubscribed > 0) {{$unscrPer}} % @else 0 @endif </span> @if($unsubscribed > 0) {{sprintf("%02d",$unsubscribed)}} @endif unsubscribed</h3>
			   
               		<h3><span class="campaign-notopens-percent">{{sprintf("%02d",$recipients-$opens)}}</span> not opened</h3>
                </div>
	    	</div>
	  </div>
      
      
    </div>
</div>    
