<?php

class TerminationModel {
  public $building_id = null;
  public $status_id = null;
  public $lineworker_id = null;
  public $name = null;
  public $code = null;
  public $oan_id = null;
  public $rimo_id = null;
  public $contact = null;
  public $phone = null;
  public $email = null;
  
  public $create_by = null;
  public $edit_by = null;
  public $create = null;
  public $edit = null;

  public static function create(Array $data) {
    $model = new Termination();
    
    foreach($data as $field => $value) {
      if(property_exists(get_called_class(), $field)) {
        $model->$field = $value;
      }
    }
    
    if(!$model->status_id) {
      $model->status_id = 1;
    }
    
    $me = new User();
    $me->loadMe();

    if($model->create_by === null) {
      $model->create_by = $me->id;
    }
    if($model->edit_by === null) {
      $model->edit_by = $me->id;
    }
    
    return $model;
  }
  
  public static function getOne($id) {
    if(!is_numeric($id) || !$id) {
      throw new Exception("Invalid number", 400);
    }
    $item = [];
    $db = FronkDB::singleton();
    
    $res = $db->select("Termination", "*", "id=$id LIMIT 1");
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      $item = new Termination($data);
    }
    return $item;
  }
  
  public static function getAll() {
    $items = [];
    
    $db = FronkDB::singleton();
    
    $res = $db->select("Termination", "*", "1=1 ORDER BY code");
    if($db->num_rows($res)) {
      while($data = $db->fetch_object($res)) {
        $items[] = new Termination($data);
      }
    }
    return $items;
    
  }
  
  public static function count($filter) {
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    $sql = "SELECT COUNT(*) as cnt FROM Termination
            LEFT JOIN Building ON (Building.id = Termination.building_id)
            LEFT JOIN Terminationstatus ON (Terminationstatus.id = Termination.status_id)
            WHERE $where
           ";
    
    $res = $db->query($sql);
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      return $data->cnt;
    }
    return 0;
  }
  
  public static function search($filter, $limit = false) {
    $items = [];
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    $sql = "SELECT Termination.* FROM Termination
            LEFT JOIN Building ON (Building.id = Termination.building_id)
            LEFT JOIN Terminationstatus ON (Terminationstatus.id = Termination.status_id)
            WHERE $where
            ORDER BY Building.network_id, code
           ";
    
    if(is_array($limit) && count($limit)) {
      if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
        $sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
      } elseif(is_numeric($count)) {
        $sql .= " LIMIT ".$limit['count'];
      }
    }
    
    $res = $db->query($sql);
    if($db->num_rows($res)) {
      while($data = $db->fetch_object($res)) {
        $items[] = new Termination($data);
      }
    }
    return $items;
  }
  
  private function getSqlFilter($filter) {
    $where = "1=1 ";
  
    if(array_key_exists("building_id", $filter)) {
      $building_id = $filter['building_id'];
      
      if(is_numeric($building_id) && $building_id > 0) {
        $where .= " AND building_id = $building_id";
      }
    }
    
    if(array_key_exists("status_id", $filter)) {
      $status_id = $filter['status_id'];
      
      if(is_numeric($status_id) && $status_id > 0) {
        $where .= " AND Termination.status_id = $status_id";
      }
    }
    
    if(array_key_exists("network_id", $filter)) {
      $network_id = $filter['network_id'];
      if(is_numeric($network_id)) {
        $where .= " AND Building.network_id=$network_id";
      } elseif(is_array($network_id) && count($network_id)) {
        $where .= " AND Building.network_id IN (". implode(",", $network_id).")";
      }
    }
    
    if(array_key_exists("networksection_id", $filter)) {
      $networksection_id = $filter['networksection_id'];
      if(is_numeric($networksection_id)) {
        $where .= " AND Building.networksection_id=$networksection_id";
      }
    }
    
    if(array_key_exists("code", $filter)) {
      $code = FronkDB::singleton()->escape($filter['code']);
      if($code) {
        $where .= " AND Termination.code LIKE '%$code%'";
      }
    }
    
    if(array_key_exists("building_code", $filter)) {
      $building_code = FronkDB::singleton()->escape($filter['building_code']);
      if($building_code) {
        $where .= " AND Building.code LIKE '%$building_code%'";
      }
    }
    
    if(array_key_exists("building_street", $filter)) {
      $building_street = FronkDB::singleton()->escape($filter['building_street']);
      if($building_street) {
        $where .= " AND Building.street LIKE '%$building_street%'";
      }
    }
    
    
    
    if(array_key_exists("status", $filter)) {
      if(in_array(substr($filter['status'], 1, 2), ["<=", ">="])) {
        $op = substr($filter['status'], 1, 2);
        $status = substr($filter['status'], 3);
      } elseif(in_array(substr($filter['status'], 1, 1), ["<", ">"])) {
        $op = substr($filter['status'], 1, 1);
        $status = substr($filter['status'], 2);
      } else {
        $op = "=";
        $status = $filter['status'];
      }
      
      if(is_numeric($status)) {
        $where .= " AND Terminationstatus.code $op $status";
      } else {
        // get code of statusname
        $code = Terminationstatus::getOne(["name" => $status]);
        if($code) {
          $status = FronkDB::singleton()->escape($status);
          $where .= " AND Terminationstatus.code $op '$status'";
        }
        
      }
    }
    
    return $where;
    
  }

  
}