<?php
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
require_once 'DAL.php';

$DLA = $DAL = new DAL('constant.php');
$default_header_load = 'http://android.kashancard.digihelp.net/';
$default_header_upload = 'Panel/core/php/';
if ($_REQUEST['request'] != null) {
    switch ($_REQUEST['request']) {
        case 'test':
            var_dump($_REQUEST);
            break;
        case 'query-select':
            $query = $_REQUEST['query'];
            $select = $DAL->Select($query);
            if (!$select) $select = [];
            echo json_encode($select, JSON_UNESCAPED_UNICODE);
            break;
        case 'query-do':
            $query = $_REQUEST['query'];
            $select = $DAL->DoCommand($query);
            echo($select ? "1" : "0");
            break;
        case 'query-do-id':
            $query = $_REQUEST['query'];
            echo $DAL->DoCommandID($query);
            break;
        case 'upload-image':
            $shop_id = $_REQUEST['shop_id'];
            $current = $DLA->Select("SELECT count(`ID`) as `count` FROM `app_acceptor_image_slider` WHERE imgFor='{$shop_id}'");
            $limit = $DLA->Select("SELECT `image_limit` as `limit` FROM `app_acceptor_list` WHERE `ID`='{$shop_id}'");
            $output = array('status' => false, 'message' => '', 'shop_id' => $shop_id, 'id' => '', 'src' => '');
            if ($current AND count($current) == 1 AND $limit AND count($limit) == 1) {
                if ((int)$current[0]['count'] < (int)$limit[0]['limit']) {
                    // mitone upload kone
                    $save = SAVE_UPLOAD_IMAGE($_REQUEST['base64'], $_REQUEST['name'], $_REQUEST['shop_id'], $default_header_upload);
                    switch ($save['status']) {
                        case '1':
                            $ID = $DLA->DoCommandID("INSERT INTO app_acceptor_image_slider (imgSrc, imgFor)VALUES ('{$save['path']}','{$save['id']}')");
                            $output['status'] = true;
                            $output['message'] = 'فایل ذخیره شد.';
                            $output['src'] = $default_header_load . $save['path'];
                            $output['id'] = $ID;
                            break;
                        case '0':
                            $output['status'] = false;
                            $output['message'] = 'فایل به درستی انتفال داده نشد.';
                            break;
                        default:
                            //file ghable shenasaye nist
                            $output['status'] = false;
                            $output['message'] = 'فایل قابل شناسایی نیست.';
                            break;
                    }

                } else {
                    //bayade yeki ro hazf konoe
                    $output['status'] = false;
                    $output['message'] = 'باید قبل از ارسال یک تصویر را حذف کنید.';
                }
            } else {
                //karbar vojod naradar ya limit 0 ast
                $output['status'] = false;
                $output['message'] = 'کاربری یافت نشد.';
            }
            echo json_encode($output, JSON_UNESCAPED_UNICODE);
            break;
        case 'delete-image':
            $id = $_REQUEST['img'];
            if (isset($id)) {
                $select = $DLA->Select("SELECT imgSrc FROM `app_acceptor_image_slider` WHERE ID=" . $id);
                if ($select AND count($select) === 1) {
                    $select = $select[0]['imgSrc'];
                    $select = str_replace($default_header_upload, '', $select);
                    unlink($select);
                    echo $DLA->DoCommand("DELETE FROM `app_acceptor_image_slider` WHERE `ID`=" . $id);
                } else {
                    echo 0;
                }
            } else {
                echo 0;
            }
            break;
        case 'select-image':
            $select = $DLA->Select(
                "(SELECT count(ID) - (SELECT image_limit
                  FROM app_acceptor_list
                  WHERE app_acceptor_list.ID = '{$_REQUEST['for']}') as `limit`
                  FROM app_acceptor_image_slider
                  WHERE imgFor = '{$_REQUEST['for']}')");

            if ($select AND $select[0]['limit'] > 0) {
                $DLA->DoCommand(
                    "DELETE FROM app_acceptor_image_slider WHERE imgFor='{$_REQUEST['for']}' LIMIT " . (int)$select[0]['limit']
                );
            }
            $select = $DLA->Select("SELECT *,CONCAT('" . $default_header_load . "','/',`imgSrc`) as `imgSrc` FROM `app_acceptor_image_slider` WHERE imgFor=" . $_REQUEST['for']);
            if (!$select) $select = [];
            echo json_encode($select, JSON_UNESCAPED_UNICODE);
            break;
        default:
            echo '[]';
            break;
    }
} else {
    echo '[]';
}


function SAVE_UPLOAD_IMAGE($base64, $name, $id, $default_header_upload)
{
    $status = '00';
    $base_to_php = explode(',', $base64);
    $data = base64_decode($base_to_php[1]);
    $filepath = "upload_image/acceptors/$id/";
    $name = uniqid() . '-' . $name;
    if (!is_dir($filepath)) mkdir($filepath, 0777, true);
    $status = (file_put_contents($filepath . $name, $data) ? "1" : '0');
    return array('status' => $status, 'path' => $default_header_upload . $filepath . $name, 'id' => $id);
}