<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_user_form extends CI_Migration {

    public function up() {
        $this->dbforge->add_field([
            'id' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'user_id' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
//            'relation_id' => [
//                'type' => 'BIGINT',
//                'constraint' => 20,
//                'unsigned' => true
//            ],
            'form_id' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
            ],
            'name' => [
                'type' => 'VARCHAR',
                'constraint' => 255,
                'null' => true,
            ],
            'phone' => [
                'type' => 'VARCHAR',
                'constraint' => 255,
                'null' => true,
            ],
//            'total_bobot' => [
//                'type' => 'SMALLINT',
//                'constraint' => 5,
//                'null' => true,
//                'default' => 0,
//            ],
            'status' => [
                'type' => 'SMALLINT',
                'constraint' => 1,
                'null' => true,
                'default' => 1,
                'comment' => '1 = ACTIVE, 1 = SUBMITTED' // define later
            ],
            'active' => [
                'type' => 'SMALLINT',
                'constraint' => 1,
                'null' => true,
                'default' => 1,
                'comment' => '0 = INACTIVE, 1 = ACTIVE'
            ],
            'submit_date' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'created_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'updated_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'deleted_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'created_by' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
            'updated_by' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
            'deleted_by' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
            'CONSTRAINT ' . $this->db->dbprefix . 'user_form_users FOREIGN KEY (user_id) REFERENCES ' . $this->db->dbprefix . 'users(id) ON UPDATE CASCADE ON DELETE CASCADE',
//             'CONSTRAINT ' . $this->db->dbprefix . 'user_form_relations_users FOREIGN KEY (relation_id) REFERENCES ' . $this->db->dbprefix . 'users(id) ON UPDATE CASCADE ON DELETE CASCADE',
            'CONSTRAINT ' . $this->db->dbprefix . 'user_form_form FOREIGN KEY (form_id) REFERENCES ' . $this->db->dbprefix . 'form(id) ON UPDATE CASCADE ON DELETE CASCADE'
        ]);
        $this->dbforge->add_key('id', true);
        $this->dbforge->create_table('user_form');
    }

    //--------------------------------------------------------------------

    public function down() {
        $this->dbforge->drop_table('user_form');
    }

}
