#!/usr/bin/perl

print "Content-type: text/html\n\n";

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

# https://itvadmin.co.za/cgi-bin/scripts/get/get_delete_attach.pl?documents/1&document_2-ITV_System1.xlsx&documents&1&document_2
# https://itvadmin.co.za/cgi-bin/scripts/get/get_delete_attach.pl?costings/1&costing_signed_quote-Overstrand_Herald_page_9_article.pdf&costings&1&costing_signed_quote

#------------------------------------------------------------------------------------------

@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});

$folder     = $ARGV[0] ;
$attachment = $ARGV[1] ;
$table      = $ARGV[2] ;
$id         = $ARGV[3] ;
$field      = $ARGV[4] ;

# our $debug = 1 ;

if ($table eq 'costings') { $table = 'quotes' ; }

#------------------------------------------------------------------------------------------

use CGI::Carp qw(fatalsToBrowser);

my $attachmentpath = $htmlpath . '/uploads/' . $folder ;

if ($folder =~ /uploads/ && $table eq 'event_quotes' && $id && $field) {
	
	&db_min_ro($table,'1,sss_quote_nr',"id='$id'",'','') ; 
	if ($db{$table}{1}{sss_quote_nr}) {
		&db_switch_conn('sss') ;
			%i = () ; $i{$field} = '' ; &db_min_upd($table,"`id`='$db{$table}{1}{sss_quote_nr}'") ;
			my $attachmentpath_2 = $htmlpath_sss . '/uploads/uploads/' . $db{$table}{1}{sss_quote_nr} ;
			&common_min_delete_files($attachmentpath_2,$attachment) ; 
		&db_switch_conn('aisa') ;
	}
}

if ($attachment eq 'all') { ### Why does tis not update the database?? 
    # &delete_all_attachments ;
	&common_min_delete_files($attachmentpath,'') ; 
} else {
    # &delete_attachment ;
	&common_min_delete_files($attachmentpath,$attachment) ; 
	if ($table and $id and $field) { %i = () ; $i{$field} = '' ; &db_min_upd($table,"`id`='$id'") ; }
}

exit ;

# if ($attachment eq 'all') {
    # &delete_all_attachments ;
    # }
# else
    # {
    # &delete_attachment ;
    # }

# exit ;

#------------------------------------------------------------------------------------------

# sub delete_all_attachments {

# my $attachmentpath = $htmlpath . '/uploads/' . $folder ;

# opendir(DIR, "$attachmentpath") or die "cant open directory: $attachmentpath - $!\n";

# while(defined($file = readdir(DIR))) {
    # if (length $file > 2) {
        # my $attachmentfile = $attachmentpath . '/' . $file ;
        # # print "<br>*** $attachmentfile" ;
        # unlink ($attachmentfile) ;
        # }
    # }

# closedir(DIR) ;

# } #------------------------------------------------------------------------------------------

# sub delete_attachment {

# my $attachmentfile = $htmlpath . '/uploads/' . $folder . '/' . $attachment ;

# # if ($table and $id) { &db_min_delete($table,"`id`='$id'") ; }
# if ($table and $id and $field) { $i{$field} = '' ; &db_min_upd($table,"`id`='$id'") ; }

# # print "table=$table, id=$id" ;

# unlink ($attachmentfile) ; # or die "can't delete : $attachmentfile : $!" ;

# } #------------------------------------------------------------------------------------------

use common ;

1;