<?php
defined('_JEXEC') or die('Restricted access');
/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/


class AdminViewOrders{

    static function orders($orders, $search, &$pageNav, $entity_list, $order_entity_array, $order_prices){
        global $user, $app, $doc, $db, $entityFieldLists;

        $db = new os_cckDb();

        $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />".cck_lang_text('COM_OS_CCK_SHOW_ORDERS')."</div>";
        $app->JComponentTitle = $html;
        $countOrders = count($orders);
        $app = JFactory::getApplication();
        $input = $app->input;
        $option = $input->get('option', '', 'STRING');
        $order = $input->get('order', 'ASC', '');
        $order_sring = ($order == 'ASC') ? '&order=DESC' : '&order=ASC';
        
        $optionStatus[] = JHTML::_('select.option','Pending', "Pending");
        $optionStatus[] = JHTML::_('select.option','Completed', 'Completed');
        $optionStatus[] = JHTML::_('select.option','Chargeback', 'Chargeback');
        $optionStatus[] = JHTML::_('select.option','Cancel', 'Cancel');
        $optionStatus[] = JHTML::_('select.option','Refund', 'Refund');

        $form_name = 'adminForm';
        $form_id = 'adminForm' ;
        $table = new os_cckViewTable($form_name, $form_id, 'cck-orders-main', 'index.php?option=com_os_cck&task=orders', 'orders');

        $what_check = 'instance' ;
        $table->create_button( cck_lang_text("COM_OS_CCK_BUTTON_DELETE_ORDER"), 'deleteOrder', 
            JURI::root() . 'components/com_os_cck/images/btn_d.png', 'fa-close','',$what_check);
        $table->create_button(cck_lang_text("COM_OS_CCK_LABEL_BUTTON_MARK_AS_VIEWED"), 'is_readed_orders', 
            JURI::root() . 'components/com_os_cck/images/btn_s.png', 'fa-check','',$what_check);
        

        $table->create_filters('<div class="search_block">
        <input type="text" placeholder="' . cck_lang_text('COM_OS_CCK_SHOW_SEARCH') .'" name="search" value="'. $search .'" class="inputbox" onChange="document.'.$form_name.'.submit();"/>
        <button type="submit" class="cck_search_button" title="" data-original-title="Search"><span class="icon-search"></span></button>
        </div>');
        $table->create_filters('<div>' . $entity_list .'</div>');
        $table->limitbox = '<div class="btn-group pull-right hidden-phone">
        <label for="limit" class="element-invisible">'. cck_lang_text('OS_LIB_SEARCH_SEARCHLIMIT_DESC') .'</label>'. $pageNav->getLimitBox() .'</div>';

        $table->create_first_row_collumn('<input type="checkbox" nal;me="toggle" value="" onClick="Joomla.checkAll(this);" />', '', 'admin-order-standart-fields-checkbox');
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=orders&orderby=id'. $order_sring .'">'. cck_lang_text("COM_OS_CCK_ORDERS_ID") .'</a>', '', 'admin-order-standart-fields-id');
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=orders&orderby=email'. $order_sring .'">'. cck_lang_text("COM_OS_CCKORDERS_EMAIL") .'</a>', '', 'admin-order-standart-fields-email');
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=orders&orderby=order_date'. $order_sring .'">'. cck_lang_text("COM_OS_CCK_ORDERS_DATE") .'</a>', '', 'admin-order-standart-fields-date');
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=orders&orderby=status'. $order_sring .'">'. cck_lang_text("COM_OS_CCK_ORDERS_STATUS") .'</a>', '', 'admin-order-standart-fields-status');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_PRICE"), '', 'admin-order-standart-fields-price');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_PAID"), '', 'admin-order-standart-fields-paid');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_DOWNLOADS"), '', 'admin-order-standart-fields-downloads');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_DETAILS"), '', 'admin-order-standart-fields-details');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_REQUEST"), '', 'admin-order-standart-fields-request');


        $data_first_row = array();
        for($i = 0; $i < $countOrders; $i++) { 
            $order_data = array();
            $data_first_row[$i] = array();
            if($orders[$i]->instance_type == "Rent"){
                $task = "edit_rent_request_instance";
            }else{
                $task = "edit_buy_request_instance";
            }
            
            $order_data[] = JHTML::_('grid.id', $i,$orders[$i]->id, false, 'cb');
            $order_data[] = $orders[$i]->id;
            $order_data[] = $orders[$i]->user_email;
            $order_data[] = $orders[$i]->order_date;

            $status = $orders[$i]->status;

            if($status == 'Completed' || $status == 'Success'){
                $status = 'Completed';
            }
            $attr = 'class="inputbox input-medium" size="1"
                onchange="return listItemTask(\'cb'.$i.'\',\'changeOrderStatus\',\''.$form_name.'\')"';
                
            $order_data[] = JHTML::_('select.genericlist',$optionStatus, 'order_status['.
                $orders[$i]->id.']', $attr, 'value', 'text', $status);

            $order_data[] = $orders[$i]->order_price." ".$orders[$i]->order_currency;
            $order_data[] = $orders[$i]->paid_price." ".$orders[$i]->paid_currency;
            $order_data[] = $orders[$i]->number_of_downloads;
            $order_data[] = '<a href="index.php?option=com_os_cck&task=order_details&order_id='.$orders[$i]->id .'">' . cck_lang_text("COM_OS_CCK_ORDERS_DETAILS") . '</a>';
            $order_data[] = '<a href="index.php?option=com_os_cck&task='.$task.'&eiid=' . $orders[$i]->fk_request_id .'">'. cck_lang_text("COM_OS_CCK_ORDERS_REQUEST") .'</a>';


            $style = ''; 
            $class = 'row' . ($i % 2) . ' '; 
            if($orders[$i]->notreaded == 1){
                $class .= ' admin-order-not-readed';
            }
            $table->create_first_row_data($order_data, $style, $class);

            $requestIstance = new os_cckEntityInstance($db->_db);
            $requestIstance->load($orders[$i]->fk_request_id);

            //second row add always(also with empty data), count the second rows Must Be === count first row !!!!!!
            os_cck_site_controller::second_row_prepere($table,$orders[$i],'show_in_orders',array($orders[$i]->fk_request_id));

        }


//        $table->paginator = $pageNav->getListFooter();
        $table->pageNav = $pageNav;

        echo $table->viewTable();
    }

    static function order_details($orders, $search, &$pageNav){
        global $user, $app, $doc, $app;
        $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />".cck_lang_text('COM_OS_CCK_SHOW_ORDERS')."</div>";
        $app->JComponentTitle = $html;
        $countOrders = count($orders);
        $orderId = protectInjectionWithoutQuote('order_id');
        $optionStatus[] = JHTML::_('select.option','Pending', "Pending");
        $optionStatus[] = JHTML::_('select.option','Completed', 'Completed');
        
        $form_name = 'adminForm';
        $form_id = 'adminForm' ;
        $table = new os_cckViewTable($form_name, $form_id, 'cck-orders-details-main', 
            'index.php?option=com_os_cck&task=orders', 'order_details');
        
        $table->create_button('Cancel', 'orders', JURI::root() . 'components/com_os_cck/images/btn_d.png', 'fa-close');
        
        $table->create_filters('<div class="search_block">
        <input type="text" placeholder="' . cck_lang_text('COM_OS_CCK_SHOW_SEARCH') .'" name="search" value="'. $search .'" class="inputbox" onChange="document.'.$form_name.'.submit();"/>
        <button type="submit" class="cck_search_button" title="" data-original-title="Search"><span class="icon-search"></span></button>
        </div>');
        
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=order_details&orderby=user&order_id='.$orderId.'">User</a>', 'width:8%', '');
        $table->create_first_row_collumn('Username', 'width:8%', '');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCKORDERS_EMAIL"), 'width:10%', '');
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=order_details&orderby=email&order_id='.$orderId.'">'.cck_lang_text("COM_OS_CCK_ORDERS_DATE").'</a>', 'width:8%', '');
        $table->create_first_row_collumn('<a href="index.php?option=com_os_cck&task=order_details&orderby=status&order_id='.$orderId.'">'.cck_lang_text("COM_OS_CCK_ORDERS_STATUS").'</a>', 'width:8%', '');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_TITLE"), 'width:8%', '');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_PRICE"), 'width:4%', '');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_PAID"), 'width:4%', '');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_ORDERS_DETAILS"), 'width:7%', '');
        $table->create_first_row_collumn(cck_lang_text("COM_OS_CCK_LABEL_INPUT_COMMENT"), 'width:12%', '');
        for($i = 0; $i < $countOrders; $i++) {
            $details_data = array();
            if($orders[$i]->instance_type == "Rent"){
                $task = "edit_rent_request_instance";
            }else{
                $task = "edit_buy_request_instance";
            }
            
            $payment_details = unserialize($orders[$i]->payment_details);
            $details_text='';
            if($orders[$i]->txn_type)
                $details_text = cck_lang_text("COM_OS_CCK_ORDERS_DET_ACCEPT").$orders[$i]->txn_type;
            if(!empty($payment_details)){
                if(isset($payment_details['view']))
                    $details_text .= '<br>'.cck_lang_text("COM_OS_CCK_ORDERS_DET_SYSTEM").$payment_details['view'];
                if(isset($payment_details['payer_email']))
                    $details_text .= '<br>'.cck_lang_text("COM_OS_CCK_ORDERS_DET_EMAIL").'<br>'.$payment_details['payer_email'];
                if(isset($payment_details['pending_reason']))
                    $details_text .= '<br>'.cck_lang_text("COM_OS_CCK_ORDERS_DET_REASON").'<br>'.$payment_details['pending_reason'];
            }

            if(!isset($orders[$i]->user_name) && $orders[$i]->user_name == '') {
                $details_data[] = cck_lang_text("COM_OS_CCK_LABEL_ANONYMOUS");
                // echo cck_lang_text("COM_OS_CCK_LABEL_ANONYMOUS");
            } else {
                
                $details_data[] = $orders[$i]->user_name;
            }

            if(!isset($orders[$i]->username) && $orders[$i]->username == '' ) {
                $details_data[] = cck_lang_text("COM_OS_CCK_LABEL_ANONYMOUS");
            } else {
                $details_data[] = $orders[$i]->username;
            }

            $details_data[] = $orders[$i]->user_email;
            $details_data[] = $orders[$i]->order_date;
            $details_data[] = $orders[$i]->status;
            $details_data[] = '<a href="index.php?option=com_os_cck&task='.$task.'&eiid=' . $orders[$i]->fk_request_id.'">'.$orders[$i]->instance_title.'</a>';
            $details_data[] = $orders[$i]->i_price." ".$orders[$i]->i_unit;
            $details_data[] = $orders[$i]->order_price." ".$orders[$i]->order_currency;
            $details_data[] = $details_text;
            $details_data[] = $orders[$i]->comment;
            $table->create_first_row_data($details_data);

        }

//        $table->paginator = $pageNav->getListFooter();
        $table->pageNav = $pageNav;

       
        
        echo $table->viewTable();
        
    }
    
    static function changeOrderStatus($orderId, $status){
        global $app;

        $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />".cck_lang_text('COM_OS_CCK_SHOW_ORDERS')."</div>";
        $app->JComponentTitle = $html;

        $form_name = 'adminForm';
        $form_id = 'adminForm' ;
        $table = new os_cckViewTable($form_name, $form_id, 'cck-orders-details-main', 'index.php?option=com_os_cck&task=orders');

        $what_check = 'fields' ;
        $table->create_button('Change status', 'updateOrderStatus', 
            JURI::root() . 'components/com_os_cck/images/btn_d.png', 'fa-check','',$what_check);
        $table->create_button('Cancel', 'orders', JURI::root() . 'components/com_os_cck/images/btn_d.png', 'fa-close');

        ob_start();
?>
        <br>
        <h2><?php echo cck_lang_text("COM_OS_CCK_LABEL_INPUT_ORDER_DETAILS"); ?>:</h2>
        <br>
        <h3><?php echo cck_lang_text("COM_OS_CCK_LABEL_INPUT_COMMENT"); ?>*</h3>
        <textarea class="order_comment required" type="text" name="comment" rows="5" style="width: 350px;"></textarea>
        <input type="hidden" name="option" value="com_os_cck" />
        <input type="hidden" name="task" value="updateOrderStatus" />
        <input type="hidden" name="orderId" value="<?php echo $orderId;?>" />
        <input type="hidden" name="status" value="<?php echo $status;?>" />
        <br>
    <?php 
        $custom_html_text = ob_get_contents();
        ob_end_clean();
        $table->set_custom_html($custom_html_text);

        echo $table->viewTable();
       
    }
}
