<?php
class AdvancedSearchForm extends Form
{

    public function __construct($controller)
    {
        $this->setName('advanced_search');
        $this->setActionForm($controller . ".php");
        switch ($controller) {
            case 'User':
            case 'SalesRecord':
            case 'Product':
                $actionController = '';
                break;
            default:
                $actionController = 'list';
                break;
        }
        $this->setActionController($actionController);
        $this->setMethod('post');
        $this->setDefaultFormLabelsColSize('4');
        $this->setDefaultFormElementsColSize('8');
        parent::__construct();
        $this->init();
    }

    public function init()
    {
        $this->addElement(
            array(
                'type' => 'select multiple',
                'name' => 'store_id',
                'label' => 'Sucursal',
                'multiOptions' => $this->getListStores()
            )
        );

        $append = "<span class = 'btn input-group-addon'><i class='fa fa-calendar'></i></span>";
        $this->addElement(
            array(
                'type' => 'text',
                'name' => 'startDate',
                'label' => 'Fecha inicio',
                'required' => false,
                'append' => $append
            )
        );

        $this->addElement(
            array(
                'type' => 'text',
                'name' => 'endDate',
                'label' => 'Fecha fin',
                'required' => false,
                'append' => $append
            )
        );

        $this->addElement(
            array(
                'type' => 'select multiple',
                'name' => 'status_payment',
                'label' => 'Status de pago',
                'multiOptions' => array('1' => 'Pendiente', '2' => 'Pagada'),
                'required' => false,
            )
        );

        $this->addElement(
            array(
                'type' => 'submit',
                'name' => 'search',
                'value' => $this->_getTranslation('Buscar'),
                'class' => 'btn btn-primary'
            )
        );
    }

    public function getPurchaseStatus()
    {
        $manifestRepo = new ManifestRepository();
        $purchaseRepo = new PurchaseGoodAndServiceRepository();

        $manifestStatus = $manifestRepo->getListStatus();
        $purchaseStatus = $purchaseRepo->getListStatus();

        $array = array();
        foreach ($manifestStatus as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus . ' - ' . $this->_getTranslation('Producto');
        }

        foreach ($purchaseStatus as $idStatus => $descriptionStatus) {
            $idStatus += 20;
            $array[$idStatus] = $descriptionStatus . ' - ' . $this->_getTranslation('Bienes y servcios');
        }

        return $array;

    }

    public function getLisUserStatus()
    {
        $shipingRequestRepo = new UserRepository();
        $salesOrderStatus = $shipingRequestRepo->getListStatus();

        $array = array();
        foreach ($salesOrderStatus as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus;
        }

        return $array;
    }

    public function getListStoreStatus()
    {
        $storeRepo = new StoreRepository();
        $storeStatus = $storeRepo->getListStatus();

        $array = array();
        foreach ($storeStatus as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus;
        }

        return $array;
    }

    public function getPaymentStatus()
    {
        $repository = new PagoRepository();
        $status = $repository->getListStatus();

        $array = array();
        foreach ($status as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus;
        }

        return $array;
    }

    public function getListShipmentStoreStatus()
    {
        $storeRepo = new ShipmentStoreRequestRepository();
        $storeStatus = $storeRepo->getListStatus();

        $array = array();
        foreach ($storeStatus as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus;
        }

        return $array;
    }

    public function getListStoreRequest()
    {
        $storeRepo = new StoreRequestRepository();
        $storeStatus = $storeRepo->getListStatus();

        $array = array();
        foreach ($storeStatus as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus;
        }

        return $array;
    }
    public function getListInvoiceStatus()
    { //UG agregamos get de status de factura
        $invoiceRepo = new InvoiceRepository();
        $storeStatus = $invoiceRepo->getListStatus();

        $array = array();
        foreach ($storeStatus as $idStatus => $descriptionStatus) {
            $array[$idStatus] = $descriptionStatus;
        }

        return $array;
    }
    public function getListStores()
    {
        $salesRecordForm = new SalesRecordForm();
        $arrayStores = $salesRecordForm->getListStores();
        if (key_exists('', $arrayStores)) {
            unset($arrayStores['']);
        }

        return $arrayStores;
    }

    public function getStringFiltersForm($controller)
    {
        $formString = '';
        //var_dump($controller);exit;
        switch ($controller) {
            case 'User':
                $userForm = new UserForm();

                $this->addElement(array('type' => 'text', 'name' => 'user', 'label' => 'Usuario', 'required' => false, ));

                $this->addElement(array('type' => 'select multiple', 'name' => 'role', 'label' => 'Rol'));
                $arrayUsers = $userForm->getListRoles();
                if (key_exists('', $arrayUsers)) {
                    unset($arrayUsers['']);
                }
                $this->setPropiedad('role', array('multiOptions' => $arrayUsers));

                $this->addElement(array('type' => 'select multiple', 'name' => 'store', 'label' => 'Sucursal'));
                $arrayStores = $userForm->getListStores();
                if (key_exists('', $arrayStores)) {
                    unset($arrayStores['']);
                }
                $this->setPropiedad('store', array('multiOptions' => $arrayStores));


                $this->addElement(array('type' => 'select multiple', 'name' => 'status', 'label' => 'Status', 'multiOptions' => $this->getLisUserStatus(), 'required' => false, ));

                $formString .= $this->getElementString('user');
                $formString .= $this->getElementString('store');
                $formString .= $this->getElementString('role');
                $formString .= $this->getElementString('status');

                break;

            case 'SalesRecord':
            case 'Deposit':
                $salesRecordForm = new SalesRecordForm();

                $this->addElement(array('type' => 'select multiple', 'name' => 'store_id', 'label' => 'Sucursal'));
                $arrayStores = $salesRecordForm->getListStores();
                if (key_exists('', $arrayStores)) {
                    unset($arrayStores['']);
                }
                $this->setPropiedad('store_id', array('multiOptions' => $arrayStores));

                $this->addElement(array('type' => 'select multiple', 'name' => 'status', 'label' => 'Status', 'multiOptions' => $this->getListStoreStatus(), 'required' => false, ));

                $formString .= $this->getElementString('store_id');
                $formString .= $this->getElementString('startDate');
                $formString .= $this->getElementString('endDate');
                $formString .= $this->getElementString('status');

                break;

            case 'ShipmentStoreRequest':

                $this->addElement(array('type' => 'text', 'name' => 'startDeliveryDate', 'label' => 'Fecha entrega inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endDeliveryDate', 'label' => 'Fecha enrega fin'));
                $this->addElement(array('type' => 'text', 'name' => 'startShipmentDate', 'label' => 'Fecha envio inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endShipmentDate', 'label' => 'Fecha envio fin'));
                $this->addElement(array('type' => 'text', 'name' => 'shipment_number', 'label' => 'Envio #', 'required' => false));
                $this->addElement(array('type' => 'text', 'name' => 'request_number', 'label' => 'Pedido #', 'required' => false));
                $this->addElement(array('type' => 'select multiple', 'name' => 'status', 'label' => 'Status', 'multiOptions' => $this->getListShipmentStoreStatus(), 'required' => false, ));

                $formString .= $this->getElementString('shipment_number');
                $formString .= $this->getElementString('request_number');
                $formString .= $this->getElementString('startShipmentDate');
                $formString .= $this->getElementString('endShipmentDate');
                $formString .= $this->getElementString('startDeliveryDate');
                $formString .= $this->getElementString('endDeliveryDate');
                $formString .= $this->getElementString('status');

                break;

            case 'StoreRequest':

                $this->addElement(array('type' => 'text', 'name' => 'startDeliveryDate', 'label' => 'Fecha entrega inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endDeliveryDate', 'label' => 'Fecha entrega fin'));
                $this->addElement(array('type' => 'text', 'name' => 'startDate', 'label' => 'Fecha pedido inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endDate', 'label' => 'Fecha pedido fin'));
                $this->addElement(array('type' => 'text', 'name' => 'store_request_id', 'label' => 'Pedido #', 'required' => false));
                $this->addElement(array('type' => 'select multiple', 'name' => 'status', 'label' => 'Status', 'multiOptions' => $this->getListStoreRequest(), 'required' => false, ));

                $formString .= $this->getElementString('store_id');
                $formString .= $this->getElementString('store_request_id');
                $formString .= $this->getElementString('startDate');
                $formString .= $this->getElementString('endDate');
                $formString .= $this->getElementString('startDeliveryDate');
                $formString .= $this->getElementString('endDeliveryDate');
                $formString .= $this->getElementString('status');

                break;
            case 'Invoice':

                $this->addElement(array('type' => 'text', 'name' => 'startDate', 'label' => 'Fecha factura inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endDate', 'label' => 'Fecha factura fin'));
                $this->addElement(array('type' => 'text', 'name' => 'num_shipment', 'label' => 'Envio #', 'required' => false));
                $this->addElement(array('type' => 'text', 'name' => 'invoice_num', 'label' => 'Factura #', 'required' => false));

                $this->addElement(array('type' => 'select multiple', 'name' => 'status', 'label' => 'Status', 'multiOptions' => $this->getListInvoiceStatus(), 'required' => false, ));

                $formString .= $this->getElementString('store_id');
                $formString .= $this->getElementString('invoice_num');

                $formString .= $this->getElementString('num_shipment');

                $formString .= $this->getElementString('startDate');
                $formString .= $this->getElementString('endDate');

                $formString .= $this->getElementString('status');

                break;
            case 'ReceivingStoreRequest':

                $this->addElement(array('type' => 'text', 'name' => 'startDeliveryDate', 'label' => 'Fecha entrega inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endDeliveryDate', 'label' => 'Fecha enrega fin'));
                $this->addElement(array('type' => 'text', 'name' => 'startShipmentDate', 'label' => 'Fecha envio inicio'));
                $this->addElement(array('type' => 'text', 'name' => 'endShipmentDate', 'label' => 'Fecha envio fin'));
                $this->addElement(array('type' => 'text', 'name' => 'shipment_number', 'label' => 'Envio #', 'required' => false));
                $this->addElement(array('type' => 'text', 'name' => 'request_number', 'label' => 'Pedido #', 'required' => false));
                $this->addElement(array('type' => 'select multiple', 'name' => 'status', 'label' => 'Status', 'multiOptions' => $this->getListShipmentStoreStatus(), 'required' => false, ));

                $formString .= $this->getElementString('shipment_number');
                $formString .= $this->getElementString('request_number');
                $formString .= $this->getElementString('startShipmentDate');
                $formString .= $this->getElementString('endShipmentDate');
                $formString .= $this->getElementString('startDeliveryDate');
                $formString .= $this->getElementString('endDeliveryDate');
                $formString .= $this->getElementString('status');


                break;
            
            case 'Product':
                       
                $productoRepo = new ProductRepository();                
                $this->addElement(array('type' => 'select multiple', 'name' => 'product_id', 'label' => 'Producto'));
                $arrayProducts = $productoRepo->getListSelectProducts();
                if (key_exists('', $arrayProducts)) {
                    unset($arrayProducts['']);
                }
                $this->setPropiedad('product_id', array('multiOptions' => $arrayProducts));
                
                $categoryRepo = new CategoryRepository();
                $this->addElement(array('type' => 'select multiple', 'name' => 'category_id', 'label' => 'Categoria'));
                $arrayCategories = $categoryRepo->getListSelectCategories();
                if (key_exists('', $arrayCategories)) {
                    unset($arrayProducts['']);
                }
                $this->setPropiedad('category_id', array('multiOptions' => $arrayCategories));
                
                $productForm = new ProductForm();     
                
                $formString .= $this->getElementString('product_id');
                $formString .= $this->getElementString('category_id');
                $formString .= $productForm->getElementString('status');

                break;

        }

        return $formString;
    }

}
