<?php

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

class CreateInterventiTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('interventi_stato', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nome', 50);
            $table->timestamps();
        });

        Schema::create('interventi_tipo', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nome', 50);
            $table->timestamps();
        });

        Schema::create('tecnici', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nome', 50);
            $table->string('cognome', 50);
            $table->string('email', 100);
            $table->string('telefono', 30);
            $table->string('ditta', 100);
            $table->timestamps();
        });

        Schema::create('interventi', function (Blueprint $table) {
            $table->increments('id');
            
            $table->integer('anno');
            $table->integer('numero');
            
            $table->date('data_inserimento_ticket')->nullable();
            
            $table->string('serial_number', 50);
            $table->string('numero_ticket_cliente', 50);
            $table->string('numero_lettera_vettura', 50);
            
            $table->string('sede_intervento', 200);
            $table->string('referente_nome', 100);
            $table->string('referente_telefono', 30);
            $table->string('referente_citta', 100);
            
            $table->text('intervento_descrizione_da_svolgere');
            $table->date('intervento_data_scadenza')->nullable();
            $table->date('intervento_data')->nullable();
            
            $table->string('intervento_ora_inizio', 10);
            $table->string('intervento_ora_fine', 10);
            $table->text('intervento_descrizione_risoluzione');
            
            $table->decimal('importo_ticket', 19, 5)->default(0);
            $table->decimal('spesa_tecnico', 19, 5)->default(0);
            $table->decimal('margine_ticket', 19, 5)->default(0);
            
            $table->text('note');

            $table->integer('intervento_tipo_id')->unsigned()->nullable();
            $table->foreign('intervento_tipo_id')->references('id')->on('interventi_tipo');
            
            $table->integer('intervento_stato_id')->unsigned()->nullable();
            $table->foreign('intervento_stato_id')->references('id')->on('interventi_stato');

            $table->integer('cliente_id')->unsigned()->nullable();
            $table->foreign('cliente_id')->references('id')->on('anagrafica');
            
            $table->integer('committente_id')->unsigned()->nullable();
            $table->foreign('committente_id')->references('id')->on('anagrafica');
            
            $table->timestamps();
        });

        Schema::create('intervento_prodotto', function (Blueprint $table) {
            $table->increments('id');
            
            $table->integer('intervento_id')->unsigned();
            $table->foreign('intervento_id')->references('id')->on('interventi')->onDelete('cascade');
            
            $table->integer('prodotto_id')->unsigned();
            $table->foreign('prodotto_id')->references('id')->on('prodotti')->onDelete('cascade');
            
            $table->timestamps();
        }); 
        
        Schema::create('intervento_tecnico', function (Blueprint $table) {
            $table->increments('id');
            
            $table->integer('intervento_id')->unsigned();
            $table->foreign('intervento_id')->references('id')->on('interventi')->onDelete('cascade');
            
            $table->integer('tecnico_id')->unsigned();
            $table->foreign('tecnico_id')->references('id')->on('tecnici')->onDelete('cascade');
            
            $table->timestamps();
        });   
        
        
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //--- ELIMINO LE FOREIGN KEY
        Schema::table('intervento_tecnico', function(Blueprint $table){
            $table->dropForeign('intervento_tecnico_intervento_id_foreign');
            $table->dropForeign('intervento_tecnico_tecnico_id_foreign');
            //$table->dropColumn('copyright_id');
        });        

        Schema::table('intervento_prodotto', function(Blueprint $table){
            $table->dropForeign('intervento_prodotto_intervento_id_foreign');
            $table->dropForeign('intervento_prodotto_prodotto_id_foreign');
            //$table->dropColumn('copyright_id');
        }); 
        
       //$table->dropForeign('posts_user_id_foreign');
        Schema::table('interventi', function(Blueprint $table){
            $table->dropForeign('interventi_intervento_tipo_id_foreign');
            $table->dropForeign('interventi_intervento_stato_id_foreign');
            $table->dropForeign('interventi_cliente_id_foreign');
            $table->dropForeign('interventi_committente_id_foreign');
            //$table->dropColumn('copyright_id');
        });
        
        Schema::drop('interventi_stato');
        Schema::drop('interventi_tipo');
        Schema::drop('tecnici');
        Schema::drop('interventi');
        Schema::drop('intervento_tecnico');
        Schema::drop('intervento_prodotto');
    }
}
