<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePVTables extends Migration {
  public function up() {
    // patvisit
    $this->forge->addField([
      'SiteID' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'InternalPVID'=> ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'PVID' => ['type' => 'VARCHAR', 'constraint' => 20, 'null' => true],
      'InternalPID' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'EpisodeID' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'EndDate' => ['type' => 'DATETIME', 'null' => true],
      'ArchivedDate'=> ['type' => 'DATETIME', 'null' => true],
      'DelDate' => ['type' => 'DATETIME', 'null' => true],  
    ]);
    
    $this->forge->addKey('InternalPVID', true);
    $this->forge->addUniqueKey('PVID');
    $this->forge->createTable('patvisit');

    // patdiag
    $this->forge->addField([
      'InternalPVID' => ['type' => 'INT', 'constraint' => 11],
      'InternalPID'  => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'DiagCode'   => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Diagnosis'    => ['type' => 'TEXT', 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'EndDate'    => ['type' => 'DATETIME', 'null' => true],
      'ArchivedDate' => ['type' => 'DATETIME', 'null' => true],
      'DelDate'    => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('InternalPVID', true);
    $this->forge->createTable('patdiag');

    // patvisitadt
    $this->forge->addField([
      'PVADTID'   => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
      'InternalPVID'=> ['type' => 'INT', 'null' => true],
      'ADTCode' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'LocationID' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'AttDoc' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'RefDoc' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'AdmDoc' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'CnsDoc' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'EndDate' => ['type' => 'DATETIME', 'null' => true],
      'ArchivedDate'=> ['type' => 'DATETIME', 'null' => true],
      'DelDate' => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('PVADTID', true);
    $this->forge->createTable('patvisitadt');

    // patvisitlog
    $this->forge->addField([
      'PatVisLogID'=> ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'TblName'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'RecID'    => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'FldName'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'FldValuePrev'=> ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Origin' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'UserID'   => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'SiteID'   => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'DIDType'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'DID'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'MachineID'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'SessionID'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'AppID'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'ProcessID'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'WebPageID'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'EventID'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'ActivityID' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Reason'   => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'LogDate' => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('PatVisLogID', true);
    $this->forge->createTable('patvisitlog');
  }

  public function down() {
    $this->forge->dropTable('patdiag', true);
    $this->forge->dropTable('patvisit', true);
    $this->forge->dropTable('patvisitadt', true);
    $this->forge->dropTable('patvisitlog', true);
  }
}