<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 3/27/2019
 * Time: 8:38 AM
 */
$data = $request['object'];
switch ($request['data-header']) {
    case 'agent-report-2':
        $query = '';
        $qTime = '';
        $qCode = '';
        $qFullName = '';
        if (strlen($data['start']) > 0 AND strlen($data['end']) == 0) {
            //senario 1 :az start ta akhar
            $start = ConvertToGregorian($data['start'], true);
            $qTime = "(`StartDate` >= CAST('" . ($start[0] . '/' . $start[1] . '/' . $start[2]) . " 00:00:01' AS datetime))";
        } elseif (strlen($data['start']) == 0 AND strlen($data['end']) > 0) {
            //senario 2 :az aval ta end
            $end = ConvertToGregorian($data['end'], true);
            $qTime = "(`EndDate` <= CAST('" . ($end[0] . '/' . $end[1] . '/' . $end[2]) . " 23:59:59' AS datetime))";
        } elseif (strlen($data['start']) > 0 AND strlen($data['end']) > 0) {
            //senario 3 :az start ta end
            $start = ConvertToGregorian($data['start'], true);
            $end = ConvertToGregorian($data['end'], true);
            $qTime = "`StartDate` BETWEEN ('" . ($start[0] . '/' . $start[1] . '/' . $start[2]) . " 00:00:01') AND ('" . ($end[0] . '/' . $end[1] . '/' . $end[2]) . " 23:59:59')";
        } else {
            $qTime = '';
        }

        if (strlen($data['code']) > 0) {
            $qCode = "`visitor_id`='{$data['code']}'";
        } else {
            $qCode = '';
        }

        if (strlen($data['full_name']) > 0) {
            $qFullName = "(CONCAT(`v_first_name`,' ',`v_last_name`))='{$data['full_name']}'";
        } else {
            $qFullName = '';
        }

        if (strlen($qTime) > 0) {
            $query .= $qTime;
        }

        if (strlen($qCode) > 0) {
            if (strlen($query) > 0)
                $query .= ' AND ';
            $query .= $qCode;
        }

        if (strlen($qFullName) > 0) {
            if (strlen($query) > 0)
                $query .= ' AND ';
            $query .= $qFullName;
        }

        if (strlen($query) > 0)
            $query = ' WHERE ' . $query;

        $query = "SELECT * FROM `app_visitor_log` " . $query . " ORDER BY `visitor_id`";
        $select = $DAL->Select($query);
        $outArray = array();
        if ($select) {
            function get_time_difference($time1, $time2)
            {
                if ($time1 == null OR $time2 == null) return "زمان خروج ثبت نشده.";
                $time1 = strtotime($time1);
                $time2 = strtotime($time2);

                return ($time2 - $time1) . ' ثانیه ';

            }

            $index = 0;
            foreach ($select as $row) {
                $time = ConvertToJalali($row['StartDate']);


                $result = json_decode($row['result_add_log'], false)[0];
                $card = $result->message->card;
                $client = $result->message->client;


                $input_log = json_decode($row['input_add_log'], false);
                $outArray [] = array(
                    'index' => ++$index,
                    'code' => $row['visitor_id'],
                    'full-name' => $row['v_first_name'] . ' ' . $row['v_last_name'],
                    'card-state' => '<pre class="text-right" dir="rtl">' . "[{$input_log->object->CardPan}]-[{$card->ResponseMessage}]-[کارت] " . '</pre>',
                    'client-state' => '<pre class="text-right" dir="rtl">' . "[{$input_log->object->Mobile}]-[{$client->ResponseMessage}]-[مبایل] " . '</pre>',
                    'transaction-time' => $time['date'],
                    'transaction-def' => $time['def'] . ' روز قبل ',
                );
//                $outArray[] = array(
//                    'index' => ++$index,
//                    'full-name' => $row['v_first_name'] . ' ' . $row['v_last_name'],
//                    'client-state' => '<pre class="text-right">' . "[کد خطا]=>[{$client->ResponseCode}]" . "\n" . "[پیام خطا]=>[{$client->ResponseMessage}]" . "\n" . "[فیلد خطا]=>[{$client->ErrorFieldName}]" . "\n" . '</pre>',
//                    'card-state' => '<pre class="text-right">' . "[کد خطا]=>[{$card->ResponseCode}]" . "\n" . "[پیام خطا]=>[{$card->ResponseMessage}]" . "\n" . "[فیلد خطا]=>[{$card->ErrorFieldName}]" . "\n" . '</pre>',
//                    'in-system' => get_time_difference($row['StartDate'], $row['EndDate']),
//                    'input-log' => ' <code dir="ltr" style="width: 100px!important;">' . $row['input_add_log'] . '</code>',
//                    'output-log' => '<code dir="ltr" style="width: 100px!important;">' . $row['output_add_log'] . '</code>',
//                    'result-log' => '<code dir="ltr" style="width: 100px!important;">' . $row['result_add_log'] . '</code>',
//                    'transaction-time' => $time['date'],
//                    'transaction-def' => $time['def'] . ' روز قبل ',
//                );

            }
        }
//        echo $query;
        echo json_encode($outArray, JSON_UNESCAPED_UNICODE);
        break;
    default:
        echo 0;
        break;
}