<?php

namespace App\Http\Controllers\report;

use Auth;
use App\Fun\Fun;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\pos\SaleModel;
use App\User;
use App\Model\people\DriverModel;
use Yajra\DataTables\DataTables;

class ReportDeliveryController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:Report Delivery List', ['only' => ['index']]);
        Fun::lang();
    }
    public function index(Request $request)
    {
        $user = SaleModel::where('delivery_status', 1)
        ->pluck('user_id');
            $user = User::whereIn('id',$user)->pluck('name','id');

        $driver = SaleModel::where('delivery_status', 1)
        ->pluck('driver_id');
           $driver = DriverModel::whereIn('id',$driver)->pluck('name','id');

        if ($request->ajax()) {
            $user = $request->user_id;
            $driver = $request->driver_id;
            $from_date = $request->start_date;
            $to_date = $request->end_date;
            $data = SaleModel::select('nso007_sales.*',
                 'nso007_customers.phone',
                 'nso007_customers.name')
                ->join('nso007_customers', 'nso007_customers.id', 'nso007_sales.id')
                ->where('nso007_sales.delivery_status',1)
                ->where(function ($query) use ($from_date, $to_date) {
                    if ($from_date) {
                        $query->whereDate('nso007_sales.sale_date', '>=', sDate($from_date));
                    }
                    if ($to_date) {
                        $query->whereDate('nso007_sales.sale_date', '<=', sDate($to_date));
                    }

                })
                ->where(function ($query) use ($user){
                    if($user){
                        $query->where('nso007_sales.user_id',$user);
                    }
                })
                ->where(function ($query) use ($driver){
                    if($driver){
                        $query->where('nso007_sales.driver_id',$driver);
                    }
                })
                ->orderBy('nso007_sales.id', 'DESC');
            return DataTables::of($data)
                ->editColumn('sale_date', function ($data) {
                    return myDate($data->sale_date);
                })
                ->editColumn('user_id', function ($data) {
                    return findUser($data->user_id)->name??'';
                })
                ->editColumn('driver_id', function ($data) {
                    return findDriver($data->driver_id)->name??'';
                })
                ->addColumn('phone', function ($data) {
                    return $data->phone??'';
                })
                ->addColumn('name', function ($data) {
                    return $data->name??'';
                })
                ->addColumn('location', function ($data) {
                    return $data->address;
                })
                ->editColumn('delivery_fee', function ($data) {
                    $currency = findCurrency($data->currency_id);
                    return ($currency ? $currency->symbol : '') . number_format($data->delivery_fee, 2)."
                    <i class='delivery_fee' data-delivery_fee='$data->delivery_fee' data-currency_id='$data->currency_id'></i>";
                })
                ->addIndexColumn()
                ->rawColumns([ 'customer_id', 'phone','driver_id','location','delivery_fee'])
                ->make(true);
        }
        return view('administrator.report-delivery.index',compact('user','driver'));
    }

}

