<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Shop\PriceList;
use App\Repositories\Shop\PriceLists;
use App\Repositories\Shop\Taxes;
use App\Repositories\Shop\Tariffs;
use App\Repositories\Shop\SaleChannels;
use App\Repositories\Shop\Unities;
use App\Datatables\Shop\PriceListsDataTable;

class PriceListController extends Controller
{
    public function index(PriceListsDataTable $dataTable)
    {
        $data = [];
        return $dataTable->render('Admin.Shop.PriceLists.list', $data);
    }

    public function create()
    {
        $data = [];
        $data['sale_channels'] = SaleChannels::getOptions();
        return view('Admin.Shop.PriceLists.create', $data);
    }

    public function edit($id)
    {
        $data['price_list'] = PriceLists::get($id);
        $data['sale_channels'] = SaleChannels::getOptions();
        return view('Admin.Shop.PriceLists.edit', $data);
    }

    public function modalCreate($tariff_id)
    {
        $data = [];
        $data['price_list']['tariff_id'] = $tariff_id;
        $data['price_list']['price_list_values'] = array_fill(0, 3, '');
        $data['sale_channels'] = SaleChannels::getOptions();
        $data['statuses'] = Tariffs::getStatuses();
        $data['taxes'] = Taxes::getOptions();
        return view('Admin.Shop.PriceLists.modal', $data);
    }

    public function modalEdit($id)
    {
        $data['price_list'] = PriceLists::edit($id);
        $data['sale_channels'] = SaleChannels::getOptions();
        $data['statuses'] = Tariffs::getStatuses();
        $data['taxes'] = Taxes::getOptions();
        return view('Admin.Shop.PriceLists.modal', $data);
    }

    public function store(Request $request)
    {
        $ret = PriceLists::store($request->all());
        return redirect()->route('Admin.Shop.PriceLists.index');
    }

    public function storeAjax(Request $request)
    {
        $data = $request->all();
        $ret = PriceLists::store($data);
        return response()->json(['code' => '200']);
    }

    public function show($id)
    {
        $data = PriceLists::get($id);
        return view('Admin.Shop.PriceLists.view', $data);
    }

    public function destroy($id)
    {
        return PriceLists::destroy($id);
    }

    public function getPrice($id)
    {
        $data['generic'] = PriceLists::getFull($id);
        return view('Admin.Shop.PriceLists.partials.table-prices', $data);
    }
}
