# -*- coding: utf8 -*-

from flask import request, render_template, redirect, Response, jsonify
from flask.ext.paginate import Pagination as PG
from sqlalchemy import cast, Numeric

from flask.views import MethodView

from app import app, db

class Pagination(PG):
    def page_href(self, page):
        return unicode(super(Pagination, self).page_href(page), 'utf-8')

# представление для af3_fond
from .models import Af3_fond

ROW2PAGE = 15

class Af3_fondView(MethodView):
    ROW2PAGE = ROW2PAGE
    def get(self):
        page = int(request.args.get('page', 1))
        lists = Af3_fond.query.order_by(cast(Af3_fond.fkod, Numeric(10, 4))).limit(self.ROW2PAGE).offset((page-1)*self.ROW2PAGE)
        total = Af3_fond.query.count()
        pagination = Pagination(lists=lists, page=page,  total=total, record_name=u'фондов',
                bs_version=3, display_msg = u' <b>{start} - {end}</b> {record_name} из <b>{total}</b>',
                alignment='right', prev_label=u'назад', next_label=u'далее', per_page=self.ROW2PAGE)
        return render_template('af3_fond.html', lists=lists, pagination=pagination)
    def post(self):
        pass

# представление для af3_opis
from .models import Af3_opis
from .forms import Af3_fondForm

class Af3_opisView(MethodView):
    ROW2PAGE = ROW2PAGE
    def get(self, fond_id=None):
        page = int(request.args.get('page', 1))
        fond = Af3_fond.query.get(fond_id)
        form=Af3_fondForm()
        if fond:
            form = Af3_fondForm(request.form, obj=fond)
        if fond_id:
            lists = Af3_opis.query.filter_by(fond_id=fond_id).limit(self.ROW2PAGE).offset((page-1)*self.ROW2PAGE)
            total = Af3_opis.query.filter_by(fond_id=fond_id).count()
        else:
            lists = Af3_opis.query.limit(self.ROW2PAGE).offset((page-1)*self.ROW2PAGE)
            total = Af3_opis.query.count()
        pagination = Pagination(lists=lists, page=page, total=total, record_name=u'описей', per_page=self.ROW2PAGE,
                bs_version=3, display_msg = u' <b>{start} - {end}</b> {record_name} из <b>{total}</b>')
        return render_template('af3_opis.html', lists=lists, pagination=pagination, fond_id=fond_id,
                fond=fond, form=form )
    def post(self):
        pass


# представление для af3_delo
from .models import Af3_delo

class Af3_deloView(MethodView):
    ROW2PAGE = ROW2PAGE
    def get(self, fond_id=None, opis_id=None, q=None):
        q = request.args.get('q')
        page = int(request.args.get('page', 1))
        fond = Af3_fond.query.get(fond_id)
        opis = Af3_opis.query.get(opis_id)
        if fond_id:
            lists = Af3_delo.query.order_by(cast(Af3_delo.l1, Numeric(10, 4))).filter_by(fond_id=fond_id, opis_id=opis_id)
            total = Af3_delo.query.filter_by(fond_id=fond_id, opis_id=opis_id)
        else:
            lists = Af3_delo.query
            total = Af3_delo.query
        if q:
            lists = lists.filter(Af3_delo.l4.like(u'%%{0}%%'.format( q )))
            total = total.filter(Af3_delo.l4.like(u'%%{0}%%'.format( q )))
        lists = lists.limit(self.ROW2PAGE).offset((page-1)*self.ROW2PAGE)
        total = total.count()
        pagination = Pagination(lists=lists, page=page, total=total, record_name=u'дел', per_page=self.ROW2PAGE,
                bs_version=3, display_msg = u' <b>{start} - {end}</b> {record_name} из <b>{total}</b>')
        return render_template('af3_delo.html', lists=lists, pagination=pagination, fond_id=fond_id, opis_id=opis_id,
                fond=fond, opis=opis, dq=q)
    def post(self):
        pass


class Af3_search(MethodView):
    ROW2PAGE = ROW2PAGE
    def get(self, fond_id=None, opis_id=None, delo_id=None, q=None):
        return jsonify(content=None)
