<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Ticket extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {

        $this->load->view('pos/ticket_view');
    }

    public function selectticket() {
        $this->load->model('pos/menu_model');
        $data['tableToken'] = $this->menu_model->getsharedticketName($this->uri->segment(4));
        $data['tableName'] = $this->menu_model->gettableNameBid($this->uri->segment(4));
        $data['tableID'] = $this->uri->segment(4);
        $this->load->view('pos/ticket_view', $data);
    }

    public function gettickets() {
        $datas['tableID'] = $this->uri->segment(4);
//        $datas['tableID'] = 1;
        $this->load->model('pos/ticket_model');
        echo $this->ticket_model->gettickets($datas);
    }

    public function itemPlus() {


        $this->load->model('pos/menu_model');
        $productDetails = $this->menu_model->getprodDetailsBid($this->uri->segment(5));

//UPDATE QUANTITY IN XML       
        $xml = new DOMDocument('1.0', 'ISO-8859-1');
        $xml->preserveWhiteSpace = false;
        $xml->formatOutput = true;
        $xml->loadXml($this->readTicketXML($this->uri->segment(4)));
        $xml->saveXML();
//SELECT ROOT TAG TO UPDATE 
        $Xpath = new DOMXPath($xml);
        $results = $Xpath->query('/ticket/item');
        $TicketLineInfoDetails = $Xpath->query('/com.posics.pos.ticket.TicketInfo/list/com.posics.pos.ticket.TicketLineInfo');
        $baz_node_of_xml = $TicketLineInfoDetails->item(0);
        $root = $xml->documentElement;
        $at1 = 5;
        foreach ($TicketLineInfoDetails as $item) {
            $id = $item->getElementsByTagName('productid')->item(0)->textContent;

//            $product = $item->getElementsByTagName('product')->item(0)->textContent;
//            $price = $item->getElementsByTagName('price')->item(0)->textContent;
            $quantity = $item->getElementsByTagName('multiply')->item(0)->textContent;
//            $kot = $item->getElementsByTagName('kot')->item(0)->textContent;
//
//
            if ($id == $this->uri->segment(5)) {
                if ($this->uri->segment(6) == 1)
                    $item->getElementsByTagName("multiply")->item(0)->nodeValue = $quantity + 1;
                elseif ($this->uri->segment(6) == 2)
                    $item->getElementsByTagName("multiply")->item(0)->nodeValue = $quantity - 1;
                elseif ($this->uri->segment(6) == 3) {
                    $p1 = $xml->getElementsByTagName('property')->item($at1);


                    foreach ($p1->attributes as $atrbt1) {
                        if ($atrbt1->nodeName == 'value') {
                            if ($atrbt1->nodeValue == 'No') {
                                echo 'ok';


                                $xml->getElementsByTagName('property')->item($at1)->nodeValue = 'Yes';
//                        $xml->attributes()->value = ((string) $xml->attributes()->value) . 'Yes';
//                          $element = $atrbt1->item($at1);
//                          var_dump($element);
//                            $element->setAttribute('value', "Yes");
//                        $xml[$at1]->property->attributes()->value = 26;
                            }
                        }
                    }
                    $at1 = $at1 + 7;
                }

//                elseif ($this->uri->segment(6) == 3) {
//                    if ($kot == NO)
//                        $item->getElementsByTagName("kot")->item(0)->nodeValue = 1;
//                    elseif ($kot == 1)
//                        $item->getElementsByTagName("kot")->item(0)->nodeValue = 0;
//                }
            }
        }

        $xml->formatOutput = true;

        $xmldataitems = $xml->saveXML();

        $updateticket = array(
            'CONTENT' => $xmldataitems
        );
        $this->load->model('pos/ticket_model');
        $this->ticket_model->itemPlus($this->uri->segment(4), $updateticket);
        $data['tableID'] = $this->uri->segment(4);
        $this->load->view('pos/ticket_view', $data);
    }

    public function billSettlement() {

//UPDATE QUANTITY IN XML       
        $xml = new DOMDocument('1.0');
        $xml->loadXml($this->readTicketXML($this->uri->segment(4)));
        $xml->saveXML();
//SELECT ROOT TAG TO UPDATE 
        $Xpath = new DOMXPath($xml);
        $results = $Xpath->query('/ticket');
        $baz_node_of_xml = $results->item(0);
        $root = $xml->documentElement;

        foreach ($results as $ticket) {
            $status = $ticket->getElementsByTagName('status')->item(0)->textContent;

            if ($status == 0) {
                $ticket->getElementsByTagName("status")->item(0)->nodeValue = 1;
            }
        }

        $xml->formatOutput = true;

        $xmldataitems = $xml->saveXML();

        $updateticket = array(
            'CONTENT' => $xmldataitems
        );


        $this->load->model('pos/ticket_model');
        $this->ticket_model->itemPlus($this->uri->segment(4), $updateticket);
        $this->load->model('pos/tables_model');
        $data['status'] = $this->tables_model->getticketStatus();
        $data['floors'] = $this->tables_model->getfloors();
        if ($data['floors']) {
            $data['places'] = $this->tables_model->getplaces();
        }

        $this->load->view('pos/tables_view', $data);
    }

    public function deleteTicket() {

        $data['tableID'] = $this->uri->segment(4);
        $this->load->model('pos/ticket_model');
        $result = $this->ticket_model->deleteTicket($data);

        if ($result) {


            $this->load->model('pos/tables_model');
            $data['status'] = $this->tables_model->getticketStatus();
            $data['floors'] = $this->tables_model->getfloors();
            if ($data['floors']) {
                $data['places'] = $this->tables_model->getplaces();
            }

            $this->load->view('pos/tables_view', $data);
        }
    }

    function readTicketXML($tableid) {

        $this->load->model('pos/tables_model');
        $result = $this->tables_model->getsharedticket($tableid);

        if ($result) {
            return $content = $result[0]['CONTENT'];
        }
        else
            return null;
    }

}