<?php

namespace App\Http\Controllers;

use App\Helpers\DateHelper;
use App\Http\Requests\ProductSKUStoreRequest;
use App\Models\ProductSKU;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

// Склад - МАФ
class ProductSKUController extends Controller
{
    protected array $data = [
        'active'    => 'product_sku',
        'title'     => 'МАФ',
        'id'        => 'product_sku',
        'header'    => [
            'id'                                => 'ID',

            'order-district_id'                 => 'Округ',
            'order-area_id'                     => 'Район',
            'order-object_address'              => 'Адрес площадки',
            'maf_order-order_number'            => 'Номер заказа МАФ',

            'status'                            => 'Статус',
            'rfid'                              => 'RFID',
            'factory_number'                    => 'Номер фабрики',
            'manufacture_date'                  => 'Дата производства',
            'service_life'                      => 'Срок службы',
            'certificate_number'                => 'Номер сертификата',
            'certificate_date'                  => 'Дата сертификата',
            'certificate_issuer'                => 'Орган сертификации',
            'certificate_type'                  => 'Вид сертификации',
            'statement_number'                  => 'Номер ведомости',
            'statement_date'                    => 'Дата ведомости',
            'upd_number'                        => 'Номер УПД',

            'product-nomenclature_number'       => 'Номер номенклатуры',
            'product-article'                   => 'Артикул',
            'product-name_tz'                   => 'Наименование ТЗ',
            'product-type_tz'                   => 'Тип по ТЗ',
            'product-type'                      => 'Тип',
            'product-manufacturer_name'         => 'Наименование производителя',

            'comment'                           => 'Примечания',

        ],
        'searchFields' =>  [
            'rfid',
            'factory_number',
            'certificate_number',
            'statement_number',
            'upd_number',
            'order-object_address',
            'product-nomenclature_number',
            'product-article',
        ],
    ];
    public function index(Request $request)
    {
        $model = new ProductSKU;

        $this->createFilters($model, 'status');
        $this->createDateFilters($model, 'certificate_date', 'manufacture_date', 'statement_date');
        $this->data['ranges'] = [];

        $q = $model::query();

        $this->acceptFilters($q, $request);
        $this->acceptSearch($q, $request);
        $this->setSortAndOrderBy($model, $request);

        $q->orderBy($this->data['sortBy'], $this->data['orderBy']);

//        dump($q->toRawSql());
        $this->data['products_sku'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
        return view('products_sku.index', $this->data);
    }

    public function update(ProductSKUStoreRequest $request, ProductSKU $product_sku)
    {
        $url = $request->redirect_url ?? route('product_sku.index');

        $product_sku->update($request->validated());
        return redirect($url);
    }

    public function show(Request $request, ProductSKU $product_sku)
    {
        $this->data['product_sku'] = $product_sku;
        return view('products_sku.edit', $this->data);
    }
}
