<?php
namespace WpBookingEventAdmin\app\models;
defined('_WPBOOKINGEVENT_EXEC') or die;

use WpBookingEvent\Factory;
use WpBookingEvent\Mvc\Model\Model;
use WpBookingEvent\Mvc\Model\ModelForm;
use WpBookingEvent\Form\Form;
use WpBookingEvent\Mvc\Model\ModelList;
use WpBookingEvent\Registry\Registry;
use WpBookingEventAdmin\app\helpers\AdminHelper;
use WpBookingEventAdmin\app\Html\HtmlBackend;

class CalendarModel extends ModelList
{
    /**
     * @var string
     */
    public $context = "calendar";
    /**
     * @var string
     */
    public $table_name = "order";
    public function getList()
    {
        $db=Factory::getDBO();
        $query=$this->getListQuery();
        $list = $db->setQuery($query)->loadObjectList();
        $now = Factory::getDate();
        $list_rate=[];
        foreach ($list as $item) {
            $query->clear()
                ->select('customer.*')
                ->from($this->getTableName('customer') . ' AS customer')
                ->where('customer.id=' . (int)$item->customer_id);
            $item->customer = $this->db->setQuery($query)->loadObjectList();
            $item->customer_fullname = $item->customer[0]->first_name .' '.$item->customer[0]->last_name;
            $query->clear()
                ->select("order_detail.*")
                ->from($this->getTableName('order_detail') . ' AS order_detail')
                ->where('order_detail.order_id=' . (int)$item->id);
            $item->list_order_detail = $this->db->setQuery($query)->loadObjectList();

            foreach ($item->list_order_detail as $order_detail) {
                $query->clear()
                    ->select("order_detail_rate.*, order_detail_rate.start_date AS start, order_detail_rate.end_date AS end")
                    ->from($this->getTableName('order_detail_rate') . ' AS order_detail_rate')
                    ->leftJoin($this->getTableName('event') . ' AS event ON event.id=order_detail_rate.event_id')
                    ->select('event.name as title,event.price as event_price,event.image as event_image, event.description as description')
                    ->leftJoin($this->getTableName('service') . ' AS service ON service.id=order_detail_rate.service_id')
                    ->select('service.name as service_title,service.price as service_price,service.image as service_image, service.description as service_description')
                    ->leftJoin($this->getTableName('location') . ' AS location  ON location.id=order_detail_rate.location_id')
                    ->select('location.address AS location_address')
                    ->leftJoin($this->getTableName('employee') . ' AS employee  ON employee.id=order_detail_rate.employee_id')
                    ->select('employee.first_name AS employee_first_name,employee.last_name AS employee_last_name,employee.cover AS employee_cover')
                    ->where('order_detail_rate.order_detail_id=' . (int)$order_detail->id)
                    ->where('order_detail_rate.end_date >='.$query->quote($now->toSql()))
                ;
                $list_rate_item = $this->db->setQuery($query)->loadObjectList();
                foreach ($list_rate_item as &$rate) {
                    $start_date = Factory::getDate($rate->start_date);
                    $end_date = Factory::getDate($rate->end_date);
                    $since_start = $start_date->diff($end_date);
                    $rate->duration = $since_start->h. ' hours '. $since_start->i. ' minutes';
                    /////////////////////
                    $startDate = HtmlBackend::render_date($rate->start_date,'Y-m-d');
                    $endDate = HtmlBackend::render_date($rate->end_date,'Y-m-d');
                    $startTime = HtmlBackend::render_date($rate->start_date,'H:i A');
                    $endTime = HtmlBackend::render_date($rate->end_date,'H:i A');
                    $rate->list_date_detail = ($start_date == $end_date) ? $startDate : $startDate . ' - ' . $endDate;
                    $rate->list_time_detail = $startTime . ' - ' . $endTime;
                    $rate->employee_fullname = $rate->employee_first_name.' '.$rate->employee_last_name;
                    $rate->customer_email = $item->customer[0]->email;
                    $rate->customer_id = $item->customer[0]->id;
                    $rate->customer_fullname=$item->customer[0]->first_name .' '.$item->customer[0]->last_name;
                    $rate->payment_id = $item->payment_id;
                }
                $order_detail->rate = $list_rate_item;
                $item->list_rate = $list_rate_item;
            }

            if(!empty($list_rate_item)) {
                array_push($list_rate, reset($list_rate_item));
            }
        }

        return $list_rate;
    }

    /**
     * @return array
     */
    public function getListOrderByEmployeeId($employee_id=0) {
        $db=Factory::getDBO();
        $query=$this->getListQuery();
        $list = $db->setQuery($query)->loadObjectList();
        $now=Factory::getDate();
        $list_rate=[];
        foreach ($list as $item) {
            $query->clear()
                ->select('customer.*')
                ->from($this->getTableName('customer') . ' AS customer')
                ->where('customer.id=' . (int)$item->customer_id);
            $item->customer = $this->db->setQuery($query)->loadObjectList();
            $item->customer_fullname = $item->customer[0]->first_name .' '.$item->customer[0]->last_name;
            $query->clear()
                ->select("DISTINCT order_detail.*")
                ->from($this->getTableName('order_detail') . ' AS order_detail')
                ->where('order_detail.order_id=' . (int)$item->id);
            if($employee_id!== 0 ) {
                $query->where('order_detail.employee_id='.(int)$employee_id);
            }
            $item->list_order_detail = $this->db->setQuery($query)->loadObjectList();
            foreach ($item->list_order_detail as $order_detail) {
                $query->clear()
                    ->select("order_detail_rate.*, order_detail_rate.start_date AS start, order_detail_rate.end_date AS end")
                    ->from($this->getTableName('order_detail_rate') . ' AS order_detail_rate')
                    ->leftJoin($this->getTableName('event') . ' AS event ON event.id=order_detail_rate.event_id')
                    ->select('event.name as title,event.price as event_price,event.image as event_image, event.description as description')
                    ->leftJoin($this->getTableName('service') . ' AS service ON service.id=order_detail_rate.service_id')
                    ->select('service.name as service_title,service.price as service_price,service.image as service_image, service.description as service_description')
                    ->leftJoin($this->getTableName('location') . ' AS location  ON location.id=order_detail_rate.location_id')
                    ->select('location.address AS location_address')
                    ->leftJoin($this->getTableName('employee') . ' AS employee  ON employee.id=order_detail_rate.employee_id')
                    ->select('employee.first_name AS employee_first_name,employee.last_name AS employee_last_name,employee.cover AS employee_cover')
                    ->where('order_detail_id=' . (int)$order_detail->id)
                    ->where('order_detail_rate.end_date >='.$query->quote($now->toSql()))
                ;

                $list_rate_item = $this->db->setQuery($query)->loadObjectList();
                foreach ($list_rate_item as &$rate) {
                    $start_date = Factory::getDate($rate->start_date);
                    $end_date = Factory::getDate($rate->end_date);
                    $since_start = $start_date->diff($end_date);
                    $rate->duration = $since_start->h. ' hours '. $since_start->i. ' minutes';
                    /////////////////////
                    $startDate = HtmlBackend::render_date($rate->start_date,'Y-m-d');
                    $endDate = HtmlBackend::render_date($rate->end_date,'Y-m-d');
                    $startTime = HtmlBackend::render_date($rate->start_date,'H:i A');
                    $endTime = HtmlBackend::render_date($rate->end_date,'H:i A');
                    $rate->list_date_detail = ($start_date == $end_date) ? $startDate : $startDate . ' - ' . $endDate;
                    $rate->list_time_detail = $startTime . ' - ' . $endTime;
                    $rate->employee_fullname = $rate->employee_first_name.' '.$rate->employee_last_name;
                    $rate->customer_email = $item->customer[0]->email;
                    $rate->customer_id = $item->customer[0]->id;
                    $rate->customer_fullname=$item->customer[0]->first_name .' '.$item->customer[0]->last_name;
                    $rate->payment_id = $item->payment_id;
                }
                $order_detail->rate = $list_rate_item;
            }
            $item->list_rate = $list_rate_item;
            if(!empty($list_rate_item)) {
                array_push($list_rate, reset($list_rate_item));
            }
        }
        return $list_rate;
    }
    public function getListOrderByCustomerId($customer_id=0) {
        $db=Factory::getDBO();
        $query=$this->getListQuery();
        $list = $db->setQuery($query)->loadObjectList();
        $now=Factory::getDate();
        $list_rate=[];
        foreach ($list as $item) {
            $query->clear()
                ->select('customer.*')
                ->from($this->getTableName('customer') . ' AS customer')
                ->where('customer.id=' . (int)$item->customer_id);
            $item->customer = $this->db->setQuery($query)->loadObjectList();
            $item->customer_fullname = $item->customer[0]->first_name .' '.$item->customer[0]->last_name;
            $query->clear()
                ->select("DISTINCT order_detail.*")
                ->from($this->getTableName('order_detail') . ' AS order_detail')
                ->where('order_detail.order_id=' . (int)$item->id);
            if($customer_id!== 0 ) {
                $query->where('order_detail.customer_id='.(int)$customer_id);
            }
            $item->list_order_detail = $this->db->setQuery($query)->loadObjectList();
            foreach ($item->list_order_detail as $order_detail) {
                $query->clear()
                    ->select("order_detail_rate.*, order_detail_rate.start_date AS start, order_detail_rate.end_date AS end")
                    ->from($this->getTableName('order_detail_rate') . ' AS order_detail_rate')
                    ->leftJoin($this->getTableName('event') . ' AS event ON event.id=order_detail_rate.event_id')
                    ->select('event.name as title,event.price as event_price,event.image as event_image, event.description as description')
                    ->leftJoin($this->getTableName('service') . ' AS service ON service.id=order_detail_rate.service_id')
                    ->select('service.name as service_title,service.price as service_price,service.image as service_image, service.description as service_description')
                    ->leftJoin($this->getTableName('location') . ' AS location  ON location.id=order_detail_rate.location_id')
                    ->select('location.address AS location_address')
                    ->leftJoin($this->getTableName('employee') . ' AS employee  ON employee.id=order_detail_rate.employee_id')
                    ->select('employee.first_name AS employee_first_name,employee.last_name AS employee_last_name,employee.cover AS employee_cover')
                    ->where('order_detail_id=' . (int)$order_detail->id)
                    ->where('order_detail_rate.end_date >='.$query->quote($now->toSql()))
                ;

                $list_rate_item = $this->db->setQuery($query)->loadObjectList();
                foreach ($list_rate_item as &$rate) {
                    $start_date = Factory::getDate($rate->start_date);
                    $end_date = Factory::getDate($rate->end_date);
                    $since_start = $start_date->diff($end_date);
                    $rate->duration = $since_start->h. ' hours '. $since_start->i. ' minutes';
                    /////////////////////
                    $startDate = HtmlBackend::render_date($rate->start_date,'Y-m-d');
                    $endDate = HtmlBackend::render_date($rate->end_date,'Y-m-d');
                    $startTime = HtmlBackend::render_date($rate->start_date,'H:i A');
                    $endTime = HtmlBackend::render_date($rate->end_date,'H:i A');
                    $rate->list_date_detail = ($start_date == $end_date) ? $startDate : $startDate . ' - ' . $endDate;
                    $rate->list_time_detail = $startTime . ' - ' . $endTime;
                    $rate->employee_fullname = $rate->employee_first_name.' '.$rate->employee_last_name;
                    $rate->customer_email = $item->customer[0]->email;
                    $rate->customer_id = $item->customer[0]->id;
                    $rate->customer_fullname=$item->customer[0]->first_name .' '.$item->customer[0]->last_name;
                    $rate->payment_id = $item->payment_id;
                }
                $order_detail->rate = $list_rate_item;
            }
            $item->list_rate = $list_rate_item;
            if(!empty($list_rate_item)) {
                array_push($list_rate, reset($list_rate_item));
            }
        }
        return $list_rate;
    }


    public function save_recurring_event($data){
        $event_id = $data['event_id'];
        $customer_id = $data['customer_id'];
        $payment_id = $data['payment_id'];
        $total=$data['total'];
        $date_log=$data['date_log'];
        $not_recurring=$data['not_recurring'];
        $daily=$data['daily'];
        $weekly=$data['weekly'];
        $monthly=$data['monthly'];
        $day_of_week=$data['day_of_week'];
        if( in_array( "0" ,$day_of_week ) )
        {
            $sunday=1;
        }
        if( in_array( "1" ,$day_of_week ) )
        {
            $monday=1;
        }
        if( in_array( "2" ,$day_of_week ) )
        {
            $tuesday=1;
        }
        if( in_array( "3" ,$day_of_week ) )
        {
            $wednesday=1;
        }
        if( in_array( "4" ,$day_of_week ) )
        {
            $thursday=1;
        }
        if( in_array( "5" ,$day_of_week ) )
        {
            $friday=1;
        }
        if( in_array( "6" ,$day_of_week ) )
        {
            $saturday=1;
        }
        $day_of_month=$data['day_of_month'];
        if( in_array( 1 ,$day_of_month ) )
        {
            $day_1=1;
        }
        if( in_array( 2 ,$day_of_month ) )
        {
            $day_2=1;
        }
        if( in_array( 3 ,$day_of_month ) )
        {
            $day_3=1;
        }
        if( in_array( 4 ,$day_of_month ) )
        {
            $day_4=1;
        }
        if( in_array( 5 ,$day_of_month ) )
        {
            $day_5=1;
        }
        if( in_array( 6 ,$day_of_month ) )
        {
            $day_6=1;
        }
        if( in_array( 7 ,$day_of_month ) )
        {
            $day_7=1;
        }
        if( in_array( 8 ,$day_of_month ) )
        {
            $day_8=1;
        }
        if( in_array( 9 ,$day_of_month ) )
        {
            $day_9=1;
        }
        if( in_array( 10 ,$day_of_month ) )
        {
            $day_10=1;
        }
        if( in_array( 11 ,$day_of_month ) )
        {
            $day_11=1;
        }
        if( in_array( 12 ,$day_of_month ) )
        {
            $day_12=1;
        }
        if( in_array( 13 ,$day_of_month ) )
        {
            $day_13=1;
        }
        if( in_array( 14 ,$day_of_month ) )
        {
            $day_14=1;
        }
        if( in_array( 15 ,$day_of_month ) )
        {
            $day_15=1;
        }
        if( in_array( 16 ,$day_of_month ) )
        {
            $day_16=1;
        }
        if( in_array( 17 ,$day_of_month ) )
        {
            $day_17=1;
        }
        if( in_array( 18 ,$day_of_month ) )
        {
            $day_18=1;
        }
        if( in_array( 19 ,$day_of_month ) )
        {
            $day_19=1;
        }
        if( in_array( 20 ,$day_of_month ) )
        {
            $day_20=1;
        }
        if( in_array( 21 ,$day_of_month ) )
        {
            $day_21=1;
        }
        if( in_array( 22 ,$day_of_month ) )
        {
            $day_22=1;
        }
        if( in_array( 1 ,$day_of_month ) )
        {
            $day_23=1;
        }
        if( in_array( 24 ,$day_of_month ) )
        {
            $day_24=1;
        }
        if( in_array( 25 ,$day_of_month ) )
        {
            $day_25=1;
        }
        if( in_array( 26 ,$day_of_month ) )
        {
            $day_26=1;
        }
        if( in_array( 27 ,$day_of_month ) )
        {
            $day_27=1;
        }
        if( in_array( 28 ,$day_of_month ) )
        {
            $day_28=1;
        }
        if( in_array( 29 ,$day_of_month ) )
        {
            $day_29=1;
        }
        if( in_array( 30 ,$day_of_month ) )
        {
            $day_30=1;
        }
        if( in_array( 31 ,$day_of_month ) )
        {
            $day_31=1;
        }
        $start_time =$data['start_time'];
        $end_time=$data['end_time'];
        $event_price=$data['event_price'];
        $startTime=$data['startTime'];
        $endTime=$data['endTime'];
        $startRecur=$data['startRecur'];
        $endRecur=$data['endRecur'];
        $employee_id=$data['employee_id'];
        $location_id=$data['location_id'];
        $start_date=$data['start_date'];
        $end_date=$data['end_date'];
        $color=$data['color'];
        $orderModel=Model::getInstance('order');
        $created_date = Factory::getDate();
        $data_order=[
            "customer_id" => $customer_id ,
            "payment_id" => $payment_id,
            "total" => $total,
            "type" => AdminHelper::getWpBookingEventKey(),
            "created_date"=>$created_date->toSql(),
            "not_recurring"=>$not_recurring,
            "daily"=>$daily,
            "weekly"=>$weekly,
            "monthly"=>$monthly,
            "re_sunday"=>$sunday,
            "re_monday"=>$monday,
            "re_tuesday"=>$tuesday,
            "re_wednesday"=>$wednesday,
            "re_thursday"=>$thursday,
            "re_friday"=>$friday,
            "re_saturday"=>$saturday,
            "day_of_week"=>$day_of_week
        ];
        $order=$orderModel->save($data_order);
        $orderDetailModel=Model::getInstance('orderdetail');
        $data_order_detail = [
            "id" => 0,
            "order_id" => $order->id,
            "start_date" => $start_date,
            "end_date" => $end_date,
            "total" => $total,
            "quantity" => 1,
            "customer_id" => $customer_id,
            "employee_id" => $employee_id,
            "event_id" => $event_id,
            "location_id" => $location_id,
            "price" => $event_price,
            "not_recurring"=>$not_recurring,
            "daily"=>$daily,
            "weekly"=>$weekly,
            "monthly"=>$monthly,
            "re_sunday"=>$sunday,
            "re_monday"=>$monday,
            "re_tuesday"=>$tuesday,
            "re_wednesday"=>$wednesday,
            "re_thursday"=>$thursday,
            "re_friday"=>$friday,
            "re_saturday"=>$saturday
        ];
        $orderdetail=$orderDetailModel->save($data_order_detail);
        $orderDetailRateModel = Model::getInstance('orderdetailrate');
        $data_order_detail_rate = [
            "id" => 0,
            "order_id" => $order->id,
            "order_detail_id" => $orderdetail->id,
            "start_date" => $start_date,
            "end_date" => $end_date,
            "employee_id" => $employee_id,
            "event_id" => $event_id,
            "location_id" => $location_id,
            "total" => $total,
            "customer_id" => $customer_id,
            "quantity" => 1,
            "price" => $event_price,
            "subtotal" => $total,
            "not_recurring"=>$not_recurring,
            "daily"=>$daily,
            "weekly"=>$weekly,
            "monthly"=>$monthly,
            "re_sunday"=>$sunday,
            "re_monday"=>$monday,
            "re_tuesday"=>$tuesday,
            "re_wednesday"=>$wednesday,
            "re_thursday"=>$thursday,
            "re_friday"=>$friday,
            "re_saturday"=>$saturday,
            "startTime"=>$startTime,
            "endTime"=>$endTime,
            "startRecur"=>$startRecur,
            "endRecur"=>$endRecur,
            "color_event"=>$color,
            "day_1"=>$day_1,
            "day_2"=>$day_2,
            "day_3"=>$day_3,
            "day_4"=>$day_4,
            "day_5"=>$day_5,
            "day_6"=>$day_6,
            "day_7"=>$day_7,
            "day_8"=>$day_8,
            "day_9"=>$day_9,
            "day_10"=>$day_10,
            "day_11"=>$day_11,
            "day_12"=>$day_12,
            "day_13"=>$day_13,
            "day_14"=>$day_14,
            "day_15"=>$day_15,
            "day_16"=>$day_16,
            "day_17"=>$day_17,
            "day_18"=>$day_18,
            "day_19"=>$day_19,
            "day_20"=>$day_20,
            "day_21"=>$day_21,
            "day_22"=>$day_22,
            "day_23"=>$day_23,
            "day_24"=>$day_24,
            "day_25"=>$day_25,
            "day_26"=>$day_26,
            "day_27"=>$day_27,
            "day_28"=>$day_28,
            "day_29"=>$day_29,
            "day_30"=>$day_30,
            "day_31"=>$day_31,
        ];
        $order_detail_rate = $orderDetailRateModel->save($data_order_detail_rate);
        return $order;

    }

    /**
     * @return array
     */
    public function getHeader()
    {
        return array();
    }


}
