<?php
defined('BASEPATH') or exit('No direct script access allowed');


class Rate_inquiry extends FrontendController
{
    public function __construct()
    {
        parent::__construct();
        hooks()->do_action('after_clients_area_init', $this);
    }

    public function index()
    {
        //load about us content
        $this->use_navigation = true;
        $this->use_footer = true;
        $this->use_submenu = false;
        $this->use_sidebar = false;
        $this->use_breadcrumb = true;
        $this->css = [
            'assets/front/js/jquery-ui/jquery-ui.min.css',
        ];
        $this->js = [
            'assets/front/js/jquery-ui/jquery-ui.js',
            'assets/plugins/datetimepicker/jquery.datetimepicker.full.min.js',
        ];
        $data = [
            'page' => 'rate_inquiry',
            'title' => _l('front.rate_inquiry'),
        ];
        $this->data($data);
        //end load about us content
        $this->view('rate_inquiry');
        $this->layout();
    }

    private function _build_description($request)
    {
        return '出發地:' . $request['departure'] . PHP_EOL
            . '目的地:' . $request['destination'] . PHP_EOL
            . '運送方法:' . $request['transportation_methods'] . PHP_EOL
            . '重量:' . $request['weight'] . PHP_EOL
            . '尺寸:' . $request['size'] . PHP_EOL
            . '商品:' . $request['commodity'] . PHP_EOL
            . '發貨日期:' . $request['delivery_date'] . PHP_EOL
            . '發貨條款:' . $request['delivery_terms'] . PHP_EOL;
    }

    public function inquiry()
    {
        $request = $this->input->post();
        $this->load->model('Leads_model');
        $hash = hash('sha256', uniqid('', true));
        $data = [
            'hash' => $hash,
            'name' => $request['contact_name'],
            'company' => $request['company_name'],
            'address' => '',
            'title' => '',
            'description' => '',
            'from_form_id' => 0,
            'status' => 2,
            'source' => 3,
            'addedfrom' => 1,
            'email' => $request['contact_email'],
            'phonenumber' => $request['contact_phone'],
            'is_public' => 1,
            'lead_value' => 0,
            'assigned' => 0,
        ];
        $leads_id = $this->Leads_model->add($data);
        $this->load->model('Proposals_model');
        $hash = hash('md5', uniqid('', true));
        $data = [
            'subject' => $request['transportation_methods'],
            'rel_id' => $leads_id,
            'rel_type' => 'lead',
            'hash' => $hash,
            'proposal_to' => $request['contact_name'],
            'email' => $request['contact_email'],
            'phone' => $request['contact_phone'],
            'address' => '',
            'allow_comments' => 1,
            'date' => date("Y-m-d"),
            'currency' => 3,
            'status' => 4,
        ];
        $proposal_id = $this->Proposals_model->add($data);
        $this->load->model('Invoice_items_model');
        $item = [
            'description' => '運送方法：' . $request['transportation_methods'],
            'long_description' => $this->_build_description($request),
            'qty' => 1,
            'rate' => 1,
            'order' => 1,
            'unit' => '',
        ];
        add_new_sales_item_post($item, $proposal_id, 'proposal');

        $this->use_navigation = true;
        $this->use_footer = true;
        $this->use_submenu = false;
        $this->use_sidebar = false;
        $this->use_breadcrumb = true;
        $data = [
            'page' => 'rate_inquiry_save',
            'title' => _l('front.rate_inquiry'),
        ];
        $this->data($data);
        //end load about us content
        $this->view('rate_inquiry_save');
        $this->layout();
    }
}
