<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "Shipment";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$zroot = $_SESSION["zdir"];

$vouchno = $_REQUEST["vouchno"];
$series = $_REQUEST["series"];

$instid = $_SESSION["instid"];
$accstkn = $_SESSION["accstkn"];

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt  FROM `shipment`  WHERE vouchno='$vouchno' ") or die(mysqli_error($conn));
$fetdata = mysqli_fetch_array($getqry);

$getparty = mysqli_query($conn, "SELECT `accode`,`name`, `mobile` FROM accmast WHERE `accode` = '$fetdata[customer]' ") or die(mysqli_error($conn));
$fetparty = mysqli_fetch_object($getparty);

$getcompdt = mysqli_query($conn, "SELECT * FROM $zroot.company ") or die(mysqli_error($conn));
$fetcompdt = mysqli_fetch_object($getcompdt);

$mobile = $fetparty->mobile;

require("library/fpdf/fpdf.php");

class pdf extends fpdf
{

    function Header()
    {
        $this->SetTopMargin(10);
    }

    function Footer()
    {

        //Position at 1.5 cm from bottom

        $this->SetY(-15);

        //Arial italic 8

        $this->SetFont('Arial', 'I', 8);

        //Page number

        // $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

}

$pdf = new pdf('P', 'mm', array(165, 140));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 14);

$pdf->SetTextColor(0, 0, 0);

$ht = 7;


$pdf->Image('images/galaxy_logo.png', 30, 3, 20,18);
$pdf->Cell(40, $ht, "", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 20);
$pdf->SetTextColor(79, 20, 54);
$pdf->Cell(80, $ht, "GALAXY CARGO", 0, 0, 'L');
$pdf->Ln(15);

$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Shipment No. ", 1, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(80, $ht, "GC-$fetdata[vouchno]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Booking Date ", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[mydt]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Marka ", 1, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(80, $ht, "$fetdata[marka]", 1, 0, 'L');
$pdf->Ln();

if ($fetdata['overflow'] > 0) {


    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(5, $ht, "", 0, 0);
    $pdf->Cell(30, $ht, "No. of Cartons ", 1, 0);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(30, $ht, "$fetdata[nocart]", 1, 0, 'L');
    $pdf->SetTextColor(255, 0, 0); // RGB for red
    $pdf->Cell(30, $ht, "Overflow", 1, 0, 'R');
    $pdf->Cell(20, $ht, "$fetdata[overflow]", 1, 0, 'R');
    $pdf->Ln();

    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(5, $ht, "", 0, 0);
    $pdf->Cell(30, $ht, "Remarks ", 1, 0);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(80, $ht, "$fetdata[overflowrem]", 1, 0, 'L');
    $pdf->Ln();

} else {

    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(5, $ht, "", 0, 0);
    $pdf->Cell(30, $ht, "No. of Cartons ", 1, 0);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(80, $ht, "$fetdata[nocart]", 1, 0, 'L');
    $pdf->Ln();

}
    $pdf->SetTextColor(0, 0, 0); // RGB for black


$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Supplier ", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[supplier]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Description ", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[description]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Pcs ", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[pcs] $fetdata[unit] ", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Total CBM", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[totcbm]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Weight", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[weight]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Origin", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[origin]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Container No. ", 1, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(80, $ht, "$fetdata[containerno]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Sailing Date", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, format_date($fetdata['saildate']), 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Destination", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, "$fetdata[destination]", 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Estimate Date", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, format_date($fetdata['estimdate']), 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(30, $ht, "Delivery Date", 1, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(80, $ht, format_date($fetdata['delvdate']), 1, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(28, $ht, "For Any Queries", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(45, $ht, "Contact : $fetcompdt->htel", 0, 0, 'L');
$pdf->Ln();



$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, "", 0, 0);
$pdf->Cell(42, $ht, "To Track Shipment Visit", 0, 0, 'L');

// Draw a cell with text
$pdf->SetFont('Arial', 'U', 10);
$pdf->SetTextColor(0, 0, 255); // Blue color for hyperlink
$pdf->Cell(45, $ht, 'GALAXY CARGO', 0, 1, 'L', false, 'https://galaxycargo.in');


ob_end_clean();
// $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/ugpos/admin/pdf/bills/" . $series . $vouchno . ".pdf";
// $pdf->Output("$nomFacture", 'F');                            

$para = $_REQUEST["para"];
//browse($para);
if ($para == "") {
    $pdf->Output("$formname - GC-" . $fetdata['vouchno'] . ".pdf", "I");
} else {
    //    $myyear = $_SESSION[$myyear];
    $z1 = explode('_', $zroot);

    $nm = $z1[1];

    $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/admin/shipmentpdf/GC-" . $fetdata['vouchno'] . ".pdf";

    //    $urlsend = "https://techfar.in/galeyr/admin/shipmentpdf/GL-" . $fetdata['vouchno'] . ".pdf"; sahil written
    $urlsend = "https://apps2ai.com/galaxycargo/manage/admin/shipmentpdf/GC-" . $fetdata['vouchno'] . ".pdf";

    $pdf->Output("F", "$nomFacture");

    $filevoucher = "GC-" . $fetdata['vouchno'];
    $filename = "GC-" . $fetdata['vouchno'] . ".pdf";

    $mobileno = $mobile;

    // $mobileno = '919082336408';

    if ($mobileno != '') {

        $msgtech = urlencode($urlsend);

        // ✅ Show alert + close page after 1 second
        echo "<script>
              alert('Message Send to Customer');
              setTimeout(function() {
                window.close();
              }, 1000); // 1 second delay
            </script>";

        // Flush response so browser processes alert/close
        ignore_user_abort(true);
        set_time_limit(0);

        if (function_exists('fastcgi_finish_request')) {
            fastcgi_finish_request();
        } else {
            ob_end_flush();
            flush();
        }

        // ---- Background work continues here ----
        $logFile = "/tmp/whatsapp_send_" . $fetdata['vouchno'] . ".log";

        $cmd = "curl -s -X POST " .
            "\"https://web.secureleads.in/api/send?number=$mobileno&type=media&message=Shipment%20Detail%20$filevoucher%20&media_url=$msgtech&filename=$filename&instance_id=$instid&access_token=$accstkn\" " .
            "-H 'Cookie: stackpost_session=m1ip03t51dk5rlgaa5blakqmva57k3nd' " .
            "> $logFile 2>&1 &";

        exec($cmd);
        
        // delete file after a short delay in background
        $delCmd = "(sleep 30 && rm -f " . escapeshellarg($nomFacture) . ") > /dev/null 2>&1 &";
        exec($delCmd);

    } else {
        echo "<script>
                alert('No. Mobile no. Found');
                window.close();
              </script>";
    }

}

?>