from flask import render_template, redirect, url_for, request
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder import ModelView, MasterDetailView, AppBuilder, BaseView, expose, has_access
from flask_appbuilder.models.sqla.filters import FilterEqual, FilterNotEqual, FilterStartsWith, FilterEqualFunction
from flask_appbuilder.actions import action
from . import appbuilder, db
from .models import *


class matchTypeView(ModelView):
    datamodel = SQLAInterface(MatchType)


class passReasonView(ModelView):
    datamodel = SQLAInterface(PassReason)


class opportunityReportModelView(ModelView):
    datamodel = SQLAInterface(opportunityReport)
    #base_filters = [['match_id', FilterNotEqual, 2]]

    @action("skipChecked", "Omit", "All checked records will be market Omit", "fa-rocket")
    def skipChecked(self, items):
        if isinstance(items, list):
            for item in items:
                item.match_id = 2
                self.datamodel.edit(item)
                #self.update_redirect()
        else:
            items.match_id = 2
            self.datamodel.edit(items)
        return redirect(request.referrer)

    @action("neutralChecked", "Neutral", "All checked records will be marked Neutral", "fa-rocket")
    def neutralChecked(self, items):
        if isinstance(items, list):
            for item in items:
                item.match_id = 6
                self.datamodel.edit(item)
                #self.update_redirect()
        else:
            items.match_id = 6
            self.datamodel.edit(items)
        return redirect(request.referrer)

    @action("goodChecked", "Good", "All checked records will be marked Good", "fa-rocket")
    def goodChecked(self, items):
        if isinstance(items, list):
            for item in items:
                item.match_id = 5
                self.datamodel.edit(item)
                #self.update_redirect()
        else:
            items.match_id = 5
            self.datamodel.edit(items)
        return redirect(request.referrer)


    base_order =   ('notif_date', 'desc')
    list_columns = ['match', 'notif_source', 'notif_details_link', 'notif_date', 'oppty_desc', 'oppty_source', 'pass_reason']
    show_columns = ['match', 'notif_source', 'notif_details_link', 'notif_link', 'notif_date', 'oppty_desc', 'oppty_link', 'oppty_source', 'naics', 'pop', 'setaside']
    add_columns =  ['match', 'notif_details', 'oppty_url', 'oppty_date', 'naics', 'pop', 'setaside']
    edit_columns = ['match', 'pass_reason']


    label_columns = {
        'notif_link':   'Bidmatch',
        'oppty_link':   'Opportunity',
        'naics':        'NAICS',
        'pop':          'PoP',
    }


class matchTypeMasterView(MasterDetailView):
    datamodel = SQLAInterface(MatchType)
    related_views = [opportunityReportModelView]


@appbuilder.app.errorhandler(404)
def page_not_found(e):
    return (
        render_template(
            "404.html", base_template=appbuilder.base_template, appbuilder=appbuilder
        ),
        404,
    )

db.create_all()
appbuilder.add_view(matchTypeView, 'Match Types', icon='fa-folder-open-o', category = "Opportunities")
appbuilder.add_view(passReasonView, 'Pass Reasons', icon='fa-folder-open-o', category = "Opportunities")
#appbuilder.add_view_no_menu(opportunityReportModelView)
appbuilder.add_view(matchTypeMasterView, 'Bid Opportunities', icon='fa-folder-open-o', category = "Opportunities")
appbuilder.add_view(opportunityReportModelView, 'Bid Details', icon='fa-folder-open-o', category = "Opportunities")
