<?php

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

class AddOnepercentToTransactionKeuntunganTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $columns = ['ujrah', 'ujrah_rebet', 'onepercent', 'onepercent_rebet', 'tempoh', 'beza_tempoh', 'margin_semasa'];

        $columns_not_exist = array_filter($columns, function ($column) {
            return !Schema::connection('mysql7')->hasColumn('transaksi_keuntungan', $column);
        });

        Schema::connection('mysql7')->table('transaksi_keuntungan', function (Blueprint $table) use ($columns_not_exist) {
            array_walk($columns_not_exist, function ($column) use ($table) {
                $table->decimal($column, 5, 2);
            });
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $columns = ['ujrah', 'ujrah_rebet', 'onepercent', 'onepercent_rebet', 'tempoh', 'beza_tempoh', 'margin_semasa'];

        $columns_exist = array_filter($columns, function ($column) {
            return Schema::connection('mysql7')->hasColumn('transaksi_keuntungan', $column);
        });

        Schema::connection('mysql7')->table('transaksi_keuntungan', function (Blueprint $table) use($columns_exist) {
            $table->dropColumn($columns_exist);
        });
    }
}
