<?php

namespace Modules\PurchaseRequisition\Http\Controllers;

use Illuminate\Routing\Controller;

class DataController extends Controller
{
    /**
     * Defines the module for superadmin package.
     *
     * @return array
     */
    public function superadmin_package()
    {
        return [
            [
                'name' => 'purchase_requisition',
                'label' => __('purchaseRequisition::lang.purchase_requisition_module'),
                'default' => false
            ]
        ];
    }

    /**
     * Get user permissions for the module.
     *
     * @return array
     */
    public function user_permissions()
    {
        return [
            [
                'value' => 'purchase_requisition.access_module',
                'label' => __('purchaseRequisition::lang.access_purchase_requisition_module'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.create',
                'label' => __('purchaseRequisition::lang.create_purchase_requisition'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.view_all',
                'label' => __('purchaseRequisition::lang.view_all_purchase_requisitions'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.view_own',
                'label' => __('purchaseRequisition::lang.view_own_purchase_requisitions'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.edit',
                'label' => __('purchaseRequisition::lang.edit_purchase_requisition'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.delete',
                'label' => __('purchaseRequisition::lang.delete_purchase_requisition'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.approve',
                'label' => __('purchaseRequisition::lang.approve_purchase_requisition'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.share_with_suppliers',
                'label' => __('purchaseRequisition::lang.share_purchase_requisition_with_suppliers'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.make_purchase',
                'label' => __('purchaseRequisition::lang.make_purchase_from_requisition'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.access_settings',
                'label' => __('purchaseRequisition::lang.access_purchase_requisition_settings'),
                'default' => true
            ],
            [
                'value' => 'purchase_requisition.manage_expenses',
                'label' => __('purchaseRequisition::lang.manage_purchase_expenses'),
                'default' => true
            ],
        ];
    }

    /**
     * Modify admin menu to include Purchase Requisition.
     *
     * @return array|null
     */
    public function modifyAdminMenu()
    {
        if (auth()->user()->can('purchase_requisition.access_module')) {
            \Menu::modify('admin-sidebar-menu', function ($menu) {
                $menu->dropdown(__('purchaseRequisition::lang.pr_module'), function ($sub) {
                    if (auth()->user()->can('purchase_requisition.view_all') || auth()->user()->can('purchase_requisition.view_own')) {
                        $sub->url(
                            action([\Modules\PurchaseRequisition\Http\Controllers\PrIndexController::class, 'index']),
                            __('purchaseRequisition::lang.list_all'),
                            ['active' => request()->segment(1) == 'pr-module' && request()->segment(2) == null]
                        );
                    }

                    if (auth()->user()->can('purchase_requisition.create')) {
                        $sub->url(
                            action([\Modules\PurchaseRequisition\Http\Controllers\PrGenerateController::class, 'index']),
                            __('purchaseRequisition::lang.generate'),
                            ['active' => request()->segment(1) == 'pr-module' && request()->segment(2) == 'generate']
                        );
                    }

                    if (auth()->user()->can('purchase_requisition.make_purchase') || auth()->user()->can('purchase_requisition.view_all')) {
                        $sub->url(
                            route('pr.purchases.index'),
                            __('purchaseRequisition::lang.purchase_orders'),
                            ['active' => request()->segment(1) == 'pr-module' && request()->segment(2) == 'purchases']
                        );
                    }

                    if (auth()->user()->can('purchase_requisition.access_settings')) {
                        $sub->url(
                            action([\Modules\PurchaseRequisition\Http\Controllers\PrSettingsController::class, 'index']),
                            __('purchaseRequisition::lang.settings'),
                            ['active' => request()->segment(1) == 'pr-module' && request()->segment(2) == 'settings']
                        );
                    }
                }, ['icon' => 'fa fas fa-file-invoice'])->order(35);
            });
        }
    }
}
