<?php


switch ($request['header-type']) {
    case 'change-agent-status':
        $object = $request['object'];
        $out = 0;
        $agent = $object['agent'];
        $status = $object['status'];
        if ((int)$agent > 0 AND ((int)$status == 0 OR (int)$status == 1)) {
            $out = (int)$DAL->cDoCommand(
                "UPDATE `app_agent_recycling_system` SET `is_active`=? WHERE `agent_id`=?",
                array('i' => (int)$status),
                array('i' => (int)$agent)
            );
        }
        echo $out;
        break;
    case 'add-agent':
        $object = $request['object'];
        $out = 0;
        $agent = $object['agent'];
        $select = $DLA->cSelect("SELECT ID FROM `app_acceptor_profile` WHERE `ID`=?",
            array('i' => $agent)
        );

        if ($select && count($select) == 1) {
            $ID = $select[0]['ID'];

            $out = $DLA->cDoCommandID("INSERT INTO `app_agent_recycling_system` (`agent_id`) VALUES (?);",
                array('i' => $ID)
            );
            if ($out > 0) {
                $out = 1;
            } else {
                $out = 0;
            }
        }


        echo $out;
        break;
    case 'remove-agent':
        echo '0';
        break;
    case 'pool'://(SELECT app_acceptor_profile.fName FROM app_acceptor_profile WHERE app_acceptor_profile.ID=725)
        $query = "SELECT *, (SELECT CONCAT(app_acceptor_profile.fName,' ', app_acceptor_profile.lName) FROM app_acceptor_profile WHERE app_acceptor_profile.ID=recycling_agent_id) as `agent-name`,(SELECT `name` FROM `app_recycling_system` WHERE `key` LIKE ? AND `code`=request_status) as status,(SELECT `name` FROM `app_recycling_system` WHERE `key` LIKE ? AND `code`=satisfaction_rate) as satisfaction FROM `app_recycling_request_system` WHERE `package_key`=(SELECT `code` FROM `app_recycling_system` WHERE `key` LIKE ? AND `name` LIKE ?)";
        $select = $DLA->cSelect($query,
            array('s' => 'request_status'),
            array('s' => 'satisfaction_rate'),
            array('s' => 'package'),
            array('s' => $package_name)
        );


        $table = '';
        $counter = 0;
        foreach ($select as $record) {
            $row = "";
            $row .= "<tr dir='rtl' itemid='" . $record['ID'] . "'>";

            $row .= "<td>" . "<button data-action='request-action' data-mode='add-agent' data-id='" . $record['ID'] . "' class='btn btn-default status waves-effect waves-amber '>" . (strlen($record['agent-name']) > 0 ? $record['agent-name'] : 'ثبت نشده') . "</button>" . "</td>";
            $row .= "<td>" . "<button data-action='request-action' data-mode='add-status' data-id='" . $record['ID'] . "' class='btn btn-default status waves-effect waves-amber '>" . $record['status'] . "</button>" . "</td>";
            $row .= "<td>" . (strlen($record['satisfaction']) > 0 ? $record['satisfaction'] : 'وارد نشده') . "</td>";
            $row .= "<td>" . ($record['type_name']) . "</td>";
            $row .= "<td>" . ($record['weight_name']) . "</td>";
            $row .= "<td>" . ($record['delivery_date'] . "<br>" . $record['delivery_time']) . "</td>";
            $row .= "<td>" . "<button data-action='request-action' data-mode='show-location' data-value='" . $record['select_location'] . "' class='btn btn-default btn-show-location waves-effect waves-amber '>نمایش</button>" . "</td>";
            $row .= "<td>" . "<button data-action='request-action' data-mode='show-location' data-value='" . $record['client_location'] . "' class='btn btn-default btn-show-location waves-effect waves-amber '>نمایش</button>" . "</td>";
            $row .= "<td>" . (strlen($record['number_2']) == 0 ? "وارد نشده." : $record['number_2']) . "</td>";
            $row .= "<td>" . ($record['address']) . "</td>";
            $row .= "<td>" . ($record['nc']) . "</td>";
            $row .= "<td>" . ($record['mobile']) . "</td>";
            $row .= "<td>" . ($record['name'] . ' ' . $record['family']) . "</td>";
            $row .= "<td>" . ($record['date']) . "</td>";
            $row .= "<td>" . ($record['ID']) . "</td>";
            $row .= "<td>" . (++$counter) . "</td>";

            $row .= "</tr>";
            $table .= $row . "\n";

        }
        echo $table;
        break;
    case 'status-request':
        $object = $request['object'];
        $out = 0;
        $id = $object['request'];
        $status = $object['status'];
        if ((int)$id > 0 AND ((int)$status > 0)) {
            $out = (int)$DAL->cDoCommand(
                "UPDATE `app_recycling_request_system` SET `request_status`=? WHERE `ID`=?",
                array('i' => (int)$status),
                array('i' => (int)$id)
            );
        }
        echo $out;
        break;

    case'load-agent';
        $out = '';

        $query = "SELECT `ID`,`pNC` as `nc` ,`pNumber` as `mobile`,CONCAT(`fName`, ' ', `lName`) as `fullName` FROM app_acceptor_profile WHERE ID IN (SELECT `agent_id` FROM `app_agent_recycling_system` WHERE  is_active=true)";
        $select = $DLA->cSelect($query);
        if ($select) {
            foreach ($select as $row) {

                $out .= '<div class="col-md-4"><button data-code="' . $row['ID'] . '" type="button" data-color="brown"';
                $out .= 'class="model-option-agent-select btn bg-light-blue btn-block waves-effect" data-text="' . $row['fullName'] . '">';
                $out .= "" . $row['fullName'] . '<br>' . $row['nc'] . '<br>' . $row['mobile'] . "";
                $out .= '</button></div>';

            }
        }
        echo $out;
        break;
    case 'add-agent-for-request':
        $object = $request['object'];
        $out = 0;
        $request_id = $object['request'];
        $agent_id = $object['agent'];

        if ((int)$agent_id > 0 AND ((int)$request_id > 0)) {
            $out = (int)$DAL->cDoCommand(
                "UPDATE `app_recycling_request_system` SET `recycling_agent_id`=? WHERE `ID`=?",
                array('i' => (int)$agent_id),
                array('i' => (int)$request_id)
            );
        }
        echo $out;
        break;
    default:
        echo 0;
        break;
}
