#!/usr/bin/perl -Ilibs -I../www-StockStatus/libs

use strict;
use warnings;

use API::Controller ();
use ECS::Warehouse ();

package API::WarehouseAdmin;

use Moose;
extends 'API::Controller';

__PACKAGE__->meta->make_immutable;

###############################################################################
# get the data required for the main page of the warehouse admin

sub warehouseIndex {
    my $self = (shift)->new({'input' => shift || {}});

    $self->data->{'templates'} = [ "index.html" ];

    $self->data->{'warehouseConfig'} = ECS::Warehouse::getConfig({
        'DBH' => $self->dbhr,
        });
    
    $self->data->{'onShelfAverage'} = ECS::Warehouse::getOnShelfAverage({
        'DBH' => $self->dbhr,
        });

    $self->data->{'shippedAverage'} = ECS::Warehouse::getShippedAverage({
        'DBH' => $self->dbhr,
        });

    $self->error("Unable to retrieve warehouse config")
        unless $self->data->{'warehouseConfig'};

    return $self->result;
}

###############################################################################
# Update warehouse config from the form in the portal

sub updateWarehouse {
    my $self = (shift)->new({'input' => shift || {}});

    ECS::Warehouse::updateConfig({
        'DBH'   => $self->dbh,
        'hash'  => $self->input,
        });

    $self->data->{'redirect'} = ".";
    return $self->result;
}

###############################################################################

return 1;

