<?php 
/**
 * Screen recordconsolidation\index
 * @version 1.0.1
 * @package Backend\DataRecords
 * 
*/
require_once 'app/class-lov-manager.php';

$rs = $this->data['recordset'];
$list1 = $this->data['list1'];
$list2 = $this->data['list2'];
$values = $this->data['values'];
$bakurl =  $this->data['bakkey'];
$list_qrs = $this->data['list_questions_rs'];
$listStates = $this->data['states'];

$lovmanager = new LovManager();
$recordstatuslist = $lovmanager->getLov_RecordsetStatus();

?>

<div class="row" >
<div class="col-lg-12" >
            <h3><?= R('page_title_recordconsolidation') ?></h3>
            <p>&nbsp;</p>
			<?php $this->load('common/message',$this) ?>
			
            <form class="form-inline" role="form" name="form1" method="post" action="<?= __SITEBASE ?>/recordconsolidation/saverecordset">
             	<input type="hidden" id="nav_backpost" name="nav_backpost" value="1"></input> 
              	<input type="hidden" id="masterid" name="masterid" value="<?= $rs->masterrecordid ?>"></input>
              	<input type="hidden" id="uid" name="uid" value="<?= $rs->id ?>"></input>
              	<input type="hidden" id="tainted" name="tainted" value="0"></input>
              <div class="panel" style="margin-top:20px;">
	             <div class="panel panel-default">
			         <div class="panel-body">
			           	 <div class="table-responsive" >
			           	 	  <p><?= R('label_records') ?></p>
				              <table class="table table-hover table-striped tablesorter">
				                <thead>
				                  <tr>
				                  
				                    <th class="header"><?= R('label_record') ?> <i class="fa fa-sort"></i></th>
				                    <th class="header"><?= R('label_date') ?> <i class="fa fa-sort"></i></th>
				                    <th class="header"><?= R('label_type') ?> <i class="fa fa-sort"></i></th>
				                    <th class="header"><?= R('label_organisation') ?> <i class="fa fa-sort"></i></th>
				                  
				                    <th class="header"><?= R('label_status') ?> <i class="fa fa-sort"></i></th>
				                  </tr>
				                </thead> 
				                <tbody>
				                <?php foreach($list1 as $line) { ?>
				                 <tr>
				                    <td><?= $line->id ?></td>
				                    <td><?= $line->datefrom ?></td>
				                    <td><?= $line->statisticaltypeid == __STATISTICAL_TYPE_VICTIMS ? R('type_victims') : ($line->statisticaltypeid == __STATISTICAL_TYPE_TRAFFICKERS ? R('type_traffickers') : '')  ?></td>
				                    <td><?= $line->organisationintname ?></td>
				                   
				                    <td>
				                    <?php 
				                    $status = new Entity('codelistitems',$line->statuscodeid);
				                    ?>
				                	<?= D($status->descriptioncode) ?>
				                	</td>
				                 </tr>
				               <?php }?>
				                </tbody>
				              </table>
			              </div>
		              </div>
	              </div>
			</div>
			<div class="panel" style="margin-top:20px;">
	             <div class="panel panel-default">
			         <div class="panel-body">
			           	 <div class="table-responsive" >
			           	 	
				            <table class="table table-hover table-striped tablesorter">
				                <thead>
				                  <tr>
				                    <th class="header"><?= R('label_group') ?> <i class="fa fa-sort"></i></th>
				                    <th class="header"><?= R('label_question') ?> <i class="fa fa-sort"></i></th>
				                   	<?php foreach($list1 as $col) {
				                   		if ($col->id != $rs->masterrecordid) { 
				                   		
					                   		switch($col->reliabilitycodeid) {
					                   			
					                   			case 21: $reliabilitycolor='green';
					                   					 break;
					                   			case 22: $reliabilitycolor='#FCDE5B';
					                   					 break;
					                   			case 23: $reliabilitycolor='orange';
					                   					 break;
					                   			case 24: $reliabilitycolor='red';
					                   					 break;
					                   		}	
				                   	
				                   			$reliabilitydesc = $lovmanager->getItemDescriptionCode($col->reliabilitycodeid);
				                   		?>
				                   		
				                    	<th class="header"><?= R('label_question_value') ?>&nbsp;<?= $col->id ?>&nbsp;<i class="fa fa-sort"></i>&nbsp;&nbsp;<i title="<?= D($reliabilitydesc) ?>" style="color: <?= $reliabilitycolor ?>" class="fa fa-thumbs-up"></i></th>
				                    <?php } 
				                   		} ?>
				                    <th class="header"><?= R('label_select') ?> </th>
				                    <th class="header"><?= R('label_master') ?>&nbsp;<?= $rs->masterrecordid ?>&nbsp; </th>
				                  </tr>
				                </thead>
				                <tbody>
				                <?php 
				                
				                foreach($list2 as $line) { ?>
				                 <tr>
				                    <td><?= D($line->group_descriptioncode) ?></td>
				                    <td><?= D($line->question_descriptioncode) ?></td>
				                    <?php 
				                    
				                    $donthavevalue = 0;
				                    $oldestrecord = null;
				                    
				                    foreach($list1 as $col) {
				                    	
				                    	if ($col->datefrom<$oldestrecord->datefrom || $oldestrecord==null) {
				                    		$oldestrecord = $col; 
				                    	}
				                    		
				                    	if ($col->id != $rs->masterrecordid) {?>
				                    	<td id="value_<?= $col->id.'_'.$line->questionid ?>">
				                    	<?php 
				                    	if ($values[$col->id.'_'.$line->questionid]) {
					                    	foreach($values[$col->id.'_'.$line->questionid] as $value) {  
					                    	 if ($line->datatypecodeid == _DATA_TYPE_RESPONSE_MAPPOINT) { ?>
						                    	<a href="javascript:openGeoModal('<?= __SITEBASE ?>/websig/modal?geo=<?= $value ?>')"><span class="glyphicon glyphicon-map-marker"></span></a><!-- <?= R('label_maplocation') ?> -->
					                    		<!--<a href="javascript:alert('<?= $value ?>')"><span class="glyphicon glyphicon-map-marker"></span></a>--><!-- <?= R('label_maplocation') ?> -->
					                    		<?php } else { ?>
					                    			<?php if ($value == null)
					                    			{
					                    				$donthavevalue++; 
					                    			}
					                    			?>
					                    			<?= $value ?><br>
					                    	<?php }
					                    	}
					                    	
					                   	} ?>
					                   	</td>
					                  <?php } 
				                    	} ?>
									<td>
									
							       	<select name="rsp_<?= $line->questionid ?>" onchange="changeMasterValue(this.value+'_'+<?= $line->questionid ?>);" >
			                    	<?php if ($rs->masterrecordid == "") {  ?>
			                    		<option value="" style="display: none;"><?= R("label_please_select_dropdown") ?></option>
			                    	<?php } ?>
			                    	<?php 
			                    	$select_oldest = false;
			                    	if ($donthavevalue == count($list1)) {   //nenhum tem valor, aplica-se o record mais antigo 
			                    			$select_oldest  = true;
			                    	} ?>
			                    	<?php foreach($list1 as $a) {
			                    	 if ($a->id != $rs->masterrecordid ) { ?>
			                    		<option value="<?= $a->id ?>" <?= ($a->id == $list_qrs[$line->questionid]) || (($select_oldest && $a->id == $oldestrecord->id) && (isset($line->questionid) && $list_qrs[$line->questionid] == null))  ? " selected " : "" ?>><?= $a->id ?></option>
			                    	<?php }
			                    	} ?>
			                    	<option value="UNK" <?= (isset($line->questionid) && $list_qrs[$line->questionid] == null) && ! $select_oldest ? " selected " : ""  ?> ><?= R('label_unknown') ?></option>
			                    	</select>
			                    	</td>
			                    	<td id="master_<?= $line->questionid ?>">
			                    	<?php if ($rs->masterrecordid != '')
     								 foreach($list1 as $col) { 
										if ($col->id == $rs->masterrecordid) {	
					                    	if ($values[$col->id.'_'.$line->questionid]) {
						                    	foreach($values[$col->id.'_'.$line->questionid] as $value) { 
 												 if ($value === null) {
													print(R('label_unknown'));
												 }elseif ($line->datatypecodeid == _DATA_TYPE_RESPONSE_MAPPOINT) { ?>
						                    		<a href="javascript:openGeoModal('<?= __SITEBASE ?>/websig/modal?geo=<?= $value ?>')"><span class="glyphicon glyphicon-map-marker"></span></a><!-- <?= R('label_maplocation') ?> -->
						                    		<?php } else { ?>
						                    		<?= $value ?><br>
						                    	<?php }
						                    	}
					                    	}			                    	
										 }
									} ?>
			                    	</td>
				        		</tr>
				               <?php }?>
				                </tbody>
				             </table>
			              </div>
		              </div>
	              </div>
			</div>
			<div class="form-group">
				    <?= R('label_state') ?>:
				    <select name="recordsettatus" class="form-control">
				  	  <?php  foreach($listStates as $item) { ?>
				   		<option value="<?= $item->to_statecode_id ?>"  <?= $rs->statuscodeid == $item->to_statecode_id ? 'selected':''?>><?= D($item->to_statecode__description) ?></option>
				    <?php  } ?>
				    </select>
					<button style="margin-left:100px;" type="submit" class="btn btn-default"><?=R('label_save') ?></button>
					<?php if (isset($bakurl)) { ?>
						<a href="<?= __SITEBASE ?>/<?= $bakurl ?>" class="btn btn-default"><?=R('label_cancel') ?></a>
					<?php } ?>		</div>
					</form>  
			</div>
		
	</div>   

<br><br>
<div class="modal" id="geoModal" data-backdrop="static">
	<div class="modal-dialog" style="width:80%">
      <div class="modal-content" >
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal"  aria-hidden="true">X</button>
          <h4 class="modal-title"><?= __NAME ?></h4>
        </div>
        <div class="modal-body">
        	<iframe id="geomod" style="border:0;width:100%;height:630px"></iframe>
        </div>
      </div>
    </div>
</div>
 <script>
 function openGeoModal(url) {
	$('#geomod').attr('src',url);
	$('#geoModal').modal({show:true});
}


 function changeMasterValue(id) {
    
	$('#tainted').val('1');
		
	var parts = id.split("_");

	if (parts[0]=='UNK') {

		$('#master_'+parts[1]).html('<?= R('label_unknown') ?>');
	} 
	else{
		if (parts[0]==0) {
			$('#master_'+parts[1]).html('');
		}
		else {
	 		$('#master_'+parts[1]).html($('#value_'+id).html());
		}
 
 	}

} 
 
</script>
 