<?php include(realpath(dirname(__FILE__)."/../")."/header.php"); ?>

    <!-- start page title -->
    <div class="row">
      <div class="col-12">
        <div class="page-title-box">
          <div class="page-title-right">
            <ol class="breadcrumb m-0">
              <li class="breadcrumb-item"><a href="<?=self::getUrl("Dashboard")?>">the-tool</a></li>
              <?php if(is_array($filter['addresstype']) && count($filter['addresstype'])): ?>
              <li class="breadcrumb-item"><a href="<?=self::getUrl("Address")?>">Netzgebiete</a></li>
              <li class="breadcrumb-item active">
                <?php foreach($filter['addresstype'] as $type) { $types[] = __($type); } ?>
                <?=implode(", ", $types)?>
              </li>
              <?php else: ?>
              <li class="breadcrumb-item active">Netzgebiete</li>
              <?php endif; ?>
            </ol>
          </div>
          <h4 class="page-title">Netzgebiete</h4>
        </div>
      </div>
    </div>
    <!-- end page title -->

    <div class="row">
      <div class="col-lg-12">
        
        <div class="card">
          <div class="card-body mb-3">
            <div class="row">
              <div class="col-lg-12">
                <div class="float-left">
                  <h4 class="header-title mb-4">Liste aller Netzgebiete</h4>
                </div>
                <div class="float-right">
                  <a class="btn btn-primary" href="<?=self::getUrl("Network", "add")?>"><i class="fas fa-plus"></i> Neues Netzgebiet anlegen</a>
                </div>
              </div>
            </div>
            
            <?php if(is_array($networks) && count($networks)): ?>
            <table class="table table-hover table-striped">
              <tr>
                <th>Name</th>
                <th>Besitzer</th>
                <th></th>
              </tr>
              <?php foreach($networks as $network): ?>
              <tr class="network-list-tr" id="network-<?=$network->id?>">
                <td onclick="toggleNetwork(<?=$network->id?>)"><?=$network->name?></td>
                <td onclick="toggleNetwork(<?=$network->id?>)"><?=nl2br($network->owner->getCompanyOrName())?></td>
                <td class="controls" style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
                  <!--<a href="<?=self::getUrl("Pop", "Index", ["filter" => ["network_id" => $network->id]])?>" title="POPs anzeigen"><i class="fas fa-project-diagram"></i></a>-->
                  <a href="<?=self::getUrl("Network", "edit", ["id" => $network->id])?>"><i class="far fa-edit"  title="Bearbeiten"></i></a>
                  <a href="<?=self::getUrl("Network", "delete", ["id" => $network->id])?>" class="text-danger" title="Löschen"><i class="fas fa-trash"></i></a>
                </td>
              </tr>
              <tr id="network-detail-<?=$network->id?>"  style="display:none; background-color:#fff">
                <td colspan="3">
                  <ul class="nav nav-pills navtab-bg nav-justified">
                    <li class="nav-item"><a class="nav-link active" href="#net-detail-<?=$network->id?>-detail" data-toggle="tab" aria-expanded="false">Details</a></li>
                    <li class="nav-item"><a class="nav-link" href="#net-detail-<?=$network->id?>-pops" data-toggle="tab" aria-expanded="false">POPs</a></li>
                    <li class="nav-item"><a class="nav-link" href="#net-detail-<?=$network->id?>-sections" data-toggle="tab" aria-expanded="false">Bauabschnitte</a></li>
                    <li class="nav-item"><a class="nav-link" href="#net-detail-<?=$network->id?>-roles" data-toggle="tab" aria-expanded="false">Berechtigungen</a></li>
                  </ul>
                  <div class="tab-content">
                    <div id="net-detail-<?=$network->id?>-detail" class="tab-pane show active">
                      <h4>Details zu <strong><?=$network->name?></strong></h4>
                      <div class="card">
                        <div class="card-body">
                          <table class="table table-bordered">
                            <tr class="bg-white">
                              <th>Name</th>
                              <td><?=$network->name?></td>
                            </tr>
                            <tr class="bg-white">
                              <th>Besitzer</th>
                              <td><?=$network->owner->getCompanyOrName()?></td>
                            </tr>
                            <tr class="bg-white">
                              <th>Interne Notiz</th>
                              <td><?=nl2br($network->note)?></td>
                            </tr>
                          </table>
                        </div>
                      </div>
                    </div>
                    
                    <div id="net-detail-<?=$network->id?>-pops" class="tab-pane">
                      <h4>POPs in <strong><?=$network->name?></strong></h4>
                      <div class="card">
                        <div class="card-body">
                          <?php if(is_array($network->pops) && count($network->pops)): ?>
                            <table class="table" >
                              <tr>
                                <th>Name</th>
                                <th>GPS (Breite, Länge)</th>
                                <th>Standortinfo</th>
                                <th></th>
                              </tr>
                            <?php foreach($network->pops as $pop): ?>
                              <tr>
                                <td><?=$pop->name?></td>
                                <td><?=$pop->gps_lat?>, <?=$pop->gps_long?></td>
                                <td><?=nl2br($pop->location)?></td>
                                <td class="controls" style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
                                  <a href="<?=self::getUrl("Pop", "edit", ["id" => $pop->id])?>"><i class="far fa-edit"  title="Bearbeiten"></i></a>
                                  <a href="<?=self::getUrl("Pop", "delete", ["id" => $pop->id])?>" class="text-danger" title="Löschen"><i class="fas fa-trash"></i></a>
                                </td>
                              </tr>
                            <?php endforeach; ?>
                            </table>
                          <?php else: ?>
                          <p><i>Keine POPs vorhanden.</i></p>
                          <?php endif; ?>
                        </div>
                      </div>
                      
                      <a class="btn btn-primary" href="<?=self::getUrl("Pop", "add", ['network_id' => $network->id])?>"><i class="fas fa-plus"></i> Neuen POP anlegen</a>
                    </div>
                    
                    <div id="net-detail-<?=$network->id?>-sections" class="tab-pane">
                      <h4>Bauabschnitte in <strong><?=$network->name?></strong></h4>
                      <div class="card">
                        <div class="card-body">
                          <form method="post" action="<?=self::getUrl("Networksection", "save")?>">
                            <input type="hidden" name="network_id" value="<?=$network->id?>" />
                            <table class="table table-bordered">
                              <tr>
                                <?php $i=1; foreach($network->sections as $section): ?>
                                <td>
                                  <div class="input-group">
                                    <input type="text" class="form-control" name="sections[<?=$section->id?>]" value="<?=$section->name?>" />
                                    <div class="input-group-append">
                                      <button type="button" class="btn btn-danger" onclick="deleteSection(<?=$section->id?>)"><i class="fas fa-trash-alt"></i></button>
                                    </div>
                                  </div>
                                </td>
                                <?php if($i > 0 && $i % 3 == 0): ?>
                                </tr><tr>
                                <?php endif; ?>
                                <?php $i++; endforeach; ?>
                              </tr>
                              <tr>
                                <td colspan="3"><input type="text" class="form-control" name="sections[new]" value="" placeholder="Neuer Bauabschnitt" /></td>
                              </tr>
                            </table>
                            <input type="submit" class="btn btn-primary" value="Speichern" />
                          </form>
                        </div>
                      </div>
                    </div>
                    
                    <div id="net-detail-<?=$network->id?>-roles" class="tab-pane">
                      <h4>Berechtigungen</h4>
                      <div class="card">
                        <div class="card-body">
                          <?php if(is_array($network->addresstypes) && count($network->addresstypes)): ?>
                            <form class="form" method="post" action="<?=self::getUrl("NetworkAddress", "save")?>">
                              <input type="hidden" name="network_id" value="<?=$network->id?>" />
                              <table class="table table-bordered">
                                <tr>
                                  <th>Name</th>
                                  <th>Netzeigentümer</th>
                                  <?php foreach(TT_NETWORK_ROLES as $role): ?>
                                  <th class="text-center"><?=__($role)?></th>
                                  <?php endforeach ?>
                                  <th></th>
                                </tr>
                              <?php foreach($network->addresstypes as $address_id => $addresstypes): ?>
                                <tr>
                                  <td><?=AddressModel::getOne($address_id)->getCompanyOrName()?></td>
                                  <td><input type="checkbox" <?=($network->owner_id == $address_id) ? "checked='checked'" : ""?> disabled="disabled" /></td>
                                  <?php foreach(TT_NETWORK_ROLES as $role): ?>
                                  <td class="text-center">
                                    <?php if(AddresstypeModel::getFirst(['address_id' => $address_id, 'addresstype' => [$role]]) !== null): ?>
                                    <input type="checkbox"
                                      type="checkbox"
                                      name="roles[<?=$address_id?>][]"
                                      value="<?=$role?>"
                                      <?php foreach($addresstypes as $type): ?>
                                        <?php if($type->type == $role): ?>
                                          checked='checked'
                                        <?php continue; endif; ?>
                                      <?php endforeach; ?>
                                    />
                                    <?php endif; ?>
                                  </td>
                                  <?php endforeach; ?>
                                  <td class="controls" style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
                                    <a href="<?=self::getUrl("NetworkAddress", "delete", ["network_id" => $network->id, "address_id" => $address_id])?>" onclick="if(!confirm('Berechtigungen wirklich löschen?')) return false;" class="text-danger" title="Löschen"><i class="fas fa-trash"></i></a>
                                  </td>
                                </tr>
                              <?php endforeach; ?>
                              </table>
                            
                              <button type="submit" class="btn btn-primary"><i class="fas fa-check"></i> Speichern</button>
                            </form>
                          <?php else: ?>
                          <p><i>Keine berechtigten Firmen/Personen vorhanden.</i></p>
                          <?php endif; ?>

                          
                          <h4 class="mt-4">Neu</h4>
                          <form class="form" method="post" action="<?=self::getUrl("NetworkAddress", "create")?>">
                            <input type="hidden" name="network_id" value="<?=$network->id?>" />
                            <table class="table table-bordered">
                              <tr>
                                <th>Person / Firma</th>
                                <?php foreach(TT_NETWORK_ROLES as $role): ?>
                                <th class="text-center"><?=__($role)?></th>
                                <?php endforeach ?>
                              </tr>

                              <tr>
                                <td>
                                  <select class="select2 form-control" name="address_id" id="address_id">
                                    <option></option>
                                    <?php foreach(AddressModel::search(["parents_only" => 1]) as $address): ?>
                                      <?php if(is_array($network->addresstypes) && array_key_exists($address->id, $network->addresstypes)) continue; ?>
                                      <option value="<?=$address->id?>"><?=($address->getCompanyOrName())?></option>
                                    <?php endforeach; ?>
                                  </select>
                                </td>
                                <?php foreach(TT_NETWORK_ROLES as $role): ?>
                                <td class="text-center">
                                  <input type="checkbox" name="roles[]" value="<?=$role?>" />
                                </td>
                                <?php endforeach; ?>
                              </tr>
                            </table>

                            <button type="submit" class="btn btn-primary" ><i class="fas fa-plus"></i> Person / Firma hinzufügen</button>
                            

                          </form>
                        </div>
                      </div>
                    </div>
                  </div>
                  
                </td>
              </tr>
              <tr style="display:none;">
                <td colspan="3"></td>
              </tr>
              <?php endforeach; ?>
            </table>
            <?php else: ?>
            <div class="row"><p class="col-md-12"><i>Keine Netzgebiete gefunden</i></p></div>
            <?php endif; ?>
          </div>
        </div>
        
      </div>
    </div>

<script type="text/javascript">
  function toggleNetwork(id) {
    $('#network-detail-' + id).toggle();
    if($('#network-detail-' + id).is(":hidden")) {
      $('#network-' + id).removeClass("table-info");
      $('#network-' + id).removeClass("text-info");
    } else {
      $('#network-' + id).addClass("text-info");
      $('#network-' + id).addClass("table-info");
    }
  }
  
  
  var network;
  var hash = window.location.hash.substr(1);
  var match = hash.match(/net=(\d+)/);
  if(match && match[1]) {
    network = match[1]
    toggleNetwork(network);
  }
  match = hash.match(/view=([^&]+)/);
  if(match && match[1] && network) {
    view = match[1];
    $('a[href="#net-detail-' + network + '-' + view + '"]').tab('show');
  }
  
  
  //window.location.hash = "";
  
  function deleteSection(id) {
    if(!confirm('Bauabschnitt wirklich löschen?')) return false;
    
    window.location="<?=self::getUrl("Networksection","delete")?>/" + id;
  }
</script>

<?php include(realpath(dirname(__FILE__)."/../")."/footer.php"); ?>