<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\BuyBillElement;
use App\Models\Company;
use App\Models\Product;
use App\Models\SaleBillElement;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class StoreController 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']]);
        $this->middleware('permission:جرد المخازن', ['only' => ['inventory_get', 'inventory_post']]);
    }

    public function index()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $stores = $company->stores;
        return view('client.stores.index', compact('company', 'company_id', 'stores'));
    }

    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.stores.create', compact('company_id', 'branches', 'company'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'store_name' => 'required',
            'branch_id' => 'required',
        ]);
        $data = $request->all();
        $store = Store::create($data);
        return response()->json(['message' => 'تم إضافة الفرع بنجاح', 'status' => true]);
    }

    public function edit($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $branches = Branch::where('company_id', $company_id)->get();
        $store = Store::findOrFail($id);
        return view('client.stores.edit', compact('store', 'branches', 'company_id', 'company'));
    }

    public function update(Request $request)
    {
        $this->validate($request, [
            'store_name' => 'required',
            'branch_id' => 'required',
        ]);
        $input = $request->all();
        $store = Store::findOrFail($request->id);
        $store->update($input);
        return response()->json(['message' => 'تم تحديث البيانات بنجاح', 'status' => true]);

    }

    public function delete(Request $request)
    {
         $request->all();
        $store = Store::findOrFail($request->id);
        $store->delete();
        return  response()->json(['success' => 'تم حذف الفرع بنجاح']);
    }


    public function inventory_get()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $stores = $company->stores;
        $products = $company->products;
        return view('client.stores.inventory', compact('stores', 'products', 'company_id', 'company'));
    }

    public function inventory_post(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $stores = $company->stores;
        $products = $company->products;
        $store_id = $request->store_id;
        $from_date = $request->from_date;
        $to_date = $request->to_date;
        $product_id = $request->product_id;
        if (!empty($store_id)) {
            if($store_id == "all"){
                // serach by store
                if (empty($from_date) || empty($to_date)) {
                    $products_k = Product::where('company_id',$company_id)->where('is_product',1)->get();
                } else {
                    $products_k = Product::where('company_id',$company_id)->where('is_product',1)->whereBetween('created_at', [$from_date, $to_date])
                        ->get();
                }
            }
            else{
                // serach by store
                if (empty($from_date) || empty($to_date)) {
                    $products_k = Product::where('store_id', $store_id)->where('is_product',1)->get();
                } else {
                    $products_k = Product::where('store_id', $store_id)->where('is_product',1)
                        ->whereBetween('created_at', [$from_date, $to_date])
                        ->get();
                }
            }
            return view('client.stores.inventory', compact('stores','store_id','product_id','from_date','to_date', 'products', 'products_k', 'company_id', 'company'));
        } elseif (!empty($product_id)) {
            // search by product
            $product_k = Product::FindOrFail($product_id);
            $buy_elements = BuyBillElement::where('product_id',$product_k->id)->get();
            $total_buy_elements = 0;
            foreach($buy_elements as $buy_element){
                $total_buy_elements = $total_buy_elements + $buy_element->quantity;
            }
            $sale_elements = SaleBillElement::where('product_id',$product_k->id)->get();
            $total_sale_elements = 0;
            foreach($sale_elements as $sale_element){
                $total_sale_elements = $total_sale_elements + $sale_element->quantity;
            }
            $total_sold = $total_sale_elements ;
            return view('client.stores.inventory', compact('stores','store_id','product_id','from_date','to_date', 'products','total_sold','total_buy_elements', 'product_k', 'company_id', 'company'));

        } else {
            return redirect()->route('client.inventory.get')->with('error', 'اختر المخزن او اختر المنتج');
        }
    }
}
