<?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("Product")?>">Produkte</a></li>
              <li class="breadcrumb-item active"><?=($product->id) ? "bearbeiten" : "Neu" ?></li>
            </ol>
          </div>
          <h4 class="page-title">Produkte</h4>
        </div>
      </div>
    </div>
    <!-- end page title -->
    
    <div class="row">
      <div class="col-lg-12">
        
        <div class="card">
          <div class="card-body">
            <h4 class="header-title mb-2"><?=($product->id) ? "Produkt bearbeiten" : "Neues Produkt"?></h4>
            
            <form class="form-horizontal" method="post" action="<?=self::getUrl("Product", "save")?>">
              <div class="card">
                <div class="card-body">
                  
                  <input type="hidden" name="id" value="<?=$product->id?>" />
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="name">Name *</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="name" id="name" value="<?=$product->name?>" />
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="description">Beschreibung</label>
                    <div class="col-lg-10">
                      <textarea name="description" id="description" class="form-control" rows="2"><?=$product->description?></textarea>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="external">Fremdprodukt *</label>
                    <div class="col-lg-10">
                      <select class="select2 form-control " name="external" id="external">
                        <option value="0" <?=($product->external != 1) ? "selected='selected'" : ""?>>Nein</option>
                        <option value="1" <?=($product->external == 1) ? "selected='selected'" : ""?>>Ja</option>
                      </select>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="productgroup_id">Produktgruppe *</label>
                    <div class="col-lg-10">
                      <select class="select2 form-control" name="productgroup_id" id="productgroup_id">
                        <option></option>
                        <option value="new">Neue Produktgruppe...</option>
                        <?php foreach($productgroups as $group): ?>
                          <option value="<?=$group->id?>" <?=($product->productgroup_id == $group->id) ? "selected='selected'" : ""?>><?=$group->name?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                  </div>
                  
                  <div class="form-group row" id="productgroup_form" style="display:none;">
                    <label class="col-lg-2 col-form-label" for="productgroup_new"></label>
                    <div class="col-lg-10">
                      <div class="row">
                        <div class="col-lg-2">Neue Produktgruppe:</div>
                        <div class="col-lg-10">
                          Name: <input type="text" class="form-control" name="productgroup_new_name" id="productgroup_new_name" value="<?=$productgroup_new_name?>">
                          Beschreibung: <textarea class="form-control" name="productgroup_new_description" id="productgroup_new_description"><?=$productgroup_new_description?></textarea>
                          Interne Notiz: <textarea class="form-control" name="productgroup_new_note" id="productgroup_new_note"><?=$productgroup_new_note?></textarea>
                        </div>
                      </div>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="producttech_id">Technologie</label>
                    <div class="col-lg-10">
                      <select class="select2 form-control " name="producttech_id" id="producttech_id">
                        <option></option>
                        <option value="new">Neue Technologie...</option>
                        <?php foreach($producttechs as $tech): ?>
                          <option value="<?=$tech->id?>" <?=($product->producttech_id == $tech->id) ? "selected='selected'" : ""?>><?=$tech->name?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                  </div>
                  
                  <div class="form-group row" id="producttech_form" style="display: none;">
                    <label class="col-lg-2 col-form-label" for="producttech_new"></label>
                    <div class="col-lg-10">
                      <div class="row">
                        <div class="col-lg-2">Neue Technologie:</div>
                        <div class="col-lg-10">
                          Name: <input type="text" class="form-control" name="producttech_new_name" id="producttech_new_name" value="<?=$producttech_new_name?>">
                          Kundentyp: <select name="producttech_new_customer_type" class="form-control"><option value="residential">Residential</option><option value="business">Business</option></select>
                          RTR Code: <input type="text" class="form-control" name="producttech_new_rtrcode" id="producttech_new_rtrcode" value="<?=$producttech_new_rtrcode?>">
                          Beschreibung: <textarea class="form-control" name="producttech_new_description" id="productgroup_new_description"><?=$producttech_new_description?></textarea>
                          Interne Notiz: <textarea class="form-control" name="producttech_new_note" id="productgroup_new_note"><?=$producttech_new_note?></textarea>
                        </div>
                      </div>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="sla_id">SLA</label>
                    <div class="col-lg-10">
                      <select class="select2 form-control " name="sla_id" id="sla_id">
                        <option></option>
                        <?php foreach($slas as $sla): ?>
                        <option value="<?=$sla->id?>" <?=($product->sla_id == $sla->id) ? "selected='selected'" : ""?>><?=$sla->name?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="networks">Netzgebiete</label>
                    <div class="col-lg-10">
                      <select class="select2 form-control select2-multiple" name="networks[]" id="networks" multiple="multiple" data-placeholder="Choose ...">
                        <option></option>
                        <?php foreach($networks as $network): ?>
                          <option value="<?=$network->id?>" <?=(is_array($product->networks) && array_key_exists($network->id, $product->networks)) ? "selected='selected'" : ""?>><?=$network->name?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                  </div>
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="ivt_id">IVT Produkt ID</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="ivt_id" id="ivt_id" value="<?=$product->ivt_id?>" />
                    </div>
                  </div>
                  
                </div>
              </div>
              
              <h4 class="header-title mb-2">Verrechnungsdaten</h4>
              <div class="card">
                <div class="card-body">
              
                  
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="price">Setup Preis Netto (Einmalig) *</label>
                    <div class="col-lg-10">
                      <input type="text" name="price_setup" id="price_setup" class="form-control" value="<?=$this::dotToComma($product->price_setup)?>" />
                    </div>
                  </div>
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="price">Verkaufspreis Netto (Periodisch) *</label>
                    <div class="col-lg-10">
                      <input type="text" name="price" id="price" class="form-control" value="<?=$this::dotToComma($product->price)?>" />
                    </div>
                  </div>
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="nne">Netznutzungsentgelt (NNE)</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="price_nne" id="price_nne" value="<?=$this::dotToComma($product->price_nne)?>">
                    </div>
                  </div>
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="nbe">Netzbetriebsentgelt (NBE)</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" name="price_nbe" id="price_nbe" value="<?=$this::dotToComma($product->price_nbe)?>">
                    </div>
                  </div>
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="billing_period">Verrechnungsperiode *</label>
                    <div class="col-lg-10">
                      <select class="select2 form-control " name="billing_period" id="billing_period">
                        <option value="12" <?=($product->billing_period == 12) ? "selected='selected'" : ""?>>Jährlich</option>
                        <option value="1" <?=($product->billing_period == 1) ? "selected='selected'" : ""?>>Monatlich</option>
                      </select>
                    </div>
                  </div>
                  <div class="form-group row">
                    <label class="col-lg-2 col-form-label" for="billing_delay">Verzögerter Verrechnungsstart (Monate)</label>
                    <div class="col-lg-10">
                      <input type="text" name="billing_delay" id="billing_delay" class="form-control" value="<?=$this::dotToComma($product->billing_delay)?>" />
                    </div>
                  </div>
                  </div>
              </div>
              
              
              <?php if(is_array($product->attributes) && count($product->attributes)): ?>
              <h4 class="header-title mb-2">Produktattribute</h4> 
              <div class="card">
                <div class="card-body">
                  <?php foreach($product->attributes as $attrib): ?>
                    <div class="form-group row">
                      <label class="col-lg-2 col-form-label" for="attributes_<?=$attrib->producttechattribute_id?>"><?=$attrib->displayname?></label>
                      <div class="col-lg-10">
                        <input type="text" class="form-control" name="attributes[<?=$attrib->producttechattribute_id?>]" id="attributes_<?=$attrib->producttechattribute_id?>" value="<?=$attrib->value?>" />
                      </div>
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>
              <?php endif; ?>
              
              <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"><?=$product->note?></textarea>
                    </div>
                  </div>
                </div>
              </div>
              
              <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>
                </div>
              </div>
              
              
              
            </form>
          </div>
        </div>
        
      </div>
    </div>


<script type="text/javascript">
  $("#productgroup_id").select2({
    allowClear: true,
    placeholder: ""
  });
  $("#producttech_id").select2({
    allowClear: true,
    placeholder: ""
  });
  $("#networks").select2({
    allowClear: true,
    placeholder: ""
  });
  
  $('#productgroup_id').change(function() {
    var value = $('#productgroup_id option:selected').val();
    console.log(value);
    if(value == "new") {
      $('#productgroup_form').show();
    } else {
      $('#productgroup_form').hide();
    }
  });
  
  $('#producttech_id').change(function() {
    var value = $('#producttech_id option:selected').val();
    console.log(value);
    if(value == "new") {
      $('#producttech_form').show();
    } else {
      $('#producttech_form').hide();
    }
  });
  
  // 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')
  });
</script>

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