# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : report.py
# @Time       : 2020/11/18 下午3:19
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
import datetime
import time
import uuid

import oss2
from flask_restful import Resource, reqparse
from flask import request, render_template
from flask import make_response

from app.api.engine import config
from app.service.report_service import get_report_list, delete_report
from app.service.template_manage_service import make_report, get_make_report_data
from app.utils.html_to_pdf import html_to_pdf
from app.api.engine import pdf_folder, redis, env, TAMP_SQL, tamp_diagnose_app_engine
from app.utils.jinjia2html_v2 import DataIntegrate


class ReportHandlers(Resource):
    """."""

    def __init__(self):
        """."""
        token = request.headers.get('Authorization', '')
        token = 's:sid:' + token.split(' ')[1]
        ifa_id = redis.get(token)
        if not ifa_id:
            return {"code": '9005', "message": "请登录", "attributes": []}
        self.ifa_id = ifa_id.decode().replace('\'', '')
        self.parser = reqparse.RequestParser()

    def get(self):
        """获取制作模版数据."""
        self.parser.add_argument('id', type=str, required=True, help='模版id不能为空')
        self.parser.add_argument('customer_id', type=str, required=True, help='customer_id不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = self.ifa_id
        data = get_make_report_data(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    def post(self):
        """制作报告."""
        self.parser.add_argument('customer_id', type=str, required=True, help='customer_id不能为空')
        args = self.parser.parse_args()
        make_report(args)


    def put(self, id):
        """."""
        pass

    def delete(self, id):
        """."""
        pass


class ReportList(Resource):
    """报告列表."""

    def __init__(self):
        """."""
        token = request.headers.get('Authorization', '')
        token = 's:sid:' + token.split(' ')[1]
        ifa_id = redis.get(token)
        if not ifa_id:
            return {"code": '9005', "message": "请登录", "attributes": []}
        self.ifa_id = ifa_id.decode().replace('\'', '')
        self.parser = reqparse.RequestParser()

    def get(self):
        """获取我的报告列表."""
        self.parser.add_argument('type', type=int, required=True, help='0为全部1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('name', type=str, required=False, help='报告名称或者客户姓名')
        self.parser.add_argument('pageNumber', type=int, required=True)
        self.parser.add_argument('pageSize', type=int, required=True)
        self.parser.add_argument('start_time', type=str, required=False)
        self.parser.add_argument('end_time', type=str, required=False)
        args = self.parser.parse_args()
        args['ifa_id'] = self.ifa_id
        data = get_report_list(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    def post(self):
        """制作报告."""
        self.parser.add_argument('customer_id', type=str, required=True, help='customer_id不能为空')
        args = self.parser.parse_args()
        make_report(args)


    def put(self, id):
        """."""
        pass

    def delete(self):
        """."""
        self.parser.add_argument('type', type=int, required=True, help='0为全部1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('id', type=int, required=True, help='报告id不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = self.ifa_id
        delete_report(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": []
        }
        return resp