<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
$show_footer = isset($show_footer) ? $show_footer : 0;
$font_wight = isset($font_wight) ? $font_wight : 'normal';
$html = 'N';

ob_start();
require APPPATH . 'views/produk/print_invoice.php';
$content = ob_get_clean();

$footerHtml = '';
if (preg_match('/<footer\b[^>]*>.*?<\/footer>/is', $content, $footerMatch)) {
    $footerHtml = $footerMatch[0];
    $content = preg_replace('/<footer\b[^>]*>.*?<\/footer>/is', '', $content, 1);
}

$desktopStyle = <<<HTML
<style>
  body{
    margin:0 !important;
    padding:28px 0 36px !important;
    background:#e9edf2 !important;
    min-height:100vh;
  }
  .invoice-box{
    width:200mm !important;
    margin:0 auto !important;
    padding:16px 18px 28px !important;
    background:#fff !important;
    box-shadow:0 8px 24px rgba(33, 53, 71, .12);
    border-radius:6px;
    position:relative;
    overflow:hidden;
    box-sizing:border-box;
    border-radius:6px 6px 0 0;
  }
  .desktop-page{
    width:200mm;
    margin:0 auto;
  }
  .desktop-page .invoice-box{
    width:100% !important;
    margin:0 !important;
  }
  .invoice-box > table,
  .invoice-box .table,
  .invoice-box .gbr{
    position:relative;
    z-index:1;
  }
  .gbr{
    width:100% !important;
    position:relative !important;
    top:0 !important;
    text-align:center;
    margin-top:10px;
  }
  .qris{
    position:static !important;
    left:auto !important;
    bottom:auto !important;
    display:inline-block;
    margin:0 auto !important;
  }
  .watermark{
    position:absolute !important;
    left:50% !important;
    top:auto !important;
    bottom:72px !important;
    margin:0 !important;
    transform:translateX(-50%) rotate(-14deg) !important;
    width:4.1cm !important;
    opacity:.11 !important;
    z-index:0 !important;
    pointer-events:none;
  }
  footer{
    position:static !important;
    display:block;
    width:100% !important;
    margin:0 !important;
    height:auto !important;
    min-height:0.5cm;
    padding:4px 6px !important;
    box-sizing:border-box;
    box-shadow:none !important;
    border-radius:0 0 6px 6px;
  }
  @media (max-width: 1024px){
    body{padding:16px 10px 24px !important;}
    .desktop-page{
      width:100% !important;
    }
  }
</style>
HTML;

$content = preg_replace('/<script type="text\/javascript">\s*window\.print\(\);\s*window\.onfocus=function\(\)\{ window\.close\(\); \}\s*<\/script>/is', '', $content);
$content = str_replace('</head>', $desktopStyle . PHP_EOL . '</head>', $content);
$content = str_replace('<body>', '<body><div class="desktop-page">', $content);
if ($footerHtml !== '') {
    $content = str_replace('</body>', PHP_EOL . $footerHtml . PHP_EOL . '</div></body>', $content);
} else {
    $content = str_replace('</body>', '</div></body>', $content);
}

echo $content;
