<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Site;

class CreateTableSite extends Migration
{
	private $convert = 'App\Models\Old\Setup';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
	    Schema::dropIfExists('site');
        Schema::create('site', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('url');
            $table->string("name");
            $table->string('devurl')->nullable();
            $table->text('aboutus')->nullable();
            $table->json('address')->nullable();
            $table->string('description')->nullable();
            $table->string('email');
	        $table->string('phone')->nullable();
	        $table->string('fax')->nullable();
	        $table->string('logo')->nullable();
	        $table->string('favicon')->nullable();
	        $table->string('theme');
        });

        if ($this->convert)
	        foreach (($this->convert)::all() as $o)
	        {
	            $so = Site::where(['url'=>rtrim($o->url,'/')])->first();

	            if (! $so)
	                $so = new Site;

	            $so->url = rtrim($o->url,'/');
	            $so->name = $o->site_details['name'];
	            $so->email = $o->site_details['email'];
	            $so->address = ['address1'=>$o->site_details['address1'],'address2'=>$o->site_details['address2'],'city'=>$o->site_details['city'],'state'=>$o->site_details['state'],'postcode'=>$o->site_details['pcode']];
		        $so->phone = $o->site_details['phone'];
		        $so->fax = $o->site_details['fax'];
		        $so->theme = 'metronic-fe';
		        $so->devurl = 'http://graytech';
		        $so->save();
	        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('site');
    }
}