<?php

namespace WpBookingEventFrontend\app\controllers;

use WpBookingEvent\Factory;
use WpBookingEvent\Filesystem\Support\StringController;
use WpBookingEvent\Mvc\Controller\ControllerFrontend;
use WpBookingEvent\Mvc\Model\Model;
use WpBookingEvent\Utilities\Utility;
use WpBookingEventFrontend\app\helpers\ConfigHelper;
use WpBookingEventFrontend\app\helpers\EmailHelper;

defined('_WPBOOKINGEVENT_EXEC') or die;

/**
 * Class BlockController
 */
class CheckoutController extends ControllerFrontend
{
    public function booknow()
    {
        $input = Factory::getInput();
        $data = $input->getData();
        $now = Factory::getDate();


        $customerModel = Model::getInstance('customer');
        $data_customer = array(
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'mobile' => $data['mobile'],
            'email' => $data['e_mail'],
            'address_1' => $data['address_line1'],
            'address_2' => $data['address_line2'],
            'city' => $data['city'],
            'country_code' => $data['country'],
            'note_internal' => $data['description'],
            'created_date' => $now->tosql(),
            'zip_code' => $data['zip_code'],
            'state' => $data['province']
        );
        $customer = $customerModel->save($data_customer);


        $tour_id = $data['tour_id'];
        $tourModel = Model::getInstance('tour');
        $tour = $tourModel->getItem($tour_id);
        $coupon_id = isset($data['coupon_id'])?$data['coupon_id']:0;
        $couponModel = Model::getInstance('coupon');
        $coupon = $couponModel->getItem($coupon_id);
        $adult = $data['adult'];
        $children = $data['children'];
        $infant = $data['infant'];
        $total = (int)$adult * (float)$tour->adult_price
            + (int)$children * (float)$tour->children_price
            + (int)$infant * (float)$tour->infant_price;

        $orderModel = Model::getInstance('order');
        $data_order = array(
            "customer_id" => $customer->id,
            "total" => $total,
            "type" => 'tour'
        );
        $order = $orderModel->save($data_order);


        $orderDetailModel = Model::getInstance('orderdetail');
        $start_date = isset($data['start_date'])?$data['start_date']:'';
        $start_date = Factory::getDate($start_date);

        $data_order_detail = array(
            "order_id" => $order->id,
            "tour_id" => $tour_id,
            "adult" => $adult,
            "children" => $children,
            "infant" => $infant,
            "start_date" => $start_date->toSql(),
            "price" => $tour->price,
            "quantity" => 1,
            "total" => $total
        );
        $orderDetail = $orderDetailModel->save($data_order_detail);
        $root_url = Factory::getUri()->toString();
        $email = EmailHelper::get_email_info_tour();
        $content = $email->content;
        $list_location = $tour->destination;
        $list_location_name = [];
        foreach ($list_location as $location) {
            foreach ($location as $key => $value) {
                if ($key == 'name') {
                    array_push($list_location_name, $value);
                }
            }
        }
        foreach ($tour as $key => $value) {
            if (is_string($value))
                $content = str_replace("{tour_$key}", $value, $content);
        }
        foreach ($order as $key => $value) {
            if (is_string($value))
                $content = str_replace("{order_$key}", $value, $content);
        }
        foreach ($orderDetail as $key => $value) {
            if (is_string($value))
                $content = str_replace("{orderdetail_$key}", $value, $content);
        }
        foreach ($customer as $key => $value) {
            if (is_string($value))
                $content = str_replace("{customer_$key}", $value, $content);
        }
        $content = str_replace('{location_i}', implode(', ', $list_location_name), $content);
        $headers = array(
            'Content-Type: text/html; charset=UTF-8',
            'From: Tour operator admin'
        );
        if (!mail($customer->email, $email->subject, $content)) {

        }
        header("Location: $root_url/?view=checkout-thanks&order_id=" . $order->id);
        die;
    }

    public function check_coupon()
    {
        $input = Factory::getInput();
        $code = $input->getString('code', "");
        $tour_id = $input->getString('tour_id', 0);
        $couponModel = Model::getInstance('coupon');
        $coupon = $couponModel->getItemByCode($code);
        return $coupon;

    }
}
