<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCoordinationTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */


     public function getActiveDB(){
        $config_db = array_keys(config("database.connections"));

        $db_connection = array_filter($config_db,function($connection){
            try{
                if($connection == 'mysql7')
                    return false;

                $db = DB::connection($connection)->getPdo();
                return true;
            } catch(\Exception $e){
                return false;
            }
        });
        return $db_connection;
    }

    public function up()
    {

        $db_connection = $this->getActiveDB();

        foreach($db_connection as $dbase){

            if (!Schema::connection($dbase)->hasTable('coordination')) {
                Schema::connection($dbase)->create('coordination', function (Blueprint $table) {
                    $table->id();
                    $table->string('details',50);
                    $table->string('glcode',10);
                    $table->string('jenis',20);
                    $table->date('date')->nullable();
                    $table->integer('reg_user_id');
                    $table->integer('ver_op_user_id');
                    $table->integer('ver_acc_user_id');
                    $table->decimal('amount', 9, 2);
                    $table->timestamps();
                });
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $db_connection = $this->getActiveDB();

        foreach($db_connection as $dbase){

            Schema::connection($dbase)->dropIfExists('akru_net_pecahan');
        };
    }
}
