<?php

/* =======================================================\
  |                        Church                        |
  |--------------------------------------------------------|
  |   Creator: Phương                                      |
  |   Date :   09-07-2017                                  |
  |   Description: Frontaccounting Payroll & Hrm Module    |
  |   Free software under GNU GPL                          |
  |                                                        |
  \======================================================= */

$page_security = 'SA_MMBR_GRP';
$path_to_root = '../../..';

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
add_access_extensions();

$js = "";
if ($SysPrefs->use_popup_windows)
    $js .= get_js_open_window(900, 500);
if (user_use_date_picker())
    $js .= get_js_date_picker();

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/modules/church/includes/church_db.inc");
include_once($path_to_root . "/modules/church/includes/church_ui.inc");

//--------------------------------------------------------------------------
$group = new Group();

function get_post_data() {
    global $group;
    $group->name = get_post('name');
    $group->description = get_post('description');
    $group->status = get_post('status');
    $group->type = get_post('type');
    $group->id = get_post('id');
}

foreach (db_query(get_groups()) as $mmbr_row) {

    if (isset($_POST[$mmbr_row['id']])) {

        $_SESSION['id'] = $mmbr_row['id'];
        $_POST['_tabs_sel'] = 'add';
        $Ajax->activate('_page_body');
    }
}

$cur_id = isset($_SESSION['id']) ? $_SESSION['id'] : '';

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

function can_process() {
    global $group;
    if (empty($group->name)) {
        display_error(_("Group Name must be entered."));
        set_focus('name');
        return false;
    }
    return true;
}

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

function can_delete($cur_id) {
    return can_delete_group($cur_id);
}

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


function status_list_cell($id) {
    $_selector = array_selector(
            'status', $id, $allocs = array(
        '1' => _("Active"),
        '0' => _("Inactive"),
            ), array('select_submit' => FALSE)
    );
    label_cells(_("Select Gender:"), $_selector);
}

function page_length_list_cell($id) {
    $_selector = array_selector(
            'page_length', $id, $allocs = array(
        '10' => _("10"),
        '25' => _("25"),
        '50' => _("50"),
        '100' => _("100"),
        '500' => _("500"),
        '1000' => _("1000"),
            ), array('select_submit' => TRUE)
    );
    label_cells(_("page length:"), $_selector);
}

function get_name($row) {
    return "<b>" . button($row['id'], $row['name']) . "</b>";
}

if (get_post('page_length')) {
    $Ajax->activate('group_table');
}

function groups_table() {
    $_SESSION['id'] = '';
    if (db_has_groups()) {

        $sql = get_groups(false, get_post('grp_name'));
        div_start('group_table');
        start_table(TABLESTYLE_NOBORDER);
        start_row();
        //page_length_list_cell(get_post('page_length'));
        text_cells(_("Name:"), 'grp_name', get_post('grp_name'));
        submit_cells('RefreshInquiry', _("Search"), '', _('Refresh Inquiry'), 'default');
        end_row();
        end_table(1);

        $cols = array(
            _('ID'),
            _('Name') => array('fun' => 'get_name'),
            _('Description'),
            'type' => 'skip',
            _('Status') => array()
        );
        $table = & new_db_pager('group_tbl', $sql, $cols);
        $table->width = "80%";
        //$table->page_len = get_post('page_length');

        // display_note(_('Press name to edit employee details.'));
        display_db_pager($table);
        div_end();
    } else
        display_note(_("No Groups defined."), 1);
}

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

function group_settings($cur_id) {
    global $path_to_root, $avatar_path;
    $group = new Group();
    if ($cur_id) {
        $group = get_groups($cur_id);
    }
    start_outer_table(TABLESTYLE2);

    table_section(1);

    hidden('id', $cur_id);
    table_section_title(_("Group Information"));

    if ($cur_id)
        label_row(_('Group Id:'), $cur_id);

    text_row(_("Name:"), 'name', $group->name, 37, 50);
    textarea_row(_("Description:"), 'description', $group->description, 35, 5);
    status_list_cell($group->status);

    end_outer_table(1);

    div_start('controls');

    if ($cur_id) {

        submit_center_first('addupdate', _("Update Group"), _('Update Group'), 'default');
        submit_center_last('delete', _("Delete Group"), _('Delete Group'), true);
    } else
        submit_center('addupdate', _("Add New Group"), true, '', 'default');

    div_end();
}

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

if (isset($_POST['addupdate'])) {

    get_post_data();
    if (!can_process())
        return;
    write_group($group);

    if ($cur_id) {
        $_SESSION['id'] = $cur_id;
        display_notification(_("Group details has been updated."));
    } else {
        $_SESSION['id'] = '';
        $cur_id = '';
        display_notification(_("A new Group has been added."));
    }

    $Ajax->activate('_page_body');
} elseif (isset($_POST['delete'])) {

    if (!can_delete($cur_id))
        return;
    delete_group($cur_id);
    display_notification(_("Group details has been deleted."));
    $Ajax->activate('_page_body');
}

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

page(_($help_context = "Manage Groups"), false, false, "", $js);

start_form(true);

tabbed_content_start(
        'tabs', array(
    'list' => array(_('Group &List'), 999),
    'add' => array(_('&Add/Edit Group'), 999)
        )
);

if (get_post('_tabs_sel') == 'list')
    groups_table();
elseif (get_post('_tabs_sel') == 'add')
    group_settings($cur_id);

br();

tabbed_content_end();

end_form();
end_page();
