<?php 
/**
 * Screen localprofile\detail
 * @version 1.0.1
 * @package Backend\DataStructures
 * 
*/
$context = $this->data["contextname"];
$dsd = $this->data['dsd'];
$version = $this->data['version'];
$list = $this->data['list'];
$listEdit = $this->data['listedit'];
$page = $this->data['page'];
$cangolive = $this->data['cangolive'];
$candisable = $this->data['candisable'];
$isdraft = $this->data['isdraft'];
$listVoid = $this->data['listvoid'];
$listNdp = $this->data['listndp'];
$ndpSelected = $this->data['ndp'];
$editMode = false;

$action = $page == "victims" ? 'newldpvictims' : 'newldptraffickers';
function sortArray(&$array, $item)
    {  
        $result_array = array();
            foreach ($array as $key => $row)
            {
                $result_array[$key] = $row->$item;
            }
          
                array_multisort($result_array, SORT_ASC , $array);
    }
if ($dsd->id) {
	$editMode = true;	
	$liveNoEdit = ($dsd->active ==1) ? ' disabled="disabled" ' :  '';
} 

?>

<script>

function t(ck,sl) {
	if(ck.prop('checked')) {
		sl.prop( "disabled", false);
	    // something when checked
	} else {
		sl.prop( "disabled", true );
	    // something else when not
	}
}

function toggleAction(action) {

		$('#formldp').attr('action','<?= __SITEBASE ?>/localprofile/'+action);

}

</script>

<div class="row" >
<div class="col-lg-12" >
            <h3><?=  $page == "victims" ? R('page_title_victims_localprofile') : R('page_title_traffickers_localprofile') ?></h3>
            	<p>&nbsp;</p>
				<?php $this->load('common/message',$this) ?>   
            	<form id="formldp" name="formldp" class="form-horizontal" action="<?= __SITEBASE ?>/localprofile/<?= $action ?>"  method="post">
				<!------------------------------------------------------------------------------------------->
				<input type="hidden" name="statisticaltype" value="<?= ($page=="victims") ? __STATISTICAL_TYPE_VICTIMS: __STATISTICAL_TYPE_TRAFFICKERS?>"/>
				
            	<?php
					$label = R('name');
					$name  = 'description';
					$type  = 'text';
					$val =  val($dsd,$name);
				?>
				
				<div class="form-group">
	    			<label class="col-sm-2 control-label" ><?= htmlentities($label) ?>:</label>
	    			<div class="col-sm-5">
	        			<input type="text" required id="<?=$name ?>" <?= $liveNoEdit ?>  maxlength="150" class="form-control" name="<?=$name ?>" placeholder="<?= $label ?>" value="<?= val($dsd,$name) ?>">
        			</div>
   				</div>
				<!------------------------------------------------------------------------------------------->
				<!------------------------------------------------------------------------------------------->
				<?php
					$label = R('LabelNationalProfile');
					$name  = 'ndp_id';
					
				?>
				
				<div class="form-group">
	    			<label class="col-sm-2 control-label" ><?= htmlentities($label) ?>:</label>
	    			<div class="col-sm-5">
								<select class="form-control" onchange="toggleAction('<?= $action ?>');this.form.submit();" <?= $liveNoEdit ?> name="<?= $name ?>" id="<?= $name ?>" >
								  <?php 
								  foreach ($listNdp as $ondp) {?>
										<option value="<?=$ondp->datastructuredefinitions_id ?>" <?=($ondp->datastructuredefinitions_id == $ndpSelected)?  ' selected="selected" ' : '' ?>><?= print_field($ondp->datastructuredefinitions_description.' ('.$ondp->versionregistry_code.')') ?></option>
		
								<?php } ?>
								</select>
        			</div>
   				</div>   				
   				
				
				<!------------------------------------------------------------------------------------------->
			
	           <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("HT_Common_Group")?>&nbsp;<i class="fa fa-sort"> </i></th>
			                    <th class="header"><?=R("HT_Common_QID")?>&nbsp;<i class="fa fa-sort"> </i></th>
			                    <th class="header"><?=R("HT_Common_Name")?>&nbsp;<i class="fa fa-sort"> </i></th>
			                    <th class="header"><?=R("HT_Common_Question")?>&nbsp;<i class="fa fa-sort"> </i></th>
			                    <th class="header"><?=R("HT_Common_Data_Type")?>&nbsp;<i class="fa fa-sort"> </i></th>
			                    <th class="header"><?=R("HT_Common_Codelist")?>&nbsp;<i class="fa fa-sort"> </i></th>
			                    <th class="header"><?=R("HT_Common_Core")?></th>
			                    <th class="header"><?=R("HT_Common_Profile")?></th>
			                    <th class="header col-sm-2"><?=R("HT_Common_VoidReason")?></th>
			                    </tr>
			                </thead>
			                <tbody>
			                <?php foreach($list as $obj)  { ?>
			                  <tr >
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "questiongroups_descriptioncode";
									$type = "text";
								?>
								<td><?= print_field(D($obj-> $name),$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "questions_code";
									$type = "text";
								?>
								<td><?= print_field($obj-> $name,$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "umlcode";
									$type = "text";
								?>
								<td><?= print_field($obj-> $name,$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "questions_descriptioncode";
									$type = "?";
								?>
								<td><?= print_field(D($obj-> $name),$type) ?></td>
								
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "codelistitems_code";
									$type = "text";
								?>
								<td><?= print_field($obj-> $name,$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "codelists_descriptioncode";
									$type = "text";
								?>
								<td><?= ($obj-> $name) ? print_field(D($obj-> $name),$type) :"-" ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "iscore";
									$type = "boolean";
								?>
								<td><?= print_field($obj-> $name,$type) ?></td>
								<!------------------------------------------------------------------------------------------->
								<?php 
									
									$name = 'ck_'.$obj->questions_code ;
									$selname = 'sel_voidreason_'.$obj->questions_code;
									$hname = 'h_'.$name;
									$value= $obj->questions_id ;
									
									if (isset($listEdit)) {
										$aquestion = array_filter(
											$listEdit,
											function ($e) use (&$value) { return $e->questionid ==$value ; }
										);

										$found = (count($aquestion)>0);
										$elquestion = array_pop($aquestion);
										
									} else {
										$found = 0;
									}

									$fiscore = (!$editMode && $obj->iscore) ? ' checked="checked" ': (($found == 1)? ' checked="checked" '.$liveNoEdit : ''.$liveNoEdit);
									?>
								<td>
								    <input type="checkbox"  name="<?= $name?>" id="<?= $name?>" onclick="t(<?= "$('#".$name."')" ?>,<?="$('#".$selname."')" ?>)" 
								     <?=$fiscore ?>/>  
								 	 	<input type="hidden" name="<?= $hname?>" value="<?= $value?>" />
								 </td>

								<!------------------------------------------------------------------------------------------->
								<?php 
									$name = "sel_voidreason_".$obj->questions_code;
									$type = "text";
									$sel = (!$editMode && $obj->iscore) ? '' :(($found == 1)? ''.$liveNoEdit:' disabled ');
								?>
								<td>
								<select class="form-control" name="<?= $name ?>" id="<?= $name ?>" <?=$sel ?> >
								  <?php 
                                                                  sortArray($listVoid, 'code');
								  foreach ($listVoid as $olv) {
										if ($olv->code != 'UNPL') { ?>
								  			<option value="<?=$olv->id ?>" <?=($found == 1 && $elquestion->voidreasoncodeid == $olv->id)?  ' selected="selected" ' : '' ?>><?= print_field(D($olv->descriptioncode),$type) ?></option>
								  <?php }		
									} ?>
								</select>
								</td>
								<!------------------------------------------------------------------------------------------->
			                  </tr>
			                <?php 
									} ?>
			                	</tbody>
			              </table>
		              </div>
	              </div>
            	</div>
            	
            		<!------------------------------------------------------------------------------------------->
				<?php
					$label = R('description');
					$name  = 'annotation';
					$type  = 'text';
					$val =  val($version,$name);
				?>
				
				<div class="form-group">
	    			<label class="col-sm-2 control-label" ><?= htmlentities($label) ?>:</label>
	    			<div class="col-sm-5">
	        			<textarea  maxlength="255"  <?= $liveNoEdit ?> class="form-control" name="<?= $name ?>" placeholder="<?= $label ?>" ><?= ($val) ? $val : "" ?></textarea>
        			</div>
   				</div>
				
				<!------------------------------------------------------------------------------------------->
				<?php
					$label = R('version');
					$name  = 'code';
					$type  = 'text';
					$val =  val($version,$name);
				?>
				
				<div class="form-group">
	    			<label class="col-sm-2 control-label" ><?= htmlentities($label) ?>:</label>
	    			<div class="col-sm-5">
	        			<input type="text" required id="<?= $name ?>" maxlength="20"  <?= $liveNoEdit ?> class="form-control" name="<?= $name ?>" placeholder="<?= $label ?>" value="<?= $val ?>">
        			</div>
   				</div>
				<!------------------------------------------------------------------------------------------->
				<?php
					$label = R('status');
					$name  = 'status';
					$type  = 'text';

					$lactive = ($dsd->active == 1) ? R('active'): R('inactive');
					
					// Button toggle

					$saveState =  ($isdraft)? '' : ' disabled="disabled" ';
					$savenewversionState = ($editMode ==false) ? ' disabled="disabled" ' :  '';
					$liveState = ($action =="new") ? ' disabled="disabled" ' : (($cangolive)? '' : ' disabled="disabled" ');
					$disableState = ($action =="new") ? ' disabled="disabled" ' : (($candisable)? '' : ' disabled="disabled" ');
					
					$onc_new = ($editMode == true) ? " onclick=\"toggleAction('save'); \"  " : " onclick=\"toggleAction('create'); \" ";
					$onc_newversion = ($dsd->active ==1)? " onclick=\"toggleAction('savenewversion'); \" " : " onclick=\"toggleAction('create'); \" ";
					
					//$onc_newversion = ($action =="new") ? '' : " onclick=\"window.location.href='".__SITEBASE."/localprofile/savenewversion?id=".$dsd->id."'\" ";
					$onc_golive = ($action =="new") ? '' : " onclick=\"window.location.href='".__SITEBASE."/localprofile/golive?id=".$dsd->id."'\" ";
					$onc_disable = ($action =="new") ? '' : " onclick=\"window.location.href='".__SITEBASE."/localprofile/disable?id=".$dsd->id."'\" ";					
				?>
				<div class="form-group">
					<label class="col-sm-2 control-label" ><?= htmlentities($label) ?>:</label>
					<div class="col-sm-5">
					<?= $lactive ?>
					</div>
				</div>
				<p>&nbsp;</p>
				<div style="margin-left: 17%">
				    	<?php if ($editMode == true) { ?>
    						<input type="hidden" name="id" value="<?= $dsd->id ?>">
    					<?php } ?>
        			<button type="submit" class="btn btn-default" name="save" <?= $onc_new?> <?=$saveState ?>><?= R('btn_save') ?></button>
        			<button type="submit" class="btn btn-default" name="savenewversion" <?= $onc_newversion?> <?=$savenewversionState ?>><?= R('btn_savenewversion') ?></button>
       				<a href="#"  permissions="true" btnaction="true" class="btn btn-default" <?= $onc_golive?> <?= $liveState ?>><?= R("btn_golive") ?></a>&nbsp;
    				<a href="#" permissions="true" btnaction="true" class="btn btn-default" <?= $onc_disable?> <?= $disableState ?>><?= R("btn_disable") ?></a>&nbsp;
	    			  
					<a href="<?= __SITEBASE ?>/localprofile/<?=$page ?>" class="btn btn-default" ><?= R('btn_cancel') ?></a>
					
				</div>
			</form>
     </div>
</div>
