<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Carbon\Carbon;


use App\Models\User;
use App\Models\AdminDetails;
use App\Models\Companies;
use App\Models\Supliers;
use App\Models\Categories;
use App\Models\Brands;
use App\Models\Products;
use App\Models\ProductGallery;
use App\Models\GRNS;
use App\Models\Batches;
use App\Models\Returns;
use App\Models\Stock;
use App\Models\GRNProducts;
use App\Models\RevenuesAndExpenses;
use App\Models\Shifts;
use App\Models\ShiftCashFlow;
use App\Models\Orders;

use File;
use Mail;
use Image;
use PDF;
class ShiftController extends Controller
{
    public function shift_in(Request $request)
    { 
    if($request->isMethod('get')){
        return view('admin::shift.shift_in');
    }
    if($request->isMethod('post')){


        $shift = new Shifts();
        $shift->user_id =Auth::user()->id;
        $shift->starting_balance =$request->starting_balance;
        $shift->date_time =Carbon::now()->format('Y-m-d H:i:s');
        $shift->save();

        $user = User::find(Auth::user()->id);
        $user->active_shift_id =$shift->id;
        $user->update();

        return redirect('admin/add-order');

    }

    }

    public function shift_out(Request $request)
    { 

    $starting_balance = Shifts::where('id',Auth::user()->active_shift_id)->value('starting_balance');
    $cash_flaws = ShiftCashFlow::where('shfit_id', Auth::user()->active_shift_id)->get();
    $expense = 0;
    $revenue = 0;
    $cash_payments = 0;
    foreach($cash_flaws as $cash_flaw){
    if($cash_flaw->type == "in" && $cash_flaw->is_sales == "0"){
        $revenue = $revenue+$cash_flaw->amount;
    }
    if($cash_flaw->type == "out"){
        $expense = $expense+$cash_flaw->amount;
    }

    if($cash_flaw->paid_type == "cash"){
        $cash_payments = $cash_payments+$cash_flaw->amount;
    }
    }
    $balance = ($starting_balance+$cash_payments+$revenue) - $expense;
    if($request->isMethod('get')){

        return view('admin::shift.shift_out',['balance' => $balance]);
    }
    if($request->isMethod('post')){

        $shift_id = Auth::user()->active_shift_id;

        $shift =  Shifts::where('id',Auth::user()->active_shift_id)->first();
        $shift->system_closing_balance = $balance;
        $shift->cashier_entered_value = $request->closing_balance;
        $shift->difference = $request->closing_balance - $balance;
         $shift->close_date_time =Carbon::now()->format('Y-m-d H:i:s');
        $shift->remarks = $request->remarks;
        $shift->update();

        $user = User::find(Auth::user()->id);
        $user->active_shift_id =null;
        $user->update();

        return redirect('admin/dashboard')->with([
            'shift_message' => 'Shift  Added.',
            'shift_id' => $shift_id,

        ]);

    }

    }
    public function shift_bill($id,Request $request)
    { if($request->isMethod('get')){
        $shift = Shifts::where('id', $id)->first();
        return view('shift_bill',['shift' => $shift]);
    }

    }

    public function shift_out_validate(Request $request)
    { 
    if($request->isMethod('post')){

        if(Orders::where("order_status", 'pending')->where("sales_person",Auth::user()->id)->exists()){
            return response()->json(['status' => 'fail']);
        }
        else{
            return response()->json(['status' => 'success']);
        }

    }

    }
}