<?php
/**
 * Screen collectdata\index
 * @version 1.0.1
 * @package Backend\DataRecords
 * 
*/

$title = "Collect data";
$context = $this->data["contextname"];
$list = $this->data['list'];
$cannewtrafficker = ($this->data['newtrafficker'] == 0)?  ' disabled="disabled" ' : '';
$cannewvictim = ($this->data['newvictim'] == 0)?  ' disabled="disabled" ' : '' ;
$pvars = $this->data['pvars'];

$checkGroup = Session::get_activerole()->role_code;

  switch ($checkGroup){
    case 'LC':
      $title= 'menu_collect';
      break;
    case 'LA':
      $title = 'menu_analyse';
      break;
    case 'LP':
      $title = 'menu_publish';
      break;
    default:
     $title= 'menu_collect';
      break;
  }

//call from external ex. alerts
$openlimeobj = $this->data["openlimeobj"];
//-------------------------------------
?>
<div class="row" >
<div class="col-lg-12" >
            <h3><?= R($title) ?></h3>
            <p>&nbsp;</p>
            <?php if ($pvars['notfound']){  ?>
            		<p style="text-align:center;padding:10px" class="bg-danger"><?= R('not_found') ?></p>
            		
            <?php } ?> 
			<?php  $this->load('common/message',$this) ?>
            
            <div class="panel panel-default">
             <?php if (Session::get_activerole()->role_code == 'LA' || Session::get_activerole()->role_code == 'LP') { ?>
            <br>
            <form class="form-inline" role="form" name="filter" method="post" action="<?= __SITEBASE ?>/collectdata">
             <div class="form-group">
			  <?= R('label_type') ?>:
			  <select name="type" permissions="false" class="form-control" onchange="this.form.submit();">
			  		<option  value="0" <?= ($_POST['type'] == "")? "selected" : "" ?>><?= R("label_all") ?></option>
					<option  value="<?= __STATISTICAL_TYPE_VICTIMS ?>" <?= ($_POST['type'] == __STATISTICAL_TYPE_VICTIMS ) ? "selected" : "" ?>><?= D(__STATISTICAL_TYPE_VICTIMS_DESCRITION) ?></option>					  		
			  		<option  value="<?= __STATISTICAL_TYPE_TRAFFICKERS ?>" <?= ($_POST['type'] == __STATISTICAL_TYPE_TRAFFICKERS ) ? "selected" : "" ?>><?= D(__STATISTICAL_TYPE_TRAFFICKERS_DESCRITION) ?></option>
			   </select>
			  </div>
			  </form>
			  <?php } ?>
             </div>
              <form id="formcollect" name="form1" method="post" onsubmit="return validate_checks();" action="<?= __SITEBASE ?>/collectdata/markpublishable">
             
		       <div class="panel panel-default">
		        <div class="panel-body">
		            <div class="table-responsive" >
		          
			              <table id="table1" class="table table-hover table-striped tablesorter">
			                <thead>
			                  <tr>
			                  	<?php if (Session::get_activerole()->role_code == 'LA' || Session::get_activerole()->role_code == 'LP') {?>
			                  	<th class="header">&nbsp;</th>
			                  	<?php } ?>
								<!------------------------------------------------------------------------------------------->
			                    <th class="header"><?= R("UID") ?> <i class="fa fa-sort"> </i></th>
								<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
			                    <th class="header"><?= R("type") ?> <i class="fa fa-sort"> </i></th>
								<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
			                    <th class="header"><?= R("user") ?> <i class="fa fa-sort"> </i></th>
								<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
			                    <th class="header"><?= R("status") ?> <i class="fa fa-sort"> </i></th>
								<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
			                     <?php if (Session::get_activerole()->role_code == 'LP') {?>
			                      <th class="header"><?= R("publish_status") ?> <i class="fa fa-sort"> </i></th>
								 <?php } ?>
			                  </tr>
			                </thead>
			                <tbody>
			                <?php foreach($list as $obj)  { ?>
			                  <tr class="annotation" title="<?=$obj->annotation ?>">
			                  
			                  	<?php if (Session::get_activerole()->role_code == 'LA' || Session::get_activerole()->role_code == 'LP') { ?>
			                  	<!------------------------------------------------------------------------------------------->
								<td>
									<input type="checkbox" name="mark_<?= $obj->id ?>">
								</td>
			                  	<?php } ?>
			                  
								<!------------------------------------------------------------------------------------------->
								<?php 

									$name = "recordcode";
									$type = "text";
								?>
								<td>
									<a href="javascript:openLimeModal('<?= __SITEBASE ?>/collectdata/gotoLIME?s=<?=$obj->statisticaltypeid ?>&t=<?= $obj->limetoken ?>&i=<?= $obj->id ?>')">
										<?php if ($obj->$name!=null && $obj->$name!="") { ?>
											<?= print_field($obj->$name,$type) ?>
										<?php } else { ?>
											...
										<?php } ?>
									</a>
								</td>
								<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "statisticaltypes_descriptioncode";
									$type = "text";
								?>
								<td><?= print_field(D($obj-> $name),$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "username";
									$type = "text";
								?>
								<td><?= print_field($obj-> $name,$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "codelistitems_descriptioncode";
									$type = "text";
								?>
								<td><?= print_field(D($obj-> $name),$type) ?>
				                 </td>
			             		<!------------------------------------------------------------------------------------------->
								<!------------------------------------------------------------------------------------------->
				                 <?php if (Session::get_activerole()->role_code == 'LP') {?>
					                 
					                 <?php 
										$name = "publishcode_descriptioncode";
										$type = "text";
									?>
			                      <td class="header"><?= print_field(D($obj-> $name),$type) ?></td>
								 <?php } ?>
			                  </tr>
			                <?php } ?>
			                	</tbody>
			              </table>
		              </div>
	              </div>
            </div>
            <?php if (Session::get_activerole()->role_code == 'LP') { ?>
            	 <button class="btn btn-default checkboxdependent"><?= R('mark_publishable') ?></button>
            <?php } ?>
            <?php if ((Session::get_activerole()->role_code == 'LA' || Session::get_activerole()->role_code == 'LP')  && $_POST["type"] && count($list) > 0 ) { ?>
            	<button style="padding-left:20px;" type="submit" onclick="toggleAction('exportexcel')" class="btn btn-default checkboxdependent" <?= $ifvoid ?>><?=R('label_exportexcel') ?></button>
            	<input type="hidden" name="type" value="<?= $_POST['type'] ?>">
            <?php } ?> 
            </form>
            <?php if (Session::get_activerole()->role_code == 'LC') {?>
            	
           		 <form name="formsearch" class="form-inline" method="post"  action="<?= __SITEBASE ?>/collectdata/finduid">
           		 	<?= R('label_UID') ?>	
	           		<input type="text" name="searchuid" required style="width:20%" class="form-control">
	           		<button class="btn btn-default"><?=R('label_find') ?></button>
	           	</form> 
	     			<br><br>
	     		<form class="form-inline">
		           	<button  <?= $cannewvictim ?> onclick="javascript:collect('victim');return false;" class="btn btn-default"><?= R('btn_add_victim') ?></button>
		            &nbsp;&nbsp;
		           	<button <?= $cannewtrafficker ?> onclick="javascript:collect('trafficker');return false;" class="btn btn-default"><?= R('btn_add_trafficker') ?></button>
		           	&nbsp;&nbsp;
		           	<span id="newUID" style="display:none"><?= R('label_UID') ?> <input type="text" required style="width:20%" id="textUID" class="form-control"> <button id="but_create_next" onclick="javascript:goToCreate();return false;" class="btn btn-default"><?= R('label_next') ?></button></span>
            	</form>
	       	<?php } ?>
              <?php if (Session::get_activerole()->role_code == 'LP') { ?>
              <span class="pull-right">
	           	 <?= R('label_publish_to_nfp') ?>
	           	 &nbsp;&nbsp;
	           	<button onclick="location.href='<?= __SITEBASE ?>/collectdata/publishvictims'" class="btn btn-default "><?=R('btn_publish_victims') ?></button>
	            &nbsp;&nbsp;
	           	<button onclick="location.href='<?= __SITEBASE ?>/collectdata/publishtraffickers'" class="btn btn-default "><?=R('btn_publish_traffickers') ?></button>
	           </span>
              <?php } ?>       
              
           </div>
     </div>

<br><br>
<div class="modal" id="limeModal" 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" onclick="javascript:location.href='<?= __SITEBASE ?>/collectdata'" aria-hidden="true">X</button>
          <h4 class="modal-title"><?= __NAME ?></h4>
        </div>
        <div class="modal-body">
        	<iframe id="limemod" style="border:0;width:100%;height:630px"></iframe>
        </div>
      </div>
    </div>
</div>
<script>

$(function() {
    $('.annotation').tooltip();
});    

var addtype = null;

function collect(type) {
	
	addtype = type;
	$('#newUID').show();
	
}


function validate_checks() {
	var numberOfChecked = $('input:checkbox:checked').length;

	if (numberOfChecked >0) {
		return true;
	} else {
	return false;
	}
}

function goToCreate() {

	var uid = $('#textUID').val();
	debugger;
	if (uid != null && uid != '') {

		url = '';

		if (addtype == 'victim') {
		 url =	'<?= __SITEBASE ?>/collectdata/addvictim?uid='+ uid;
		}
		
		if (addtype == 'trafficker') {
			 url =	'<?= __SITEBASE ?>/collectdata/addtrafficker?uid='+ uid;
		}
		
		openLimeModal(url);

	} 
	
}
 
function openLimeModal(url) {
			
	$('#limemod').attr('src',url);
	$('#limeModal').modal({show:true});
}

$(document).ready(function() {
<?php 
if ($openlimeobj){
	$obj = $openlimeobj;	
	?>
	openLimeModal('<?= __SITEBASE ?>/collectdata/gotoLIME?s=<?=$obj->statisticaltypeid ?>&t=<?= $obj->limetoken ?>&i=<?= $obj->id ?>');
<?php  }  ?>
});


function toggleAction(action) {
	
	if (action == 'exportexcel')
	{
		$('#formcollect').attr('action','<?= __SITEBASE ?>/collectdata/exportexcel');
	}
}

// enable and disable buttons dependent off checkboxes
$(document).ready(function() {
	$(".checkboxdependent").prop('disabled', true);

	$("[type=checkbox]").bind("click",function(){
	
		if($("[type=checkbox]:checked").length == 0) {
			$(".checkboxdependent").prop('disabled', true);
		} else {
			$(".checkboxdependent").prop('disabled', false);;
		}
		
	});
});
//-----------------------------------------------------------
</script>
