<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Models\Order;
use App\Models\OrderPayment;
use Illuminate\View\View;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class CommissionController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/commissions';
    /** View folder */
    private $viewDirectory= 'admin.commissions.';

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('commissions.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = User::where('role','seller')->orderBy('id','DESC');

        if ($request->name) {
            $query->where('name', 'LIKE', '%'.$request->name.'%');
        }
        if ($request->phone) {
            $query->where('phone', $request->phone);
        }

        if ($request->email) {
            $query->where('email', 'LIKE', '%'.$request->email.'%');
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $commissions= $query->paginate(100);

        return view($this->viewDirectory.'grid',compact('commissions'));
    }
    
    public function edit($id)
    {
        $user = User::where('role','seller')->where('id',$id)->firstOrFail();
        $head = __('commissions.edit') .' : '.$user->name;
        $action= route('commissions.update', $id);
        
        $orders = Order::query()
            ->join('order_payments AS op', 'orders.id', 'op.order_id')
            ->join('cobons AS co', 'op.cobon_id', 'co.id')
            ->select(['op.*','co.*', 'orders.*'])->where('orders.status','recieved');
        
        $show =  Order::query()
            ->join('order_payments AS op', 'orders.id', 'op.order_id')
            ->join('cobons AS co', 'op.cobon_id', 'co.id')
            ->select(['op.*','co.*', 'orders.*'])->where('orders.status','recieved')->where('op.seller_payment_status','not')->first();
        
        $orders = $orders->get();
            
        return view($this->viewDirectory.'form',compact('orders','user','action','show','head'));
    }
    public function update(Request $request, $id)
    {
        $user = User::where('role','seller')->where('id',$id)->firstOrFail();
        $orders = Order::query()
            ->join('order_payments AS op', 'orders.id', 'op.order_id')
            ->join('cobons AS co', 'op.cobon_id', 'co.id')
            ->select(['op.*','co.*', 'orders.*'])->get();
        
        foreach($orders as $row)
        {
            $order = OrderPayment::where('order_id',$row->id)->first();
            $order->update(['seller_payment_status'=>'paid']);
        }
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

}
