<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;
use CodeIgniter\I18n\Time;

class DropIsDeletedField extends Migration
{
    public function up()
    {

        $m = $this->db->table('clientes');
        $m->where('is_deleted',1)->update(['deleted_at' => Time::now()]);
        $m->where('is_deleted',0)->update(['deleted_at' => null]);
        $this->forge->dropColumn('clientes',"is_deleted");

        $m = $this->db->table('cliente_contactos');
        $m->where('is_deleted',1)->update(['deleted_at' => Time::now()]);
        $m->where('is_deleted',0)->update(['deleted_at' => null]);
        $this->forge->dropColumn('cliente_contactos',"is_deleted");

        $m = $this->db->table('cliente_precios');
        $m->where('is_deleted',1)->update(['deleted_at' => Time::now()]);
        $m->where('is_deleted',0)->update(['deleted_at' => null]);
        $this->forge->dropColumn('cliente_precios',"is_deleted");

        $m = $this->db->table('cliente_plantilla_precios_lineas');
        $m->where('is_deleted',1)->update(['deleted_at' => Time::now()]);
        $m->where('is_deleted',0)->update(['deleted_at' => null]);
        $this->forge->dropColumn('cliente_plantilla_precios_lineas',"is_deleted");

        $m = $this->db->table('cliente_plantilla_precios');
        $m->where('is_deleted',1)->update(['deleted_at' => Time::now()]);
        $m->where('is_deleted',0)->update(['deleted_at' => null]);
        $this->forge->dropColumn('cliente_plantilla_precios',"is_deleted");


    }

    public function down()
    {
        $column = [
            'is_deleted' => [
                'type'       => 'BOOLEAN',
                'default'    => false,
            ],
        ];
        $this->forge->addColumn('clientes',$column);
        $m = $this->db->table('clientes');
        $m->where('deleted_at IS NOT NULL',null,false)->update(['is_deleted' => true]);
        $m->where('deleted_at',null)->update(['is_deleted' => false]);

        $this->forge->addColumn('cliente_contactos',$column);
        $m = $this->db->table('cliente_contactos');
        $m->where('deleted_at IS NOT NULL',null,false)->update(['is_deleted' => true]);
        $m->where('deleted_at',null)->update(['is_deleted' => false]);

        $this->forge->addColumn('cliente_precios',$column);
        $m = $this->db->table('cliente_precios');
        $m->where('deleted_at IS NOT NULL',null,false)->update(['is_deleted' => true]);
        $m->where('deleted_at',null)->update(['is_deleted' => false]);

        $this->forge->addColumn('cliente_plantilla_precios_lineas',$column);
        $m = $this->db->table('cliente_plantilla_precios_lineas');
        $m->where('deleted_at IS NOT NULL',null,false)->update(['is_deleted' => true]);
        $m->where('deleted_at',null)->update(['is_deleted' => false]);

        $this->forge->addColumn('cliente_plantilla_precios',$column);
        $m = $this->db->table('cliente_plantilla_precios');
        $m->where('deleted_at IS NOT NULL',null,false)->update(['is_deleted' => true]);
        $m->where('deleted_at',null)->update(['is_deleted' => false]);
    }
}
