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

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$zdb = $_SESSION["zdbyear_" . $dbname];

$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);
/* =====================================================
   DATE FILTER
===================================================== */

$mfrmdate = isset($_GET['frmdate']) ? $_GET['frmdate'] : date("Y-m-d");
$mtodate = isset($_GET['todate']) ? $_GET['todate'] : date("Y-m-d");


/* =====================================================
   STOCK TOTAL
===================================================== */

$qStock = mysqli_query($conn, "
    SELECT 
        SUM(amount) totalamount,
        SUM(qty) totalqty
    FROM stockent 
    WHERE vouchdt >= '$mfrmdate'
    AND vouchdt <= '$mtodate'
");

$rStock = mysqli_fetch_assoc($qStock);

$stockAmount = $rStock['totalamount'] ?: 0;
$stockQty = $rStock['totalqty'] ?: 0;


/* =====================================================
   BANK TOTAL + SAT/WED
===================================================== */

$qBank = mysqli_query($conn, "
    SELECT 
        `group`,
        SUM(amount) totalamount

    FROM bankent

    WHERE vouchdt >= '$mfrmdate'
    AND vouchdt <= '$mtodate'

    GROUP BY `group`
");

$bankAmount = 0;

$satAmount = 0;
$wedAmount = 0;

while ($rBank = mysqli_fetch_assoc($qBank)) {

    $grp = $rBank['group'];
    $amt = $rBank['totalamount'];

    $bankAmount += $amt;

    /* SATURDAY = GROUP 2 */
    if ($grp == '2') {
        $satAmount += $amt;
    }

    /* WEDNESDAY = GROUP 3 */
    if ($grp == '3') {
        $wedAmount += $amt;
    }
}


/* =====================================================
   ACCESSORIES TOTAL
===================================================== */

/* =====================================================
   ACCESSORY / FABRIC TOTAL
===================================================== */

$qAcc = mysqli_query($conn, "
    SELECT 

        rm.type,

        SUM(rt.amount) totalamount

    FROM rawtran rt

    LEFT JOIN rawmat rm
        ON rm.code = rt.item

    LEFT JOIN rawmain rmain
        ON rmain.vouchno = rt.vouchno

    WHERE rt.vouchdt >= '$mfrmdate'
    AND rt.vouchdt <= '$mtodate' AND rmain.shop != '3'

    GROUP BY rm.type
");

$accAmount = 0;
$fabricAmount = 0;
$totalAccessory = 0;

while ($rAcc = mysqli_fetch_assoc($qAcc)) {

    $type = $rAcc['type'];
    $amt = $rAcc['totalamount'];

    $totalAccessory += $amt;

    /* ACCESSORY */
    if ($type == 'A') {

        $accAmount += $amt;
    }

    /* FABRIC */
    if ($type == 'F') {

        $fabricAmount += $amt;
    }
}


/* =====================================================
   PURCHASE ACCESSORY / FABRIC TOTAL
===================================================== */

$qPurchase = mysqli_query($conn, "
    SELECT 

        rm.type,

        SUM(pt.amount) totalamount

    FROM purrawtran pt

    LEFT JOIN rawmat rm
        ON rm.code = pt.item

    LEFT JOIN purrawmain pm
        ON pm.vouchno = pt.vouchno

    WHERE pt.vouchdt >= '$mfrmdate'
    AND pt.vouchdt <= '$mtodate'

    GROUP BY rm.type
");

$purchaseAmount = 0;

$purchaseAccessory = 0;
$purchaseFabric = 0;

while ($rPurchase = mysqli_fetch_assoc($qPurchase)) {

    $type = $rPurchase['type'];
    $amt = $rPurchase['totalamount'];

    $purchaseAmount += $amt;

    /* ACCESSORY */
    if ($type == 'A') {

        $purchaseAccessory += $amt;
    }

    /* FABRIC */
    if ($type == 'F') {

        $purchaseFabric += $amt;
    }
}


/* =====================================================
   CASH / CREDIT COUNTER
===================================================== */

$qCash = mysqli_query($conn, "SELECT SUM(netamt) totalcash FROM rawmain 
                                    WHERE party = '49' AND cashamt > 0 AND vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate'");

$rCash = mysqli_fetch_assoc($qCash);

$qCredit = mysqli_query($conn, "SELECT SUM(netamt) totalcredit  FROM rawmain 
                                      WHERE party = '49' AND credamt > 0 AND vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate'");
$rCredit = mysqli_fetch_assoc($qCredit);

$qBank = mysqli_query($conn, "SELECT SUM(netamt) totalbank  FROM rawmain 
                                      WHERE party = '49' AND ccamt > 0 AND vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate'");
$rBank = mysqli_fetch_assoc($qBank);

$cashAmount = $rCash['totalcash'] ?: 0;

$creditAmount = $rCredit['totalcredit'] ?: 0;

$CCAmount = $rBank['totalbank'] ?: 0;

/* TOTAL COUNTER */

$totalCounter = $cashAmount + $creditAmount + $CCAmount;


/* =====================================================
   STOCK TOTAL WITH ACCESSORY / FABRIC ONLY
===================================================== */

$qStock = mysqli_query($conn, "
    SELECT 

        rm.code,
        rm.type,
        rm.cost,

        SUM(t.qty) totalqty

    FROM rawsacn t

    LEFT JOIN rawmat rm
        ON rm.code = t.itemcode

    GROUP BY t.itemcode,t.color
    
");

// WHERE  rm.type IN ('A','F','O')

// GROUP BY rm.type

$totalstockQty = 0;
$totalstockAmt = 0;
$accQty = 0;
$fabQty = 0;
$othQty = 0;

$accQtyAmt = 0;
$fabQtyAmt = 0;
$othQtyAmt = 0;
// print table($qStock);

while ($rStock = mysqli_fetch_assoc($qStock)) {

    $type = $rStock['type'];
    $cost = $rStock['cost'];
    $qty = $rStock['totalqty'];
    $icode = $rStock['code'];
    
    if ($cost > 0) {
        $actcost = $cost;
    } else {
        $getpur = mysqli_query($conn, "SELECT `rate` FROM `rawtran` WHERE `item`='$icode' and rate > 0 ") or die(mysqli_error($conn));
        $chkpurrate = mysqli_fetch_array($getpur);
        $actcost = $chkpurrate['rate'];
    }
    $newcost = $actcost;



    /* ACCESSORY */
    if ($type == 'A') {
        $accQtyAmt += $qty * $newcost;
    }

    /* FABRIC */ elseif ($type == 'F') {
        $fabQtyAmt += $qty * $newcost;
    }
    /* OTHER */ elseif ($type == '' || $type == 'O') {
        $othQtyAmt += $qty * $newcost;
    }

    /* GRAND TOTAL */
    $totalstockAmt = $accQtyAmt + $fabQtyAmt + $othQtyAmt;
}
?>


<!-- DASHBOARD AREA -->
<link href="https://cdn.jsdelivr.net/npm/remixicon/fonts/remixicon.css" rel="stylesheet">

<style>
    /* =====================================================
   MODERN COMPACT ERP DASHBOARD
===================================================== */

    body {
        background: #f4f6fb;
        /* font-family: 'Segoe UI', sans-serif; */
        font-family: 'Arial Narrow';
        margin: 0;
        padding: 0;
    }

    /* =====================================================
   WRAPPER
===================================================== */

    .dashboard-wrapper {
        width: 100%;
        padding: 16px;
        display: flex;
        justify-content: center;
        box-sizing: border-box;
    }

    /* =====================================================
   GRID
===================================================== */

    .dashboard-grid {

        width: 100%;
        max-width: 980px;

        display: grid;

        /* 3 CARDS IN ROW */
        grid-template-columns: repeat(3, 300px);

        justify-content: center;

        gap: 14px;
    }

    /* =====================================================
   CARD
===================================================== */

    .dash-card {

        background: #fff;

        border-radius: 16px;

        overflow: hidden;

        box-shadow:
            0 4px 14px rgba(0, 0, 0, 0.06);

        transition: .25s ease;

        min-width: 0;
    }

    .dash-card:hover {

        transform: translateY(-3px);

        box-shadow:
            0 10px 24px rgba(0, 0, 0, 0.10);
    }

    /* =====================================================
   HEADER
===================================================== */

    .card-head {

        padding: 10px 14px;

        color: #fff;

        font-size: 15px;
        font-weight: 700;

        display: flex;
        align-items: center;
        justify-content: center;

        gap: 8px;

        letter-spacing: .3px;
    }

    .card-head i {
        font-size: 17px;
    }

    /* =====================================================
   COLORS
===================================================== */

    .purple {
        background: linear-gradient(135deg, #c86dd7, #3023ae);
    }

    .blue {
        background: linear-gradient(135deg, #396afc, #2948ff);
    }

    .green {
        background: linear-gradient(135deg, #11998e, #38ef7d);
    }

    .dark {
        background: linear-gradient(135deg, #434343, #000000);
    }

    .sky {
        background: linear-gradient(135deg, #36d1dc, #5b86e5);
    }

    /* =====================================================
   BODY
===================================================== */

    .card-body {

        padding: 14px 10px;

        text-align: center;
    }

    .card-value {

        font-size: 30px;

        font-weight: 800;

        color: #111;

        line-height: 1.15;

        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .card-sub {

        margin-top: 2px;

        color: #777;

        font-size: 12px;

        font-weight: 600;
    }

    /* =====================================================
   FOOTER
===================================================== */

    .card-footer {

        background: #fafafa;

        border-top: 1px solid #eee;

        padding: 8px 12px;
    }

    .footer-row {

        display: flex;

        justify-content: space-between;

        align-items: center;

        font-size: 13px;

        font-weight: 600;

        margin-bottom: 5px;

        color: #333;
    }

    .footer-row:last-child {
        margin-bottom: 0;
    }

    /* =====================================================
   TABLET
===================================================== */

    @media(max-width:992px) {

        .dashboard-grid {

            grid-template-columns:
                repeat(2, 280px);
        }

        .card-value {
            font-size: 26px;
        }
    }

    /* =====================================================
   MOBILE
===================================================== */

    @media(max-width:650px) {

        .dashboard-wrapper {
            padding: 10px;
        }

        .dashboard-grid {

            grid-template-columns: 1fr;

            max-width: 350px;

            gap: 12px;
        }

        .card-head {
            font-size: 14px;
        }

        .card-head i {
            font-size: 15px;
        }

        .card-value {
            font-size: 24px;
        }

        .footer-row {
            font-size: 12px;
        }
    }

    .qty-text {
        font-size: 13px;
        font-weight: 600;
        color: #777;
    }

    /* =====================================================
   FILTER
===================================================== */

    .filter-form {
        display: flex;
        justify-content: center;
        margin: 10px 0 18px;
    }

    .filter-box {

        background: #fff;

        padding: 12px 16px;

        border-radius: 14px;

        display: flex;
        align-items: end;
        gap: 14px;

        box-shadow:
            0 4px 14px rgba(0, 0, 0, 0.06);
    }

    .filter-group {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }

    .filter-group label {
        font-size: 12px;
        font-weight: 700;
        color: #555;
    }

    .filter-group input {

        height: 38px;

        border: 1px solid #ddd;

        border-radius: 10px;

        padding: 0 10px;

        font-size: 13px;

        outline: none;
    }

    .filter-box button {

        height: 38px;

        border: none;

        background: linear-gradient(135deg, #396afc, #2948ff);

        color: #fff;

        border-radius: 10px;

        padding: 0 18px;

        font-size: 13px;
        font-weight: 700;

        cursor: pointer;

        display: flex;
        align-items: center;
        gap: 6px;
    }

    .filter-box button:hover {
        opacity: .9;
    }

    /* MOBILE */

    @media(max-width:650px) {

        .filter-box {

            width: 100%;

            flex-direction: column;

            align-items: stretch;
        }

        .filter-box button {
            justify-content: center;
        }
    }

    .reset-btn {

        height: 38px;

        padding: 0 18px;

        background: #f1f3f6;

        color: #333;

        border-radius: 10px;

        text-decoration: none;

        font-size: 13px;
        font-weight: 700;

        display: flex;
        align-items: center;
        gap: 6px;

        border: 1px solid #ddd;

        transition: .2s ease;
    }

    .reset-btn:hover {

        background: #e5e7eb;

        color: #000;
    }
</style>

<!-- =====================================================
     DATE FILTER
===================================================== -->

<form method="GET" class="filter-form">

    <div class="filter-box">

        <div class="filter-group">
            <label>From Date</label>

            <input type="date" name="frmdate" value="<?php echo $mfrmdate; ?>">
        </div>

        <div class="filter-group">
            <label>To Date</label>

            <input type="date" name="todate" value="<?php echo $mtodate; ?>">
        </div>

        <div style="display:flex; gap:10px; align-items:end;">

            <button type="submit">
                <i class="ri-filter-3-line"></i>
                Filter
            </button>

            <a href="dashboard.php" class="reset-btn">
                <i class="ri-refresh-line"></i>
                Reset
            </a>

        </div>

    </div>

</form>

<div class="dashboard-wrapper">

    <div class="dashboard-grid">

        <!-- CARD -->
        <div class="dash-card">

            <div class="card-head purple">
                <i class="ri-store-2-line"></i>
                STOCK
            </div>

            <div class="card-body">
                <div class="card-value">
                    <?= $stockQty ?>
                    <span class="qty-text">(Qty.)</span>
                </div>
            </div>

            <div class="card-footer">
                <div class="footer-row">
                    <span>Amount</span>
                    <span><?= number_format($stockAmount) ?></span>
                </div>
                <div class="footer-row">
                    <span>&nbsp;</span>
                    <span>&nbsp;</span>
                </div>
            </div>

        </div>

        <!-- CARD -->
        <div class="dash-card">

            <div class="card-head dark">
                <i class="ri-bank-line"></i>
                BANK AMOUNT
            </div>

            <div class="card-body">
                <div class="card-value"> <?= number_format($bankAmount) ?></div>
            </div>

            <div class="card-footer">
                <div class="footer-row">
                    <span>Saturday</span>
                    <span><?= number_format($satAmount) ?></span>
                </div>
                <div class="footer-row">
                    <span>Wednesday</span>
                    <span><?= number_format($wedAmount) ?></span>
                </div>
            </div>

        </div>

        <!-- CARD -->
        <div class="dash-card">

            <div class="card-head blue">
                <i class="ri-handbag-line"></i>
                ACCESSORIES
            </div>

            <div class="card-body">
                <div class="card-value"><?php echo number_format($totalAccessory); ?></div>
            </div>

            <div class="card-footer">
                <div class="footer-row">
                    <span>Accessory</span>
                    <span><?php echo number_format($accAmount); ?></span>
                </div>

                <div class="footer-row">
                    <span>Fabric</span>
                    <span><?php echo number_format($fabricAmount); ?></span>
                </div>
            </div>

        </div>

        <!-- CARD -->
        <div class="dash-card">

            <div class="card-head sky">
                <i class="ri-shopping-cart-line"></i>
                PURCHASE
            </div>

            <div class="card-body">
                <div class="card-value"><?php echo number_format($purchaseAmount); ?></div>
            </div>

            <div class="card-footer">
                <div class="footer-row">
                    <span>Accessory</span>
                    <span><?php echo number_format($purchaseAccessory); ?></span>
                </div>

                <div class="footer-row">
                    <span>Fabric</span>
                    <span><?php echo number_format($purchaseFabric); ?></span>
                </div>
            </div>

        </div>

        <!-- CARD -->
        <div class="dash-card">

            <div class="card-head green">
                <i class="ri-money-rupee-circle-line"></i>
                CASH COUNTER
            </div>

            <div class="card-body">
                <div class="card-value"><?= number_format($totalCounter, 2); ?></div>
            </div>

            <div class="card-footer">
                <div class="footer-row">
                    <span>Cash</span>
                    <span><?= number_format($cashAmount, 2); ?></span>
                </div>

                <div class="footer-row">
                    <span>Credit</span>
                    <span><?= number_format($creditAmount, 2); ?></span>
                </div>

                <div class="footer-row">
                    <span>CC</span>
                    <span><?= number_format($CCAmount, 2); ?></span>
                </div>
            </div>

        </div>

        <!-- CARD -->
        <div class="dash-card">

            <div class="card-head purple">
                <i class="ri-bar-chart-box-line"></i>
                TOTAL STOCK
            </div>

            <div class="card-body">
                <div class="card-value">
                    <?= number_format($totalstockAmt, 2) ?>
                </div>
            </div>

            <div class="card-footer">
                <div class="footer-row">
                    <span>Accessory</span>
                    <span><?= number_format($accQtyAmt, 2) ?></span>
                </div>

                <div class="footer-row">
                    <span>Fabric</span>
                    <span><?= number_format($fabQtyAmt, 2) ?></span>
                </div>
                <div class="footer-row">
                    <span>Other</span>
                    <span><?= number_format($othQtyAmt, 2) ?></span>
                </div>
            </div>

        </div>

    </div>

</div>