<?php defined('BASEPATH') or exit('No direct script access allowed');

$CI = &get_instance();

$declarations = db_prefix() . 'staff_declarations';
$items        = db_prefix() . 'staff_declaration_items';
$files        = db_prefix() . 'staff_declaration_files';

if (!$CI->db->table_exists($declarations)) {
    $CI->db->query("CREATE TABLE `$declarations` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `staff_id` INT(11) NOT NULL,
        `staff_name` VARCHAR(191) NULL,
        `staff_code` VARCHAR(64) NULL,
        `position` VARCHAR(191) NULL,
        `department` VARCHAR(191) NULL,
        `employee_signature` VARCHAR(191) NOT NULL,
        `employee_date` DATE NOT NULL,
        `created_at` DATETIME NOT NULL,
        `updated_at` DATETIME NULL,
        PRIMARY KEY (`id`),
        KEY `staff_id` (`staff_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET={$CI->db->char_set};");
}

if (!$CI->db->table_exists($items)) {
    $CI->db->query("CREATE TABLE `$items` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `declaration_id` INT(11) NOT NULL,
        `sort_order` INT(11) NOT NULL DEFAULT 1,
        `date_received` DATE NULL,
        `description` TEXT NULL,
        `estimated_value` DECIMAL(10,2) NULL,
        `gift_from` VARCHAR(191) NULL,
        `relationship` VARCHAR(191) NULL,
        `purpose` VARCHAR(191) NULL,
        `action_taken` VARCHAR(191) NULL,
        PRIMARY KEY (`id`),
        KEY `declaration_id` (`declaration_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET={$CI->db->char_set};");
}

if (!$CI->db->table_exists($files)) {
    $CI->db->query("CREATE TABLE `$files` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `declaration_id` INT(11) NOT NULL,
        `staff_id` INT(11) NOT NULL,
        `file_name` VARCHAR(191) NOT NULL,
        `original_name` VARCHAR(191) NULL,
        `filetype` VARCHAR(100) NULL,
        `filesize` INT(11) NULL,
        `dateadded` DATETIME NOT NULL,
        PRIMARY KEY (`id`),
        KEY `declaration_id` (`declaration_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET={$CI->db->char_set};");
}
// Notification recipients for staff_declaration
if (function_exists('add_option')) {
    if (get_option('sd_notify_role_ids') === null) {
        add_option('sd_notify_role_ids', '');
    }
    if (get_option('sd_notify_staff_ids') === null) {
        add_option('sd_notify_staff_ids', '');
    }
}
