<?php

namespace App\Http\Controllers\DataCenter;

use App\Http\Controllers\Controller;
use App\Http\Resources\DailySalesReportResource;
use App\Models\DailySalesReport;
use App\Utils\FormatUtils;
use Illuminate\Http\Request;

class DataCenterController extends Controller
{
    public function salesReport(Request $request)
    {
        $sort = $request->get('sort', 'sales_num');
        $order = $request->get('sortOrder', 'DESC');
        if (empty($request->get('date'))) {
            $request->offsetSet('date', date('Y-m-d'));
        }

        $dailySalesReports = DailySalesReport::query()
            ->filter()
            ->orderBy($sort, $order)
            ->paginate($request->get('per_page'));

        foreach ($dailySalesReports as &$dailySalesReport) {
            $dailySalesReport['stock'] = $dailySalesReport['inventory'] + $dailySalesReport['arrived_today_num'];
            $dailySalesReport['goal_rate'] = FormatUtils::getPercent($dailySalesReport['goal_rate'], 1);
            $dailySalesReport['S1_rate'] = FormatUtils::getPercent($dailySalesReport['S1_rate'], 1);
            $dailySalesReport['S2_rate'] = FormatUtils::getPercent($dailySalesReport['S2_rate'], 1);
            $dailySalesReport['S3_rate'] = FormatUtils::getPercent($dailySalesReport['S3_rate'], 1);
            $dailySalesReport['S4_rate'] = FormatUtils::getPercent($dailySalesReport['S4_rate'], 1);
            $dailySalesReport['S5_rate'] = FormatUtils::getPercent($dailySalesReport['S5_rate'], 1);
            $dailySalesReport['S6_rate'] = FormatUtils::getPercent($dailySalesReport['S6_rate'], 1);
            $dailySalesReport['S7_rate'] = FormatUtils::getPercent($dailySalesReport['S7_rate'], 1);
        }

        return DailySalesReportResource::collection($dailySalesReports);
    }
}