<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace Modules\Member\Observers;

use Modules\Member\Models\Fields;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class FieldObserver {

	public $type = [
		'string'  => 'string',
		'audio'   => 'string',
		'video'   => 'string',
		'editor'  => 'longText',
		'text'    => 'text',
		'image'   => 'string',
		'images'  => 'text',
		'file'    => 'string',
		'files'   => 'text',
		'number'  => 'integer',
		'bool'    => 'boolean',
		'date'    => 'date',
		'datetime' => 'dateTime',
		'select'  => 'string',
		'radio'   => 'string',
		'checkbox' => 'text'
	];
    /**
     * 处理 "created" 事件。
     */
    public function created(Fields $fields): void {
		$table = 'member_extends';
		if(Schema::hasTable($table)){
			Schema::table($table, function (Blueprint $table) use ($fields) {
				$type = $this->type[$fields->type] ? $this->type[$fields->type] : 'string';
				if($fields->length){
					$field = $table->{$type}($fields->name, length: $fields->length);
				}else{
					$field = $table->{$type}($fields->name);
				}
				if($fields->default_value){
					$field = $field->default($fields->default_value);
				}
				if($fields->type == 'required'){
					$field = $field->nullable();
				}
				$field = $field->comment($fields->title)->after($fields->after);
			});
		}
    }

	/**
     * 处理 "updated" 事件。
     */
	public function updated(Fields $fields): void {
		$table = 'member_extends';
		if (!Schema::hasTable($table) && !Schema::hasColumn($table, $fields->name)) {
			return;
		}
		if($fields->isDirty('name') && Schema::hasColumn($table, $fields->getOriginal('name'))){
			Schema::table($table, function (Blueprint $table) use ($fields) {
				$table->renameColumn($fields->getOriginal('name'), $fields->name);
			});
		}

		if($fields->isDirty()){
			Schema::table($table, function (Blueprint $table) use ($fields) {
				$type = $this->type[$fields->type] ? $this->type[$fields->type] : 'string';
				if($fields->length){
					$field = $table->{$type}($fields->name, length: $fields->length);
				}else{
					$field = $table->{$type}($fields->name);
				}
				if($fields->default_value){
					$field = $table->default($fields->default_value);
				}else{
					if($fields->type == 'required'){
						$field = $field->nullable(false);
					}else{
						$field = $field->nullable();
					}
				}
				$field->after($fields->after)->comment($fields->title)->change();
			});
		}
	}

	public function deleted(Fields $fields): void {
		if($fields->status == 1){
			$table = 'member_extends';
			if (Schema::hasTable($table) && Schema::hasColumn($table, $fields->name)) {
				Schema::table($table, function (Blueprint $table) use ($fields) {
					$table->dropColumn($fields->name);
				});
			}
		}
	}
}
