<?php
  $includes_external_product = false;
?>
<?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"><a href="<?=self::getUrl("Order")?>">Bestellungen</a></li>
              <li class="breadcrumb-item active"><?=($order->id) ? "bearbeiten" : "Neu" ?></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">
            <h4 class="mb-2"><?=($order->id) ? "Bestellung bearbeiten" : "Neue Bestellung"?></h4>
            
            <form class="form-horizontal" method="post" action="<?=self::getUrl("Order", "save")?>" name="orderForm" id="orderForm" enctype="multipart/form-data">
              <div class="card">
                <div class="card-body">
                  
                  <input type="hidden" name="id" value="<?=$order->id?>" />
                  
                  
                  
                  <div class="row">
                    <div class="col-lg-6">
                      
                    
                      <div class="form-group row">
                        
                        
                        
                        <div class="col-lg-12 mb-2">
                          <h4>Vertragsinhaber</h4>
                          <select class="form-control select2" name="owner_id" id="owner_id">
                            <option></option>
                            <option value="new" <?=($order->owner_id == "new") ? "selected='selected'" : ""?>>Neu...</option>
                            <?php foreach($addresses as $address): ?>
                              <option value="<?=$address->id?>" <?=($address->id == $order->owner_id) ? "selected='selected'" : ""?>><?=$address->getCompanyOrName()?> (<?=$address->zip?> <?=$address->city?>, <?=$address->street?>)</option>
                            <?php endforeach; ?>
                          </select>
                        </div>
                        
                        <div class="col-lg-12 hidden" id="owner-form">
                          <div class="row">
                            <div class="col-lg-12">
                              <div class="form-group">
                                <label class="form-label" for="owner_company">Firmenname</label>
                                <textarea class="form-control" name="owner_company" id="owner_company"><?=$order->owner_company?></textarea>
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="owner_firstname">Vorname</label>
                                <input type="text" class="form-control" name="owner_firstname" id="owner_firstname" value="<?=$order->owner_firstname?>" />
                              </div>
                            </div>
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="owner_lastname">Nachname</label>
                                <input type="text" class="form-control" name="owner_lastname" id="owner_lastname" value="<?=$order->owner_lastname?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-12">
                              <div class="form-group">
                                <label class="form-label" for="owner_street">Straße *</label>
                                <input type="text" class="form-control" name="owner_street" id="owner_street" value="<?=$order->owner_street?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-3">
                              <div class="form-group">
                                <label class="form-label" for="owner_zip">Postleitzahl *</label>
                                <input type="text" class="form-control" name="owner_zip" id="owner_zip" value="<?=$order->owner_zip?>" />
                              </div>
                            </div>
                            <div class="col-lg-9">
                              <div class="form-group">
                                <label class="form-label" for="owner_city">Ort *</label>
                                <input type="text" class="form-control" name="owner_city" id="owner_city" value="<?=$order->owner_city?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-12">
                              <div class="form-group">
                                <label class="form-label" for="owner_country">Land</label>
                                <input type="text" class="form-control" name="owner_country" id="owner_country" value="<?=$order->owner_country?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="owner_phone">Telefon *</label>
                                <input type="text" class="form-control" name="owner_phone" id="owner_phone" value="<?=$order->owner_phone?>" />
                              </div>
                            </div>
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="owner_mobile">Mobil</label>
                                <input type="text" class="form-control" name="owner_mobile" id="owner_mobile" value="<?=$order->owner_mobile?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="owner_email">Emailadresse *</label>
                                <input type="text" class="form-control" name="owner_email" id="owner_email" value="<?=$order->owner_email?>" />
                              </div>
                            </div>
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="owner_fax">Fax</label>
                                <input type="text" class="form-control" name="owner_fax" id="owner_fax" value="<?=$order->owner_fax?>" />
                              </div>
                            </div>
                          </div>

                        </div>
                      </div>
                      
                    </div>
                    <div class="col-lg-6">
                  
                      <div class="form-group row">
                        
                        <div class="col-lg-12 mb-2">
                          <h4>Rechungsadresse</h4>
                          <select class="form-control select2" name="billingaddress_id" id="billingaddress_id">
                            <option></option>
                            <option value="new" <?=($order->billingaddress_id == "new") ? "selected='selected'" : ""?>>Neu...</option>
                            <?php foreach($addresses as $address): ?>
                              <option value="<?=$address->id?>" <?=($address->id == $order->billingaddress_id) ? "selected='selected'" : ""?>><?=$address->getCompanyOrName()?> (<?=$address->zip?> <?=$address->city?>, <?=$address->street?>)</option>
                            <?php endforeach; ?>
                          </select>
                        </div>
                        
                        <div class="col-lg-12 hidden" id="billingaddress-form">
                          <div class="row">
                            <div class="col-lg-12">
                              <div class="form-group">
                                <label class="form-label" for="owner_company">Firmenname</label>
                                <textarea class="form-control" name="billing_company" id="billing_company"><?=$order->billing_company?></textarea>
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="billing_firstname">Vorname</label>
                                <input type="text" class="form-control" name="billing_firstname" id="billing_firstname" value="<?=$order->billing_firstname?>" />
                              </div>
                            </div>
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="billing_lastname">Nachname</label>
                                <input type="text" class="form-control" name="billing_lastname" id="billing_lastname" value="<?=$order->billing_lastname?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-12">
                              <div class="form-group">
                                <label class="form-label" for="billing_street">Straße *</label>
                                <input type="text" class="form-control" name="billing_street" id="billing_street" value="<?=$order->billing_street?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-3">
                              <div class="form-group">
                                <label class="form-label" for="billing_zip">Postleitzahl *</label>
                                <input type="text" class="form-control" name="billing_zip" id="billing_zip" value="<?=$order->billing_zip?>" />
                              </div>
                            </div>
                            <div class="col-lg-9">
                              <div class="form-group">
                                <label class="form-label" for="billing_city">Ort *</label>
                                <input type="text" class="form-control" name="billing_city" id="billing_city" value="<?=$order->billing_city?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-12">
                              <div class="form-group">
                                <label class="form-label" for="billing_country">Land</label>
                                <input type="text" class="form-control" name="billing_country" id="billing_country" value="<?=$order->billing_country?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="billing_phone">Telefon *</label>
                                <input type="text" class="form-control" name="billing_phone" id="billing_phone" value="<?=$order->billing_phone?>" />
                              </div>
                            </div>
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="billing_mobile">Mobil</label>
                                <input type="text" class="form-control" name="billing_mobile" id="billing_mobile" value="<?=$order->billing_mobile?>" />
                              </div>
                            </div>
                          </div>

                          <div class="row">
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="billing_email">Emailadresse *</label>
                                <input type="text" class="form-control" name="billing_email" id="billing_email" value="<?=$order->billing_email?>" />
                              </div>
                            </div>
                            <div class="col-lg-6">
                              <div class="form-group">
                                <label class="form-label" for="billing_fax">Fax</label>
                                <input type="text" class="form-control" name="billing_fax" id="billing_fax" value="<?=$order->billing_fax?>" />
                              </div>
                            </div>
                          </div>

                        </div>
                      </div>
                    </div> 
                  </div>
                  
                  <hr />
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="order_date">Bestelldatum</label>
                    <div class="col-lg-4">
                      <input type="text" class="form-control" name="order_date" id="order_date" value="<?=($order->order_date) ? date("d.m.Y", $order->order_date) : date("d.m.Y")?>" />
                    </div>
                  </div>
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="partner_number">Partnernummer</label>
                    <div class="col-lg-4">
                      <input type="text" class="form-control" name="partner_number" id="partner_number" value="<?=$order->partner_number?>" />
                    </div>
                  </div>
                  
                  <h5 class="mt-3">Verzögerte Herstellung:</h5>
                  
                      <div class="form-group row">
                        <label class="col-lg-2 col-form-label" for="finish_after">Herstellbar ab</label>
                        <div class="col-lg-4">
                          <input type="text" class="form-control" name="finish_after" id="finish_after" value="<?=($order->finish_after) ? date("d.m.Y", $order->finish_after) : ""?>" />
                        </div>
                      </div>
                      
                      <div class="form-group row">
                        <label class="col-lg-2 col-form-label" for="finish_after_comment">Verzögerungsgrund</label>
                        <div class="col-lg-4">
                          <textarea class="form-control" name="finish_after_comment" id="finish_after_comment" placeholder="Verzögerungsgrund"><?=$order->finish_after_comment?></textarea>
                        </div>
                      </div>
                      
                  
                  
                </div>
              </div>
              
              
              <h4>Verrechnung</h4>
              <div class="card">
                <div class="card-body">
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="billing_type">Verrechnungsart</label>
                    <div class="col-lg-10">
                      <select class="form-control" name="billing_type" id="billing_type">
                        <option value="invoice" <?=($order->billing_type == "invoice") ? "selected='selected'" : ""?>>Rechnung</option>
                        <option value="sepa" <?=($order->billing_type == "sepa") ? "selected='selected'" : ""?>>SEPA Bankeinzug</option>
                      </select>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="bank_account_bank">Kreditinstitut</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="bank_account_bank" id="bank_account_bank" value="<?=$order->bank_account_bank?>" />
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="bank_account_owner">Kontoinhaber</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="bank_account_owner" id="bank_account_owner" value="<?=$order->bank_account_owner?>" />
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="bank_account_iban">IBAN</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="bank_account_iban" id="bank_account_iban" value="<?=$order->bank_account_iban?>" />
                      <small id="iban_error" class="hidden">IBAN ungültig!</small>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="bank_account_bic">BIC</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="bank_account_bic" id="bank_account_bic" value="<?=$order->bank_account_bic?>" />
                    </div>
                  </div>
                </div>
              </div>
              
              <h4>Einwilligungen</h4>
              <div class="card">
                <div class="card-body">
                  <div class="form-group row">
                    <div class="col-lg-2"></div>
                    <div class="col-lg-10">
                      <label class="form-check-label"><input type="checkbox" class="form-check-input" name="allow_contact" id="allow_contact" <?=($order->allow_contact) ? "checked='checked'" : ""?> /> Informationen per Post/Email/Telefon</label>
                    </div>
                  </div>
                  <div class="form-group row">
                    <div class="col-lg-2"></div>
                    <div class="col-lg-10">
                      <label class="form-check-label"><input type="checkbox" class="form-check-input" name="allow_spin" id="allow_spin" <?=($order->allow_spin) ? "checked='checked'" : ""?> /> Auskunft mit Service PIN</label>
                    </div>
                  </div>
                </div>
              </div>
              
              
              <div class="card">
                <div class="card-body">
                  <h4>Produkte</h4>
                  
                  <?php if(is_array($order->products) && count($order->products)): ?>
                  <?php foreach($order->products as $product): ?>
                  <?php
                    if($product->product->external == 1) {
                      $includes_external_product = true;
                    }
                  ?>
                  <div class="row product-container">
                    <div class="col-md-1 product-<?=$product->id?>">
                      <!-- pos number -->
                      <div class="form-group">
                        <label class="form-label" for="pos-<?=$product->id?>">Position</label>
                        <input type="number" class="form-control" name="products[<?=$product->id?>][pos]" id="pos-<?=$product->id?>" value="<?=$product->pos?>" placeholder="Position" />
                      </div>
                    </div>
                    
                    <div class="col-md-11">
                      <!-- product lines -->
                      <div class="row">
                        <!-- line 1 -->
                        <div class="col-4">
                          <label class="form-label" for="product_id-<?=$product->id?>">Produkt</label>
                          <select name="products[<?=$product->id?>][product_id]" id="product_id-<?=$product->id?>" class="form-control" onchange="updateProduct(<?=$product->id?>)">
                            <option></option>
                            <?php foreach($products as $p): ?>
                              <option value="<?=$p->id?>" <?=($p->id == $product->product_id) ? "selected='selected'" : ""?>><?=$p->name?></option>
                            <?php endforeach; ?>
                          </select>
                          
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="amount-<?=$product->id?>">Anzahl/Menge</label>
                          <input type="number" class="form-control" name="products[<?=$product->id?>][amount]" id="amount-<?=$product->id?>" value="<?=$product->formatAmount()?>" placeholder="Anzahl/Menge" />
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="billing_period-<?=$product->id?>">Verrechungsperiode</label>
                          <select class="form-control" name="products[<?=$product->id?>][billing_period]" id="billing_period-<?=$product->id?>" placeholder="Verrechnungsperiode">
                            <option value="1" <?=($product->billing_period == 1) ? "selected='selected'" : ""?>>Monatlich</option>
                            <option value="12" <?=($product->billing_period == 12) ? "selected='selected'" : ""?>>Jährlich</option>
                          </select>
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="price-<?=$product->id?>">Preis p. Periode</label>
                          <input type="text" class="form-control" name="products[<?=$product->id?>][price]" id="price-<?=$product->id?>" value="<?=$this::dotToComma($product->price)?>" placeholder="Preis p. Periode" />
                        </div>
                        <div class="col-2"> 
                          <label class="form-label" for="price_setup-<?=$product->id?>">Preis Setup</label>
                          <input type="text" class="form-control" name="products[<?=$product->id?>][price_setup]" id="price_setup-<?=$product->id?>" value="<?=$this::dotToComma($product->price_setup)?>" placeholder="Preis Setup" />
                        </div>
                        
                        
                      </div>
                      
                      <?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
                              ): ?>
                      <div class="row mt-1 mb-2" id="termination_id-<?=$product->id?>-line">
                      <?php else: ?>
                      <div class="row mt-1 mb-2 hidden" id="termination_id-<?=$product->id?>-line">
                      <?php endif; ?>
                        <!-- line to choose termination -->
                        <div class="col-12">
                          <label class="form-label" for="termination_id-<?=$product->id?>">Anschluss</label>
                          <select name="products[<?=$product->id?>][termination_id]" id="termination_id-<?=$product->id?>" class="form-control select2">
                            <option></option>
                            <?php foreach($terminations as $t): ?>
                              <option value="<?=$t->id?>" <?=($t->id == $product->termination_id) ? "selected='selected'" : ""?>><?=$t->code?> - <?=$t->getAddress()?></option>
                            <?php endforeach; ?>
                          </select>
                        </div>
                      </div>
                      
                      
                      
                      <div class="row mt-1 mb-2">
                        <!-- line 2 -->
                        <div class="col-8">
                          <label class="form-label" for="description-<?=$product->id?>" >Zusatztext</label>
                          <textarea class="form-control" name="products[<?=$product->id?>][description]" id="description-<?=$product->id?>" placeholder="Zusatztext"><?=$product->description?></textarea>
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="billing_delay-<?=$product->id?>" >Verzögerter Verrechnungsstart</label>
                          <input type="number" class="form-control" name="products[<?=$product->id?>][billing_delay]" id="billing_delay-<?=$product->id?>" value="<?=$product->billing_delay?>" placeholder="Freimonate" />
                          <small>Max. 6 Monate</small>
                        </div>
                        <?php if($me->is("Admin")): ?>
                        <div class="col-1"> 
                          <label class="form-label" for="price_nne-<?=$product->id?>">Preis NNE</label>
                          <input type="text" class="form-control" name="products[<?=$product->id?>][price_nne]" id="price_nne-<?=$product->id?>" value="<?=$this::dotToComma($product->price_nne)?>" placeholder="NNE" />
                        </div>
                        <div class="col-1"> 
                          <label class="form-label" for="price_nbe-<?=$product->id?>">Preis NBE</label>
                          <input type="text" class="form-control" name="products[<?=$product->id?>][price_nbe]" id="price_nne-<?=$product->id?>" value="<?=$this::dotToComma($product->price_nbe)?>" placeholder="NBE" />
                        </div>
                        <?php endif; ?>
                        
                      </div>
                    </div>
                  </div>
                  <?php endforeach; ?>
                  <?php endif; ?>
                  
                  <div class="row product-container">
                    
                    <div class="col-md-1 product-new">
                      <!-- pos number -->
                      <div class="form-group">
                        <label class="form-label" for="pos-new" >Position</label>
                        <input type="number" class="form-control" name="products[new][pos]" id="pos-new" value="" placeholder="Position" />
                      </div>
                    </div>
                    
                    <div class="col-md-11">
                      <!-- product lines -->
                      <div class="row">
                        <!-- line 1 -->
                        <div class="col-4">
                          <label class="form-label" for="product_id-new">Produkt</label>
                          <select name="products[new][product_id]" id="product_id-new" class="form-control" onchange="updateProduct('new')">
                            <option></option>
                            <?php foreach($products as $p): ?>
                              <option value="<?=$p->id?>" <?=($p->id == $product->id) ? "checked='checked'" : ""?>><?=$p->name?></option>
                            <?php endforeach; ?>
                          </select>
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="amount-new">Anzahl/Menge</label>
                            <input type="number" class="form-control" name="products[new][amount]" id="amount-new" value="" placeholder="Anzahl/Menge (Standard: 1)" />
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="billing_period-new" >Verrechungsperiode</label>
                          <select class="form-control" name="products[new][billing_period]" id="billing_period-new" placeholder="Verrechnungsperiode">
                            <option value="1">Monatlich</option>
                            <option value="12">Jährlich</option>
                          </select>
                        </div>
                        <div class="col-2">
                          <label class="form-label" for="price-new" >Preis p. Periode</label>
                          <input type="text" class="form-control" name="products[new][price]" id="price-new" value="" placeholder="Preis p. Periode" />
                        </div>
                        
                        <div class="col-2">
                          <label class="form-label" for="price_setup-new" >Preis Setup</label>
                          <input type="text" class="form-control" name="products[new][price_setup]" id="price_setup-new" value="" placeholder="Preis Setup" />
                        </div>
                        
                        
                        
                      </div>
                      
                      <div class="row mt-1 mb-2 hidden" id="termination_id-new-line">
                        <!-- line to choose termination -->
                        <div class="col-12">
                          <label class="form-label" for="termination_id-new">Anschluss</label>
                          <select name="products[new][termination_id]" id="termination_id-new" class="form-control">
                            <option></option>
                            <?php foreach($terminations as $t): ?>
                              <option value="<?=$t->id?>"><?=$t->code?> - <?=$t->getAddress()?></option>
                            <?php endforeach; ?>
                          </select>
                        </div>
                      </div>
                      
                      <div class="row mt-1 mb-2">
                        <!-- line 2 -->
                        <div class="col-8">
                          <label class="form-label" for="description-new" >Zusatztext</label>
                          <textarea class="form-control" name="products[new][description]" id="description-new" placeholder="Zusatztext"></textarea>
                        </div>
                        
                        <div class="col-2">
                          <label class="form-label" for="billing_delay-new" >Verzögerter Verrechnungsstart</label>
                          <input type="number" class="form-control" name="products[new][billing_delay]" id="billing_delay-new" value="" placeholder="Freimonate" />
                          <small>Max. 6 Monate</small>
                        </div>
                        
                        <?php if($me->is("Admin")): ?>
                        <div class="col-1"> 
                          <label class="form-label" for="price_nne-new">Preis NNE</label>
                          <input type="text" class="form-control" name="products[new][price_nne]" id="price_nne-new" value="" placeholder="NNE" />
                        </div>
                        <div class="col-1"> 
                          <label class="form-label" for="price_nbe-new">Preis NBE</label>
                          <input type="text" class="form-control" name="products[new][price_nbe]" id="price_nbe-new" value="" placeholder="NBE" />
                        </div>
                        <?php endif; ?>
                        
                      </div>
                    </div>
                    
                  </div>
                
                </div>
              </div>
              
              
              <h4>Dokumente</h4>
              
              
              <div class="card">
                <div class="card-body">
                  <?php if(!$order->id): ?>
                  <i class="text-pink">Verfügbar nach speichern.</i>
                  <?php else: ?>
                  
                    <?php if(is_array($order->files) && count($order->files)): ?>
                      <table class="table table-striped table-hover">
                        <tr>
                          <th>Name</th>
                          <th>Beschreibung</th>
                          <th>Dateiname</th>
                          <th>Original Dateiname</th>
                          <th>Hochgeladen</th>
                          <th>Zuletzt bearbeitet</th>
                          <th></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><?=$file->file->orig_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>
                          <td>
                            <a href="<?=self::getUrl("OrderFile", "edit", ["id" => $file->id])?>"><i class="far fa-edit"  title="Bearbeiten"></i></a>
                            <a href="<?=self::getUrl("OrderFile", "delete", ["id" => $file->id])?>" onclick="if(!confirm('Dokument wirklich löschen?')) return false;" class="text-danger" title="Löschen"><i class="fas fa-trash"></i></a>
                          </td>
                        </tr>

                        <?php endforeach; ?>
                      </table>
                    <?php endif; ?>
                  
                    <h4>Neues Dokument</h4>
                    <div class="form-group row">
                      <label class="col-lg-2 col-form-label" for="file_name">Name</label>
                      <div class="col-lg-10">
                        <input type="text" class="form-control" name="file_name" id="file_name" />
                      </div>
                    </div>

                    <div class="form-group row">
                      <label class="col-lg-2 col-form-label" for="file_filename">Download Dateiname</label>
                      <div class="col-lg-10">
                        <input type="text" class="form-control" name="file_filename" id="file_filename" />
                      </div>
                    </div>

                    <div class="form-group row">
                      <label class="col-lg-2 col-form-label" for="file_description">Beschreibung</label>
                      <div class="col-lg-10">
                        <textarea class="form-control" name="file_description"></textarea>
                      </div>
                    </div>

                    <div class="form-group row">
                      <label class="col-lg-2 col-form-label" for="note">Datei auswählen</label>
                      <div class="col-lg-10">
                        <input type="file" name="OrderFileUpload" class="form-control" />
                        <small>Erlaubte Dateiendungen: <?=MFUPLOAD_ALLOWED_EXTENSIONS_STR?></small>
                      </div>
                    </div>
                  <?php endif; ?>
                </div>
              </div>
              
              
              <div class="card">
                <div class="card-body">
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="note">Interne Notiz</label>
                    <div class="col-lg-10">
                      <textarea id="note" class="form-control" name="note" rows="5"><?=$order->note?></textarea>
                    </div>
                  </div>
                </div>
              </div>
              
              
              <div class="card">
                <div class="card-body">
                  
                  <?php if($me->is("Admin", "netoperator")): ?>
                    <?php if($order->id && !$includes_external_product): ?>
                    <div class="form-group row">
                      <label class="col-lg-2"></label>
                      <div class="col-lg-10 mb-2">
                        <button type="button" id="savetoivt-button" class="btn btn-pink">Kunde an IVT übertragen</button>
                        <span id="savetoivt-info"></span>
                      </div>
                    </div>
                    <?php endif; ?>
                  <div class="form-group row">
                    <label class="col-lg-2"></label>
                    <div class="col-lg-10">
                      <label class="form-check-label"><input type="checkbox" class="form-check-input" name="order_finished" value="1" id="order_finished" <?=($order->finish_date) ? "checked='checked'" : ""?> /> Bestellung abschließen</label>
                    </div>
                  </div>
                  
                  <div class="form-group row" id="finish_date_field">
                    <label class="col-lg-2 col-form-label" for="finish_date">Abgeschlossen am</label>
                    <div class="col-lg-4">
                      <input type="text" class="form-control" name="finish_date" id="finish_date" value="<?=($order->finish_date) ? date("d.m.Y", $order->finish_date) : date("d.m.Y")?>" />
                    </div>
                  </div>
                  <?php endif; ?>
                  
                  <div class="form-group row">
                    <label class="col-lg-2"></label>
                    <div class="col-lg-10">
                      <button type="submit" class="btn btn-primary">Speichern</button>
                      <span id="form-error" class="text-danger hidden">Bitte alle benötigten Felder ausfüllen!</span>
                    </div>
                  </div>
                  
                </div>
              </div>
              
            </form>
          </div>
        </div>
        
      </div>
    </div>
    
    


<script type="text/javascript">
  /*$("#owner_id").select2({
    allowClear: true,
    placeholder: ""
  });*/
  /*$("#billingaddress_id").select2({
    allowClear: true,
    placeholder: ""
  });*/
  
  $('.select2').each(function(){
    $(this).select2({
      allowClear: true,
      placeholder: ""
    });
  });
  
  
  <?php if($order->id): ?>
  $('#savetoivt-button').click(function() {
    $('#savetoivt-button').prop("disabled", true);
    $('#savetoivt-info').html("<img src='<?=self::getResourcePath()?>img/ajax-loader.gif' />");
    $.get("<?=self::getUrl("IvtCustomer","create", ["order_id" => $order->id])?>",
      {},
      function(data) {
        console.log(data);
        if(data.status == "OK") {
          $('#savetoivt-info').removeClass("text-danger");
          $('#savetoivt-info').addClass("text-success");
          $('#savetoivt-info').text("Kunde erfolgreich an IVT übertragen!");
          $('#savetoivt-button').remove();
        } else {
          $('#savetoivt-info').removeClass("text-success");
          $('#savetoivt-info').addClass("text-danger");
          if(data.reason) {
            if(data.reason == "no-cust-num") {
              $('#savetoivt-info').text("Inhaber und/oder Rechungsempfänger hat keine Kundennummer!");
            }
            if(data.reason == "ivt-exist") {
              $('#savetoivt-info').text("Kunde existiert bereits im IVT!");
            }
          } else {
            $('#savetoivt-info').text("Fehler bei übertragung.");
          }
          $('#savetoivt-button').prop("disabled", false);
        }
      },
      "json"
    );
  });
  <?php endif; ?>
  
  $('#owner_id').change(function() {
    var val = $('#owner_id').val();
    
    if(val == "new") {
      $('#owner-form').show();
    } else {
      $('#owner-form').hide();
    }
  });
  
  $('#billingaddress_id').change(function() {
    var val = $('#billingaddress_id').val();
    
    if(val == "new") {
      $('#billingaddress-form').show();
    } else {
      $('#billingaddress-form').hide();
    }
  });
  
  $('#order_finished').change(function() {
    if($('#order_finished').is(":checked")) {
      $('#finish_date_field').show();
    } else {
      $('#finish_date_field').hide();
    }
  });
  
  $('#order_date').datepicker({
      language: 'de',
      format: "dd.mm.yyyy",
      showWeekDays: true,
      todayBtn: 'linked',
      autoclose: true
  });
  
  $('#finish_after').datepicker({
      language: 'de',
      format: "dd.mm.yyyy",
      showWeekDays: true,
      todayBtn: 'linked',
      autoclose: true
  });
  
  $('#finish_date').datepicker({
      language: 'de',
      format: "dd.mm.yyyy",
      showWeekDays: true,
      todayBtn: 'linked',
      autoclose: true
  });
  
  
  // disable mousewheel on input number field when in focus
  $('form').on('focus', 'input[type=number]', function (e) {
    $(this).on('wheel.disableScroll', function (e) {
      e.preventDefault()
    })
  });
  $('form').on('blur', 'input[type=number]', function (e) {
    $(this).off('wheel.disableScroll')
  });

  function updateProduct(id) {
    var pid = $('#product_id-' + id).val();
    
    $.get("<?=self::getUrl("Product", "Api")?>",
    {
      "do": "getProduct",
      product_id: pid,
      form_id: id
    },
    function (success) {
      //console.log(success);
      p = success.result.product;
      id = success.result.form_id;
      
      $('#billing_delay-' + id).val(p.billing_delay);
      $('#billing_period-' + id).val(p.billing_period);
      $('#price-' + id).val(p.price);
      $('#price_setup-' + id).val(p.price_setup);
      $('#price_nne-' + id).val(p.price_nne);
      $('#price_nbe-' + id).val(p.price_nbe);
      
      console.log(p.attributes);
      console.log("form_id: " + id);
      if(typeof p.attributes === 'object' && "termination_required" in p.attributes && p.attributes.termination_required == 1) {
        console.log("termination_required");
        $('#termination_id-' + id + '-line').show();
        console.log($('#termination_id-' + id).val());
        $('#termination_id-' + id).select2({
          allowClear: true,
          placeholder: ""
        });
      } else {
        $('#termination_id-' + id + '-line').hide();
        //$('#termination_id-' + id + '-line').hide();
      }
    },
    'json');
    
  }
  
  /*********************
  * Form validation
  */

  // iban validation
  function validateIban(iban) {
    if(!iban) {
      return false;
    }
    iban = iban.toUpperCase().replace(/\s+/, '');
    //check format
    var m;
    
    if(!iban.match(/^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/)) {
      return false;
    }
    
    var cc = iban.substr(0, 2);
    var check = parseInt(iban.substr(2, 2));
    var account = iban.substr(4);

    var searchRange = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
    //var replaceRange = [];
    var checkStr = account + cc + "00";
    for(var i = 0; i <= 25; i++) {
      checkStr = checkStr.replace(searchRange[i], i + 10);
    };
    // make checksum
    var checksum = parseInt(checkStr.substr(0,1));
    for(pos = 1; pos < checkStr.length; pos++) {
      checksum *= 10;
      checksum += parseInt(checkStr.substr(pos, 1));
      checksum %= 97;
    }
    
    if(98 - checksum == check) {
      return true;
    } else {
      return false;
    }
  }
  
  
  
  function validateOrderForm() {
    var allFields = [
      "owner_id", "owner_company", "owner_firstname", "owner_lastname", "owner_street", "owner_zip", "owner_city", "owner_country", "owner_phone", "owner_email",
      "billingaddress_id", "billing_company", "billing_firstname", "billing_lastname", "billing_street", "billing_zip", "billing_city", "billing_country", "billing_phone", "billing_email",
      "order_date", "finish_after", "finish_after_comment", "billing_type", "bank_account_bank", "bank_account_owner", "bank_account_iban", "bank_account_bic"
    ];
    var checkEmpty = [];
    var error = false;

    // remove invalid class
    allFields.forEach(function(field) {
      //console.log("remove invalid " + field);
      $("#" + field).removeClass("invalid");
    });
    $("#form-error").hide();
    $("#iban_error").hide();
    $('#owner_id').next().find('.select2-selection').removeClass('invalid');
    $('#billingaddress_id').next().find('.select2-selection').removeClass('invalid');
    
    checkEmpty.push("order_date");
    if($('#finish_after').val()) {
      checkEmpty.push("finish_after_comment");
    }

    if($('#owner_id').val() == "new") {
      if(!$('#owner_company').val()) {
        //console.log("no owner_company");
        if(!$('#owner_firstname').val() || !$('#owner_lastname').val()) {
          $('#owner_company').addClass("invalid");
          $('#owner_firstname').addClass("invalid");
          $('#owner_lastname').addClass("invalid");
          error = true;
        }
      }
      checkEmpty.push("owner_street");
      checkEmpty.push("owner_zip");
      checkEmpty.push("owner_city");
      //checkEmpty.push("owner_country");
      checkEmpty.push("owner_phone");
      checkEmpty.push("owner_email");
    }
    
    if(!$('#owner_id :selected').val()) {
      $('#owner_id').addClass("invalid");
      $('#owner_id').next().find('.select2-selection').addClass('invalid');
      error = true;
    }
    
    if($('#billingaddress_id').val() == "new") {
      if(!$('#billing_company').val()) {
        //console.log("no billing_company");
        if(!$('#billing_firstname').val() || !$('#billing_lastname').val()) {
          $('#billing_company').addClass("invalid");
          $('#billing_firstname').addClass("invalid");
          $('#billing_lastname').addClass("invalid");
          error = true;
        }
      }
      checkEmpty.push("billing_street");
      checkEmpty.push("billing_zip");
      checkEmpty.push("billing_city");
      //checkEmpty.push("billing_country");
      checkEmpty.push("billing_phone");
      checkEmpty.push("billing_email");
    }
    
    //alert("error: " + error);
    
    if(!$('#billingaddress_id :selected').val()) {
      if(!$('#owner_id :selected').val()) {
        $('#billingaddress_id').addClass("invalid");
        $('#billingaddress_id').next().find('.select2-selection').addClass('invalid');
        error = true;
      }
    }

    // check bankdaten
    if($('#billing_type').val() == "sepa") {
      checkEmpty.push("bank_account_bank");
      checkEmpty.push("bank_account_owner");
      checkEmpty.push("bank_account_bic");

      if(!validateIban($("#bank_account_iban").val())) {
        $("#bank_account_iban").addClass("invalid");
        $("#iban_error").show();
        error = true;
      }
    }
    
    if(checkEmpty.length) {
      checkEmpty.forEach(function(field) {
        if(!$("#" + field).val().length) {
          console.log(field + " empty");
          $("#" + field).addClass("invalid");
          error = true;
        }
      });
    }
    
    if(error) {
      $("#form-error").show();
      return false;
    }
    
    console.log("empty: " + checkEmpty);
    return true;
   }

  
   
  $(document).ready(function() {
    $('#orderForm').submit(function(e) {
       console.log("submit");
       if(!validateOrderForm()) {
         console.log("nope");
         e.preventDefault();
         return false;
       }
       return true;
     });
     
    <?php if(!$order->finish_date): ?>
      $('#finish_date_field').hide();
    <?php endif; ?>
   });

</script>

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