#!/usr/bin/perl

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

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

use Opals::Mail qw(
    mail_send
);

use Opals::Eq_SolrSearch;

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        => '/eqmnt/report/save_sendDelExpFromHitList.tmpl',
        reqPermission   => 'eq_record_edit',
    }
);


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 = $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>Equipment Name</th><th>Manufacture</th><th>Model</th><th>Barcode</th>";
        print "</tr>";
        print "</thead>";
        print "<tbody>";
        my $i =0;
        foreach my $d( @{$data}){
            $i++;
            print "<tr>";
            print "<td>$i</td>";
            print "<td>$d->{'eq_name'}</td>";
            print "<td>$d->{'1'}</td>";
            print "<td>$d->{'3'}</td>";
            print "<td>$d->{'barcode'}</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 "\"Equipment Name\",\"Manufacture\",\"Model\",\"Barcode\" \n";
        foreach my $d( @{$data}){
            print "\"$d->{'eq_name'}\",\"$d->{'1'}\",\"$d->{'3'}\",\"$d->{'barcode'}\" ";
            print "\n";
        }
    }
}
else{
    tmpl_write($dbh, $cgi, $cookieList, $template);
}

sub getList {

    my($dbh,$date,$list) = @_;
    my $rootDir   = Opals::Context->config('rootDir');

    my $template = HTML::Template->new(    
        filename            => "save_sendRec.tmpl",
        path                => "$rootDir/htdocs/theme/opals/eqmnt/util/",
        global_vars         => 1,
        die_on_bad_params   => 0,
        cache               => 1,
        shared_cache        => 0,
        loop_context_vars   => 1,);

    $template->param(
        list=>$list,
        date=>$date,
        );
    return  $template->output();
}

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

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

}
