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


class Contact_us 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_breadcrumb = true;
        $this->use_sidebar = false;
        $data = [
            'page' => 'contact_us',
            'title' => _l('front.contact_us'),
        ];
        $this->data($data);
        //end load about us content
        $this->view('contact_us');
        $this->layout();
    }

    private function _build_description($request)
    {
        return '運送方法:' . implode(',', $request['transportation_methods']) . PHP_EOL
            . '訊息:' . $request['message'] . PHP_EOL;
    }

    public function inquiry()
    {
        $request = $this->input->post();
//        print_r ($request);
//        exit;
        $this->load->model('Leads_model');
        $hash = hash('sha256', uniqid('', true));
        $data = [
            'hash' => $hash,
            'name' => $request['contact_name'],
            'company' => '',
            'address' => '',
            'title' => '',
            'description' => '',
            'from_form_id' => 0,
            'status' => 2,
            'source' => 3,
            'addedfrom' => 1,
            'email' => $request['email'],
            'phonenumber' => $request['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' => implode(', ', $request['transportation_methods']),
            'rel_id' => $leads_id,
            'rel_type' => 'lead',
            'hash' => $hash,
            'proposal_to' => $request['contact_name'],
            'email' => $request['email'],
            'phone' => $request['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' => '運送方法：' . implode(',', $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_breadcrumb = true;
        $this->use_sidebar = false;
        $data = [
            'page' => 'contact_us_save',
            'title' => _l('front.contact_us'),
        ];
        $this->data($data);
        //end load about us content
        $this->view('contact_us_save');
        $this->layout();
    }
}
