<?php

class client extends user
{
  
  public function getDropdownClients()
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM kunden WHERE is_active = :is_active AND belong_to_user_uuid = :belong_to_user_uuid ORDER BY firma ASC';
    $db->query($sql);
    $db->bind(':is_active', 1);
    $db->bind(':belong_to_user_uuid', $_SESSION[ALLOW]);
    return $db->resultset();
  }
  
  public function saveNewClientData($values)
  {
    $insertArray                            = array(
        'uuid'                              => $values['uuid'],
        'firma'                             => makeHtmlEntities($values['firma']),
        'name'                              => makeHtmlEntities($values['name']),
        'adresse'                           => makeHtmlEntities($values['adresse']),
        'email'                             => makeHtmlEntities($values['email']),
        'tel'                               => makeHtmlEntities($values['tel']),
        'zugehoerigkeit'                    => $values['zugehoerigkeit'],
        'uebergreifende_notizen'            => makeHtmlEntities($values['uebergreifende_notizen']),
        'angelegt'                          => time(),
        'password'                          => $this->createPassword($values['password']),
        'belong_to_user_uuid'               => $_SESSION[ALLOW],
        'is_active'                         => 1,
    );
    pdoDbInsert("kunden", $insertArray);
    $this->setTomQuestions($values['uuid']);
    $this->setVvtQuestions($values['uuid']);
  }
  
  private function getClientIdByUuid($clientUuid)
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT id FROM kunden WHERE uuid = :uuid';
    $db->query($sql);
    $db->bind(':uuid', $clientUuid);
    $row                                    = $db->single();
    return $row['id'];
  }
  
  public function getClientUuidById($clientId)
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT uuid FROM kunden WHERE id = :id';
    $db->query($sql);
    $db->bind(':id', $clientId);
    $row                                    = $db->single();
    return $row['uuid'];
  }
  
  private function setTomQuestions($clientUuid)
  {
    $clientID                               = $this->getClientIdByUuid($clientUuid);
    
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM tom_vorlagen ORDER BY id ASC';
    $db->query($sql);
    $rows                                   = $db->resultset();
    
    foreach ($rows as $row) {
      $insertArray                          = array(
          'kunden_id'                       => $clientID,
          'frage_id'                        => $row['id'],
          'frage'                           => $row['frage'],
          'bemerkung_begruendung'           => $row['bemerkung_begruendung'],
          'themenblock_id'                  => $row['themenblock_id'],
          'themenfrage_id'                  => $row['themenfrage_id'],
          'empfehlung'                      => $row['empfehlung'],
          'angelegt'                        => time(),
          'status'                          => 5,
          'belong_to_user_uuid'             => $_SESSION[ALLOW],
      );
      pdoDbInsert("tom_kunden", $insertArray);
    }
  }
  
  private function setVvtQuestions($clientUuid)
  {
    $clientID                               = $this->getClientIdByUuid($clientUuid);
    
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM vvt_vorlagen ORDER BY id ASC';
    $db->query($sql);
    $rows                                   = $db->resultset();
    
    foreach ($rows as $row) {
      $insertArray                          = array(
          'themenblock_id'                  => $row['themenblock_id'],
          'name'                            => $row['name'],
          'zweck'                           => $row['zweck'],
          'beschr_warum_erlaubt'            => $row['beschr_warum_erlaubt'],
          'betr_personen'                   => $row['betr_personen'],
          'betr_daten_der_pers'             => $row['betr_daten_der_pers'],
          'weitergabe'                      => $row['weitergabe'],
          'uebermittlung_laender'           => $row['uebermittlung_laender'],
          'aufbewahrungsfrist'              => $row['aufbewahrungsfrist'],
          'tom'                             => $row['tom'],
          'risiko_quellen_schaden'          => $row['risiko_quellen_schaden'],
          'vvt_id'                          => $row['id'],
          'kunden_id'                       => $clientID,
          'angelegt'                        => time(),
          'status'                          => 6,
          'belong_to_user_uuid'             => $_SESSION[ALLOW],
      );
      pdoDbInsert("vvt_kunden", $insertArray);
    }
  }
  
  public function getDropdownZugehoerigkeit()
  {
    $dropdownArray                          = array(
        'Firma 1'                           => 1,
        'Firma 2'                           => 2,
    );
    return $dropdownArray;
  }
  
  public function showClientData()
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT * FROM kunden WHERE is_active = :is_active AND belong_to_user_uuid = :belong_to_user_uuid ORDER BY firma ASC';
    $db->query($sql);
    $db->bind(':is_active', 1);
    $db->bind(':belong_to_user_uuid', $_SESSION[ALLOW]);
    return $db->resultset();
  }
  
  public function saveUpdateClientData($values)
  {
    $updateArray                            = array(
        'firma'                             => makeHtmlEntities($values['firma']),
        'name'                              => makeHtmlEntities($values['name']),
        'adresse'                           => makeHtmlEntities($values['adresse']),
        'email'                             => makeHtmlEntities($values['email']),
        'tel'                               => makeHtmlEntities($values['tel']),
        'zugehoerigkeit'                    => $values['zugehoerigkeit'],
        'uebergreifende_notizen'            => makeHtmlEntities($values['uebergreifende_notizen']),
    );
    $whereCause                             = 'uuid = :uuid';
    $whereVal                               = $values['uuid'];
    pdoDbUpdate("kunden", $updateArray, $whereCause, $whereVal);
  }
  
  public function deleteClient($uuid)
  {
    $db                                     = $this->Database;
    $sql                                    = 'SELECT id FROM kunden WHERE uuid = :uuid';
    $db->query($sql);
    $db->bind(':uuid', $uuid);
    $row                                    = $db->single();
    $kundenId                               = $row['id'];
    
    $sqlDelKunden                           = 'DELETE FROM kunden WHERE uuid = :uuid';
    $db->query($sqlDelKunden);
    $db->bind(':uuid', $uuid);
    $db->execute();
    
    $sqlDelTom                              = 'DELETE FROM tom_kunden WHERE kunden_id = :kunden_id';
    $db->query($sqlDelTom);
    $db->bind(':kunden_id', $kundenId);
    $db->execute();
    
    $sqlDelVVT                              = 'DELETE FROM vvt_kunden WHERE kunden_id = :kunden_id';
    $db->query($sqlDelVVT);
    $db->bind(':kunden_id', $kundenId);
    $db->execute();
  }
  
  public function getAmountKunden()
  {
    $db                               = $this->Database;
    $sql                              = 'SELECT COUNT(id) as amount FROM kunden WHERE belong_to_user_uuid = :belong_to_user_uuid';
    $db->query($sql);
    $db->bind(':belong_to_user_uuid', $_SESSION[ALLOW]);
    $row                              = $db->single();
    return $row['amount'];
  }
  
  public function getClientCompany($kundenId)
  {
    $db                               = $this->Database;
    $sql                              = 'SELECT firma FROM kunden WHERE id = :id';
    $db->query($sql);
    $db->bind(':id', $kundenId);
    $row                              = $db->single();
    return $row['firma'];
  }
  
  public function getClientData($kundenId)
  {
    $db                               = $this->Database;
    $sql                              = 'SELECT * FROM kunden WHERE id = :id';
    $db->query($sql);
    $db->bind(':id', $kundenId);
    return $db->single();
  }
}

