<?php


namespace Mehrab\Dashboard\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Mehrab\OrderService\Repositories\OrderServiceRepo;
use Mehrab\Payment\Repositories\PaymentRepo;
use Mehrab\RolePermission\Models\Permission;
use Mehrab\Sms\Services\SmsService;
use Mehrab\User\Helpers\SmsVerifyMethod;
use SanjabVerify\Support\Facades\Verify;
class DashboardController extends Controller
{
    public $paymentRepo;
    public $orderServiceRepo;

    public function __construct(PaymentRepo $paymentRepo, OrderServiceRepo $orderServiceRepo)
    {
        $this->paymentRepo = $paymentRepo;
        $this->orderServiceRepo = $orderServiceRepo;
    }

    public function index()
    {
       // $result = SmsService::ultra('register', [322465], "09202005156");
       // dd($result);

        $user_id = auth()->id();

        if (auth()->user()->hasPermissionTo(Permission::PERMISSION_SUPER_ADMIN)) {
            $all_orders = $this->orderServiceRepo->getAllCount();
            $past_7_days_orders = $this->orderServiceRepo->getOrderServiceByPeriod(now(), now()->addDays(-7));
            $past_30_days_orders = $this->orderServiceRepo->getOrderServiceByPeriod(now(), now()->addDays(-30));
            $get_Orders_today = $this->orderServiceRepo->getOrdersByDays(now());

            $datesOrder = collect();
            foreach (range(-30, 0) as $i) {
                $datesOrder->put(now()->addDays($i)->format('Y-m-d'), 0);
            }
            $summeryOrderService = $this->orderServiceRepo->getDailySummery($datesOrder, $user_id);
        } else {
            $datesOrder = $summeryOrderService = $all_orders = $past_7_days_orders = $past_30_days_orders = $get_Orders_today = 0;
        }

        if (auth()->user()->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_OPERATOR)) {
            $all_orders_active_operator = $this->orderServiceRepo->getAllActiveOperatorCount($user_id);
            $all_orders_cancel_request_operator = $this->orderServiceRepo->allOrdersCancelRequestOperator($user_id);
            $all_orders_final_approved_cancel_operator = $this->orderServiceRepo->getAllActiveOperatorNotFinalApprovedCount($user_id);
            $all_orders_operator = $this->orderServiceRepo->all_orders_operator($user_id);
        } else {
            $all_orders_active_operator = $all_orders_cancel_request_operator = $all_orders_final_approved_cancel_operator = $all_orders_operator = 0;
        }

        if (auth()->user()->hasPermissionTo(Permission::PERMISSION_TEAM_LEADER)) {
            $all_orders_active_teamLeader = $this->orderServiceRepo->getAllActiveTeamLeaderCount($user_id);
            $all_orders_cancel_request_teamLeader = $this->orderServiceRepo->allOrdersCancelRequestTeamLeader($user_id);
            $all_orders_in_queue_teamLeader = $this->orderServiceRepo->getAllOrdersInQueueTeamLeader($user_id);
            $all_orders_teamLeader = $this->orderServiceRepo->all_orders_team_leader($user_id);
        } else {
            $all_orders_cancel_request_teamLeader = $all_orders_active_teamLeader = $all_orders_in_queue_teamLeader = $all_orders_teamLeader = 0;
        }

        if (auth()->user()->hasPermissionTo(Permission::PERMISSION_EXPERT)) {
            $total_sales = $this->paymentRepo->getUserTotalSuccessPayment($user_id);
            $total_sales_pure_today = $this->paymentRepo->getUserTotalSuccessPaymentByDays($user_id, now());
            $total_sales_today_count = $this->paymentRepo->getUserTotalSuccessPaymentCountByDay($user_id, now());
            $total_sales_today = $this->paymentRepo->getUserTotalSuccessAmountPaymentByDay($user_id, now());
            $total_sales_pure_period = $this->paymentRepo->getUserTotalSuccessPaymentByPeriod($user_id, now(), now()->addDays(-30));
            $total_sales_Pure = $this->paymentRepo->getUserTotalSuccessPaymentPure($user_id);
            $total_sales_site_share = $this->paymentRepo->getUserTotalSuccessPaymentSiteShare($user_id);

//        $lastDaysTotal = $this->paymentRepo->lastDaysTotal(Payment::STATUS_SUCCESS);
//        $lastDaysTotalPure = $this->paymentRepo->lastDaysTotalPure(Payment::STATUS_SUCCESS);
//        $last30Days = CarbonPeriod::create(now()->addDays(-30),now());

            $all_orders_active_teamLeader = $this->orderServiceRepo->getAllActiveExpertCount($user_id);
            $all_orders_cancel_request_teamLeader = $this->orderServiceRepo->allOrdersCancelRequestExpert($user_id);
            $all_orders_in_queue_teamLeader = $this->orderServiceRepo->getAllOrdersInQueueExpert($user_id);
            $all_orders_teamLeader = $this->orderServiceRepo->all_orders_Expert($user_id);

            $dates = collect();
            foreach (range(-30, 0) as $i) {
                $dates->put(now()->addDays($i)->format('Y-m-d'), 0);
            }
            $summery = $this->paymentRepo->getDailySummery($dates, $user_id);
        } else {
            $dates = $summery = $total_sales = $total_sales_pure_today = $total_sales_today_count = $total_sales_today = $total_sales_pure_period = $total_sales_Pure = $total_sales_site_share = 0;
        }

        return view('Dashboard::Admin.dashboard', compact(
            'user_id',
            'total_sales',
            'total_sales_Pure',
            'total_sales_site_share',
            'total_sales_pure_today',
            'total_sales_pure_period',
            'total_sales_today_count',
            'total_sales_today',
            'summery',
            'dates',
            'all_orders',
            'past_7_days_orders',
            'past_30_days_orders',
            'get_Orders_today',
            'summeryOrderService',
            'datesOrder',
            'all_orders_active_operator',
            'all_orders_cancel_request_operator',
            'all_orders_final_approved_cancel_operator',
            'all_orders_operator',
            'all_orders_cancel_request_teamLeader',
            'all_orders_active_teamLeader',
            'all_orders_in_queue_teamLeader',
            'all_orders_teamLeader',

        ));
    }
}
