# -*- coding: utf-8 -*-

"""
    Request Management System - Controllers
"""

module = request.controller

if module not in deployment_settings.modules:
    session.error = T("Module disabled!")
    redirect(URL(r=request, c="default", f="index"))

# Options Menu (available in all Functions' Views)
response.menu_options = [
    [T("Home"), False, URL(r=request, f="index")],
    [T("Requests"), False, URL(r=request, f="req")],
#    [T("Requests"), False, URL(r=request, f="req"),
#     [T("Add"), False, URL(r=request, f="req", args="create")],
#     ],
    [T("All Requested Items"), False, URL(r=request, f="ritem")],
    [T("All Pledges"),False, URL(r=request, f="pledge")]
]

# S3 framework functions
def index():
    "Module's Home Page"

    """ Default to the rms_req list view - TODO does not work with paginate!!!"""

    request.function = "req"
    request.args = []
    return req()

    #module_name = deployment_settings.modules[module].name_nice

    #return dict(module_name=module_name, a=1)

def req():
    """ RESTful CRUD controller """

    resource = request.function
    tablename = module + "_" + resource
    table = db[tablename]

    # Don't send the locations list to client (pulled by AJAX instead)
    table.location_id.requires = IS_NULL_OR(IS_ONE_OF_EMPTY(db, "gis_location.id"))

    # Pre-processor
    def prep(r):
        if r.representation in ("html", "popup"):
            if r.method == "create":
                table.timestmp.default = request.utcnow
                person = session.auth.user.id if auth.is_logged_in() else None
                if person:
                    person_uuid = db(db.auth_user.id == person).select(db.auth_user.person_uuid, limitby=(0, 1)).first().person_uuid
                    person = db(db.pr_person.uuid == person_uuid).select(db.pr_person.id, limitby=(0, 1)).first().id
                table.person_id.default = person
                table.pledge_status.readable = False
            elif r.method == "update":
                table.pledge_status.readable = False
            shn_action_buttons(r)
        return True
    response.s3.prep = prep

    # Filter out non-actionable SMS requests:
    #response.s3.filter = (db.rms_req.actionable == True) | (db.rms_req.source_type != 2) # disabled b/c Ushahidi no longer updating actionaable fielde

    # Post-processor
    def req_postp(jr, output):
        if jr.representation in ("html", "popup"):
            if not jr.component:
                response.s3.actions = [
                    dict(label=str(T("Open")), _class="action-btn", url=str(URL(r=request, args=["update", "[id]"]))),
                    dict(label=str(T("Items")), _class="action-btn", url=str(URL(r=request, args=["[id]", "ritem"]))),
                    dict(label=str(T("Pledge")), _class="action-btn", url=str(URL(r=request, args=["[id]", "pledge"])))
                ]
            elif jr.component_name == "pledge":
                response.s3.actions = [
                    dict(label=str(T("Details")), _class="action-btn", url=str(URL(r=request, args=["pledge", "[id]"])))
                ]
        return output
    response.s3.postp = req_postp

    response.s3.pagination = True
    output = shn_rest_controller(module, resource,
                                 editable=True,
                                 #listadd=False,
                                 rheader=shn_rms_rheader)

    return output

def ritem():
    "RESTful CRUD controller"
    resource = request.function
    tablename = "%s_%s" % (module, resource)
    table = db[tablename]

    def postp(jr, output):
        shn_action_buttons(jr)
        return output
    response.s3.postp = postp

    #rheader = lambda jr: shn_item_rheader(jr,
    #                                      tabs = [(T("Requests for Item"), None),
    #                                              (T("Inventories with Item"), "location_item"),
    #                                              (T("Requests for Item"), "req"),
    #                                             ]
    #                                     )

    return shn_rest_controller(module,
                               resource,
                               #rheader=rheader
                               )