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

$maindb = "diamast";
//$trandb = "inwtran";
$formname = "Different Dia Types";
$file = "diaprint_fr.php";

//$zroot = 'cepheisy_msp_root';
//$fseries = $_SESSION["series_" . $dbname];
$keycond = "";

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

$dia = trim($_POST["dia"]);
//$itemcode = $_POST["itemcode"];
//$itemname = $_POST["itemname"];
//$fdate = $_POST["fdate"];
//$tdate = $_POST["tdate"];
//browse($dia);
//$dtypecond = "";
//$mscopecond = "";
//$mfrmdate = trim($_POST["fdate"]);
//$mtodate = trim($_POST["tdate"]);
//$orderbycond = " ORDER BY `name` ASC";

$diacond = "";

if (!empty($_POST["dia"])) {
    $diacond = " AND grp = '$dia' ";
//    $diacond = " and `name`='$dia' ";
}

//if (empty($_POST["fdate"])) {
//    browse("From Date Cannot Be Empty");
//    header("refresh:1; inweg.php");
//}
//if (empty($_POST["tdate"])) {
//    browse("To Date Cannot Be Empty");
//    header("refresh:1; inwreg.php");
//}

$getpmain = mysqli_query($conn,"select * from `$maindb` where name != '' $diacond  order by grp")or die(mysqli_error($conn));
//print table($getpmain);
//die();

//print "select * from `$maindb` where `code` != '' $diacond ";

if (isset($_POST["view"])) {

    $get = mysqli_num_rows($getpmain);
//    if ($get > 0) {
//        browse(1);
//    } else {
//        browse(0);
//    }
    include("includes/linkshead.php");
//    $mfdt = date("d-m-Y", strtotime($mfrmdate));
//    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                        <!--<thead class="mySearchTr">-->
<!--                            <tr style="background-color: #59CAF7;">
                                <th style="width:25%"> Dia</th>
                                <th style="width:25%"> Shape</th>
                            </tr>-->
                        <!--</thead>-->
                        <tbody>
                            <?php
//                            $ctr = 0;
//                            $mtqty = 0;
//                            $mgtot = 0;
                            $tchar = 0 ; 
                            $col = 0 ;
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $mchar = $pmain[grp] ;
                                if ($mchar != $tchar) {
                                   Echo "<tr>";
                                   $tchar = $mchar ;
                                }   
                                Echo "<td><b>$pmain[name]</b>&nbsp;&nbsp;&nbsp;&nbsp;" . '   ' . "$pmain[shape]</td>";
                                $col++;
                                if ($col > 6) {
                                    Echo "</tr>";
                                    Echo "<tr>";
                                    $col = 0 ;
                                }
                            }
                            Echo "</tr>";

//                            echo "<tr> ";
//                            echo "<td></td>";
//                            echo "<td></td>";
//                            echo "<th class='text-right' style='color: red;'>Grand Total</td>";
//                            echo "<th class='text-right' style='color: red;'>$mtqty</td>";
//                            echo "<th class='text-right' style='color: red;'>$mgtot</td>";
//                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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



if (isset($_POST["excel"])) {


    include 'includes/head_rep_excel.php';

    $contents .= "Dia,Shape\n";

    while ($pmain = mysqli_fetch_array($getpmain)) {

//        $ctr++;
        $pshape = $pmain[shape];
        $pname = $pmain[name];

        $contents .= $pname . ",";
        $contents .= $pshape . "\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;
    }
}
?>