<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\ProductGroup;
use App\Models\ProfitCenter;
use App\Models\Supplier;
use App\Repositories\ProductRepository;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;

class PriceListProfitCenterController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {

        $suppliers = Supplier::orderBy('name')->get(['id', 'name'])->toArray();
        $profitCenters = ProfitCenter::orderBy('name')->get(['id', 'name'])->toArray();

        // var_dump($this->testFileMod);
        return view('admin.priceListProfitCenter.index')->with(
            ['suppliers' => $suppliers,
                'profitCenters' => $profitCenters,
                // 'testFileMod'=>($this->testFileMod)?'true':'false'
            ]

        );
    }

    public function getDataTableContent(): JsonResponse
    {
        $groupId = null;
        $searchStr = null;
        if (\request()->get('profitCenterId')) {
            $profitCenterId = \request()->get('profitCenterId');
        } else {
            return \response()->json(['data' => []]);
        }

        if (\request()->get('supplierId')) {
            $supplierId = \request()->get('supplierId');
        } else {
            return \response()->json(['data' => []]);
        }

        if (\request()->get('deliveryDate')) {
            $deliveryDate = \request()->get('deliveryDate');
        } else {
            $deliveryDate = Carbon::now()->format('Y-m-d');
        }

        $special = [];
        $special['withFavorites'] = true;
        if (\request()->get('searchStr')) {
            $searchStr = \request()->get('searchStr');
            if (substr($searchStr, 0, 2) == '**') {
                $special['type'] = str_replace('*', '', $searchStr);
                $special['profitCenterId'] = $profitCenterId;
                $searchStr = null;
            }
        } else {
            if (\request()->get('groupId')) {
                $groupId = \request()->get('groupId');
            }
        }

        if (\request()->get('krel')) {
            if (request()->get('krel') === 'true') {
                $special['krel'] = 1;
            }
        }

        debug(\request()->all());
        $productRepo = new ProductRepository;
        $productRepo->setRelation(
            'ProductGroup', 'Supplier', 'Producer', 'PriceList', 'LastPrice');
        // $products=$productRepo->getByGroupId($groupId,true);
        $products = $productRepo->getSupplierProductFromPriceList($supplierId, $profitCenterId, $deliveryDate, $groupId, $searchStr, $special);
        // debug($products[1]);
        $ret = ['data' => []];
        // dd($products[0]);
        foreach ($products as $product) {
            // dd(count($product['price_list']));
            // if(count($product['price_list'])>0){
            if ($product['price_list_price']) {
                $data = [];
                $data['id'] = $product['id'];
                $data['name'] = $product['name'];
                $data['packing'] = $product['packing'];
                $data['unitValue'] = $product['unitValue'];
                $data['productUnit'] = $product['productUnit'];
                $data['sellerUnit'] = $product['sellerUnit'];
                $data['unitMultiplier'] = $product['unitMultiplier'];
                $data['amountUnit'] = $product['amountUnit'];

                $data['note'] = ($product['note'] ? $product['note'] : '');
                // $product['product_group']['name'];
                // $data['price']=$product['last_price'][0]['pivot']['price'];
                $data['price'] = $product['price_list_price']['price'];
                $data['hooreycaId'] = $product['hooreycaId'];
                $data['office'] = '';
                $data['extn'] = '';
                $data['favorites'] = $product['favorites'];
                if ($product['krel'] == 1) {
                    $product['krel'] = '*';
                } else {
                    $product['krel'] = '';
                }
                $data['krel'] = $product['krel'];

                $ret['data'][] = $data;
            } else {

            }
        }

        return \response()->json($ret);

        return \response()->file('exapmle2.json');

    }

    public function getProductGroupHTML(): View
    {
        $productRepo = new ProductRepository;
        $productsGroupIds = $productRepo->getSupplierProductGroupsIdFromPriceList(
            \request()->get('supplierId'), \request()->get('deliveryDate'));
        $productGroupArray = [];
        if ($productsGroupIds && count($productsGroupIds) > 0) {
            $ProductGroups = new ProductGroup;
            $productGroupArray = $ProductGroups->nodesWithParentToTree($productsGroupIds);
        }

        return view('modules.order.productSelectSidebarProductGroupPartial')->with(['productGroup' => $productGroupArray]);
        dd($productGroupArray);
    }
}
