<?php

class ControllerMyTickets extends Controller

{

function index()

{

        $customer =& $this->locator->get('customer');

        $language =& $this->locator->get('language');

        $module   =& $this->locator->get('module');

        $response =& $this->locator->get('response');

        $session  =& $this->locator->get('session');

        $template =& $this->locator->get('template');

        $url      =& $this->locator->get('url');

        $currency =& $this->locator->get('currency');

        $database =& $this->locator->get('database');

        $request  =& $this->locator->get('request');

        $view = $this->locator->create('template');



        if (!$customer->isLogged()) {

            $session->set('redirect', $url->ssl('ticket'));

            $response->redirect($url->ssl('account_login'));

        }



        if ($request->has('page')) {

           $page=$request->get('page');

        } else {

            $page = 1;

        }

        $customer_id = $customer->getId();

        $ticket_data=array();



        $ticket_list = $database->getRows($database->splitQuery("select * from ticket where customer_id=$customer_id order by ticket_id desc", $page, 10));



        foreach ($ticket_list as $tl) {



            $ticket_value=$database->getRow("select ticket_value_id,message,staff from ticket_value where ticket_id='".$tl['ticket_id']."' order by ticket_value_id desc");

            $customer_info=$database->getRow("select firstname,lastname from customer where customer_id='".$tl['customer_id']."'");



            $ticket_data[]=array(

               'ticket_id'=>$tl['ticket_id'],

               'ticket_no'=>$tl['ticket_no'],

               'subject'=>$tl['subject'],

               'status'=>$tl['status'],

               'fullname'=>$customer_info['firstname'].' '.$customer_info['lastname'],

               'staff'   =>!empty($ticket_value['staff'])?$ticket_value['staff']:$customer_info['firstname'].' '.$customer_info['lastname'],

               'status_image' =>($tl['status']==1 ? 'https://www.beststoreitems.com/catalog/template/beststoreitems/image/enabled.png' : 'https://www.beststoreitems.com/catalog/template/beststoreitems/image/disabled.png'),

               'status_value'=>$tl['status']==1,

               'message'=>$ticket_value['message']

            );



        }

        $view->set('ticket_data',$ticket_data);



        $page_data = array();

        for ($i = 1; $i <= $database->getPages(); $i++) {

                $page_data[] = array(

                    'text'  => $language->get('text_pages', $i, $database->getPages()),

                    'value' => $i

                );

              }

        $view->set('pages', $page_data);

        $view->set('page', $page);

        $view->set('text_results', $language->get('text_results',$page, $database->getPages()));

        $view->set('action', $url->href('my_tickets'));



        $template->set('content', $view->fetch('content/my_tickets.tpl'));

        $template->set($module->fetchAccount());

        $response->set($template->fetch('layout.tpl'));

    }

    public function add_ticket_email()

    {

            $config   =& $this->locator->get('config');

            $currency =& $this->locator->get('currency');

            $customer =& $this->locator->get('customer');

            $database =& $this->locator->get('database');

            $language =& $this->locator->get('language');

            $module   =& $this->locator->get('module');

            $response =& $this->locator->get('response');

            $session  =& $this->locator->get('session');

            $template =& $this->locator->get('template');

            $url      =& $this->locator->get('url');

            $request  =& $this->locator->get('request');

            $template->set('title', 'Add Email Address');

            $tax      =& $this->locator->get('tax');

           if ($request->isPost()) {

              $check_email=$database->getRow("select ticket_email_id from ticket_email where customer_id='".$customer->getId()."' and email_address='".$request->get('email_address', 'post')."'");

              if ($check_email) {

                  echo '<script>alert("Email Address is already!");</script>';

              } else {

                       $email_address=$request->get('email_address', 'post');

                       if (empty($email_address)) {

                           echo '<script>alert("Email address is not empty");</script>';

                       } else {

                           $sql="insert into ticket_email set customer_id='?',email_address='?'";

                        $database->query($database->parse($sql,$customer->getId(),$request->get('email_address', 'post')));

                       echo '<script>alert("Add successfully");window.location.href="https://www.beststoreitems.com/index.php?controller=ticket"; </script>';

                       }

              }

           }

            $view = $this->locator->create('template');

            $template->set('content', $view->fetch('content/add_ticket_email.tpl'));

                $template->set($module->fetchAccount());

        $response->set($template->fetch('layout.tpl'));

    }



    public function create_ticket()

    {

        $config   =& $this->locator->get('config');

        $currency =& $this->locator->get('currency');

        $customer =& $this->locator->get('customer');

        $database =& $this->locator->get('database');

        $language =& $this->locator->get('language');

        $module   =& $this->locator->get('module');

        $response =& $this->locator->get('response');

        $session  =& $this->locator->get('session');

        $template =& $this->locator->get('template');

        $url      =& $this->locator->get('url');

        $request  =& $this->locator->get('request');

        $template->set('title', 'Create a New Ticket');

        $tax      =& $this->locator->get('tax');

        if (!$customer->isLogged()) {

            $session->set('redirect', $url->ssl('ticket'));

            $response->redirect($url->ssl('account_login'));

        }

        if ($request->isPost()) {

            $sql="insert into ticket set ticket_no='?',ticket_email='?',customer_id='?',subject='?',reference='?',status='?',date_added=now()";

            $database->query($database->parse($sql,date('Ymd',time()).rand(10,99),$request->get('email_address', 'post'),$customer->getId(),$request->get('subject', 'post'),$request->get('reference', 'post'),0));

            $insert_id = $database->getLastId();

            $sql_value="insert into ticket_value set ticket_id='?',message='?',status='?',reference='?',date_added=now()";

            $database->query($database->parse($sql_value,$insert_id,$request->get('message', 'post'),0,$request->get('reference', 'post')));

            echo '<script>alert("Create ticket successfully");window.location.href="https://www.beststoreitems.com/index.php?controller=my_tickets";</script>';



        }

        $ticket_email_list=$database->getRows("select email_address from ticket_email where customer_id='".$customer->getId()."'");

        $order_info=$database->getRows("select reference,total,order_status_id,currency,value from `order` where customer_id='".$customer->getId()."' order by order_id desc");

        foreach ($order_info as $o) {

            $order_status_info=$database->getRow("select order_status_id,name from order_status where order_status_id='".$o['order_status_id']."'");

            $order_data[]=array(

               'reference'=>$o['reference'],

               'total'    => $currency->format($o['total'], $o['currency'], $o['value']),

               'order_status_name'=>$order_status_info['name']

            );

        }

        $waiting_payment_order=$database->getRows("select order_id,order_status,reference,data from order_data where customer_id='".$customer->getId()."'");

        $waiting_payment_order_data=array();

        foreach ($waiting_payment_order as $w) {

            $waiting_order_data = unserialize($w['data']);

            $waiting_payment_order_data[]=array(

                'reference'=>$w['reference'],

                'total'    => $currency->format($waiting_order_data['total'], $waiting_order_data['currency'], $waiting_order_data['value']),

                'order_status_name'=>($w['order_status']==1)?'Waiting for Payment':'Canceled'

            );

        }

        $customer_info=$database->getRow("select email from customer where customer_id='".$customer->getId()."'");

        $view = $this->locator->create('template');

        $view->set('ticket_email_list',$ticket_email_list);

        $view->set('order_data',$order_data);

        $view->set('waiting_payment_order_data',$waiting_payment_order_data);

        $view->set('customer_info',$customer_info);

        $template->set('content', $view->fetch('content/create_ticket.tpl'));

            $template->set($module->fetchAccount());

        $response->set($template->fetch('layout.tpl'));

    }

    public function detail_ticket()

    {

        $config   =& $this->locator->get('config');

        $currency =& $this->locator->get('currency');

        $customer =& $this->locator->get('customer');

        $database =& $this->locator->get('database');

        $language =& $this->locator->get('language');

        $module   =& $this->locator->get('module');

        $response =& $this->locator->get('response');

        $session  =& $this->locator->get('session');

        $template =& $this->locator->get('template');

        $url      =& $this->locator->get('url');

        $request  =& $this->locator->get('request');



        $tax      =& $this->locator->get('tax');

        $view = $this->locator->create('template');

        if (!$customer->isLogged()) {

            $session->set('redirect', $url->ssl('ticket'));

            $response->redirect($url->ssl('account_login'));

        }

        $page=($request->has('path') ? $session->get('detail_ticket.' . $request->get('path') . '.page') : $session->get('detail_ticket.page'));

        if ($request->get('ticket_id', 'get')) {

            $ticket_data=$database->getRow("select ticket_id,ticket_no,ticket_email,subject,status,date_added,reference,customer_id from ticket where ticket_id='".$request->get('ticket_id', 'get')."'");

            $ticket_value_list = $database->getRows($database->splitQuery("select message,reply,staff,date_added,date_reply,reference from ticket_value where ticket_id='".$request->get('ticket_id', 'get')."' order by ticket_value_id desc", $page, 16));

            $ticket_value_data=array();

            foreach ($ticket_value_list as $tvl) {

                $customer_info=$database->getRow("select firstname,lastname from customer where customer_id='".$ticket_data['customer_id']."'");

                $order_info=$database->getRow("select order_id from `order` where reference='".$tvl['reference']."'");



                if (!$order_info) {

                    $order_info=$database->getRow("select order_id from `order_data` where reference='".$tvl['reference']."'");

                     $order_status_type=0;

                } else {

                     $order_status_type=1;

                }

                $ticket_value_data[]=array(

                   'order_id'=>$order_info['order_id'],

                   'message'=>$tvl['message'],

                   'reply'=>$tvl['reply'],

                   'staff'=>$tvl['staff'],

                   'date_added'=>$tvl['date_added'],

                   'date_reply'=>$tvl['date_reply'],

                   'reference'=>$tvl['reference'],

                   'fullname'=>$customer_info['firstname'].' '.$customer_info['lastname'],

                   'order_status_type'=>$order_status_type



                );

            }



            if (($request->has('path') ? $session->get('detail_ticket.' . $request->get('path') . '.page') : $session->get('detail_ticket.page'))==null) {

                $page = 1;

            } else {

                $page = ($request->has('path') ? $session->get('detail_ticket.' . $request->get('path') . '.page') : $session->get('detail_ticket.page'));

            }

            $last_ticket_info=$database->getRow("select ticket_value_id,staff from ticket_value where ticket_id='".$request->get('ticket_id', 'get')."' order by ticket_value_id desc");



            $view->set('text_results', "<dt><b>".$database->getTotal()."</b> Records</dt>");

            $view->set('entry_page', $language->get('entry_page'));

            $view->set('action', $url->href('ticket', 'page', array('detail_ticket' => $request->get('detail_ticket'),'ticket_id' => $request->get('ticket_id'))));

            $view->set('page', ($request->has('path') ? $session->get('detail_ticket.' . $request->get('path') . '.page') : $session->get('detail_ticket.page')));

            $page_data = array();

            for ($i = 1; $i <= $database->getPages(); $i++) {

                $page_data[] = array(

                    'text'  => $language->get('text_pages', $i, $database->getPages()),

                    'value' => $i

                );

            }

            $view->set('pages', $page_data);

            $view->set('path', $request->get('path'));

            $view->set('last_ticket_info',$last_ticket_info);

            $view->set('customer_info',$customer_info);



            $view->set('ticket_data',$ticket_data);

            $view->set('ticket_value_list',$ticket_value_data);

            $template->set('title', $ticket_data['ticket_no']);

        }



        $template->set('content', $view->fetch('content/detail_ticket.tpl'));

            $template->set($module->fetchAccount());

        $response->set($template->fetch('layout.tpl'));

    }

    public function page()

    {

        $request  =& $this->locator->get('request');

        $response =& $this->locator->get('response');

        $session  =& $this->locator->get('session');

        $url      =& $this->locator->get('url');



        if ($request->has('page', 'post')) {

            $session->set(($request->has('path') ? 'detail_ticket.' . $request->get('path') . '.page' : 'detail_ticket.page'), $request->get('page', 'post'));

        }

        $response->redirect($url->ssl('my_tickets&action=detail_ticket', FALSE, array('detail_ticket' => $request->get('detail_ticket'),'ticket_id' => $request->get('ticket_id'))));

    }

    public function pagelist()

    {

        $request  =& $this->locator->get('request');

        $response =& $this->locator->get('response');

        $session  =& $this->locator->get('session');

        $url      =& $this->locator->get('url');



        if ($request->has('page', 'post')) {

            $session->set(($request->has('path') ? 'ticket.' . $request->get('path') . '.page' : 'ticket.page'), $request->get('page', 'post'));

        }



        $response->redirect($url->ssl('my_tickets', FALSE, array('path' => $request->get('path'))));

    }

   public function ticket_reply()

   {

        $config   =& $this->locator->get('config');

        $currency =& $this->locator->get('currency');

        $customer =& $this->locator->get('customer');

        $database =& $this->locator->get('database');

        $language =& $this->locator->get('language');

        $module   =& $this->locator->get('module');

        $response =& $this->locator->get('response');

        $session  =& $this->locator->get('session');

        $template =& $this->locator->get('template');

        $url      =& $this->locator->get('url');

        $request  =& $this->locator->get('request');

        if ($request->isPost()) {

            $insert_id = $request->get('ticket_id', 'post');

            $sql_value="insert into ticket_value set ticket_id='?',message='?',reference='?',status='?',date_added=now()";

            $database->query($database->parse($sql_value,$insert_id,$request->get('message', 'post'),$request->get('reference', 'post'),1));

            $database->query("update ticket set status=0 where ticket_id='".$insert_id."'");

            echo '<script>alert("Reply successfully");window.history.back(-1);</script>';



        }

   }



}

