#!/usr/bin/perl

use strict;
use CGI;
use Time::localtime;
use JSON;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
);

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookieList, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'txtbk/report/save_sendDelExpFromHitList.tmpl',
    }
);

my $tm = localtime;
my $curTime = sprintf("%04d%02d%02d%02d%02d%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday, $tm->hour, $tm->min, $tm->sec);
my $today = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday, $tm->hour, $tm->min, $tm->sec);

my $op  = $input->{'op'};
my $type = lc($input->{'type'});
my $logId = $input->{'logId'};
my $data = _getDataFromlog($dbh,$logId);
$data  = decode_json($data);
my $filename = "deleteFromHitList_$curTime";

if ($op eq 'save'){
    if ($type eq 'html') {
        print "Content-Type:application/x-download\n";
        print "Content-Disposition:attachment;filename=$filename".".html\n\n";  
        print "<html> <head>";
        print "<style type='text/css'>";
        print "table.reportTbl{border-collapse:separate;border-spacing:0;empty-cells:show} ";
        print "table.reportTbl th,td {font-size:12px;}";
        print "table.reportTbl thead {  background: #C0C0C0;  color: #fff;}";
        print "table.reportTbl th {  font-weight: bold;  text-align:center;  border:none;}";
        print "table.reportTbl tbody tr:nth-child(even) {  background: #f0f0f2;}";
        print "table.reportTbl td {border-bottom: 1px solid #F0F0F0; border-right: 1px solid #F0F0F0;}";
        print " table.reportTbl td:first-child {  border-left: 1px solid #F0F0F0;}";
        print "</style>";
        print "<body>";
        print "<div style='margin-left:auto;margin-right:auto;text-align:center;font-size:12px;'>";
        print "</div>";
        print "<table class='reportTbl'>";
        print "<thead>";
        print "<tr><th>No.</th><th>Title</th><th>Barcode</th><th>Call Number</th>";
        print "</tr>";
        print "</thead>";
        print "<tbody>";
        my $i =0;
        foreach my $d( @{$data}){
            $i++;
            print "<tr>";
            print "<td>$i</td>";
            print "<td>$d->{'title'}</td>";
            print "<td>$d->{'barcode'}</td>";
            print "<td>$d->{'classNumber'}</td>";
            print "</tr>";
        }
        print "</tbody>";
        print "</table>";
        print "</body></head></html>";
    }
    elsif($type eq 'csv'){
        print "Content-Encoding: UTF-8\n";
        print "Content-type: text/csv; charset=UTF-8\n";
        print "Content-Disposition:attachment;filename=$filename".".csv\n\n"; 
        print "\"title\",\"Barcode\",\"Call Number\" \n";
        foreach my $d( @{$data}){
            print "\"$d->{'title'}\",\"$d->{'barcode'}\",\"$d->{'classNumber'}\" ";
            print "\n";
        }
    }
}
else{
    tmpl_write($dbh, $cgi, $cookieList, $template);
}

sub _getDataFromlog {
    my ($dbh,$id) = @_;

    my $sql = "select content from log where id=$id";
    my $data = $dbh->selectrow_array($sql);
    return $data;

}
