<?php
require_once './connection.php';

if (!isset($_POST['start_date_month']) == "" && !isset($_POST['end_date_month']) == "") {
    $start_bill_date = $_POST['start_date_month'];
    $start_bill_date = $start_bill_date . ' 00:00:05';
    $end_bill_date = $_POST['end_date_month'];
    $end_bill_date = $end_bill_date . ' 23:50:05';

    $header_date = $_POST['header_date'];
    ?>
    <div class="tab-pane tabs-animation fade active show" id="tab-content-0" role="tabpanel">

        <div class="main-card mb-3 card">
            <h5 class="card-title" style="float: none;text-align: center;margin-top: 10px">વેચાણની માહીતી</h5>

            <div class="card-body" id="broket_div" tabindex="7">
                <div class="col-md-4" style="margin-bottom: 10px">
                    <!--                                    <label for="companay_name" class="col1">દુધ લેવાનો સમય</label>
                                                        <select name="select" id="milk_take_time_search" tabindex="9" class="form-control">
                                                            <option name="MORNING" selected="" id="MORNING"> MORNING </option>
                                                            <option name="EVENING" id="EVENING"> EVENING </option>
                                                            <option name="MORNING & EVENING" id="MORNING/EVENING"> MORNING & EVENING </option>
                                                        </select>-->
                    <div id="msg_milk_take_time_search"></div>
                </div>
                <table class="mb-0 table table-hover">
                    <thead style="text-align: center">
                        <tr>
                            <th>#</th>
                            <th>ગ્રાહકનું નામ</th>
                            <th>મોબાઇલ નંબર</th>
    <!--                            <th>સરનામું</th>
                            <th>Customer Type</th>-->
                            <th>Total Sell Amount</th>
                            <th>Product Details</th>
                            <th>Sell Date</th>
                        </tr>
                    </thead>
                    <tbody id="table_body">
                        <?php
                        $query = 'SELECT * FROM tbl_sell_product where flag="1" and sell_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" ORDER BY id asc';
//                        echo '$query '.$query;
                        mysqli_set_charset($con, 'utf8');
                        $result = mysqli_query($con, $query);
                        $total_sell = 0;
                        if (mysqli_num_rows($result) > 0) {
                            $i = 1;
                            while ($row = mysqli_fetch_assoc($result)) {
                                $sell_id = $row["id"];
                                $query1 = 'SELECT tsl.*,tp.name FROM tbl_sell_list as tsl LEFT join tbl_product as tp on tsl.product_id=tp.id where tsl.flag="1" and tsl.sell_id=' . $sell_id;
//                                                    echo '$query1 '.$query1;
                                mysqli_set_charset($con, 'utf8');
                                $result12 = mysqli_query($con, $query1);

                                $html_data = "";
                                if (mysqli_num_rows($result12) > 0) {
                                    $j = 1;
                                    while ($row12 = mysqli_fetch_assoc($result12)) {
                                        $html_data .= $j . '. ' . $row12["name"] . '  ' . $row12["price"] . '*' . $row12["quantity"] . '=' . $row12["total"] . '<br>';
                                        $j = $j + 1;
                                    }
                                }
                                $total_sell = $total_sell + $row["sell_total_price"];
                                ?>
                                <tr style="text-align: center">
                                    <td scope="row"><?php echo $i; ?></td>
                                    <td><?php echo $row["cust_name"]; ?></td>
                                    <td><?php echo $row["mobile_no"]; ?></td>
                                    <td><?php echo $row["sell_total_price"]; ?></td>
                                    <td>
                                        <?php echo $html_data; ?>
                                        <?php echo 'Shipping Cost - ' . $row["ship_cost"]; ?>
                                    </td>
                                    <td><?php
                                        $orgDate = $row["sell_date"];
//                                                            $newDate = date("d-m-Y H:i:s", strtotime($orgDate));
                                        $newDate = date("d-m-Y", strtotime($orgDate));
                                        echo $newDate;
//                                                            echo $row["sell_date"]; 
                                        ?>
                                    </td>
                                </tr>
                                <?php
                                $i = $i + 1;
                            }
                            ?>
                            <tr style="text-align: center">
                                <td scope="row"></td>
                                <td></td>
                                <td><b>Total Sell</b></td>
                                <td><b><?php echo $total_sell; ?></b></td>
                                <td></td>
                                <td></td>
                            </tr>
                            <?php
                        } else {
                            ?>
                            <tr>
                                <td>0 results</td>
                            </tr>
                            <?php
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <?php
}
?>