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

import json

from django.shortcuts import render

from account.models import LensmanInfo, TourGuideInfo
from utils.redis.connect import r


def lensman_oauth(request):
    unionid = request.GET.get('unionid', '')

    try:
        lensman = LensmanInfo.objects.get(unionid=unionid)
    except LensmanInfo.DoesNotExist:
        lensman = None

    return render(request, 'page/lensman_oauth.html', {
        'lensman_info': lensman and lensman.data,
        'modified': bool((not lensman) or (lensman and lensman.user_status in [LensmanInfo.UNVERIFIED, LensmanInfo.REFUSED])),  # 是否可以更改信息
    })


def tourguide_oauth(request):
    unionid = request.GET.get('unionid', '')

    try:
        tourguide = TourGuideInfo.objects.get(unionid=unionid)
    except TourGuideInfo.DoesNotExist:
        tourguide = None

    return render(request, 'page/tourguide_oauth.html', {
        'tourguide_info': tourguide and tourguide.data,
        'modified': bool((not tourguide) or (tourguide and tourguide.user_status in [TourGuideInfo.UNVERIFIED, TourGuideInfo.REFUSED])),  # 是否可以更改信息
    })


def login_qrcode(request):
    unionid = request.GET.get('unionid', '')

    data = {
        'unionid': unionid,
        'token': r.token(unionid)
    }

    return render(request, 'page/login_qrcode.html', {
        'data': json.dumps(data)
    })
