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

$admin = $_SESSION["admin_" . $dbname];


if (isset($_POST["submit"])) {
    require("includes/dbconnect.php");

    $cond = "";

    if (!empty($_POST["code"])) {
        $pcode = trim($_POST["code"]);
        $cond.= "AND code LIKE '%$pcode%'";
    }

    if (!empty($_POST["name"])) {
        $pname = trim($_POST["name"]);
        $cond.= "AND `name` LIKE '%$pname%'";
    }



    $get = mysqli_query($conn, "SELECT code, `name` FROM `country` where `name`!='' $cond ORDER by `name`")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($get);

    /* Normal Browser View Part */
    if ($_POST["submit"] == "View") {
        ?>    
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>

                <style type="text/css">
                    <!--
                    @import url("css/table.css");
                    -->
                </style>
                <script>
                    $("table").on("click", "span.show", function () {

                        $(this).hide();
                        $(this).siblings("span.hide").show();

                    })

                    $("table").on("click", "span.hide", function () {

                        $(this).hide();
                        $(this).siblings("span.show").show();

                    })
                </script>

                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

            </head>
            <body>

                <?php
                $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
                $zcomp = mysqli_fetch_assoc($comp);
                echo "<font size='4px' color='#039'><b>$zcomp[compname]</font></b></font><br />";
                echo "<font size='2px' color='#039'><b> $zcomp[add1]</b></font><br /><br />";
                echo "<b><font color='#039'>Country List &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : " . date("M-d-Y") . "</b><br />";
                echo "<b>" . "Total Countries - $getno</b><br />";
                echo "<br />";
                ?>

                <table id="hor-minimalist-b" summary="Employee Pay Sheet">

                    <thead>
                        <tr>
                            <th style="text-align:left;" scope="col" width="6"><b>Sr.</b></th>
                            <th style="text-align:left;" scope="col" width="120"><b>Full Name</b></th>
                            <th style="text-align:left;" scope="col" width="2"><b>Code</b></th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        while ($city = mysqli_fetch_assoc($get)) {
                            $ctr++;
                            echo "<tr> \n";
                            echo "<td style=\"text-align:left;\" width=\"6\"> \n <span class='show'> $ctr </span> \n </td> \n";
                            echo "<td style=\"text-align:left;\" width=\"120\"> \n <span> $city[name] </span> \n </td> \n";
                            echo "<td style=\"text-align:left;\" width=\"2\"> \n <span> $city[code] </span> \n </td> \n";
                            echo "</tr>";
                            echo "<tr>";
                            echo "<td style=\"text-align:left;\" width=\"6\"> \n <span class='hide' style='display:none'> $ctr </span> \n </td> \n";
                            echo "<td style=\"text-align:left;\" width=\"120\"> \n <span> $city[name] </span> \n </td> \n";
                            echo "<td style=\"text-align:left;\" width=\"2\"> \n <span> $city[code] </span> \n </td> \n";
                            echo "</tr>";
                        }
                        ?>
                    </tbody>
                    <tfoot>

                    </tfoot>
                </table>
                </div>
            </body>
        </html>

        <?php
    } elseif ($_POST["submit"] == "Export to PDF") {


        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();
        $pdf->SetMargins(10, 10, 10);
        $pdf->SetXY(5, 5);


        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(0, 0, "Total Cities : " . $getno, 0, 0, 'L');

        $pdf->Ln(10);
        $pdf->SetFont('Arial', 'BI', 10);

        $pdf->Cell(10, 8, 'Sr.', 1);

        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(120, 8, 'Name', 1);
        $pdf->Cell(25, 8, 'Code', 1);
        $pdf->Ln();
        $ctr = 0;
        $pdf->SetFont('Arial', '', 10);


        while ($cty = mysqli_fetch_assoc($get)) {
            $ctr++;
            $pdf->SetFont('Arial', 'I', 8);

            $pdf->Cell(10, 7, $ctr, 1);

            $pdf->SetFont('Arial', '', 10);

            $pdf->Cell(120, 7, $cty['name'], 1);
            $pdf->Cell(25, 7, $cty['code'], 1);
            $pdf->Ln();
        }

        $pdf->Output('Country_list.pdf', 'D');
    }



    /* For Excel */ else {

        function cleanData(&$str) {
            $str = preg_replace("/\t/", "\\t", $str);
            $str = preg_replace("/\r?\n/", "\\n", $str);
            if (strstr($str, '"'))
                $str = '"' . str_replace('"', '""', $str) . '"';
        }

// filename for download
        $filename = "Country_List_" . date('Ymd') . ".xls";

        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Content-Type: application/vnd.ms-excel");

        $flag = false;
        /* $result = mysqli_query($conn, "SELECT code, `name` FROM country ORDER BY `name` $cond") or die(mysqli_error($conn)); */
        /* $result = mysqli_fetch_array($get); */

        while (false !== ($row = mysqli_fetch_assoc($get))) {
            if (!$flag) {
                echo "" . implode("\t", array_keys($row)) . "\r\n";
                $flag = true;
            }
            array_walk($row, 'cleanData');
            echo implode("\t", array_values($row)) . "\r\n";
        }
        exit;
    }
}// Submit Tag Close
?>