<?php

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

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

$js = "";
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");
include_once($path_to_root . '/modules/kanban/includes/kanban_db.inc');
include_once($path_to_root . '/modules/kanban/includes/kanban_ui.inc');


page(_($help_context = 'Group Targets'), false, false, '', $js);

if (isset($_GET['id']))
    $selected_id = $_GET['id'];
elseif (isset($_POST['selected_id']))
    $selected_id = $_POST['selected_id'];

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

if (isset($_POST['addupdate'])) {
    if (strlen($_POST['proj_id']) == 0 || $_POST['proj_id'] == '') {
        display_error(_('Project must be selected.'));
        set_focus('proj_id');
    }
    if (strlen($_POST['per_Group']) == 0 || $_POST['per_Group'] == '') {
        display_error(_('Group must be selected.'));
        set_focus('per_Group');
    }
    if (!is_numeric(input_num('proj_amt'))) {
        display_error(_('Project Target should be a number.'));
        set_focus('proj_amt');
    }
    if (!isset($_POST['end_date']) || !is_date($_POST['end_date'])) {
        display_error(_("The entered date is invalid. Please enter a valid date."));
        set_focus('end_date');
        return false;
    } else {
        write_project_target(@$selected_id, get_post('proj_id'), get_post('per_Group'), input_num('proj_amt'), get_post('end_date'));

        if (empty($selected_id)) {
            $id = db_insert_id();
            if ($id)
                display_notification(_('New Group Target created'));
            else {
                display_error(_('Could not create Group Target'));
            }
        } else
            display_notification(_('Group Target has been updated.'));
    }
    $sav = get_post('selected_id');
    unset($_POST);
    $_POST['selected_id'] = $sav;
    $Ajax->activate('_page_body');
}

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

start_form();

if (!empty($selected_id)) {
    $proj = get_project_target($selected_id);
    $_POST['proj_id'] = $proj['proj_id'];
    $_POST['per_Group'] = $proj['per_Group'];
    $_POST['proj_amt'] = $proj['proj_amt'];
    $_POST['end_date'] = sql2date($proj['end_date']);

    hidden('selected_id', $selected_id);
}

start_table(TABLESTYLE2);
 if (!empty($selected_id))
        label_row(_('Target ID:'), $selected_id);
projects_list_row(_('Project : '), 'proj_id', get_post('proj_id'), _('Select Project'));
group_list_row(_('Group:'), 'per_Group',  get_post('per_Group'), _('Select Group'));
amount_row(_('Project Amount') . ':', 'proj_amt', null, '');
date_row(_('Target Date') . ':', 'end_date', null, null, 0, 0, 1);
end_table(1);

div_start('controls');

$txt = empty($selected_id) ? _('Add Project Target') : _('Update Project Target');
submit_center('addupdate', $txt, true, '', 'default');

div_end();

end_form(1);
end_page();
