<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\BuyCash;
use App\Models\Cash;
use App\Models\Company;
use App\Models\Safe;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class SafeController extends Controller
{
    
    public function __construct()
    {
        $this->middleware('permission:قائمة خزائن الشركة', ['only' => ['index']]);
        $this->middleware('permission:اضافة خزينة جديد', ['only' => ['create', 'store']]);
        $this->middleware('permission:تعديل خزينة', ['only' => ['edit', 'update']]);
        $this->middleware('permission:حذف خزينة', ['only' => ['delete']]);
    }

    public function index(){
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $safes = $company->safes;
        return view('client.safes.index',compact('company','company_id','safes'));
    }
    public function create()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $branches = Branch::where('company_id',$company_id)->get();
        return view('client.safes.create',compact('company_id','branches','company'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'safe_name' => 'required',
            'branch_id' => 'required',
            'balance' => 'required'
        ]);
        $data = $request->all();
        $data['balance_start'] = $request->balance ;
        $safe = safe::create($data);
        return response()->json(['status'=>true,'message'=>'تم اضافة الخزينة بنجاح']);

    }

    public function edit($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $branches = Branch::where('company_id',$company_id)->get();
        $safe = safe::findOrFail($id);
        return view('client.safes.edit', compact('safe','branches', 'company_id', 'company'));
    }
    public function update(Request $request)
    {
        $this->validate($request, [
            'safe_name' => 'required',
            'branch_id' => 'required',
            'balance_start' => 'required'
        ]);
        $input = $request->all();
        $safe = safe::findOrFail($request->id);
        $safe->update($input);
        return response()->json(['status'=>true,'message'=>'تم تحديث الخزينة بنجاح']);

    }
    public function delete(Request $request)
    {
        $company_id = Auth::user()->company_id;

       $safe = safe::findOrFail($request->id);
       $safe_opreation_in = Cash::where('company_id',$company_id)->where('safe_id',$safe->id)->get();
       $safe_opreation_out = BuyCash::where('company_id',$company_id)->where('safe_id',$safe->id)->get();

       if($safe_opreation_in->isEmpty() && $safe_opreation_out->isEmpty() ){
       
        $safe->delete();
        return response()->json(['status'=>true,'message'=>'تم حذف الخزينة بنجاح','sol'=>'']);

       }else{

        return response()->json(['status'=>false,'message'=>'لا يمكن حذف الخزينة','sol'=>'يرجي تسوية العمليات علي هذة الخزنة أولا']);

       }

    }
}
