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

$maindb = "promocode";
$formname = "Promo Code List";
$file = "promo_print.php";

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

if (isset($_POST["view"])) {
    $name = $_POST[name];
    $discperc = $_POST[discperc];
    $expdt = $_POST[expdt];

    $namecond = "";

    if (!empty($name)) {
        $namecond = " where `code`='$name' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` $namecond")or die(mysqli_error($conn));

    include("../includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">
            <?php include("../includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered"">
                        <tr class="mySearchTr">
                            <th style="background-color: #B9408F">Sr</th>
                            <th style="background-color: #B9408F; width:40%">Name</th>
                            <th style="background-color: #B9408F">Discount</th>
                            <th style="background-color: #B9408F">Expiry Date</th>
                        </tr>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $ctr++;

                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[discperc]</td>";
                            Echo "<td>$pmain[expdt]</td>";
                            Echo "</tr>";
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

        <?php include("../includes/linksfoot.php"); ?>
    </body>
    </html>
    <?
}

if (isset($_POST["subpdf"])) {
    $name = $_POST[name];

    $namecond = "";

    if (!empty($name)) {
        $namecond = " where `code`='$name' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` $namecond")or die(mysqli_error($conn));

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

    class pdf extends fpdf {

        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', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
//    include("../includes/head_rep_pdf.php");
//$pdf->SetMargins(1,1,1);
//$pdf->SetXY(5,5);

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(185,64,143);
    $pdf->Cell(13, 8, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(180, 8, 'Name', 1, 0, '', 1);
    $pdf->Ln();

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;

        $pdf->SetFont('Arial', '', 8);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(13, 8, $ctr, 1, 0);
        $pdf->Cell(180, 8, $pmain['name'], 1, 0);
        $pdf->Ln();
    }
    $pdf->Output('Size.pdf', 'D');
}

if (isset($_POST["excel"])) {
    $name = $_POST[name];

    $namecond = "";

    if (!empty($name)) {
        $namecond = " where `code`='$name' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` $namecond")or die(mysqli_error($conn));

    $contents .= "Sr,Name\n";

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;
        $pname = $pmain[name];

        $contents .= $ctr . ",";
        $contents .= $pname . "\n";
    }
    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>
