@extends('backend.app_template')
@section('title','Dashboard')
@section('content')
<main class="app-wrapper">
    <div class="container-fluid">

        <!-- <div class="row mt-5">
            <div class="col-md-6 col-xl-4 col-xxl-3">
                <div class="card card-h-100 webGradient text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between gap-5 mb-5">
                            <div>
                                <h4 class="text-white mb-1">Web Request</h4>
                            </div>
                            <div class="flex-shrink-0">
                                <div
                                    class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold"><?= isset($reqCont) ? $reqCont : 0 ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-xl-4 col-xxl-3">
                <div class="card card-h-100 webGradient1 text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between gap-5 mb-5">
                            <div>
                                <h4 class="text-white mb-1">Total Advertisement</h4>
                            </div>
                            <div class="flex-shrink-0">
                                <div
                                    class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold"><?= isset($adCount) ? $adCount : 0 ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-xl-4 col-xxl-3">
                <div class="card card-h-100 webGradient2 text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between gap-5 mb-5">
                            <div>
                                <h4 class="text-white mb-1">Total Vendor</h4>
                            </div>
                            <div class="flex-shrink-0">
                                <div
                                    class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold"><?= isset($vendorCount) ? $vendorCount : 0 ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-xl-4 col-xxl-3">
                <div class="card card-h-100 IntelligenceGradient text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between gap-5 mb-5">
                            <div>
                                <h4 class="text-white mb-1">Accepted</h4>
                            </div>
                            <div class="flex-shrink-0">
                                <div
                                    class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold">
                                    <?= isset($data->accept) ? $data->accept : 0 ?></div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-xl-4 col-xxl-3">
                <div class="card card-h-100 datascienceGradient text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between gap-5 mb-5">
                            <div>
                                <h4 class="text-white mb-1">Decline</h4>

                            </div>
                            <div class="flex-shrink-0">
                                <div
                                    class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold">
                                    <?= isset($data->decline) ? $data->decline : 0 ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0"><i class="bi bi-question-circle"></i>&nbsp;&nbsp;Queries</h5>
                </div>
                <div class="card-body">
                    <div class="row mt-5">
                        <div class="col-md-6 col-xl-4 col-xxl-3">
                            <div class="card card-h-100 webGradient3 text-white">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between gap-5 mb-5">
                                        <div>
                                            <h4 class="text-white mb-1">Total Queries</h4>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <div
                                                class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold"><?= isset($support->total) ? $support->total : 0 ?></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6 col-xl-4 col-xxl-3">
                            <div class="card card-h-100 webGradient4 text-white">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between gap-5 mb-5">
                                        <div>
                                            <h4 class="text-white mb-1">Total Under Review</h4>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <div
                                                class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold"><?= isset($support->review) ? $support->review : 0 ?></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6 col-xl-4 col-xxl-3">
                            <div class="card card-h-100 webGradient5 text-white">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between gap-5 mb-5">
                                        <div>
                                            <h4 class="text-white mb-1">Total Resolved</h4>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <div
                                                class="h-48px w-48px bg-white fs-5 rounded d-flex justify-content-center align-items-center text-black fw-semibold"><?= isset($support->resolve) ? $support->resolve : 0 ?></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>



                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <h5 class="card-title mb-0">Location wise Web Request</h5>
                            </div>
                            <div class="card-body">
                                <div id="location-chart"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <h5 class="card-title mb-0">Service Wise</h5>
                            </div>
                            <div class="card-body">
                                <div id="sales_managment"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xxl-7">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <h5 class="card-title mb-0">User Analytics</h5>
                            </div>
                            <div class="card-body">
                                <div id="userChart"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xxl-7">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <h5 class="card-title mb-0">Total Web Request</h5>
                            </div>
                            <div class="card-body">
                                <div id="chart"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
        </div> -->

    </div>
</main>

<!-- <script>
    var statusCont = '<?php echo isset($monthChart) ? json_encode($monthChart) : 0 ?>';

    if (statusCont.length > 0) {
        var rows = JSON.parse(statusCont);
        var cont = [];
        var sts = [];
        rows.forEach(function(i) {
            cont.push(i.cont);
            sts.push(i.month);
        })

        var options = {
            series: [{
                name: 'Request',
                data: cont
            }],
            chart: {
                height: 320,
                type: 'bar',
                toolbar: {
                    show: false
                },
            },
            plotOptions: {
                bar: {
                    columnWidth: '35%',
                    borderRadius: 8,
                }
            },
            dataLabels: {
                enabled: false
            },
            legend: {
                show: false
            },
            xaxis: {
                categories: sts,
                labels: {
                    style: {
                        fontSize: '12px'
                    }
                }
            },
            fill: {
                type: 'gradient',
                gradient: {
                    shade: 'light',
                    type: 'vertical',
                    shadeIntensity: 0.4,
                    gradientToColors: ['#ffcc80', '#ffe0b2'],
                    inverseColors: false,
                    opacityFrom: 1,
                    opacityTo: 1,
                    stops: [0, 100]
                }
            },
            colors: ['#ffb74d']
        };

        var chart = new ApexCharts(document.querySelector("#chart"), options);
        chart.render();
    }


    var pieChartData = '<?php echo isset($pieChart) ? json_encode($pieChart) : 0 ?>';
    if (pieChartData.length > 0) {
        var rows = JSON.parse(pieChartData);
        var cont1 = [];
        var service = [];
        rows.forEach(function(i) {
            cont1.push(parseInt(i.cont));
            service.push(i.service_type);
        })
        var options = {
            series: cont1,
            chart: {
                width: 380,
                type: 'pie',
            },
            labels: service,
            responsive: [{
                breakpoint: 480,
                options: {
                    chart: {
                        width: 300,
                        height: 300
                    },
                    legend: {
                        position: 'bottom'
                    }
                }
            }]
        };

        var chart = new ApexCharts(document.querySelector("#sales_managment"), options);
        chart.render();
    }

    userAnalytics();

    function userAnalytics() {

    var requestCont = <?php echo isset($monthChart) ? json_encode($monthChart) : '[]'; ?>;
    var adCont      = <?php echo isset($adChart) ? json_encode($adChart) : '[]'; ?>;
    var vendorCont  = <?php echo isset($usersChart) ? json_encode($usersChart) : '[]'; ?>;

    let months = [];
    let cont1 = {}, cont2 = {}, cont3 = {};

    // Build maps
    requestCont.forEach(i => {
        months.push(i.month);
        cont1[i.month] = parseInt(i.cont);
    });

    adCont.forEach(i => {
        cont2[i.month] = parseInt(i.cont);
        if (!months.includes(i.month)) months.push(i.month);
    });

    vendorCont.forEach(i => {
        cont3[i.month] = parseInt(i.cont);
        if (!months.includes(i.month)) months.push(i.month);
    });

    // Sort months (optional if format is YYYY-MM)
    months.sort();

    // Align data
    let reqData = [];
    let adData = [];
    let vendorData = [];

    months.forEach(m => {
        reqData.push(cont1[m] ?? 0);
        adData.push(cont2[m] ?? 0);
        vendorData.push(cont3[m] ?? 0);
    });

    var options = {
        series: [
            { name: 'Total Requests', data: reqData },
            { name: 'Total Advertisement', data: adData },
            { name: 'Total Vendors', data: vendorData }
        ],
        chart: {
            height: 350,
            type: 'area'
        },
        dataLabels: { enabled: false },
        stroke: { curve: 'smooth' },
        xaxis: {
            categories: months
        },
        yaxis: {
            title: { text: 'Count' }
        },
        tooltip: { shared: true }
    };

    var chart = new ApexCharts(document.querySelector("#userChart"), options);
    chart.render();
}


    


      



    var locationChart = '<?php echo isset($locationChart) ? json_encode($locationChart) : "[]"; ?>';

    if (locationChart !== "[]" && locationChart.length > 2) {

        var rows = JSON.parse(locationChart);
        var locData = [];
        rows.forEach(function(i) {
            locData.push({
                x: i.location,
                y: i.cont
            });
        });
        var options = {
            series: [{
                data: locData
            }],
            chart: {
                height: 350,
                type: 'treemap'
            },
            legend: {
                show: false
            },
            title: {
                text: 'Location Request'
            }
        };
        var chart = new ApexCharts(document.querySelector("#location-chart"), options);
        chart.render();
    }
</script> -->
@endsection