<?php

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

class CreateK13HasilBelajarsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('k13_hasil_belajars', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('siswa_id');
            $table->longText('meta');
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('siswa_id')
                  ->references('id')
                  ->on('siswas')
                  ->onUpdate('cascade')
                  ->onDelete('restrict');
        });

        // Import data siswa
        DB::unprepared(file_get_contents(dirname(dirname(__FILE__)).'/imports/siswas_01-05-2020.sql'));

        // Import k13 hasil belajar
        Artisan::call('ba:import-excel resources/imports/hasil_belajar_ba_1920.xlsx -k k13');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('k13_hasil_belajars');
        DB::table('siswas')->where('id', '>', '401')->delete();
    }
}
