CREATE TABLE `tbladmin_roles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `code_name` varchar(150) NOT NULL,
  `permissions` longtext DEFAULT NULL,
  `added_by` int(10) NULL,
  `added_on` datetime NULL,
  `updated_by` int(10) NULL,
  `updated_on` datetime NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


ALTER TABLE `tbladmin_roles` CHANGE `added_on` `added_on` DATETIME NULL DEFAULT CURRENT_TIMESTAMP;

ALTER TABLE `tbladmin_roles` CHANGE `updated_on` `updated_on` DATETIME on update CURRENT_TIMESTAMP NULL DEFAULT NULL;

ALTER TABLE `tblapp_admin` 
ADD `role_code_name` VARCHAR(150) NULL AFTER `name`, 
ADD `permissions` LONGTEXT NULL AFTER `role_code_name`, 
ADD `added_by` INT(10) NOT NULL AFTER `permissions`, 
ADD `added_on` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `added_by`, 
ADD `updated_by` INT(10) NOT NULL AFTER `added_on`, 
ADD `updated_on` DATETIME NULL DEFAULT NULL on update CURRENT_TIMESTAMP AFTER `updated_by`;

ALTER TABLE `tbladmin_roles` ADD UNIQUE `code_name` (`code_name`(150));

CREATE TABLE `tblapp_admin_history` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `action` ENUM('I','U','D','') NOT NULL,
  `admin_id` int(10) NOT NULL,
  `name` varchar(255) NOT NULL,
  `role_code_name` varchar(150) DEFAULT NULL,
  `permissions` longtext DEFAULT NULL,
  `email` varchar(500) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `added_by` int(10) NOT NULL,
  `added_on` datetime DEFAULT NULL,
  `updated_by` int(10) NOT NULL,
  `updated_on` datetime DEFAULT NULL,
  `history_added_on` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

ALTER TABLE `tblapp_admin` ADD UNIQUE(`email`);

INSERT INTO `tbladmin_roles` (`id`, `name`, `code_name`, `permissions`, `added_by`) VALUES (NULL, 'Super Admin', 'super_admin', 'a:14:{s:13:\"notifications\";a:4:{i:0;s:4:\"send\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:4:\"view\";}s:21:\"logo_designing_orders\";a:5:{i:0;s:4:\"view\";i:1;s:12:\"view_details\";i:2;s:13:\"update_status\";i:3;s:10:\"send_draft\";i:4;s:13:\"fulfill_order\";}s:21:\"manage_designed_logos\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:5:\"users\";a:12:{i:0;s:4:\"view\";i:1;s:12:\"view_details\";i:2;s:4:\"edit\";i:3;s:18:\"upgrade_membership\";i:4;s:13:\"login_as_user\";i:5;s:15:\"remove_wisetack\";i:6;s:17:\"enable_cp_payment\";i:7;s:29:\"enable_disable_lowes_material\";i:8;s:17:\"update_home_depot\";i:9;s:12:\"skip_sign_up\";i:10;s:11:\"export_user\";i:11;s:6:\"delete\";}s:3:\"sms\";a:2:{i:0;s:4:\"view\";i:1;s:4:\"send\";}s:21:\"promotional_campaigns\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:27:\"other_promotional_campaigns\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:5:\"roles\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:13:\"manage_admins\";a:3:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";}s:9:\"affiliate\";a:15:{i:0;s:4:\"view\";i:1;s:12:\"view_details\";i:2;s:6:\"create\";i:3;s:4:\"edit\";i:4;s:13:\"view_tracking\";i:5;s:16:\"view_payment_acc\";i:6;s:15:\"add_payment_acc\";i:7;s:16:\"edit_payment_acc\";i:8;s:18:\"delete_payment_acc\";i:9;s:11:\"payout_view\";i:10;s:19:\"payout_view_details\";i:11;s:22:\"affiliate_make_payment\";i:12;s:13:\"referral_view\";i:13;s:21:\"referral_view_comment\";i:14;s:15:\"referral_update\";}s:10:\"refer_earn\";a:1:{i:0;s:4:\"view\";}s:13:\"manage_coupon\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:8:\"settings\";a:1:{i:0;s:4:\"edit\";}s:5:\"notes\";a:5:{i:0;s:3:\"add\";i:1;s:4:\"edit\";i:2;s:6:\"delete\";i:3;s:17:\"delete_attachment\";i:4;s:4:\"view\";}}', '1');


UPDATE `tblapp_admin` SET `role_code_name` = 'super_admin', `permissions` = 'a:14:{s:13:\"notifications\";a:4:{i:0;s:4:\"send\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:4:\"view\";}s:21:\"logo_designing_orders\";a:5:{i:0;s:4:\"view\";i:1;s:12:\"view_details\";i:2;s:13:\"update_status\";i:3;s:10:\"send_draft\";i:4;s:13:\"fulfill_order\";}s:21:\"manage_designed_logos\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:5:\"users\";a:12:{i:0;s:4:\"view\";i:1;s:12:\"view_details\";i:2;s:4:\"edit\";i:3;s:18:\"upgrade_membership\";i:4;s:13:\"login_as_user\";i:5;s:15:\"remove_wisetack\";i:6;s:17:\"enable_cp_payment\";i:7;s:29:\"enable_disable_lowes_material\";i:8;s:17:\"update_home_depot\";i:9;s:12:\"skip_sign_up\";i:10;s:11:\"export_user\";i:11;s:6:\"delete\";}s:3:\"sms\";a:2:{i:0;s:4:\"view\";i:1;s:4:\"send\";}s:21:\"promotional_campaigns\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:27:\"other_promotional_campaigns\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:5:\"roles\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:13:\"manage_admins\";a:3:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";}s:9:\"affiliate\";a:15:{i:0;s:4:\"view\";i:1;s:12:\"view_details\";i:2;s:6:\"create\";i:3;s:4:\"edit\";i:4;s:13:\"view_tracking\";i:5;s:16:\"view_payment_acc\";i:6;s:15:\"add_payment_acc\";i:7;s:16:\"edit_payment_acc\";i:8;s:18:\"delete_payment_acc\";i:9;s:11:\"payout_view\";i:10;s:19:\"payout_view_details\";i:11;s:22:\"affiliate_make_payment\";i:12;s:13:\"referral_view\";i:13;s:21:\"referral_view_comment\";i:14;s:15:\"referral_update\";}s:10:\"refer_earn\";a:1:{i:0;s:4:\"view\";}s:13:\"manage_coupon\";a:4:{i:0;s:4:\"view\";i:1;s:6:\"create\";i:2;s:4:\"edit\";i:3;s:6:\"delete\";}s:8:\"settings\";a:1:{i:0;s:4:\"edit\";}s:5:\"notes\";a:5:{i:0;s:3:\"add\";i:1;s:4:\"edit\";i:2;s:6:\"delete\";i:3;s:17:\"delete_attachment\";i:4;s:4:\"view\";}}' WHERE `tblapp_admin`.`id` = 1;

CREATE TABLE `tbladmin_audit_trail` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NULL DEFAULT NULL,
  `event` enum('insert','update','delete') NOT NULL,
  `table_name` varchar(128) NOT NULL,
  `old_values` text,
  `new_values` text NOT NULL,
  `url` varchar(255) NOT NULL,
  `ip_address` varchar(45) NOT NULL,
  `user_agent` varchar(255) NULL,
  `created_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


ALTER TABLE `tbladmin_audit_trail`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `tbladmin_audit_trail`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tbladmin_audit_trail` ADD `where_clause` TEXT NULL DEFAULT NULL AFTER `table_name`;
ALTER TABLE `tbladmin_audit_trail` ADD INDEX(`user_id`);
ALTER TABLE `tbladmin_audit_trail` ADD INDEX(`table_name`);
ALTER TABLE `tbladmin_audit_trail` ADD INDEX(`created_at`);
ALTER TABLE `tbladmin_audit_trail` ADD INDEX(`event`);
