from django.shortcuts import render, redirect
# from django.views.generic import ListView, DetailView,  DeleteView, UpdateView, CreateView
from django.views import View, generic
from django.db import connection as conn


# Create your views here.
class Dashboard(generic.TemplateView):
    template_name = "layout/layout.html"
    # @method_decorator(login_required(login_url='maps:login'))
        
    def get(self, request):
        with conn.cursor() as kel:
            kel.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (kode_desa, desa, kecamatan, kab_kota, provinsi, jumlah_pen, jumlah_kk, luas_desa),'penduduk', (pria, wanita, belum_kawin, kawin, cerai_hidup, cerai_mati, wajib_ktp, islam, kristen, khatolik, hindu, budha, konghucu, kepercayaan_lain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75),'pekerjaan_pendidikan', (tidak_sekolah, belum_tamat, tamat_sd, sltp, slta, diploma_i, diploma_ii, diploma_iv, strata_ii, strata_iii, tidak_bekerja, aparatur_pemerintah, tenaga_pendidik, wiraswasta, pertanian, tenaga_kesehatan, pensiunan, pegawai, tentara, kepolisian, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, dosen, guru, pilot, pengacara, notaris, arsitek, akuntan, konsultan, dokter, bidan, perawat, psikiater, sopir, lainnya),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kelurahan")
            kel_res_ = kel.fetchall()
        
        with conn.cursor() as kec:
            kec.execute("SELECT kecamatan, json_build_object('type', 'Feature','geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kecamatan")
            kec_res_ = kec.fetchall()
            
        kec_res = []
        for i in kec_res_:
            kec_res.append(i[1])
        
        kel_res = []
        for i in kel_res_:
            kel_res.append(i[0])
            
        context = {
        'title':'NA - Dashboard',
        'kelurahan':kel_res,
        'kecamatan':kec_res,
        }
        return render (request, self.template_name, context)
    
class SearchLocation(generic.TemplateView):
    template_name = "maps/maps.html"
    
    # @method_decorator(login_required(login_url='maps:login'))
    def get(self, request):
        return redirect("apps:dashboard")
        
    def post(self, request):        

                
        return render (request, self.template_name, {"title":"NA - CheckByPolygon"})#,"polbang":polbang,"poltrees":poltrees,'jumlah_bangunan' : len(polbang)+len(poltrees), 'batas_co':batas_desa, 'search_by': 'address', 'address':f'{self.search_desa},{self.search_kab}', 'poly_desa':poly_desa, 'lat':lat, 'lng':lng, 'zoom':zoom, 'line_jalan':line_jalan})
    
class SearchRadius(generic.TemplateView):
    template_name = "maps/maps.html"
    
    # @method_decorator(login_required(login_url='maps:login'))
    def get(self, request):
        return redirect("apps:dashboard")
        
    def post(self, request):        

        return render (request, self.template_name, {"title":"NA - CheckByRadius"})#, "polbang":polbang, "poltrees":poltrees, 'jumlah_bangunan' : len(polbang)+len(poltrees), 'batas_co':zon, 'search_by': 'radius', 'address':'None'})

import re
class SearchPolygon(generic.TemplateView):
    template_name = "maps/maps.html"

    # @method_decorator(login_required(login_url='maps:login'))
    def get(self, request):
        return redirect("apps:dashboard")
        
    def post(self, request):        
        
        return render (request, self.template_name, {"title":"NA - CheckByPolygon"})#,"polbang":polbang,"poltrees":poltrees,'jumlah_bangunan' : len(polbang)+len(poltrees), 'batas_co':zon, 'search_by': 'polygon', 'address':'None', 'lat':lat, 'lng':lng, 'zoom':zoom, 'line_jalan':line_jalan})
