<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_LATTER_HAED';
$path_to_root="..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Latter Head Setting"));

include($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/admin/db/latter_head_setting_db.inc");
include_once($path_to_root . "/admin/db/latter_head_db.inc");

simple_page_mode(true);

if ($_POST['UpdateOrder']) //$Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM' 
{
	$input_error = 0;

	// if (strlen($_POST['name']) == 0) 
	// {
	// 	$input_error = 1;
	// 	display_error(_("The area name cannot be empty."));
	// 	set_focus('name');
	// }

	// if ($input_error != 1)
	// {
    // 	if ($selected_id != -1) 
    // 	{
	$previous_img = get_latter_hand_setting($_POST['latter_head_id']);
    if ($_FILES['file_name']['name'] != '') {
        $new_filename = $_FILES['file_name']['name'];
        $dir = company_path() . "/images";
        $destination = $dir . "/" . $new_filename;
        move_uploaded_file($_FILES['file_name']['tmp_name'], $destination);
    }else{
    	$new_filename = $previous_img['file_name'];
    } 
        update_latter_hand_setting($_POST['email'], $_POST['ntn_no'], $_POST['strn_no'], $_POST['contact_no'], $_POST['address'], $new_filename, $_POST['latter_head_id']);
        $note = _('Selected latter head setting has been updated');

    	// } 
    	// else 
    	// {
        //    if (isset($_FILES['file_name'])) {
		// 	    $filename = $_FILES['file_name']['name'];
		// 	    $temp_file = $_FILES['file_name']['tmp_name'];
		// 	    $dir = company_path() . "/images";
		// 	    $destination = $dir . "/" . $filename;
		// 	    move_uploaded_file($temp_file, $destination);
		// 	}
	    //     add_latter_hand_setting($_POST['latter_head_id'], $_POST['email'], $_POST['ntn_no'], $_POST['strn_no'],
	    //         $_POST['contact_no'], $_POST['address'], $filename);
	    //     $note = _('New latter head setting has been added');
    	// }
		display_notification($note);    	
		meta_forward($path_to_root.'/admin/latter_head_setting.php?');
		$Mode = 'RESET';
	}


// if($_POST['UpdateOrder']){

//  	if (isset($_FILES['file_name'])) {
// 		    $filename = $_FILES['file_name']['name'];
// 		    $temp_file = $_FILES['file_name']['tmp_name'];
// 		    $dir = company_path() . "/images";
// 		    $destination = $dir . "/" . $filename;
// 		    move_uploaded_file($temp_file, $destination);
// 	}

//    update_latter_hand_setting($_POST['latter_head_id'], $_POST['email'], $_POST['ntn_no'], $_POST['strn_no'], $_POST['contact_no'], $_POST['address'], 
// 	            	$filename);
//     $note = _('Selected latter head setting has been updated');
// }


if ($Mode == 'Delete')
{

	$cancel_delete = 0;

	// PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'

	// if (key_in_foreign_table($selected_id, 'cust_branch', 'area'))
	// {
	// 	$cancel_delete = 1;
	// 	display_error(_("Cannot delete this area because customer branches have been created using this area."));
	// } 
	if ($cancel_delete == 0) 
	{
		delete_latter_hand_setting($selected_id);

		display_notification(_('Selected latter head setting has been deleted'));
	} //end if Delete area
	$Mode = 'RESET';
} 

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

//-------------------------------------------------------------------------------------------------
start_form(true);

global $Ajax;
echo "<center>";
latter_head_row( _("Latter Head:"), 'latter_head_id', null, false, true);
echo "<hr></center>";

$result = get_latter_hand_settings(check_value('show_inactive'));

start_table(TABLESTYLE, "width='30%'");

// $th = array(_("Latter Head"), _("Email"), _("NTN NO."), _("STRN NO."), _("Contact No."),_("Address"),_("File Name"), "", "");
// inactive_control_column($th);

// table_header($th);
// $k = 0; 

// while ($myrow = db_fetch($result)) 
// {
	
// 	alt_table_row_color($k);
		
// 	label_cell(get_latter_hand_name($myrow["latter_head_id"]));
// 	label_cell($myrow["email"]);
// 	label_cell($myrow["ntn_no"]);
// 	label_cell($myrow["strn_no"]);
// 	label_cell($myrow["contact_no"]);
// 	label_cell($myrow["address"]);
// 	$file_path = company_path() . "/images/" . $myrow['file_name'];
// 	$link_view_one = "<a href='" . $file_path . "' target='_blank'>" . $myrow['file_name'] . "</a><br>";
// 	label_cell($link_view_one);
// 	inactive_control_cell($myrow["id"], $myrow["inactive"], 'latter_head_setting', 'id');
//  	// edit_button_cell("Edit".$myrow["id"], _("Edit"));
//  	// delete_button_cell("Delete".$myrow["id"], _("Delete"));
// 	end_row();
// }
	
// inactive_control_row($th);
// end_table();
// echo '<br>';

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

start_table(TABLESTYLE2);

// if ($selected_id != -1) 
// {
//  	if ($Mode == 'Edit') {
		//editing an existing area
$myrow = get_latter_hand_setting($_POST['latter_head_id']);

$_POST['email']  = $myrow["email"];
$_POST['ntn_no']  = $myrow["ntn_no"];
$_POST['strn_no']  = $myrow["strn_no"];
$_POST['contact_no']  = $myrow["contact_no"];
$_POST['file_name']  = $myrow["file_name"];
$_POST['address']  = $myrow["address"];

// 	}
// 	hidden("selected_id", $selected_id);
// } 

text_row_ex(_("Email:"), 'email', 30); 
text_row_ex(_("Ntn Number:"), 'ntn_no', 30); 
text_row_ex(_("STRN Number:"), 'strn_no', 30); 
text_row_ex(_("Contact Number:"), 'contact_no', 30); 
file_row(_("New Company Logo") . ":", 'file_name', 'file_name');
if($myrow['file_name'] != ''){
	$file_path = company_path() . "/images/" . $myrow['file_name'];
	$link_view_one = "<a href='" . $file_path . "' target='_blank'>" . $myrow['file_name'] . "</a><br>";
	label_row(_(" ") ,$link_view_one);
}
textarea_row(_("Address:"), "address", null, 35, 5);

end_table(1);
// submit_add_or_update_center($selected_id == -1, '', 'both');
submit_center_first('UpdateOrder', _("Update"), _('Validate changes and update document'), 'default');
end_form();
end_page();
