<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 2/23/2019
 * Time: 10:59 PM
 */
$User = array("User" => "romouzi", "Pass" => "rmz_13111363rmz", 'GroupCode' => 3131);

$object = $request['object'];
$type = UserType($object['user']);
if ($type === 'direct') {
    switch ($request['header-type']) {
        case 'load':
            $temp = array();
            $select = $DLA->Select("SELECT * , concat(`fName`, ' ',`lName`) as `full-name` FROM `app_acceptor_profile` WHERE `ID` IN (SELECT client_id FROM app_client_credit_card GROUP BY client_id)");
            if ($select) {
                $counter = 0;


                foreach ($select as $row) {
                    $code2 = $DLA->Select("SELECT `KashanCardSerial` FROM `app_client_credit_card` WHERE `client_id`='{$row['ID']}' GROUP BY `KashanCardSerial`");
                    if ($code2) {
                        $item = '';
                        foreach ($code2 as $code) {
                            if (strlen($item) > 0) $item .= '<br>';
                            $item .= $code['KashanCardSerial'];
                        }
                        if (strlen($item) == 0) $code2 = "وارد نشده است";
                        else $code2 = $item;
                    } else {
                        $code2 = "وارد نشده است";
                    }

                    $cards = $DLA->Select("SELECT `card_number`,`Mode` FROM `app_client_credit_card` where `client_id`='{$row['ID']}' ");
                    $mode = -100;
                    if ($cards) {
                        $item = '';
                        foreach ($cards as $card) {
                            if (strlen($item) > 0) $item .= '<br>';
                            $item .= $card['card_number'];
                            $mode = $card['Mode'];
                        }
                        if (strlen($item) == 0) $cards = "وارد نشده است";
                        else $cards = $item;
                    } else {
                        $cards = "وارد نشده است";
                        $mode = -100;//دسترسی یافت نشد
                    }

                    $temp[] = array(
                        "counter" => ++$counter,
                        "ID" => $row['ID'],
                        "full-name" => $row['full-name'],
                        "rCode" => $row['pNC'],
                        "rCode2" => $code2,
                        "bank" => $cards,
                        "phone" => $row['pNumber'],
                        "time" => 'ندارد',
                        "time2" => 'ندارد',
                        "option" => Option($row['ID'], $counter - 1, $mode)
                    );
                }
                echo json_encode($temp, JSON_UNESCAPED_UNICODE);
            } else {
                echo "[]";
            }


//            $select = $DLA->Select("SELECT * FROM `app_client_register` order by `Time` desc");
//            if ($select) {
//                $counter = 0;
//                $temp = [];
//                foreach ($select as $row) {
//                    $profile = $DLA->Select("SELECT pNC,pNumber,fName,lName FROM `app_acceptor_profile` WHERE ID='{$row['profile_id']}'");
//                    $profile = $profile[0];
//                    $bank = $DLA->Select("SELECT card_number FROM `app_client_credit_card` WHERE client_id='{$row['profile_id']}'");
//                    $cards = '';
//                    if ($bank) foreach ($bank as $card) $cards .= $card['card_number'] . "<hr>";
//                    else $cards = 'ندارد';
//                    $counter += 1;
//                    $temp[] = array(
//                        "counter" => $counter,
//                        "ID" => $row['ID'],
//                        "full-name" => $profile['fName'] . ' ' . $profile['lName'],
//                        "rCode" => $profile['pNC'],
//                        "rCode2" => $row['NationalSecurityCode'],
//                        "bank" => $cards,
//                        "phone" => $profile['pNumber'],
//                        "time" => ConvertToJalali($row['Time'])['date'],
//                        "time2" => ($row['ATime'] !== null ? (ConvertToJalali($row['ATime'])['date']) : "در انتظار تایید."),
//                        "option" => Option($row['ID'], $counter - 1, $row['Mode'])
//                    );
//                }
//                echo json_encode($temp, JSON_UNESCAPED_UNICODE);
//            } else {
//                echo "[]";
//            }
            break;
        case 'scenario-change':
            $fanava = null;
            $state = '0';
            $webService = null;
//            if ($object['mode'] == 1) {
////                $select = $DLA->Select("SELECT `CardExpireDate`,`FirstName`,`LastName`,`NationalCode`,`Mobile`,`BankCardNumber` FROM `app_client_register` WHERE `ID`='{$object['ID']}'");
////                if ($select) {
////                    $select = $select[0];
////                    $data = array(
////                        "GroupCode" => 3131,
////                        "CategoryCode" => 220744,
////                        "FirstName" => $select['FirstName'],
////                        "LastName" => $select['LastName'],
////                        "NationalCode" => $select['NationalCode'],
////                        "Mobile" => (strlen($select['Mobile']) == 11 ? substr($select['Mobile'], 1, 11) : $select['Mobile']),
////                        "CardPan" => $select['BankCardNumber'],
////                        "CardExpireDate" => $select['CardExpireDate']
////                    );
////                    $webService = $WS->BusApi()->json('NoCodeCustomerServiceNafis', array_merge($User, GetParam($data)), true);
////                    $fanava = true;
////                    $webService = json_decode($webService)[0]->response;
////                    if ($webService->client->ResponseCode === '0000' OR $webService->client->ResponseCode === '8602') {
////                        $state = ($DLA->DoCommand("UPDATE `app_client_register` SET `Mode`='{$object['mode']}' , `ATime`=CURRENT_TIMESTAMP WHERE `ID`='{$object['ID']}'") ? "1" : "0");
////                    }
//                } else {
//                    $fanava = false;
//                }
//            } else {
//                $state = ($DLA->DoCommand("UPDATE `app_client_register` SET `Mode`='{$object['mode']}' , `ATime`=CURRENT_TIMESTAMP WHERE `ID`='{$object['ID']}'") ? "1" : "0");
//            }

            switch ($object['mode']) {
                case '-10':
                case'2':
                case'1':
                    $STATE = ($DLA->DoCommand("UPDATE `app_client_credit_card` SET `Mode`='{$object['mode']}' WHERE `client_id`='{$object['ID']}'") ? "1" : "0");
                    echo json_encode(array("state" => $STATE), JSON_UNESCAPED_UNICODE);
                    break;
                default:
                    echo 0;
                    break;
            }
            //$state = ($DLA->DoCommand("UPDATE `app_client_register` SET `Mode`='{$object['mode']}' , `ATime`=CURRENT_TIMESTAMP WHERE `ID`='{$object['ID']}'") ? "1" : "0");
            //echo json_encode(array('code' => $object['mode'], "state" => $state, 'fanava' => $fanava, 'ws' => $webService), JSON_UNESCAPED_UNICODE);
            break;
        case 'delete':
            echo($DLA->DoCommand("DELETE FROM `app_client_register` WHERE `ID`='{$object['ID']}'") ? "1" : "0");
            break;
        default:
            echo "[]";
            break;
    }
} else {
    echo 0;
}

function Option($ID, $index, $mode)
{

    $mode = "$mode";
    $html = '';
    $html .= '<div class="btn-group-vertical " role="group" aria-label="Vertical button group">';
    $html .= '<button data-index="' . $index . '" class="btn btn-xs btn-danger waves-effect" data-mod="client" data-action="delete-item" data-id="' . $ID . '"><i class="material-icons">delete</i></button>';

    $option = $GLOBALS['DLA']->Select("SELECT * FROM `app_client_scenario`");
    if ($option) {
        $html .= '<select data-id="' . $ID . '" class="form-control text-center scenario-change">';
        for ($i = 0; $i < count($option); $i += 1) {
            $row = $option[$i];
            $html .= "<option data-id='$ID' value='" . $row['code'] . "' " . ($row['code'] === $mode ? "selected" : "") . ">" . $row['name'] . "</option >";
        }
        $html .= '</select>';
    } else {
        $html .= '<button class="btn btn-xs btn-warning waves-effect">سناریو تعریف نشده است</button>';
    }
    $html .= '</div>';
    return $html;
}