<?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>
              <li class="breadcrumb-item active">Bestellungen</li>
            </ol>
          </div>
          <h4 class="page-title">Bestellungen</h4>
        </div>
      </div>
    </div>
    <!-- end page title -->

    <div class="row">
      <div class="col-lg-12">
        
        <div class="card">
          <div class="card-body mb-3">
            <h4 class="header-title mb-3">Filter</h4>
            
            <form method="get" action="<?=self::getUrl("Order")?>">
              <div class="row">
                
                <div class="col-2">
                  <label class="form-label" for="filter_network_id">Netzgebiet</label>
                  <select name="filter[network_id]" id="filter_network_id" class="form-control">
                    <?php foreach($mynetworks as $fnet): ?>
                    <option value="<?=$fnet->id?>" <?=($filter['network_id'] == $fnet->id) ? "selected='selected'" : ""?>><?=$fnet->name?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
                
                <div class="col-1">
                  <label class="form-label" for="filter_building_networksection_id">Bauabschnitt</label>
                  <select name="filter[building_networksection_id]" id="filter_building_networksection_id" class="form-control">
                    <option></option>
                    <?php foreach($mynetworks as $fnet): ?>
                      <?php if(is_array($fnet->sections) && count($fnet->sections)): ?>
                        <optgroup label="<?=$fnet->name?>">
                        <?php foreach($fnet->sections as $section): ?>
                          <option value="<?=$section->id?>" <?=($filter['building_networksection_id'] == $section->id) ? "selected='selected'" : ""?>><?=$section->name?></option>
                        <?php endforeach; ?>
                        </optgroup>
                      <?php endif; ?>
                    <?php endforeach; ?>
                  </select>
                </div>
                
                <div class="col-2">
                  <label class="form-label" for="filter_status_id">Anschlussstatus</label>
                  <select name="filter[termination_status]" id="filter_building_status_id" class="form-control">
                    <option></option>
                    <option value="pipework_needed" <?=($filter['termination_status'] == "pipework_needed") ? "selected='selected'" : ""?>>Tiefbau ausständig</option>
                    <option value="building_connected" <?=($filter['termination_status'] == "building_connected") ? "selected='selected'" : ""?>>Tiefbau erledigt</option>
                    <option value="term_connected" <?=($filter['termination_status'] == "term_connected") ? "selected='selected'" : ""?>>Anschluss passiv erschlossen</option>
                    <?php if($me->is("Admin")): ?><option value="systemowner_action_status" <?=($filter['termination_status'] == "systemowner_action_status") ? "selected='selected'" : ""?>>Admin Workorder</option><?php endif; ?>
                  </select>
                </div>
                
                <div class="col-1">
                  <label class="form-label" for="filter_building_code">Objekt ID</label>
                  <input type="text" class="form-control" name="filter[building_code]" id="filter_building_code" value="<?=$filter['building_code']?>" />
                </div>
                
                <div class="col-1">
                  <label class="form-label" for="filter_owner">Kunde</label>
                  <input type="text" class="form-control" name="filter[owner]" id="filter_owner" value="<?=$filter['owner']?>" />
                </div>
                
                <div class="col-2">
                  <label class="form-label" for="filter_building_street">Straße</label>
                  <input type="text" class="form-control" name="filter[building_street]" id="filter_building_street" value="<?=$filter['building_street']?>" />
                </div>
                
                <div class="col-1">
                  <label class="form-label" for="filter_partner_number">Partnernummer</label>
                  <input type="text" class="form-control" name="filter[partner_number]" id="filter_partner_number" value="<?=$filter['partner_number']?>" />
                </div>
                
                <div class="col-2">
                  <label class="form-label" for="filter_finish_date">Bestellstatus</label>
                  <select name="filter[finish_date]" id="filter_finish_date" class="form-control">
                    <option></option>
                    <option value="0" <?=( (!array_key_exists("finish_date", $filter) || $filter["finish_date"] != "1") ? 'selected="selected"' : "")?>>Offen</option>
                    <option value="1" <?=($filter["finish_date"] == "1" ? 'selected="selected"' : "")?>>Fertiggestellt</option>
                  </select>
                </div>
                
                
              </div>
              <div class="row mt-2">
                <div class="col">
                  <button type="submit" class="btn btn-primary">Filter anwenden</button>
                  <a class="btn btn-secondary" href="<?=self::getUrl("Order")?>">Filter zurücksetzen</a>
                </div>
              </div>
            </form>
            
          </div>
        </div>
        
        <?php if(is_array($lonelyOrders) && count($lonelyOrders)): ?>
        <div class="card">
          <div class="card-body mb-3">
            <div class="float-left">
              <h4 class="header-title">Bestellungen ohne Anschluss</h4>
            </div>
            
            <table class="table">
              <tr>
                <th></th>
                <th></th>
                <th>Kunde</th>
                <th>Adresse</th>
                <th>Bestelldatum</th>
                <th>Zuletzt bearbeitet</th>
                <th>Bearbeitet von</th>
                <th></th>
              </tr>
              <?php $order_count = 0; ?>
              <?php foreach($lonelyOrders as $order): ?>
              <tr class="order-list-tr pb-0 <?=($order_count % 2 == 0) ? "table-bg-even" : ""?>" id="order-<?=$order->id?>">
                <td onclick="toggleOrder(<?=$order->id?>)"><?=$order->id?></td>
                <td>
                  <?php
                  $voip = false;
                  $hw = false;
                  $cpe_config_finished = false;
                  $patched = false;

                  if(count($order->products)) {
                    foreach($order->products as $product) {
                      if(is_array($product->product->attributes) && count($product->product->attributes)) {
                        if(array_key_exists("hw_only", $product->product->attributes)) {
                          if($product->product->attributes["hw_only"]->value) {
                            $hw = true;
                          }
                        }
                        if(array_key_exists("voip_chan", $product->product->attributes)) {
                          if($product->product->attributes["voip_chan"]->value) {
                            $voip = true;
                          }
                        }
                        if(array_key_exists("bras_type", $product->product->attributes)) {
                          if($product->cpeprovisioning->routerconfig_finished) {
                            $cpe_config_finished = true;
                          }
                        }
                        if($hw && $voip_chan && $patched && $cpe_config_finished) {
                          break;
                        }
                      }
                    }
                  }
                  if(is_array($order->terminations) && count($order->terminations)) {
                    if($order->terminations[0]->patching->patched) {
                      $patched = true;
                    }
                  }

                  ?>
                  <?=($hw) ? "<i class='fas fa-shopping-bag text-purple ml-1' title='Zusatzprodukt vorhanden'></i>" : ""?>
                  <?=($voip) ? "<i class='fas fa-phone text-purple ml-1' title='Voice Produkt vorhanden'></i>" : ""?>

                  <?=($patched) ? "<i class='fas fa-ethernet text-pink ml-1' title='Anschluss gepatched'></i>" :""?>
                  <?=($cpe_config_finished) ? "<i class='fas fa-shipping-fast text-pink ml-1' title='CPE vorbereitet'></i>" :""?>
                </td>
                <td onclick="toggleOrder(<?=$order->id?>)">
                  <?=nl2br($order->owner->getCompanyOrName())?>
                  <?=($order->owner->spin) ? "<br /><span class='text-pink'>".$order->owner->spin."</span>" : ""?>
                  <?=($order->partner_number) ? "<br /><span class='text-pink'>".$order->partner_number."</span>" : ""?>
                </td>
                <td onclick="toggleOrder(<?=$order->id?>)">
                  <?=$order->owner->street?><br />
                  <?=$order->owner->zip?> <?=$order->owner->city?>
                </td>
                
                <td onclick="toggleOrder(<?=$order->id?>)" class="text-monospace"><?=date("d.m.Y", $order->order_date)?></td>
                <td onclick="toggleOrder(<?=$order->id?>)" class="text-monospace"><?=date("d.m.Y", $order->edit)?></td>
                <td onclick="toggleOrder(<?=$order->id?>)" class="text-monospace"><?=$order->editor->name?></td>
                <td style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
                  <a href="<?=self::getUrl("Order", "downloadServicePin", ["id" => $order->id])?>"><i class="fas fa-file-alt"  title="Service PIN als PDF herunterladen"></i></a>
                  <a href="<?=self::getUrl("Order", "sendServicePin", ["id" => $order->id])?>" onclick="if(!confirm('Soll der Service-PIN an den Vertragsinhaber gesendet werden?')) return false;"><i class="fas fa-paper-plane"  title="Service PIN als PDF per Email an Vertragsinhaber"></i></a>
                  <a href="<?=self::getUrl("Order", "edit", ["id" => $order->id])?>"><i class="far fa-edit"  title="Bearbeiten"></i></a>
                  <a href="<?=self::getUrl("Order", "delete", ["id" => $order->id])?>" onclick="if(!confirm('Bestellung wirklich löschen?')) return false;" class="text-danger" title="Löschen"><i class="fas fa-trash"></i></a>
                </td>
              </tr>
              <tr class="building-list-tr <?=($order_count % 2 == 0) ? "table-bg-even" : ""?>" id="order-dates-<?=$order->id?>">
                <td onclick="toggleOrder(<?=$order->id?>)" colspan="2" class="pt-0" style="border-top: none;"></td>
                <td onclick="toggleOrder(<?=$order->id?>)" colspan="6" class="pt-0" style="border-top: none;">
                  <div class="row">
                    <div class="mr-4 order-date-pill <?=($order->shippingdate) ? "active" : ""?>">Versandtermin: <span class="text-monospace"><?=($order->shippingdate) ? "<span class='text-success'>".date("d.m.Y", $order->shippingdate)."</span>" : "--.--.----"?></span></div>
                    <div class="order-date-pill <?=($order->install_date) ? "active" : ""?>">Vorortinstallation: <span class="text-monospace"><?=($order->install_date) ? "<span class='text-success'>".date("d.m.Y",$order->install_date)."</span>" : "--.--.----"?></span></div>
                  </div>
                </td>
              </tr>
              <tr id="order-detail-<?=$order->id?>" class="hidden" style="background-color:#cddef7">
                <td colspan="8">
                  
                  <div class="card">
                    <div class="card-body">
                      <div class="row justify-content-center mt-2">
                        <div class="col-xl-8 col-lg-12">
                      
                          <ul class="nav nav-tabs">
                            <li class="nav-item"><a class="nav-link active" href="#order-detail-<?=$order->id?>-journal" data-toggle="tab" aria-expanded="false">Journal</a></li>
                            <li class="nav-item"><a class="nav-link" href="#order-detail-<?=$order->id?>-contact" data-toggle="tab" aria-expanded="false">Kontakt</a></li>
                            <li class="nav-item"><a class="nav-link" href="#order-detail-<?=$order->id?>-details" data-toggle="tab" aria-expanded="false">Details</a></li>
                            <li class="nav-item"><a class="nav-link" href="#order-detail-<?=$order->id?>-files" data-toggle="tab" aria-expanded="false">Dokumente</a></li>
                          </ul>
                        </div>
                      
                      </div>

                      <div class="tab-content">
                        <div id="order-detail-<?=$order->id?>-journal" class="tab-pane show active">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">

                              <div class="card">

                                <div class="card-body">
                                  <div class="card-header bg-info text-white pl-2 pr-2 pt-1 pb-1">Bestelljournal</div>
                                  <div class="card mb-3">
                                    <div class="card-header bg-light"><button type="button" class="btn btn-sm btn-info" onclick="$('#new-journal-<?=$order->id?>').toggle()"><i class="fas fa-plus"></i> Neuer Eintrag</button></div>
                                    <div id="new-journal-<?=$order->id?>" class="card-body hidden">
                                      <form method="post" action="<?=self::getUrl("OrderJournal", "save")?>">
                                        <input type="hidden" name="order_id" value="<?=$order->id?>" />
                                        <input type="hidden" name="s" value="<?=$pagination['start']?>" />
                                        <?php if(is_array($filter) && count($filter)) foreach($filter as $n => $v): ?>
                                          <input type="hidden" name="filter[<?=$n?>]" value="<?=$v?>" />
                                        <?php endforeach; ?>
                                        <textarea name="text" class="form-control mb-2" style="height:120px;"></textarea>
                                        <button class="btn btn-sm btn-primary" type="submit"><i class="fas fa-save"></i> Speichern</button>
                                      </form>
                                    </div>
                                  </div>

                                  <?php if(is_array($order->journals) && count($order->journals)): ?>
                                    <?php foreach($order->journals as $journal): ?>
                                      <div class="card mb-1" style="border: 1px solid #ddd">
                                        <div class="card-header bg-light text-monospace text-muted pl-2 pr-2 pt-1 pb-1"><span class="font-weight-bold" style="float: right"><i class="fas fa-clock"></i> <?=date("d.m.Y H:i:s",$journal->create)?></span> Eingetragen von <strong><?=$journal->creator->name?></strong></div>
                                        <div class="card-body pl-2 pr-2 pt-1 pb-1">
                                          <?=nl2br($journal->text)?>
                                        </div>
                                      </div>
                                    <?php endforeach; ?>
                                  <?php endif; ?>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>

                        
                        <div id="order-detail-<?=$order->id?>-contact" class="tab-pane">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">

                              <div class="card">

                                <div class="card-body">
                                  <div class="card-header bg-info text-white pl-2 pr-2 pt-1 pb-1">Kontaktdaten</div>
                                  
                                  <div class="row">
                                    <div class="col-lg-6">
                                      <h4>Vertragsinhaber</h4>
                                      <table class="table table-sm table-striped table-bordered">
                                        <tr>
                                          <th>Firma</th>
                                          <td><?=$order->owner->company?></td>
                                        </tr><tr>
                                          <th>Vorname</th>
                                          <td><?=$order->owner->firstname?></td>
                                        </tr><tr>
                                          <th>Nachname</th>
                                          <td><?=$order->owner->lastname?></td>
                                        </tr><tr>
                                          <th>Straße</th>
                                          <td><?=$order->owner->street?></td>
                                        </tr><tr>
                                          <th>PLZ</th>
                                          <td><?=$order->owner->zip?></td>
                                        </tr><tr>
                                          <th>Ort</th>
                                          <td><?=$order->owner->city?></td>
                                        </tr><tr>
                                          <th>Land</th>
                                          <td><?=$order->owner->country?></td>
                                        </tr><tr>
                                          <th>Telefon</th>
                                          <td class="text-monospace"><?=($order->owner->phone) ? "<a href='tel:".$order->owner->phone."'>".$order->owner->phone."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Mobiltelefon</th>
                                          <td class="text-monospace"><?=($order->owner->mobile) ? "<a href='tel:".$order->owner->mobile."'>".$order->owner->mobile."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Fax</th>
                                          <td class="text-monospace"><?=($order->owner->fax) ? "<a href='tel:".$order->owner->fax."'>".$order->owner->fax."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Email</th>
                                          <td class="text-monospace"><?=($order->owner->email) ? "<a href='mailto:".$order->owner->email."'>".$order->owner->email."</a>" : ""?></td>
                                        </tr>
                                      </table>
                                    </div>
                                    
                                    <?php if($order->billingaddress_id): ?>
                                    <div class="col-lg-6">
                                      <h4>Rechungsempfänger</h4>
                                      <table class="table table-sm table-striped table-bordered">
                                        <tr>
                                          <th>Firma</th>
                                          <td><?=$order->billingaddress->company?></td>
                                        </tr><tr>
                                          <th>Vorname</th>
                                          <td><?=$order->billingaddress->firstname?></td>
                                        </tr><tr>
                                          <th>Nachname</th>
                                          <td><?=$order->billingaddress->lastname?></td>
                                        </tr><tr>
                                          <th>Straße</th>
                                          <td><?=$order->billingaddress->street?></td>
                                        </tr><tr>
                                          <th>PLZ</th>
                                          <td><?=$order->billingaddress->zip?></td>
                                        </tr><tr>
                                          <th>Ort</th>
                                          <td><?=$order->billingaddress->city?></td>
                                        </tr><tr>
                                          <th>Land</th>
                                          <td><?=$order->billingaddress->country?></td>
                                        </tr><tr>
                                          <th>Telefon</th>
                                          <td class="text-monospace"><?=($order->billingaddress->phone) ? "<a href='tel:".$order->billingaddress->phone."'>".$order->billingaddress->phone."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Mobiltelefon</th>
                                          <td class="text-monospace"><?=($order->billingaddress->mobile) ? "<a href='tel:".$order->billingaddress->mobile."'>".$order->billingaddress->mobile."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Fax</th>
                                          <td class="text-monospace"><?=($order->billingaddress->fax) ? "<a href='tel:".$order->billingaddress->fax."'>".$order->billingaddress->fax."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Email</th>
                                          <td class="text-monospace"><?=($order->billingaddress->email) ? "<a href='mailto:".$order->billingaddress->email."'>".$order->billingaddress->email."</a>" : ""?></td>
                                        </tr>
                                      </table>
                                    </div>
                                    <?php endif; ?>
                                  </div>
                                  
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        
                        
                        <div id="order-detail-<?=$order->id?>-details" class="tab-pane">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">
                              <div class="card">
                                <div class="card-body">
                              
                                      <table class="table table-sm table-bordered">
                                        <tr>
                                          <th class="bg-light">Bestelldatum:</th>
                                          <td><?=date("d.m.Y",$order->order_date)?></td>
                                          
                                          <th class="bg-light">Herstellbar ab:</th>
                                          <td><?=($order->finish_after) ? date("d.m.Y",$order->finish_after) : "--.--.----" ?></td>
                                          
                                          <th class="bg-light">Rechnungsart:</th>
                                          <td><?=($order->billing_type == "sepa") ? "SEPA" : "Rechnung"?></td>
                                          
                                          <th class="bg-light">Einwilligungen:</th>
                                          <td><?=($order->allow_contact) ? "<span class='order-date-pill active'><i class='fas fa-check text-success'></i> Info</span>" : ""?> <?=($order->allow_spin) ? "<span class='order-date-pill active ml-1'><i class='fas fa-check text-success'></i> SPIN</span>" : ""?></td>
                                        </tr>
                                      </table>

                                  
                                  <div class="row">
                                    <div class="form-group col-lg-4 col-md-6 col-sm-8">
                                      <label>Vorortinstallation:</label>
                                      <div class="input-group">
                                        <input type="text" class="form-control datepicker" id="vororttermin-<?=$order->id?>" name="vororttermin" value="<?=($order->install_date) ? date("d.m.Y",$order->install_date) : ""?>" placeholder="--.--.----" />
                                        <div class="input-group-append">
                                          <button class="btn btn-outline-primary" id="vororttermin-button-<?=$order->id?>" type="button" title="Vororttermin speichern" onclick="saveVororttermin(<?=$order->id?>)"><i class="fas fa-save"></i></button>
                                          <button class="btn btn-outline-danger ml-1" id="vororttermin-delete-<?=$order->id?>" type="button" title="Vororttermin speichern" onclick="saveVororttermin(<?=$order->id?>, true)"><i class="fas fa-trash"></i></button>
                                        </div>
                                        
                                      </div>
                                    </div>
                                  </div>

                                  <?php if(is_array($order->products) && count($order->products)): ?>
                                  <h4>Produkte</h4>
                                  <table class="table table-sm table-striped table-bordered">
                                    <tr>
                                      <th class="text-right">Pos</th>
                                      <th class="text-right">Anzahl</th>
                                      <th>Produkt</th>
                                      <th>Rechn.Periode</th>
                                      <th class="text-right">Preis p. P</th>
                                      <th class="text-right">Preis Setup</th>
                                      <th>Rechn.verz.</th>
                                    </tr>
                                    <?php foreach($order->products as $product): ?>
                                      <tr>
                                        <td class="text-right"><?=$product->pos?></td>
                                        <td class="text-right"><?=$product->formatAmount()?></td>
                                        <td>
                                          <?=$product->product->name?>
                                          <?php
                                          if(
                                            (is_array($product->product->attributes) && count($product->product->attributes))
                                            && (array_key_exists(TT_ATTRIB_TERMINATION_REQUIRED_NAME, $product->product->attributes)
                                                && $product->product->attributes[TT_ATTRIB_TERMINATION_REQUIRED_NAME]->value == 1)
                                            || $product->termination_id
                                            ):
                                          ?>
                                          <br /><small class="text-pink"><?=$product->termination->code?> - <?= str_replace("\n", " - ", $product->termination->getAddress())?></small>
                                          <?php endif; ?>
                                        </td>
                                        <td><?=($product->billing_period == 12) ? "Jährlich" : "Monatlich"?></td>
                                        <td class="text-monospace text-right"><?=number_format($product->price, 4, ",", ".")?></td>
                                        <td class="text-monospace text-right"><?=number_format($product->price_setup, 4, ",", ".")?></td>
                                        <td><?=($product->billing_delay) ? $product->billing_delay : ""?></td>
                                      </tr>
                                    <?php endforeach; ?>
                                  </table>
                                  <?php endif; ?>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        
                        <div id="order-detail-<?=$order->id?>-files" class="tab-pane">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">
                              <div class="card">
                                <div class="card-body">
                                  <?php if(is_array($order->files) && count($order->files)): ?>
                                    <table class="table table-sm table-striped table-hover">
                                      <tr>
                                        <th>Name</th>
                                        <th>Beschreibung</th>
                                        <th>Dateiname</th>
                                        <th>Hochgeladen</th>
                                        <th>Zuletzt bearbeitet</th>
                                      </tr>
                                      <?php foreach($order->files as $file): ?>
                                      <tr>
                                        <td><a href="<?=self::getUrl("File", "Download", ['id' => $file->file->id])?>"><?=$file->name?></a></td>
                                        <td><?=nl2br($file->description)?></td>
                                        <td><?=$file->file->filename?></td>
                                        <td class="text-monospace"><?=date("d.m.Y", $file->create)?> (<?=$file->creator->name?>)</td>
                                        <td class="text-monospace"><?=date("d.m.Y", $file->edit)?> (<?=$file->editor->name?>)</td>
                                      </tr>

                                      <?php endforeach; ?>
                                    </table>
                                  <?php else: ?>
                                    <p>Keine Dokumente hinterlegt.</p>
                                  <?php endif; ?>
                                  
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        
                      </div>
                      
                    </div>
                  </div>
                  
                </td>
              </tr>
              <tr style="display:none;">
                <td colspan="9"></td>
              </tr>
              <?php $order_count++; ?>
              <?php endforeach; ?>
            </table>
          </div>
        </div>
        <?php endif; ?>
        
        <div class="card">
          <div class="card-body mb-3">
            <div class="float-left">
              <h4 class="header-title">Bestellungen mit Anschluss</h4>
            </div>
            <div class="float-right">
              <a class="btn btn-primary mb-2" href="<?=self::getUrl("Order", "add")?>"><i class="fas fa-plus"></i> Neue Bestellung anlegen</a>
            </div>
            
            <?php if($pagination['maxItems'] > $pagination['count']): ?>
              <nav>
                <ul class="pagination justify-content-center mb-1">
                  <li class="page-item <?=($pagination['start'] - $pagination['count'] < 0) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod, "Index", ["filter" => $filter])?>" tabindex="-1">Erste</a>
                  </li>
                  <li class="page-item <?=($pagination['start'] - $pagination['count'] < 0) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => $pagination['start'] - $pagination['count'], "filter" => $filter])?>" tabindex="-1">Zurück</a>
                  </li>
                  <?php for($i = 0; $i < ($pagination['maxItems'] / $pagination['count']); $i++): ?>
                    <li class="page-item <?=($pagination['start'] == $i*$pagination['count']) ? "disabled font-weight-bold" : ""?>"><a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => $i*$pagination['count'], "filter" => $filter])?>"><?=$i+1?></a></li>
                  <?php endfor; ?>
                  <li class="page-item <?=($pagination['maxItems'] / ($pagination['start'] + $pagination['count'])  < 1) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => $pagination['start'] + $pagination['count'], "filter" => $filter])?>">Vor</a>
                  </li>
                  <li class="page-item <?=($pagination['maxItems'] / ($pagination['start'] + $pagination['count'])  < 1) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => ($pagination['maxItems'] - $pagination['maxItems'] % $pagination['count']), "filter" => $filter])?>">Letzte</a>
                  </li>
                </ul>
              </nav>
            <?php endif; ?>
            <div class="pagination justify-content-center">
              <?php if($pagination['maxItems']): ?>
                Bestellungen <?=$pagination['start']+1?> bis <?=($pagination['start'] + $pagination['count'] > $pagination['maxItems']) ? $pagination['maxItems'] : $pagination['start'] + $pagination['count']?> von <?=$pagination['maxItems']?>
              <?php else: ?>
                Keine Bestellungen gefunden
              <?php endif; ?>
            </div>
            
            <table class="table">
              <tr>
                <th></th>
                <th></th>
                <th>Kunde</th>
                <th>Adresse</th>
                <th>Anschlussadresse</th>
                <th>Bestelldatum</th>
                <th>Zuletzt bearbeitet</th>
                <th>Bearbeitet von</th>
                <th></th>
              </tr>
              <?php $order_count = 0; ?>
              <?php foreach($orders as $order): ?>
              <tr class="order-list-tr pb-0 <?=($order_count % 2 == 0) ? "table-bg-even" : ""?>" id="order-<?=$order->id?>">
                <td><?=$order->id?></td>
                <td onclick="toggleOrder(<?=$order->id?>)">
                  <?php if(is_array($order->terminations) && count($order->terminations)): ?>
                    <?php if($order->terminations[0]->status->code >= TT_TERMSTATUS_CONNECTED): ?>
                      <span class="status connected" title="Anschluss connected">CON</span>
                    <?php elseif($order->terminations[0]->building->status->code >= TT_BUILDINGSTATUS_CONNECTED): ?>
                      <a href="<?=self::getUrl("Linework", "Index", ["filter" => ["code" => $order->terminations[0]->code, "status_id" => ""]])?>" target="_blank"><span class="status building-connected" title="Tiefbau erledigt">B-C</span></a>
                    <?php else: ?>
                      <a href="<?=self::getUrl("Pipework", "Index", ["filter" => ["code" => $order->terminations[0]->building->code, "status_id" => ""]])?>" target="_blank"><span class="status not-connected" title="Tiefbau ausständig">BNC</span></a>
                    <?php endif; ?>
                  <?php endif; ?>
                  
                  <?php 
                  
                  $voip = false;
                  $hw = false;
                  $cpe_config_finished = false;
                  $patched = false;
                  
                  if(count($order->products)) {
                    foreach($order->products as $product) {
                      if(is_array($product->product->attributes) && count($product->product->attributes)) {
                        if(array_key_exists("hw_only", $product->product->attributes)) {
                          if($product->product->attributes["hw_only"]->value) {
                            $hw = true;
                          }
                        }
                        if(array_key_exists("voip_chan", $product->product->attributes)) {
                          if($product->product->attributes["voip_chan"]->value) {
                            $voip = true;
                          }
                        }
                        if(array_key_exists("bras_type", $product->product->attributes)) {
                          if($product->cpeprovisioning->routerconfig_finished) {
                            $cpe_config_finished = true;
                          }
                        }
                        if($hw && $voip_chan && $patched && $cpe_config_finished) {
                          break;
                        }
                      }
                    }
                  }
                  if(is_array($order->terminations) && count($order->terminations)) {
                    if($order->terminations[0]->patching->patched) {
                      $patched = true;
                    }
                  }
                  
                  ?>
                  <?=($hw) ? "<i class='fas fa-shopping-bag text-purple ml-1' title='Zusatzprodukt vorhanden'></i>" : ""?>
                  <?=($voip) ? "<i class='fas fa-phone text-purple ml-1' title='Voice Produkt vorhanden'></i>" : ""?>
                  
                  <?=($patched) ? "<i class='fas fa-ethernet text-pink ml-1' title='Anschluss gepatched'></i>" :""?>
                  <?=($cpe_config_finished) ? "<i class='fas fa-shipping-fast text-pink ml-1' title='CPE vorbereitet'></i>" :""?>
                </td>
                <td onclick="toggleOrder(<?=$order->id?>)">
                  <?=nl2br($order->owner->getCompanyOrName())?>
                  <?=($order->owner->spin) ? "<br /><span class='text-pink'>".$order->owner->spin."</span>" : ""?>
                  <?=($order->partner_number) ? "<br /><span class='text-pink'>".$order->partner_number."</span>" : ""?>
                </td>
                <td onclick="toggleOrder(<?=$order->id?>)">
                  <?=$order->owner->street?><br />
                  <?=$order->owner->zip?> <?=$order->owner->city?>
                </td>
                <td onclick="toggleOrder(<?=$order->id?>)">
                  <?php if(is_array($order->terminations) && count($order->terminations)): ?>
                    <?=$order->terminations[0]->building->street?><?=($order->terminations[0]->name) ? " ".$order->terminations[0]->name : ""?><br />
                    <?=$order->terminations[0]->building->zip?> <?=$order->terminations[0]->building->city?>
                  <?php endif; ?>
                </td>
                <td onclick="toggleOrder(<?=$order->id?>)" class="text-monospace"><?=date("d.m.Y", $order->order_date)?></td>
                <td onclick="toggleOrder(<?=$order->id?>)" class="text-monospace"><?=date("d.m.Y", $order->edit)?></td>
                <td onclick="toggleOrder(<?=$order->id?>)" class="text-monospace"><?=$order->editor->name?></td>
                <td style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
                  <a href="<?=self::getUrl("Order", "downloadServicePin", ["id" => $order->id])?>"><i class="fas fa-file-alt"  title="Service PIN als PDF herunterladen"></i></a>
                  <a href="<?=self::getUrl("Order", "sendServicePin", ["id" => $order->id])?>" onclick="if(!confirm('Soll der Service-PIN an den Vertragsinhaber gesendet werden?')) return false;"><i class="fas fa-paper-plane"  title="Service PIN als PDF per Email an Vertragsinhaber"></i></a>
                  <a href="<?=self::getUrl("Order", "edit", ["id" => $order->id])?>"><i class="far fa-edit"  title="Bearbeiten"></i></a>
                  <a href="<?=self::getUrl("Order", "delete", ["id" => $order->id])?>" onclick="if(!confirm('Bestellung wirklich löschen?')) return false;" class="text-danger" title="Löschen"><i class="fas fa-trash"></i></a>
                </td>
              </tr>
              <tr class="building-list-tr <?=($order_count % 2 == 0) ? "table-bg-even" : ""?>" id="order-dates-<?=$order->id?>">
                <td onclick="toggleOrder(<?=$order->id?>)" colspan="2" class="pt-0" style="border-top: none;"></td>
                <td onclick="toggleOrder(<?=$order->id?>)" colspan="7" class="pt-0" style="border-top: none;">
                  <div class="row">
                    <div class="mr-4 order-date-pill <?=($order->terminations[0]->workflowitems["bautermin"]->value->value_string) ? "active" : ""?>">Tiefbau: <span class="text-monospace"><?=($order->terminations[0]->workflowitems["bautermin"]->value->value_string) ? "<span class='text-success'>".$order->terminations[0]->workflowitems["bautermin"]->value->value_string."</span>" : "--.--.----"?></span></div>
                    <div class="mr-4 order-date-pill <?=($order->terminations[0]->workflowitems["backbone_setup_date"]->value->value_string) ? "active" : ""?>">Leitungsbau Backbone: <span class="text-monospace"><?=($order->terminations[0]->workflowitems["backbone_setup_date"]->value->value_string) ? "<span class='text-success'>".$order->terminations[0]->workflowitems["backbone_setup_date"]->value->value_string."</span>" : "--.--.----"?></span></div>
                    <div class="mr-4 order-date-pill <?=($order->terminations[0]->workflowitems["customer_setup_date"]->value->value_string) ? "active" : ""?>">Leitungsbau Kunde: <span class="text-monospace"><?=($order->terminations[0]->workflowitems["customer_setup_date"]->value->value_string) ? "<span class='text-success'>".$order->terminations[0]->workflowitems["customer_setup_date"]->value->value_string."</span>" : "--.--.----"?></span></div>
                    <div class="mr-4 order-date-pill <?=($order->shippingdate) ? "active" : ""?>">Versandtermin: <span class="text-monospace"><?=($order->shippingdate) ? "<span class='text-success'>".date("d.m.Y", $order->shippingdate)."</span>" : "--.--.----"?></span></div>
                    <div id="date-pill-<?=$order->id?>-install_date" class="order-date-pill <?=($order->install_date) ? "active" : ""?>" id="order-install-date-<?=$order->id?>">Vorortinstallation: <span class="text-monospace"><?=($order->install_date) ? "<span class='text-success'>".date("d.m.Y",$order->install_date)."</span>" : "--.--.----"?></span></div>
                  </div>
                </td>
              </tr>
              <tr id="order-detail-<?=$order->id?>" class="hidden top-no-border" style="background-color:#cddef7">
                <td colspan="9">
                  <div class="card">
                    <div class="card-body">
                      <div class="row justify-content-center mt-2">
                        <div class="col-xl-8 col-lg-12">
                      
                          <ul class="nav nav-tabs">
                            <li class="nav-item"><a class="nav-link active" href="#order-detail-<?=$order->id?>-journal" data-toggle="tab" aria-expanded="false">Journal</a></li>
                            <li class="nav-item"><a class="nav-link" href="#order-detail-<?=$order->id?>-contact" data-toggle="tab" aria-expanded="false">Kontakt</a></li>
                            <li class="nav-item"><a class="nav-link" href="#order-detail-<?=$order->id?>-details" data-toggle="tab" aria-expanded="false">Details</a></li>
                            <li class="nav-item"><a class="nav-link" href="#order-detail-<?=$order->id?>-files" data-toggle="tab" aria-expanded="false">Dokumente</a></li>
                          </ul>
                        </div>
                      
                      </div>

                      <div class="tab-content">
                        <div id="order-detail-<?=$order->id?>-journal" class="tab-pane show active">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">

                              <div class="card">

                                <div class="card-body">
                                  <div class="card-header bg-info text-white pl-2 pr-2 pt-1 pb-1">Bestelljournal</div>
                                  <div class="card mb-3">
                                    <div class="card-header bg-light"><button type="button" class="btn btn-sm btn-info" onclick="$('#new-journal-<?=$order->id?>').toggle()"><i class="fas fa-plus"></i> Neuer Eintrag</button></div>
                                    <div id="new-journal-<?=$order->id?>" class="card-body hidden">
                                      <form method="post" action="<?=self::getUrl("OrderJournal", "save")?>">
                                        <input type="hidden" name="order_id" value="<?=$order->id?>" />
                                        <input type="hidden" name="s" value="<?=$pagination['start']?>" />
                                        <?php if(is_array($filter) && count($filter)): foreach($filter as $n => $v): ?>
                                          <input type="hidden" name="filter[<?=$n?>]" value="<?=$v?>" />
                                        <?php endforeach; endif; ?>
                                        <textarea name="text" class="form-control mb-2" style="height:120px;"></textarea>
                                        <button class="btn btn-sm btn-primary" type="submit"><i class="fas fa-save"></i> Speichern</button>
                                      </form>
                                    </div>
                                  </div>

                                  <?php if(is_array($order->journals) && count($order->journals)): ?>
                                    <?php foreach($order->journals as $journal): ?>
                                      <div class="card mb-1" style="border: 1px solid #ddd">
                                        <div class="card-header bg-light text-monospace text-muted pl-2 pr-2 pt-1 pb-1"><span class="font-weight-bold" style="float: right"><i class="fas fa-clock"></i> <?=date("d.m.Y H:i:s",$journal->create)?></span> Eingetragen von <strong><?=$journal->creator->name?></strong></div>
                                        <div class="card-body pl-2 pr-2 pt-1 pb-1">
                                          <?=nl2br($journal->text)?>
                                        </div>
                                      </div>
                                    <?php endforeach; ?>
                                  <?php endif; ?>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        
                        
                        <div id="order-detail-<?=$order->id?>-contact" class="tab-pane">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">

                              <div class="card">

                                <div class="card-body">
                                  <div class="card-header bg-info text-white pl-2 pr-2 pt-1 pb-1">Kontaktdaten</div>
                                  
                                  <div class="row">
                                    <div class="col-lg-6">
                                      <h4>Vertragsinhaber</h4>
                                      <table class="table table-sm table-striped table-bordered">
                                        <tr>
                                          <th>Firma</th>
                                          <td><?=$order->owner->company?></td>
                                        </tr><tr>
                                          <th>Vorname</th>
                                          <td><?=$order->owner->firstname?></td>
                                        </tr><tr>
                                          <th>Nachname</th>
                                          <td><?=$order->owner->lastname?></td>
                                        </tr><tr>
                                          <th>Straße</th>
                                          <td><?=$order->owner->street?></td>
                                        </tr><tr>
                                          <th>PLZ</th>
                                          <td><?=$order->owner->zip?></td>
                                        </tr><tr>
                                          <th>Ort</th>
                                          <td><?=$order->owner->city?></td>
                                        </tr><tr>
                                          <th>Land</th>
                                          <td><?=$order->owner->country?></td>
                                        </tr><tr>
                                          <th>Telefon</th>
                                          <td class="text-monospace"><?=($order->owner->phone) ? "<a href='tel:".$order->owner->phone."'>".$order->owner->phone."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Mobiltelefon</th>
                                          <td class="text-monospace"><?=($order->owner->mobile) ? "<a href='tel:".$order->owner->mobile."'>".$order->owner->mobile."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Fax</th>
                                          <td class="text-monospace"><?=($order->owner->fax) ? "<a href='tel:".$order->owner->fax."'>".$order->owner->fax."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Email</th>
                                          <td class="text-monospace"><?=($order->owner->email) ? "<a href='mailto:".$order->owner->email."'>".$order->owner->email."</a>" : ""?></td>
                                        </tr>
                                      </table>
                                    </div>
                                    
                                    <?php if($order->billingaddress_id): ?>
                                    <div class="col-lg-6">
                                      <h4>Rechungsempfänger</h4>
                                      <table class="table table-sm table-striped table-bordered">
                                        <tr>
                                          <th>Firma</th>
                                          <td><?=$order->billingaddress->company?></td>
                                        </tr><tr>
                                          <th>Vorname</th>
                                          <td><?=$order->billingaddress->firstname?></td>
                                        </tr><tr>
                                          <th>Nachname</th>
                                          <td><?=$order->billingaddress->lastname?></td>
                                        </tr><tr>
                                          <th>Straße</th>
                                          <td><?=$order->billingaddress->street?></td>
                                        </tr><tr>
                                          <th>PLZ</th>
                                          <td><?=$order->billingaddress->zip?></td>
                                        </tr><tr>
                                          <th>Ort</th>
                                          <td><?=$order->billingaddress->city?></td>
                                        </tr><tr>
                                          <th>Land</th>
                                          <td><?=$order->billingaddress->country?></td>
                                        </tr><tr>
                                          <th>Telefon</th>
                                          <td class="text-monospace"><?=($order->billingaddress->phone) ? "<a href='tel:".$order->billingaddress->phone."'>".$order->billingaddress->phone."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Mobiltelefon</th>
                                          <td class="text-monospace"><?=($order->billingaddress->mobile) ? "<a href='tel:".$order->billingaddress->mobile."'>".$order->billingaddress->mobile."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Fax</th>
                                          <td class="text-monospace"><?=($order->billingaddress->fax) ? "<a href='tel:".$order->billingaddress->fax."'>".$order->billingaddress->fax."</a>" : ""?></td>
                                        </tr><tr>
                                          <th>Email</th>
                                          <td class="text-monospace"><?=($order->billingaddress->email) ? "<a href='mailto:".$order->billingaddress->email."'>".$order->billingaddress->email."</a>" : ""?></td>
                                        </tr>
                                      </table>
                                    </div>
                                    <?php endif; ?>
                                  </div>
                                  
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        

                        <div id="order-detail-<?=$order->id?>-details" class="tab-pane">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">
                              <div class="card">
                                <div class="card-body">
                              
                                      <table class="table table-sm table-bordered">
                                        <tr>
                                          <th class="bg-light">Bestelldatum:</th>
                                          <td><?=date("d.m.Y",$order->order_date)?></td>
                                          
                                          <th class="bg-light">Herstellbar ab:</th>
                                          <td><?=($order->finish_after) ? date("d.m.Y",$order->finish_after) : "--.--.----"?></td>
                                          
                                          <th class="bg-light">Rechnungsart:</th>
                                          <td><?=($order->billing_type == "sepa") ? "SEPA" : "Rechnung"?></td>
                                          
                                          <th class="bg-light">Einwilligungen:</th>
                                          <td><?=($order->allow_contact) ? "<span class='order-date-pill active'><i class='fas fa-check text-success'></i> Info</span>" : ""?> <?=($order->allow_spin) ? "<span class='order-date-pill active ml-1'><i class='fas fa-check text-success'></i> SPIN</span>" : ""?></td>
                                        </tr>
                                      </table>

                                  
                                  <div class="row">
                                    <div class="form-group col-lg-4 col-md-6">
                                      <label for="vororttermin-<?=$order->id?>">Vorortinstallation:</label>
                                      <div class="input-group">
                                        <input type="text" class="form-control datepicker" id="vororttermin-<?=$order->id?>" name="vororttermin" value="<?=($order->install_date) ? date("d.m.Y",$order->install_date) : ""?>" placeholder="--.--.----" />
                                        <div class="input-group-append">
                                          <button class="btn btn-outline-primary" id="vororttermin-button-<?=$order->id?>" type="button" title="Vororttermin speichern" onclick="saveVororttermin(<?=$order->id?>)"><i class="fas fa-save"></i></button>
                                          <button class="btn btn-outline-danger ml-1" id="vororttermin-delete-<?=$order->id?>" type="button" title="Vororttermin speichern" onclick="saveVororttermin(<?=$order->id?>, true)"><i class="fas fa-trash"></i></button>
                                        </div>
                                      </div>
                                      <small id="vororttermin-<?=$order->id?>-error" class="text-danger hidden">Fehler beim Speichern!</small>
                                    </div>
                                    
                                    <div class="form-group col-lg-4 col-md-6 text-center">
                                      <label for="cpeprovisioning_enabled-<?=$order->id?>">CPE Konfig freigegeben:</label>
                                      <div class="input-group">
                                        <input type="checkbox" class="form-control" id="cpeprovisioning_enabled-<?=$order->id?>" name="cpeprovisioning_enabled" value="1" onchange="saveCpeprovisioning(<?=$order->id?>)" <?=($order->cpeprovisioning_enabled) ? "checked='checked'" : ""?> />
                                      </div>
                                      <small id="cpeprovisioning_enabled-<?=$order->id?>-success" class="text-success hidden">Gespeichert!</small>
                                      <small id="cpeprovisioning_enabled-<?=$order->id?>-error" class="text-danger hidden">Fehler beim Speichern!</small>
                                    </div>
                                    
                                  </div>

                                  <?php if(is_array($order->products) && count($order->products)): ?>
                                  <h4>Produkte</h4>
                                  <table class="table table-sm table-striped table-bordered">
                                    <tr>
                                      <th class="text-right">Pos</th>
                                      <th class="text-right">Anzahl</th>
                                      <th>Produkt</th>
                                      <th>Rechn.Periode</th>
                                      <th class="text-right">Preis p. P</th>
                                      <th class="text-right">Preis Setup</th>
                                      <th>Rechn.verz.</th>
                                    </tr>
                                    <?php foreach($order->products as $product): ?>
                                      <tr>
                                        <td class="text-right"><?=$product->pos?></td>
                                        <td class="text-right"><?=$product->formatAmount()?></td>
                                        <td>
                                          <?=$product->product->name?>
                                          <?php
                                          if(
                                            (is_array($product->product->attributes) && count($product->product->attributes))
                                            && (array_key_exists(TT_ATTRIB_TERMINATION_REQUIRED_NAME, $product->product->attributes)
                                                && $product->product->attributes[TT_ATTRIB_TERMINATION_REQUIRED_NAME]->value == 1)
                                            || $product->termination_id
                                            ):
                                          ?>
                                          <br /><small class="text-pink"><?=$product->termination->code?> - <?= str_replace("\n", " - ", $product->termination->getAddress())?></small>
                                          <?php endif; ?>
                                        </td>
                                        <td><?=($product->billing_period == 12) ? "Jährlich" : "Monatlich"?></td>
                                        <td class="text-monospace text-right"><?=number_format($product->price, 4, ",", ".")?></td>
                                        <td class="text-monospace text-right"><?=number_format($product->price_setup, 4, ",", ".")?></td>
                                        <td><?=($product->billing_delay) ? $product->billing_delay : ""?></td>
                                      </tr>
                                    <?php endforeach; ?>
                                  </table>
                                  <?php endif; ?>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        
                        <div id="order-detail-<?=$order->id?>-files" class="tab-pane">
                          <div class="row justify-content-center">
                            <div class="col-xl-8 col-lg-12">
                              <div class="card">
                                <div class="card-body">
                                  <?php if(is_array($order->files) && count($order->files)): ?>
                                    <table class="table table-sm table-striped table-hover">
                                      <tr>
                                        <th>Name</th>
                                        <th>Beschreibung</th>
                                        <th>Dateiname</th>
                                        <th>Hochgeladen</th>
                                        <th>Zuletzt bearbeitet</th>
                                      </tr>
                                      <?php foreach($order->files as $file): ?>
                                      <tr>
                                        <td><a href="<?=self::getUrl("File", "Download", ['id' => $file->file->id])?>"><?=$file->name?></a></td>
                                        <td><?=nl2br($file->description)?></td>
                                        <td><?=$file->file->filename?></td>
                                        <td class="text-monospace"><?=date("d.m.Y", $file->create)?> (<?=$file->creator->name?>)</td>
                                        <td class="text-monospace"><?=date("d.m.Y", $file->edit)?> (<?=$file->editor->name?>)</td>
                                      </tr>

                                      <?php endforeach; ?>
                                    </table>
                                  <?php else: ?>
                                    <p>Keine Dokumente hinterlegt.</p>
                                  <?php endif; ?>
                                  
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        
                      </div>
                    </div>
                  </div>
                </td>
              </tr>
              <tr style="display:none;">
                <td colspan="9"></td>
              </tr>
              <?php $order_count++ ?>
              <?php endforeach; ?>
            </table>
            
            <?php if($pagination['maxItems'] > $pagination['count']): ?>
              <nav>
                <ul class="pagination justify-content-center mb-1">
                  <li class="page-item <?=($pagination['start'] - $pagination['count'] < 0) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod, "Index", ["filter" => $filter])?>" tabindex="-1">Erste</a>
                  </li>
                  <li class="page-item <?=($pagination['start'] - $pagination['count'] < 0) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => $pagination['start'] - $pagination['count'], "filter" => $filter])?>" tabindex="-1">Zurück</a>
                  </li>
                  <?php for($i = 0; $i < ($pagination['maxItems'] / $pagination['count']); $i++): ?>
                    <li class="page-item <?=($pagination['start'] == $i*$pagination['count']) ? "disabled font-weight-bold" : ""?>"><a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => $i*$pagination['count'], "filter" => $filter])?>"><?=$i+1?></a></li>
                  <?php endfor; ?>
                  <li class="page-item <?=($pagination['maxItems'] / ($pagination['start'] + $pagination['count'])  < 1) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => $pagination['start'] + $pagination['count'], "filter" => $filter])?>">Vor</a>
                  </li>
                  <li class="page-item <?=($pagination['maxItems'] / ($pagination['start'] + $pagination['count'])  < 1) ? "disabled" : ""?>">
                    <a class="page-link" href="<?=$this->getUrl($Mod,"Index",['s' => ($pagination['maxItems'] - $pagination['maxItems'] % $pagination['count']), "filter" => $filter])?>">Letzte</a>
                  </li>
                </ul>
              </nav>
            <?php endif; ?>
            <div class="pagination justify-content-center">
              <?php if($pagination['maxItems']): ?>
                Bestellungen <?=$pagination['start']+1?> bis <?=($pagination['start'] + $pagination['count'] > $pagination['maxItems']) ? $pagination['maxItems'] : $pagination['start'] + $pagination['count']?> von <?=$pagination['maxItems']?>
              <?php else: ?>
                Keine Bestellungen gefunden
              <?php endif; ?>
            </div>
          </div>
        </div>
        
      </div>
    </div>

    <script type="text/javascript">
      $(document).ready(function() {
        $('.datepicker').each(function() {
          $(this).datepicker({
              language: 'de',
              format: "dd.mm.yyyy",
              showWeekDays: true,
              todayBtn: 'linked',
              autoclose: true
          });
        });
        
        sortSelect(document.getElementById("filter_network_id"));
        
        var order;
        var hash = window.location.hash.substr(1);
        var match = hash.match(/order=(\d+)/);
        if(match && match[1]) {
          order = match[1];
          toggleOrder(order);
          //$('body').scrollTop($('#order-' + order).offset() - 50);
          scrollToDiv('#order-' + order);
        }
      });
      
      
      function toggleOrder(id) {
        $('#order-detail-' + id).toggle();
        if($('#order-detail-' + id).is(":hidden")) {
          $('#order-' + id).removeClass("table-info");
          $('#order-dates-' + id).removeClass("table-info");
        } else {
          $('#order-' + id).addClass("table-info");
          $('#order-dates-' + id).addClass("table-info");
        }
      }
      
      
      function scrollToDiv(element){
        element = element.replace("link", "");
        $('html,body').unbind().animate({scrollTop: $(element).offset().top-180},'slow');
      };
      
      <?php if($highlight): ?>
        toggleOrder(<?=$highlight?>);
        scrollToDiv("#order-<?=$highlight?>");
        <?php if($addJournal): ?>
          $('#new-journal-<?=$highlight?>').toggle();
          $('#new-journal-<?=$highlight?> textarea[name=text]').focus();
        <?php endif; ?>
      <?php endif; ?>
      
      
      function saveVororttermin(id, removeDate = false) {
        date = $('#vororttermin-' + id).val();
        if(removeDate) {
          date = "";
        }
        /*if(!date) {
          $('#vororttermin-button-' + id).addClass("btn-danger");
          $('#vororttermin-button-' + id).removeClass("btn-outline-success");
          $('#vororttermin-button-' + id).removeClass("btn-outline-primary");
          $('#vororttermin-' + id).addClass("is-invalid");
          return false;
        }*/
        
        $.post('<?=self::getUrl("Order", "saveVororttermin")?>',
        {
          'id': id,
          install_date: date
        },
        function(data) {
          console.log(data);
          id = data.order.id;
          if(data.status == "OK") {
            $('#vororttermin-button-' + id).addClass("btn-outline-success");
            $('#vororttermin-button-' + id).removeClass("btn-danger");
            $('#vororttermin-button-' + id).removeClass("btn-outline-primary");
            $('#vororttermin-' + id).addClass("is-valid");
            $('#vororttermin-' + id).removeClass("is-invalid");
            
            if(data.order.date) {
              $('#date-pill-' + id + '-install_date').html('Vorortinstallation: <span class="text-monospace"><span class="text-success">' + data.order.date + '</span></span>');
              $('#date-pill-' + id + '-install_date').addClass("active");
              $('#cpeprovisioning_enabled-' + id).prop("checked",true);
            } else {
              $('#date-pill-' + id + '-install_date').removeClass("active");
              $('#date-pill-' + id + '-install_date').html('Vorortinstallation: <span class="text-monospace"><span class="text-success">--.--.----</span></span>');
              $('#vororttermin-' + id).val("");
              $('#cpeprovisioning_enabled-' + id).prop("checked",false);
            }
            
            setTimeout(function() {
              $('#vororttermin-' + id).removeClass("is-valid");
              $('#vororttermin-button-' + id).removeClass("btn-outline-success");
              $('#vororttermin-button-' + id).addClass("btn-outline-primary");
            }, 3000, id);
          } else {
            $('#vororttermin-button-' + id).addClass("btn-danger");
            $('#vororttermin-button-' + id).removeClass("btn-outline-success");
            $('#vororttermin-button-' + id).removeClass("btn-outline-primary");
            $('#vororttermin-' + id).addClass("is-invalid");
            $('#vororttermin-' + id).removeClass("is-valid");
            
            $('#vororttermin-' + id + '-error').text(data.msg);
            $('#vororttermin-' + id + '-error').show();
          }
        },
        "json"
        );
      }
      
      function saveCpeprovisioning(id) {
        var enabled = 0;
        
        $('#cpeprovisioning_enabled-' + id + '-error').hide();
        $('#cpeprovisioning_enabled-' + id + '-success').hide();
        
        if($('#cpeprovisioning_enabled-' + id).is(":checked")) {
          enabled = 1;
        }
        
        $.post('<?=self::getUrl("Order", "saveCpeprovisioning")?>',
        {
          'id': id,
          'enabled': enabled
        },
        function(data) {
          console.log(data);
          var id = data.order.id;
          var enabled = data.order.enabled;
          
          if(data.status == "OK") {
            $('#cpeprovisioning_enabled-' + id + '-error').hide();
            $('#cpeprovisioning_enabled-' + id + '-success').show();
            setTimeout(function() {
              $('#cpeprovisioning_enabled-' + id + '-success').hide(1000);
            }, 3000, id);
          } else {
            $('#cpeprovisioning_enabled-' + id + '-success').hide();
            $('#cpeprovisioning_enabled-' + id + '-error').text(data.msg);
            $('#cpeprovisioning_enabled-' + id + '-error').show();
            if(enabled) {
              $('#cpeprovisioning_enabled-' + id).prop("checked",true);
            } else {
              $('#cpeprovisioning_enabled-' + id).prop("checked",false);
            }
          }
        },
        "json"
        );
      
        
      }
      
      
      function sortSelect(selElem) {
        var tmpAry = new Array();
        for (var i=0;i<selElem.options.length;i++) {
          tmpAry[i] = new Array();
          tmpAry[i][0] = selElem.options[i].text;
          tmpAry[i][1] = selElem.options[i].value;
        }
        tmpAry.sort();
        while (selElem.options.length > 0) {
          selElem.options[0] = null;
        }
        for (var i=0;i<tmpAry.length;i++) {
          var op = new Option(tmpAry[i][0], tmpAry[i][1]);
          selElem.options[i] = op;
        }
        return;
      }
    </script>
    
<?php include(realpath(dirname(__FILE__)."/../")."/footer.php"); ?>
