<?php $__env->startSection('content'); ?>
    <?php
    use App\Model\pos\SaleModel;Fun::lang();

    $purchase = \App\Model\stock_inventory\PurchaseModel::count();
    $invoice = SaleModel::join('users', 'users.id', 'nso007_sales.user_id')->where(function ($q) {
        $mybr = myBranch();
        if (!auth()->user()->can('View Sale Request All Branch')) {
            $q->where('nso007_sales.user_id', auth()->id());
        }
        if (!$mybr->main) {
            $q->where('nso007_sales.branch_id', $mybr->id);
        }
    })
        ->where('nso007_sales.status', '!=', 5)
        ->where('payment_status', '!=', 'paid')->count();


    $receipt = SaleModel::join('users', 'users.id', 'nso007_sales.user_id')->where(function ($q) {
        $mybr = myBranch();
        if (!auth()->user()->can('View Sale Request All Branch')) {
            $q->where('nso007_sales.user_id', auth()->id());
        }
        if (!$mybr->main) {
            $q->where('nso007_sales.branch_id', $mybr->id);
        }
    })
        ->where('nso007_sales.status', '!=', 5)
        ->where('payment_status', 'paid')->count();
    $sale_request = SaleModel::where('branch_id', myBranch()->id)->where('is_role', 'out_shop')->count();
    $arr_card = [
        [
            'title' => __('administrator.purchase_item'),
            'value' => $purchase,
            'url' => route('purchases-item.index'),
            'icon' => 'industry',
            'color' => 'primary',
        ],
        [
            'title' => __('administrator.sale_invoice'),
            'value' => $invoice,
            'url' => route('sale-invoice'),
            'icon' => 'credit-card',
            'color' => 'warning',
        ],
        [
            'title' => __('administrator.sale_receipt'),
            'value' => $receipt,
            'url' => route('sale-receipt'),
            'icon' => 'money',
            'color' => 'success',
        ],
        [
            'title' => __('administrator.sale_request'),
            'value' => $sale_request,
            'url' => route('sale-request.index'),
            'icon' => 'reply-all',
            'color' => 'info',
        ],
    ];

    $arr_card_income = [
        [
            'title' => __('administrator.total_income'),
            'url' => route('report-sale-income'),
            'icon' => 'line-chart',
            'color' => 'success',
            'class' => 'set-income',
            'gate' => 'Report Income',
        ],
        [
            'title' => __('administrator.total_expense'),
            'url' => route('report-operation-expense'),
            'icon' => 'money',
            'color' => 'danger',
            'class' => 'set-expense',
            'gate' => 'Report Expense',
        ],
        [
            'title' => __('administrator.total_cross_income'),
            'url' => route('report-cross-income'),
            'icon' => 'arrows-h',
            'color' => 'warning',
            'class' => 'set-cross',
            'gate' => 'Report Cross Income',
        ],
        [
            'title' => __('administrator.total_cost'),
            'url' => route('report.stock-item'),
            'icon' => 'archive',
            'color' => 'primary',
            'gate' => 'Stock Total Cost',
            'class' => 'set-cost',
        ],
        [
            'title' => __('administrator.cash_on_hand'),
            'url' => '#',
            'icon' => 'money',
            'color' => 'success',
            'class' => 'set-cash_on_hand',
            'gate' => 'Report Cross Income',
        ],
    ];
    ?>
    <link rel="stylesheet" href="<?php echo e(asset('assets/chart/demo.css')); ?>">
    <link rel="stylesheet" href="<?php echo e(asset('assets/chart/paper-dashboard.css')); ?>">
    <div class="container-fluid">

        <div class="row">
            <?php $__currentLoopData = $arr_card; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $val): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php
                $gate = @$val['gate'];
                ?>
                <?php if($gate?uCan($gate):1): ?>
                    <div class="col-lg-3 col-md-6 col-sm-6">
                        <div class="card card-stats">
                            <div class="card-body ">
                                <div class="row">
                                    <div class="col-5 col-md-4">
                                        <div class="icon-big text-center icon-warning">
                                            <i class="fa fa-<?php echo e($val['icon']); ?> text-<?php echo e($val['color']); ?>"></i>
                                        </div>
                                    </div>
                                    <div class="col-7 col-md-8">
                                        <div class="numbers">
                                            <p class="card-category"><?php echo e($val['title']); ?></p>
                                            <p class="card-title"><?php echo e($val['value']); ?><p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-footer">
                                <hr>
                                <div class="stats">
                                    <a href="<?php echo e($val['url']); ?>"> <i class="fa fa-arrow-right"></i>
                                        <?php echo e(__('administrator.more_detail')); ?></a>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header">
                        <form class="form_data">
                            <div class="row">
                                <div class="col-lg-3">
                                    <span><?php echo e(__('administrator.from_date')); ?></span>
                                    <?php echo Form::text('from_date',date('01-m-Y'),['class'=>'form-control date_picker']); ?>

                                </div>
                                <div class="col-lg-3">
                                    <span><?php echo e(__('administrator.to_date')); ?></span>
                                    <?php echo Form::text('to_date',date('d-m-Y'),['class'=>'form-control date_picker']); ?>

                                </div>
                                <div class="col-lg-3">
                                    <span><?php echo e(__('administrator.branch')); ?></span>
                                    <?php echo Form::select('branch_id',\App\Model\setup\BranchModel::pluck('name','id')->prepend(__('administrator.please_select'),''),null,['class'=>'form-control']); ?>

                                </div>
                                <div class="col-lg-3">
                                    <span>&nbsp;<br></span>
                                    <button type="button" class="btn-info btn" onclick="getIncome()">
                                        <?php echo e(__('administrator.search')); ?>

                                    </button>
                                </div>
                            </div>
                        </form>
                        <hr>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <?php $__currentLoopData = $arr_card_income; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $val): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                $gate = @$val['gate'];
                                ?>
                                <?php if($gate?uCan($gate):1): ?>
                                    <div class="col-lg-3 col-md-6 col-sm-6">
                                        <div class="card card-stats" style="border: 1px solid rgba(1,1,1,0.1)">
                                            <div class="card-body ">
                                                <div class="row">
                                                    <div class="col-5 col-md-4">
                                                        <div class="icon-big text-center icon-warning">
                                                            <i class="fa fa-<?php echo e($val['icon']); ?> text-<?php echo e($val['color']); ?>"></i>
                                                        </div>
                                                    </div>
                                                    <div class="col-7 col-md-8">
                                                        <div class="numbers">
                                                            <p class="card-category"><?php echo e($val['title']); ?></p>
                                                            <p class="card-title <?php echo e($val['class']); ?>"><i class="fa fa-spinner fa-pulse fa-fw"></i>
                                                            <p>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="card-footer">
                                                <hr>
                                                <div class="stats">
                                                    <a href="<?php echo e($val['url']); ?>"> <i class="fa fa-arrow-right"></i>
                                                        <?php echo e(__('administrator.more_detail')); ?></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4">
                <div class="card ">
                    <div class="card-header ">
                        <h5 class="card-title"><?php echo e(__('administrator.invoice')); ?> & <?php echo e(__('administrator.receipt')); ?></h5>
                    </div>
                    <div class="card-body">
                        <canvas id="chartRINV" height="235"></canvas>
                    </div>
                    <div class="card-footer ">
                        <hr>
                        <div class="stats">
                            <div class="legend">
                                <i class="fa fa-circle" style="color: #f39c12"></i> <?php echo e(__('administrator.invoice')); ?>

                                <i class="fa fa-circle" style="color: #2ecc71"></i> <?php echo e(__('administrator.receipt')); ?>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-8">
                <div class="card card-chart">
                    <div class="card-header">
                        <h5 class="card-title">លក់ដាច់ (<?php echo e(date('Y')); ?>)</h5>
                    </div>
                    <div class="card-body">
                        <canvas id="speedChart" width="400" height="180"></canvas>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script src="<?php echo e(asset('assets/chart/chartjs.min.js')); ?>"></script>
    <script>
        $(document).ready(function () {
            // Javascript method's body can be found in assets/assets-for-demo/js/demo.js
            // demo.initChartsPages();
            inv_rec()
            line_chart()
            getIncome()
            getTotalCost()
        });

        function getIncome() {
            $.get('<?php echo e(route('dashboard-branch.index')); ?>?' + $('.form_data').serialize(), {getIncome: 1}, function (res) {
                let income_text = '', expense_text = '', cross_text = '', cash_on_hand_text = ''
                $.each(res.income, function (i, v) {
                    income_text += addComma(v.toFixed(2)) + ' ' + i + '<br>'
                })
                $.each(res.expense, function (i, v) {
                    expense_text += addComma(v.toFixed(2)) + ' ' + i + '<br>'
                })
                $.each(res.cross_income, function (i, v) {
                    cross_text += addComma(v.toFixed(2)) + ' ' + i + '<br>'
                })
                $.each(res.cash_on_hand, function (i, v) {
                    cash_on_hand_text += addComma(v.toFixed(2)) + ' ' + i + '<br>'
                })

                $('.set-income').html(income_text)
                $('.set-expense').html(expense_text)
                $('.set-cross').html(cross_text)
                $('.set-cash_on_hand').html(cash_on_hand_text)
            })
        }

        function getTotalCost() {
            $.get('<?php echo e(route('dashboard-branch.index')); ?>', {getTotalCost: 1}, function (res) {
                let cost = ''
                if (res.total_cost.length) {
                    let riel = toFloat(res.total_cost[0].total_cost_riel);
                    let usd = toFloat(res.total_cost[0].total_cost_usd);

                    cost += addComma(riel.toFixed(2)) + ' ៛<br>'
                    cost += '<i class="fa fa-exchange" aria-hidden="true"></i> ' + addComma(usd.toFixed(2)) + ' $<br>'
                }
                $('.set-cost').html(cost)
            })
        }

        function inv_rec() {
            ctx = document.getElementById('chartRINV').getContext("2d");
            new Chart(ctx, {
                type: 'pie',
                data: {
                    labels: ['<?php echo e(__('administrator.invoice')); ?>', '<?php echo e(__('administrator.receipt')); ?>'],
                    datasets: [{
                        label: "<?php echo e(__('administrator.invoice')); ?> & <?php echo e(__('administrator.receipt')); ?><",
                        pointRadius: 0,
                        pointHoverRadius: 0,
                        backgroundColor: [
                            '#f39c12',
                            '#2ecc71',
                        ],
                        borderWidth: 1,
                        data: ['<?php echo e($invoice); ?>', '<?php echo e($receipt); ?>']
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    tooltips: {
                        enabled: true
                    },
                }
            });
        }

        function line_chart() {
            let product_arr = []
            let color = ['rgba(231, 76, 60,0.9)', 'rgba(52, 152, 219,0.9)', 'rgba(155, 89, 182,0.9)', 'rgba(26, 188, 156,0.9)', 'rgba(52, 73, 94,0.9)'];
            $.get('<?php echo e(route('dashboard-branch.index')); ?>', {getTopProduct: 1}, function (res) {
                let m_c = 0;

                var speedCanvas = document.getElementById("speedChart");
                let month = [];
                let data_set = [];
                let qty = [];
                $.each(res, function (i, v) {// loop month
                    let p_c = 0;
                    $.each(v, function (ii, vv) { // loop product
                        month[m_c] = vv.month;
                        let pp = vv.product;
                        let p_id = vv.product_id;

                        if (typeof product_arr[p_id] === 'undefined') {
                            product_arr[p_id] = p_c;
                        }
                        if (typeof qty[p_id] === 'undefined') {
                            qty[p_id] = ',' + vv.sale_qty
                        } else {
                            qty[p_id] = qty[p_id] + ',' + vv.sale_qty
                        }
                        let aa = qty[p_id].split(',');
                        aa = aa.filter(function (el) {
                            return el != null && el != "";
                        });


                        let scolor = color[product_arr[p_id]]
                        data_set[p_id] = {
                            label: pp,
                            data: aa,
                            fill: false,
                            borderColor: scolor,
                            backgroundColor: 'transparent',
                            pointBorderColor: scolor,
                            pointRadius: 4,
                            pointHoverRadius: 4,
                            pointBorderWidth: 8
                        }
                        p_c++
                    })
                    m_c++
                })
                data_set = data_set.filter(function (el) {
                    return el != null && el != "";
                });
                var speedData = {
                    labels: month,
                    datasets: data_set
                };

                var chartOptions = {
                    legend: {
                        display: true,
                        position: 'top'
                    }
                };
                new Chart(speedCanvas, {
                    type: 'line',
                    hover: false,
                    data: speedData,
                    options: chartOptions
                });
            })
        }
    </script>
<?php $__env->stopSection(); ?>


<?php echo $__env->make(branchMenu(), \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/administrator/dashboard-administrator.blade.php ENDPATH**/ ?>