<?php

class ModelShippingPickup extends Model
{
    function getQuote($address)
    {
        $this->language->load('shipping/pickup');
        $this->load->type('tipocomum');

        $method_data = array();
        $quote_data = array();
        $sort_order = array();
        $results = $this->config->get('pickup_module');
        $pickup_address_text = '';

            
        foreach ($results as $key => $value) {
            $sort_order[$key] = $value['sort_order'];
        }
        array_multisort($sort_order, SORT_ASC, $results);

        // Pega endereço de retirada
        foreach ($results as $key => $value) {
            if ($value['status'] == Type_Status::Tp_Habilitado){
                
                if ($value['zone_id']){
                    $this->load->model('localisation/zone');
                    $states = $this->model_localisation_zone->getZonesByCountryId(Type_PaisId::Tp_Brasil);
                    foreach ($states as $title_info => $info){
                        if ($info['zone_id'] == $value['zone_id']){
                            $state_name = $info['name'];
                        }
                    }
                }

                if ($value) {
                    if (!empty($value) && !empty($value['address_1']) && !empty($value['numero']) && !empty($value['address_2']) && !empty($value['city']) && !empty($value['postcode'])){
                        $complemento = empty($value['complemento']) ? '' : ', ' . $value['complemento'];
                        $pickup_address_text = $value['address_1'] . ', ' . $value['numero'] . ', ' . $value['address_2'] . $complemento . ', ' . $value['city'] . ', ' . $state_name . ', ' . $value['postcode'];
                        
                        $quote_data[$value['apelido']] = array(
                            'code' => 'pickup.' . $value['apelido'],
                            'title' => $value['apelido'],
                            'cost' => '0.00',
                            'text' => $this->currency->format(0.00),
                            'message' => (isset($value['pickup_note'])) ? $value['pickup_note'] : false,
                            'pickup_address' => $pickup_address_text,
                            'isPickup' => 'Retirar na loja',
                            'pickup_zone_id' => $value['zone_id'],
                        );
                    }
                } 
            }
        }

        $method_data = array(
            'code' => 'pickup',
            'title' => $this->language->get('text_title'),
            'quote' => $quote_data,
            'sort_order' => $this->config->get('pickup_sort_order'),
            'error' => false
        );

        return $method_data;
    }
}
