<?php
/**
 * Screen organizations\index
 * @version 1.0.1
 * @package Backend\Organisations
 * 
*/
$listDPA = $this->data["listdpa"];
$list = $this->data['list'];
function sortArray(&$array, $item)
    {  
        $result_array = array();
            foreach ($array as $key => $row)
            {
                $result_array[$key] = $row->$item;
            }
          
                array_multisort($result_array, SORT_ASC , $array);
    }
?>
<div class="row" >
<div class="col-lg-12" >
            <h3><?=R('page_organization_management_title') ?></h3>
			<?php $this->load('common/message',$this) ?>  
             <div class="panel panel-default">
	         <div class="panel-body">
	        	<button class="btn" onclick="window.location.href='<?= __SITEBASE ?>/organizations/neworganization'"><?= R("btn_addorganization")?></button>
	         </div>
	         </div>
	           <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>
                    <th class="header"><?= R("label_name")?> <i class="fa fa-sort"></i></th>
                    <th class="header"><?= R("label_active")?> <i class="fa fa-sort"></i></th>
                    <th class="header"><?= R("label_dpa_victims")?> <i class="fa fa-sort"></i></th>
                    <th class="header"><?= R("label_dpa_traffickers")?> <i class="fa fa-sort"></i></th>
                    <th class="header"><?= R("label_actions")?> </th>
                  </tr>
                </thead>
                <tbody>
                <?php 
                sortArray($list, 'organisationname');
                foreach($list as $org)  {
                    $button_status='';
                    if($org->active==FALSE || Authorization::context_access_mode() == __ACCESS_READ) {
                            $button_status='disabled="disabled"';
                       }
                	$desc_dpa_v = "";
                	$desc_dpa_t = "";
                	$value = 0;
                	if (isset($listDPA)) {

						$value = $org->id;
                		$dpaFound = array_filter(
                				$listDPA,
                				function ($e) use (&$value) { return $e->provisionagreements_fromorganisationid ==$value; }
                		);
                	
                		foreach ($dpaFound as $dpa) {
							
							if ($dpa->datastructuredefinitions_statisticaltypeid == __STATISTICAL_TYPE_TRAFFICKERS) {
								$desc_dpa_t = $dpa->padesccodeversion;
							}
							if ($dpa->datastructuredefinitions_statisticaltypeid == __STATISTICAL_TYPE_VICTIMS) {
								$desc_dpa_v = $dpa->padesccodeversion;
							}
						}

                	} 
                	
                	?>
                  <tr onclick="window.location.href='<?= __SITEBASE ?>/organizations/edit?id=<?= $org->id ?>'">
                    <td><?= $org->organisationname ?></td>
                    <td><?= print_field($org->active,'boolean') ?></td>
                    <td><?= $desc_dpa_v ?></td>
                    <td><?= $desc_dpa_t ?></td>
                    <td><a class="btn btn-default" href="<?= __SITEBASE ?>/users/newuser/?organisationid=<?= $org->id ?>" <?= $button_status;?>><?= R('add_user') ?></a></td>
                  </tr>
                <?php }?>
                </tbody>
              </table>
              </div>
              </div>
            </div>
       </div>
</div> 
          