<?php

namespace App\Http\Controllers\StockManagement;

use App\DataTables\Warehouses\WarehouseDataTable;
use App\DataTables\Warehouses\WarehouseDataTableEditor;
use App\Http\Controllers\Controller;
use App\Models\LocalizationManagement\City;
use App\Models\StockManagement\Warehouse;
use App\Models\StockManagement\WareHouseType;
use App\Models\UserManagement\User;
use Illuminate\Http\Request;

class WarehouseController extends Controller{


    public function indexV1(Request $request){
        $warehouseType = $request->warehouseType ?? WareHouseType::STORE;
        $type = ($warehouseType == WareHouseType::STORE) ? __('Stores') : __("Points of Sales");
        $title = __("Manage :type",['type' => $type]);

        $dataTable = new WarehouseDataTable($warehouseType);
        return $dataTable->render("backend.stocks.warehouses.index",compact("title"));
    }

    public function index(Request $request, WarehouseDataTable $warehouseDataTable){
        $title = __("Managing Warehouses");
        $menus = config("stocks-management.tabs.warehouse");

        if ($request->ajax() or $request->expectsJson() or $request->wantsJson()){
            return $warehouseDataTable->ajax();
        }

        return view('backend.stocks.warehouses.index1', compact('title','menus'));
    }

    public function dtEdit(WarehouseDataTableEditor $editor){
        return $editor->process(request());
    }

    public function list(Request $request){
        $datas = Warehouse::with(["type","managers"])
                           ->when($request->filled("warehouseType"),function ($query) use($request){
                               return $query->where(WareHouseType::FK_ID,$request->warehouseType);
                           })
                           ->latest("updated_at")
                           ->get();

        return datatables($datas)
            ->addIndexColumn()
            ->addColumn('checkbox', null)
            ->editColumn('managers', function ($row){
                // return view('backend.stocks.warehouses.blocs.datatable-list-managers',compact("row"));
                return count($row->managers);
            })
            ->editColumn('cities', function ($row){
                return count($row->cities);
            })
            ->editColumn('created_at', function ($row){
                return format_date( $row->created_at,__("d-m-Y à H:i"));
            })
            ->editColumn('created_by', function ($row){
                return $row->createdBy->fullName ?? "";
            })
            ->addColumn('bad_goods', function ($row){
                return  ($row->is_bad_goods) ? __("Oui") : __("Non");
            })
            ->addColumn('type', function ($row){
                return $row->type->name ?? "";
            })
            ->addColumn('city', function ($row){
                return $row->city->name ?? "";
            })
            ->only(["checkbox","id","name","city","managers","bad_goods","type","cities_ids","city_id","managers_ids",
                'created_at',"created_by","cities","warehouse_type_code"])
            ->make(true);
    }

    public function show(Warehouse $warehouse){
        return $warehouse;
    }
}
