<?php
namespace App\Models\Patient;

use App\Models\BaseModel;
use App\Models\BaseUtcModel;

class PatIdtModel extends BaseModel {
  protected $table      = 'patidt';
  protected $primaryKey = 'PatIdtID';
  protected $allowedFields = ['InternalPID', 'IdentifierType', 'Identifier', 'EffectiveDate', 'ExpirationDate', 'CreateDate', 'DelDate'];
    
  protected $useTimestamps  = true;
  protected $createdField   = 'CreateDate';
  protected $updatedField   = '';
  protected $useSoftDeletes = true;
  protected $deletedField   = 'DelDate';

  protected $beforeInsert = ['normalizeDatesToUTC'];
  protected $beforeUpdate = ['normalizeDatesToUTC'];
  protected $afterFind    = ['convertDatesToUTCISO'];
  protected $afterInsert  = ['convertDatesToUTCISO'];
  protected $afterUpdate  = ['convertDatesToUTCISO'];

  public function createPatIdt($patidt, string $newInternalPID) {
    $this->insert(["InternalPID" => $newInternalPID, "IdentifierType" => $patidt['IdentifierType'], 'Identifier' => $patidt['Identifier']]);
  }

  public function updatePatIdt($patidt, string $InternalPID) {
    $exists = $this->where('InternalPID', $InternalPID)->first();
    // $exists = $db->table('patidt')->where('InternalPID', $InternalPID)->get()->getRowArray();

    if ($exists) {
      // Update
      $this->where('InternalPID', $InternalPID)->set($patidt)->update();
      // $db->table('patidt')->where('InternalPID', $InternalPID)->update($patidt);

    } else {
      // Insert
      $patidt['InternalPID'] = $InternalPID;
      $this->insert($patidt);
      
      // $patidt['InternalPID'] = $InternalPID;
      // $db->table('patidt')->insert($patidt);
    }
  }

  public function deletePatIdt(string $InternalPID) {
    $this->where('InternalPID', $InternalPID)->delete();
  }

}
