<?php
//the global file operation
require_once('../library/http_call.php');
include("../public/impOpreation.php");
$do = $_GET['do'];

if (empty($do)) {
    $smarty->display("header.html");
    $smarty->display("electronicinvoiceview/index.html");
    $smarty->display("footer.html");
} else if ($do == 'showajax') {
    showajax();
} else if ($do == 'select2client') {
    select2client();     
}else if ($do == 'detail') {
    $smarty->display("header.html");
    $ebillsetting = R::getRow("SELECT * FROM `ebillsetting`");
    $smarty->assign('ebillsetting', $ebillsetting);  
    $uuid = $_GET['uuid'];
    $response = CURL_IT2(array('uuid'=> $uuid),  "eBillRequests.php?do=bills_detail");
    $receivedata = json_decode($response); 
    $smarty->assign('datas', $receivedata->data);  
    $smarty->display("electronicinvoiceview/detail.html");
    $smarty->display("footer.html");
}else if ($do == 'cancel') {
    $smarty->display("header.html");
    $ebillsetting = R::getRow("SELECT * FROM `ebillsetting`");
    $smarty->assign('ebillsetting', $ebillsetting);  
    $uuid = $_GET['uuid'];
    $response = CURL_IT2(array('uuid'=> $uuid),  "eBillRequests.php?do=bills_detail");
    $receivedata = json_decode($response); 
    $smarty->assign('datas', $receivedata->data);  
    $smarty->display("electronicinvoiceview/detail.html");
    $smarty->display("footer.html");
}


function select2client() {
    $row_array = array();
    $return_arr = array();
    $name = $_POST['searchTerm'];
    
    $productsData = R::getAll("SELECT taxnumber, clientname as name
    FROM client 
    WHERE clientname LIKE '%" . $name . "%'  limit 50");
    foreach ($productsData as $pro) {
        $row_array['id'] = $pro['taxnumber'];
        $row_array['text'] = $pro['name'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}

function showajax() {
    $ebillsetting = R::getRow("SELECT * FROM `ebillsetting`");
    $eexport = filter_input(INPUT_POST, 'eexport');
    $emport = filter_input(INPUT_POST, 'emport');
    $client = filter_input(INPUT_POST, 'client');

    $response = CURL_IT2(array(
        'pageNo'=> ($_POST['length'] + $_POST['start']) / $_POST['length'],
        'pageSize'=>$_POST['length'], 'eexport'=>$eexport, 'emport'=>$emport, 'client'=>$client, 'eTaxNum'=>$ebillsetting['eTaxNum']),  "eBillRequests.php?do=bills");
    $receivedata = json_decode($response);   
   

    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $output = array(  
        "draw" => intval($_POST["draw"]),
        "recordsTotal" => count($receivedata->data->items),
        "recordsFiltered" => $receivedata->data->total,
        "data" => array()
    );
   
  
       foreach ($receivedata->data->items as $row) {
        $sub_array = array();
        $sub_array[] = $row->dateTimeReceived;
        $sub_array[] = $row->uuid;
        $sub_array[] = $row->internalId;
        $sub_array[] = $row->documentTypeNameSecondaryLang;
        $sub_array[] = $row->issuerName;
        $sub_array[] = $row->receiverName;
        if ($ebillsetting['eTaxNum'] == $row->issuerId){
              $sub_array[] = 'صادر';
        }else{
              $sub_array[] = 'وارد     ';

        }
        if ($row->cancelRequestDate && $row->status == 'Valid'){
             $sub_array[] = $row->status . ' ->  cancel';
             $sub_array[] = '';
        }elseif ($row->status == 'Valid'){ 
            $sub_array[] = $row->status;    
            $sub_array[] = '<div id="cancel'.$row->uuid.'"><a data-uuid="'.$row->uuid.'" data-sellbillid="'.$row->internalId.'" type="button" class="btn btn-default  sendcancel" target="_blank">cancel</a><div>';
        }else{
           $sub_array[] = $row->status; 
           $sub_array[] = '';
        }
        
        $sub_array[] = '<a href="electronicinvoice.php?do=detail&uuid='.$row->uuid.'" type="button" class="btn btn-default btn-lg notesicon" target="_blank"></a>';
        $output['data'][] = $sub_array;
       }
    
       echo json_encode($output, JSON_UNESCAPED_SLASHES);

}
function CURL_IT2($data_arr, $url) {
    ##preparing session data
    $url = 'http://' . $_SERVER['HTTP_HOST'] . explode('controllers', $_SERVER['REQUEST_URI'])[0] . 'controllers/' . $url;

    ##appending necessary data
    $data_arr['curlpost'] = '1';
    $data_arr['sessionlist'] = json_encode($_SESSION);


    // set post fields
//    $post = [
//        'data_arr' => json_encode($data_arr), //encode it as it is array
//    ];
    $post = $data_arr;
    $ch = curl_init($url);
    //
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //
    //Disable CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER by
    //setting them to false.
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_TIMEOUT, 10); //
    // execute!
    $response = curl_exec($ch);
    if ($response === false) {
        //echo 'Error : ' . curl_error($ch);
    }

    // close the connection, release resources used
    curl_close($ch);
    // do anything you want with your response
    //var_dump($response);
    //print_r($response);
    return $response;
}

?>