<?php

namespace App\Http\Controllers;

use \App\expenses;
use Illuminate\Http\Request;
use App\transactions;
use App\banks;

class ExpensesController extends Controller
{



   /* public function per_month($month)
    {

        $expenses = \App\expenses::whereYear('created_at', '=', $year)
              ->whereMonth('created_at', '=', Request::segment(3))
              ->get();

        return view('admin.expenses.list' , compact('expenses')); // while query
    }*/
/*
$dateS = new Carbon('first day of January 2016');
$dateE = new Carbon('first day of November 2016'); */ 





    public function report_from_to() {
       // $expense_id = request()->input('expense_id');
        $from = request()->input('date_from');
        $to = request()->input('date_to');

        // $expesnes = expenses::between();
        $expenses = expenses::whereBetween('expense_date', [$from, $to])->get();

        $total = 0;
        foreach($expenses as $r)
        {
           $total = $total + $r->expense_price;
        }


        return view('admin.expenses.show_between',compact('expenses','total'));

    }


    public function index()
    {
        $expenses = expenses::OrderBy('id','desc')->get(); // Select
        return view('admin.expenses.list' , compact('expenses')); // while query
    }


    public function insert_expenses($expense_id,$expense_price,$expense_note)
    {
        $expenses = new expenses(); // Select
        $expenses->expense_id = $expense_id;
        $expenses->expense_price = $expense_price;
        $expenses->expense_note = $expense_note;
        $expenses->save();

        return ('تمت الاضافة بنجاح');
        //return view('admin.expenses.list' , compact('expenses')); // while query
    }

    public function create()
    {

        return view('admin.expenses.create');
    }


    public function store()
    {


      //  dd($this->validateData()['expense_price']);

        $expense = expenses::create($this->validateData()); // insert

       //  dd($expense->id);
       // dd($this->validateData()['expense_price']);

        $type = 'depit';

        $trans = new transactions;
        $trans->type = 'depit';
        $trans->amount = $this->validateData()['expense_price'];
        $trans->payment_method = $this->validateData()['payment_method'];
        $trans->notes = " منصرفات   - ". $this->validateData()['expense_name']. " " . $this->validateData()['expense_note'];
        $trans->trans_type = "expenses";
        $trans->trans_type_id = $expense->id;
        $trans->save();

        $banks = banks::where('id','1')->first();

        if($type == "credit") {
        $banks->balance = $banks->balance +  $this->validateData()['expense_price'];
        }
        if($type == "depit") {
        $banks->balance = $banks->balance -  $this->validateData()['expense_price'];
        }

        $banks->update();





        $this->storeImage($expense);
        return redirect('/admin/expenses/create')->with("success","Created Successfully"); // send id of inserted

    }

    public function show(expenses $expenses)
    {
        //
    }


    public function edit(expenses $expense)
    {

        return view('admin.expenses.edit',compact('expense'));
    }


    public function update(Request $request, expenses $expense)
    {


        $expense->update($this->validateData());

        $type = 'depit';


        $trans =  transactions::where('trans_type_id',$expense->id)
        ->where('trans_type','expenses')->first();

        $trans->type = 'depit';
        $trans->amount = $this->validateData()['expense_price'];
        $trans->payment_method = $this->validateData()['payment_method'];
        $trans->notes = " منصرفات   - ". $this->validateData()['expense_name']. " " . $this->validateData()['expense_note'];
        $trans->trans_type = "expenses";
        $trans->trans_type_id = $expense->id;
        $trans->update();

        $banks = banks::where('id','1')->first();

        if($type == "credit") {
        $banks->balance = $banks->balance +  $this->validateData()['expense_price'];
        }
        if($type == "depit") {
        $banks->balance = $banks->balance -  $this->validateData()['expense_price'];
        }

        $banks->update();



        $this->storeImage($expense);
        return redirect('/admin/expenses/'. $expense->id . "/edit")->with('success', 'successfully updated');

    }


    public function destroy(expenses $expense) {

        $trans =  transactions::where('trans_type_id',$expense->id)
        ->where('trans_type','expenses');
        $trans->delete();
        $expense->delete();
        return redirect('/admin/expenses');
    }




    private function storeImage($expense) {


        if(request()->hasFile('header_img')) {
            $expense->update([


            'header_img'=>request()->header_img->store('uploads','public')



            ]);
        }

    }



    protected function validateData() {


        $mydata =  [
            'expense_price'=>'required',
            'expense_name'=>'',
            'expense_date'=>'',
            'payment_method'=>'',
            'expense_note'=>'',
        ];

        if(request()->hasFile('header_img')) {
            $mydata['header_img'] = 'file|image';
        }

        $validatedData = request()->validate($mydata);



        return $validatedData;


    }

}
