from django.shortcuts import render
from BizPit_app.models import *
import requests
from time import time, strftime, gmtime, sleep
import datetime
import os

from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.shortcuts import render_to_response

# Create your views here.

@csrf_exempt
def mainpage(request):

	return render_to_response('login.html')


import xlrd
import MySQLdb
@csrf_exempt
def Excel_add(request):
	
	#return render_to_response('mainpage.html')
        print "am innn "
       	list1=[]
        path="static/my_data.xlsx"
        book1 = xlrd.open_workbook(path)

	for sheet in book1.sheets():
		
		for r in range(1, sheet.nrows):
			property_name=sheet.cell(r,0).value
			property_type=sheet.cell(r,1).value
			subtype=sheet.cell(r,2).value
			status=sheet.cell(r,3).value
			location=sheet.cell(r,4).value
			address=sheet.cell(r,5).value
			lat=sheet.cell(r,6).value
			lon=sheet.cell(r,7).value
			price=sheet.cell(r,8).value
			submitted_by=sheet.cell(r,9).value

			my_id1=0
			data1 = Property_Details.objects.all()
			for i in data1:
				my_id1=i.property_id
			myid=my_id1+1


			print "My data... ",property_name,property_type,subtype,status,location,address,lat,lon,price,submitted_by


			data=Property_Details(property_id=myid,status=status,property_type=property_type,property_subtype=subtype,property_name=property_name,location=location,address=address,latitude=lat,longitude=lon,starting_price=price,submitted_by=submitted_by)

			data.save()

        return render_to_response('login.html')

@csrf_exempt
def add_property(request):
	print" Iam in add property....."
	user=request.POST["user"]
	print "user.....",user
        return render_to_response('property.html',{"user":user})


from geopy.geocoders import Nominatim
import geopy

@csrf_exempt
def Save_property(request):

	new_cur_pathlist=[]
	prop_name=request.POST["name_proj"]
	address=request.POST["searchTextField"]
	phone=request.POST["phone"]
	price=request.POST["price"]
	
	user=request.POST["user"]

	print "user........",user

	#geolocator = Nominatim()

	#addres = str(raw_input("Enter the name or address of the location : "))
	print address
	#location = geolocator.geocode(address)
	#print(location.address)
	#print((location.latitude, location.longitude))
	#lat=location.latitude
	#lon=location.longitude
	#add=location.address
	#split_loc=add.split(' ', 1 )
	#loc=split_loc[0]

	lat=request.POST['cityLat']
	lon=request.POST['cityLng']
	loc=request.POST['city2']
	print "latit..",lat
	print "long...",lon
	print "loc....",loc
	
	time=datetime.datetime.now()

	typ_list=[]
	
	my_type=request.POST.getlist("type_list[]")


	print "my value",my_type

	for i in my_type:
		
              typ_list.append(str(i))
       	print "type_list",typ_list

	if "Plug & Play" in typ_list:
		my_val_plug = "Plug & Play"
	else:
		my_val_plug = "none"

	if "Training" in typ_list:
		my_val_Training = "Training"
	else:
		my_val_Training = "none"
	if "Event Hall" in typ_list:
		my_val_event = "Event Hall"
	else:
		my_val_event = "none"
	#if(typ_list=="Plug & Play"):
	#	my_val="PlugPlay"
	#	print my_val
	#else:
	#	my_val="null"
	
       	new_cur_typlist=",".join(typ_list)
       	print "typeeeeeeeee",new_cur_typlist
	#my_val=new_cur_typlist
	#print "my_val.............",my_val
	

	start_time=request.POST["str_time"]
	end_time=request.POST["end_time"]

	#try:
	upimages=request.FILES.getlist('new_images')
	#except:
	#	up_images=0
	#upimages = 'new_images' in request.POST and request.POST['new_images']
	print "up images...................",upimages
	sub_id1=0
	data_sub = Property_Details.objects.all()
	for s in data_sub:
		sub_id1=s.submitted_by_id
	subid=sub_id1+1

	own_id1=0
	data_own = Property_Details.objects.all()
	for o in data_own:
		own_id1=s.Owner_id
	ownid=own_id1+1	

	my_id1=0
	data1 = Property_Details.objects.all()
	for i in data1:
		my_id1=i.property_id
	myid=my_id1+1


	if upimages:
	      print "in this code..............."
	      cur_pathlist=[]
              path=upload_image(upimages,myid)

              print "path",path
              print "\n\n path_of_image :",path,type(path)
              for link in path:
                     tmp_path1 = link.split("/")
                     cur_path1 = tmp_path1[1]+'/'+tmp_path1[2]+'/'+tmp_path1[3]
                     print "temp path1 :",cur_path1
                     cur_pathlist.append(cur_path1)
              new_cur_pathlist=",".join(cur_pathlist)
              print "new pathhhhhhhhhhhhhhhhhhhhhhhhhhhhhHHHHHHHHHHHHHHHHH",new_cur_pathlist,type(new_cur_pathlist)
              print "aasd",new_cur_pathlist

	data=Property_Details(property_id=myid,status="active",property_type=new_cur_typlist,property_subtype=use_case,property_name=prop_name,address=address,starting_price=price,operating_strattime=start_time,operating_stoptime=end_time,timestamp=time,submitted_by=user,latitude=lat,longitude=lon,location=loc,Owner_id=ownid,submitted_by_id=subid,images=new_cur_pathlist)

	data.save()

	
	return render_to_response('nextpage.html',{"my_val_event":my_val_event,"my_val_plug" : my_val_plug, "my_val_Training":my_val_Training,"myid":myid})



@csrf_exempt
def Save_property_next(request):
	print "Iam in..."
	new_cur_pathlist=[]
	new_cur_pathlist1=[]
	new_cur_pathlist12=[]
	new_cur_pathlist123=[]
	
	prop_id=request.POST['prop_id']
	print "property id.........",prop_id

	try:
		shared_capacity=request.POST["capacity1"]
		print "my capacity...",shared_capacity
		if(shared_capacity):
			print"am here"
		else:
			shared_capacity=0

	except:
		
		shared_capacity=0

	try:
		shared_hr=request.POST["rate_hr"]
	except:
		shared_hr=0
	try:
		shared_daily=request.POST["rate_daily"]
	except:
		shared_daily=0
	try:	
		shared_weekly=request.POST["rate_week"]
	except:
		shared_weekly=0
	try:	
		shared_monthly=request.POST["rate_month"]
	except:
		shared_monthly=0

	
        sh_images=request.FILES.getlist('shared_img')
	print "shared.................",sh_images

	
	'''my_id1=0
	data1 = Shared_area.objects.all()
	for i in data1:
		my_id1=i.property_id
	myid=my_id1+1'''

	if sh_images:
	      print "in this code..............."
	      cur_pathlist=[]
              path1=upload_image(sh_images,prop_id)

              print "path",path1
              print "\n\n path_of_image :",path1,type(path1)
              for link in path1:
                     tmp_path1 = link.split("/")
                     cur_path1 = tmp_path1[1]+'/'+tmp_path1[2]+'/'+tmp_path1[3]
                     print "temp path1 :",cur_path1
                     cur_pathlist.append(cur_path1)
              new_cur_pathlist=",".join(cur_pathlist)
              print "new pathhhhhhhhhhhhhhhhhhhhhhhhhhhhhHHHHHHHHHHHHHHHHH",new_cur_pathlist,type(new_cur_pathlist)
              print "aasd",new_cur_pathlist
	data=Shared_area(property_id=prop_id,capacity=shared_capacity,rate_hr=shared_hr,rate_daily=shared_daily,rate_weekly=shared_weekly,rate_monthly=shared_monthly,shared_images=new_cur_pathlist)

	data.save()



	room_no=0


	try:
		meeting_capacity=request.POST["capacity2"]
		print "now capacity...",meeting_capacity
		if(meeting_capacity):
			print"am here"
		else:
			meeting_capacity=0
		
		room_no1=room_no+1
		
	except:
		
		meeting_capacity=0
		
	print "dxfyghdrfhdfghdgfh"
	try:
		meeting_hr=request.POST["rate_hr2"]
	except:
		meeting_hr=0
	try:
		meeting_daily=request.POST["rate_daily2"]
	except:
		meeting_daily=0
	try:	
		meeting_weekly=request.POST["rate_week2"]
	except:
		meeting_weekly=0
	print"am hererererererererer"
	#meeting_images=request.FILES.getlist('b2212')	
	try:
               	meeting_images=request.FILES.getlist('meeting_img')
		print "image.........",meeting_images

       	except:

                meeting_images=0

	'''my_id12=0
	data12 = Meeting_room.objects.all()
	for i in data12:
		my_id12=i.property_id
	myid1=my_id12+1'''

	if meeting_images:
	      print "in this code..............."
	      cur_pathlist1=[]
              path2=upload_image(meeting_images,prop_id)

              print "path",path2
              print "\n\n path_of_image :",path2,type(path2)
              for link in path2:
                     tmp_path1 = link.split("/")
                     cur_path1 = tmp_path1[1]+'/'+tmp_path1[2]+'/'+tmp_path1[3]
                     print "temp path1 :",cur_path1
                     cur_pathlist1.append(cur_path1)
              new_cur_pathlist1=",".join(cur_pathlist1)
              print "new pathhhhhhhhhhhhhhhhhhhhhhhhhhhhhHHHHHHHHHHHHHHHHH",new_cur_pathlist1,type(new_cur_pathlist1)
              print "aasd",new_cur_pathlist1

	data1=Meeting_room(property_id=prop_id,room_no=1,capacity=meeting_capacity,rate_hr=meeting_hr,rate_daily=meeting_daily,
                           rate_weekly=meeting_weekly,meeting_room_images=new_cur_pathlist1)

	data1.save()


	training_no=0


	try:
		training_capacity=request.POST["capacity7"]
		print "now capacity...",training_capacity
		if(training_capacity):
			print"am here"
		else:
			training_capacity=0
		
		training_no1=training_no+1
		
	except:
		
		training_capacity=0
		
	print "dxfyghdrfhdfghdgfh"
	try:
		training_hr=request.POST["rate_half7"]
	except:
		training_hr=0

	try:
		training_dura=request.POST["half_duration7"]
	except:
		training_dura=0
	try:
		training_daily=request.POST["rate_daily2"]
	except:
		training_daily=0
	try:	
		training_weekly=request.POST["rate_week2"]
	except:
		training_weekly=0
	
	try:
               	training_images=request.FILES.getlist('training_img')

       	except:

                training_images=0

	'''my_id123=0
	data123 = Training_room.objects.all()
	for i in data123:
		my_id123=i.property_id
	myid12=my_id123+1'''

	if training_images:
	      print "in this code..............."
	      cur_pathlist12=[]
              path3=upload_image(training_images,prop_id)

              print "path",path3
              print "\n\n path_of_image :",path3,type(path3)
              for link in path3:
                     tmp_path1 = link.split("/")
                     cur_path12 = tmp_path1[1]+'/'+tmp_path1[2]+'/'+tmp_path1[3]
                     print "temp path1 :",cur_path12
                     cur_pathlist12.append(cur_path12)
              new_cur_pathlist12=",".join(cur_pathlist12)
              print "new pathhhhhhhhhhhhhhhhhhhhhhhhhhhhhHHHHHHHHHHHHHHHHH",new_cur_pathlist12,type(new_cur_pathlist12)
              print "aasd",new_cur_pathlist12

	data123=Training_room(property_id=prop_id,
                              training_room_no=1,
                              capacity=training_capacity,
                              
                              rate_half_day=training_hr,
                              rate_full_day=training_daily,
                              rate_weekly=training_weekly,
                              rate_monthly=training_weekly,
                              training_room_images=new_cur_pathlist12)

	data123.save()




	try:
		event_capacity=request.POST["capacity12"]
		print "my capacity...",event_capacity
		if(event_capacity):
			print"am here"
		else:
			event_capacity=0

	except:
		
		event_capacity=0

	try:
		event_hr=request.POST["rate_half12"]
	except:
		event_hr=0
	try:
		event_daily=request.POST["rate_daily12"]
	except:
		event_daily=0
	try:
		event_dura=request.POST["half_duration12"]
	except:
		event_dura=0
	try:
               	event_images=request.FILES.getlist('event_img')

       	except:

                event_images=0

	'''my_id10=0
	data10 = Event_Halls.objects.all()
	for i in data10:
		my_id10=i.property_id
	myid01=my_id10+1'''

	if event_images:
	      print "in this code..............."
	      cur_pathlist123=[]
              path4=upload_image(event_images,prop_id)

              print "path",path4
              print "\n\n path_of_image :",path4,type(path4)
              for link in path4:
                     tmp_path1 = link.split("/")
                     cur_path123 = tmp_path1[1]+'/'+tmp_path1[2]+'/'+tmp_path1[3]
                     print "temp path1 :",cur_path123
                     cur_pathlist123.append(cur_path123)
              new_cur_pathlist123=",".join(cur_pathlist123)
              print "new pathhhhhhhhhhhhhhhhhhhhhhhhhhhhhHHHHHHHHHHHHHHHHH",new_cur_pathlist123,type(new_cur_pathlist123)
              print "aasd",new_cur_pathlist123


	data01=Event_Halls(property_id=prop_id,
                           capacity=event_capacity,
                           rate_halfday=event_hr,
                           duration_half=event_dura,
                           rate_daily=event_daily,
                           shared_images=new_cur_pathlist123)

	data01.save()

	return render_to_response('facility.html',{"prop_id":prop_id})


@csrf_exempt
def Save_data(request):

	prop_id=request.POST["prop_id"]
	print "prop.....id...",prop_id
	
	try:

              wifi=request.POST["wifi"]
       	except:
              wifi=0

	try:

              coffe=request.POST["coffe"]
       	except:
              coffe=0


	try:

              water=request.POST["water"]
       	except:
              water=0


	try:

              printer=request.POST["printer"]
       	except:
              printer=0


	try:

              charging=request.POST["charging"]
       	except:
              charging=0


	try:

              changing=request.POST["changing"]
       	except:
              changing=0

	try:

              library=request.POST["library"]
       	except:
              library=0

	try:

              chair=request.POST["chair"]
       	except:
              chair=0

	try:

              locker=request.POST["locker"]
       	except:
              locker=0

	try:

              ac=request.POST["ac"]
       	except:
              ac=0

	try:

              hr_deal=request.POST["hr_deal"]
       	except:
              hr_deal=0

	try:

              half_deal=request.POST["half_deal"]
       	except:
              half_deal=0
	try:

              full_deal=request.POST["full_deal"]
       	except:
              full_deal=0
	try:

              projector=request.POST["projector"]
       	except:
              projector=0
	try:

              screen=request.POST["screen"]
       	except:
              screen=0
	try:

              pa=request.POST["pa"]
       	except:
              pa=0
	try:

              pc=request.POST["pc"]
       	except:
              pc=0
	
	seat_up=request.POST["seat_up"]

	chair_r=request.POST["chair_r"]

	stationary=request.POST["stationary"]

	complement=request.POST["complement"]

	add_on=request.POST["add_on"]




	'''my_id10=0
	data10 =Property_Facility.objects.all()
	for i in data10:
		my_id10=i.property_id
	myid01=my_id10+1'''

	data=Property_Facility(property_id=prop_id)
	
	if(wifi!=0):
              data.Wifi=True
       	else:
      	      data.Wifi=False

	if(coffe!=0):
              data.Coffee_tea=True
       	else:
      	      data.Coffee_tea=False

	if(water!=0):
              data.Water_bottle=True
       	else:
      	      data.Water_bottle=False

	if(printer!=0):
              data.Printer=True
       	else:
      	      data.Printer=False

	if(charging!=0):
              data.Charging_Point=True
       	else:
      	      data.Charging_Point=False

	if(changing!=0):
              data.Changing_room=True
       	else:
      	      data.Changing_room=False

	if(library!=0):
              data.Library=True
       	else:
      	      data.Library=False
	if(chair!=0):
              data.Massage_chair=True
       	else:
      	      data.Massage_chair=False
	if(locker!=0):
              data.Locker=True
       	else:
      	      data.Locker=False
	if(ac!=0):
              data.AC=True
       	else:
      	      data.AC=False

	if(hr_deal!=0):
              data.Hourly_deal=True
       	else:
      	      data.Hourly_deal=False

	if(half_deal!=0):
              data.Half_day_deal=True
       	else:
      	      data.Half_day_deal=False

	if(full_deal!=0):
              data.Full_day_deal=True
       	else:
      	      data.Full_day_deal=False


	if(projector!=0):
              data.Projector=True
       	else:
      	      data.Projector=False
	if(screen!=0):
              data.Projection_screen=True
       	else:
      	      data.Projection_screen=False
	if(pa!=0):
              data.PA_system=True
       	else:
      	      data.PA_system=False
	if(pc!=0):
              data.PC_or_Laptop=True
       	else:
      	      data.PC_or_Laptop=False

	data.Seat_up=seat_up
	data.Chair=chair_r
	data.Stationary=stationary
	data.Complemetry=complement
	data.Add_On=add_on

	data.save()
	msg="Property data saved.."
	data=Property_Details.objects.all().order_by('property_id')
	

        result=[]
        result1=[]
        first=[]
        names=[]
        print "am here"
        res="Ascending"
        res1="None"
        #print "sdafsdfdsfsdf",request.POST['desc']

        for i in data:
			first.append(i.property_id)
			
		        temp = int(i.property_id)
		        names.append(i.property_name)
		       
		        #print "namesEERRRRERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR",names
		        first.append(i.property_name)
		        address=i.address
			first.append(i.address)
			result.append(first)
			first=[]
	       #print"result adsfasdfadsfasdfasdf",result  
        return render_to_response('my_property.html',{"property":result})

@csrf_exempt
def login(request):
       response = render_to_response('login.html',{})
       #u_name=request.COOKIES.get('usr_name')
       response.delete_cookie('logged_in_liveup_status')
       #print u_name
       return response


@csrf_exempt
def property_page(request):

       print "am in............"
       data=Property_Details.objects.all().order_by('property_id')

       result=[]
       result1=[]
       first=[]
       names=[]
       print "am here"
       res="Ascending"
       res1="None"
      #print "sdafsdfdsfsdf",request.POST['desc']

       for i in data:
		first.append(i.property_id)
                temp = int(i.property_id)
                names.append(i.property_name)
                names.sort()
                #print "namesEERRRRERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR",names
                first.append(i.property_name)
                address=i.address
		first.append(address)
		result.append(first)

		

       return render_to_response('my_property.html',{"property":result})



@csrf_exempt
def display_homepage(request):
    
    try:
           
           username = request.POST["Username"]
           password = request.POST["Password"]
    

    except:
           msg = "Username not found Please Sign Up!"
##           return render_to_response("login.html",{'flag':msg})
           

    try:
        instance = Sign_Up.objects.get(username = username).password
        if password == instance:
               print "am in............"
               return render_to_response('secondpage.html',{})
##	       data=Property_Details.objects.all().order_by('property_id')
##
##	       result=[]
##	       result1=[]
##	       first=[]
##	       names=[]
##	       print "am here"
##	       res="Ascending"
##	       res1="None"
##	      #print "sdafsdfdsfsdf",request.POST['desc']
##
##	       for i in data:
##			first.append(i.property_id)
##			
##		        temp = int(i.property_id)
##		        names.append(i.property_name)
##		       
##		        #print "namesEERRRRERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR",names
##		        first.append(i.property_name)
##		        address=i.address
##			first.append(i.address)
##			result.append(first)
##			first=[]
##	       #print"result adsfasdfadsfasdfasdf",result  
##	       return render_to_response('my_property.html',{"property":result,"user":username})
                
               response.set_cookie('logged_in_liveup_status', username)
               return response
        else:
            msg = "Password is Invalid!!"
            return render_to_response("login.html",{'flag':msg})
    except :
        msg = "Username not found.Please Sign Up!"
        return render_to_response("login.html",{'flag':msg})		



@csrf_exempt
def signup_data(request):
    print "In sign up data"
    username=request.POST["username"]
    firstname=request.POST["firstname"]
    lastname=request.POST["lastname"]
    mobilenumber=request.POST["mobilenumber"]
    password=request.POST["password"]
    confirmpassword=request.POST["confirm_pass"]
    admin_pass = request.POST["admin_pass"]
    print username

    if password != confirmpassword:
        msg="Password don't match!"
        return render(request,'signup.html',{'flag':msg,"username":username,"firstname":firstname,"lastname":lastname,"mobilenumber":mobilenumber,"password":password,
                                             "confirm_pass":confirmpassword,"admin_pass":admin_pass})
    elif password != confirmpassword and len(mobilenumber)!= 10:
        msg= "Password don't match! And also mobilenumber should be 10 digits only!"
        return render(request,'signup.html',{'flag':msg,"username":username,"firstname":firstname,"lastname":lastname,"mobilenumber":mobilenumber,"password":password,
                                             "confirm_pass":confirmpassword,"admin_pass":admin_pass})
    elif len(mobilenumber)!= 10:
        msg = "Mobile number isn't 10 digits!"
        return render(request,'signup.html',{'flag':msg,"username":username,"firstname":firstname,"lastname":lastname,"mobilenumber":mobilenumber,"password":password,
                                             "confirm_pass":confirmpassword,"admin_pass":admin_pass})
    elif admin_pass != "brisa_admin":
        msg = "Admin password Error!"
        return render(request,'signup.html',{'flag':msg,"username":username,"firstname":firstname,"lastname":lastname,"mobilenumber":mobilenumber,"password":password,
                                             "confirm_pass":confirmpassword,"admin_pass":admin_pass})
    elif Sign_Up.objects.filter(username = username).exists():
        msg = "Username Already exists! Please log in or request for new password"
        return render_to_response("login.html",{'flag':msg})

    else:
        from_mail = "hr@brisa-tech.com"
        TO = username
        message = "Dear "+firstname+" "+lastname+",\n"+"Your account has been created successfully in Grihhpravesh with following credentials.\n"+"Username :"+username+"\n"+"Password :"+password+"\n"+"Please log in with your new credentials"


        obj = Sign_Up(username=username,firstname=firstname,lastname=lastname,mobilenumber=mobilenumber,password=password)
        obj.save()
        msg = "Account successfully created. Please login!"
        return render(request,'login.html',{'flag':msg})


@csrf_exempt
def sign_up(request):
       return render_to_response('signup.html',{})

@csrf_exempt
def owner_details(request):

       owner=Login.objects.all()
       result=[]
       result1=[]
       first=[]
       
       print "am here"
       res="Ascending"
       res1="None"
      #print "sdafsdfdsfsdf",request.POST['desc']

       for i in owner:
                first.append(i.user_id)
                
                temp = int(i.user_id)
                names=(i.email_id)
                first.append(i.email_id)
                first.append(i.user_password)
                data=Property_Details.objects.filter(submitted_by=names)
                if(data):
                        count=Property_Details.objects.filter(submitted_by=names).count()
                else:
                        count=0
                first.append(count)

                result.append(first)
                first=[]

       return render_to_response('owner_details.html',{"property":result})

@csrf_exempt
def click_details(request):

       owner_id=request.POST["newID"]
       print "iddddddddd",owner_id
       result=[]
       result1=[]
       first=[]
       names=[]
       print "Haiiiiiiiiiiiiiiiiiiiiiii"
       data=Login.objects.filter(user_id=owner_id)
       if(data):
               print "Helloooooooooo"
               for d in data:
                       print "Haiiiiiiiiiiiiiiiiiiiiiii"
                       email=d.email_id
                       print "email...",email
                       new_data=Property_Details.objects.filter(submitted_by=email)
                       if(new_data):
                               print "Iam innnnnnnnnnnnnnnnnnnnnn"

                               for n in new_data:
                                        first.append(n.property_id)
                                        if(data):
                                                count=Property_Details.objects.filter(submitted_by=names).count()
                                        else:
                                                count=0
                                        temp = int(n.property_id)
                                        names.append(n.property_name)
##                                        invoice.append(str(d.invoice_no))
                                        #print "namesEERRRRERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR",names
                                        first.append(n.property_name)
                                        address=n.address
                                        first.append(n.address)
                                        first.append(count)
##                                        invoice1=','.join(invoice)
##                                        first.append(invoice1)

##                                        result.append(first)
##                                        invoice=[]
                                        result.append(first)
                                        first=[]
       return render_to_response('my_property.html',{"property":result,"owner_id":owner_id})

@csrf_exempt
def super_user(request):

       print "Iam in super user........"
       
                       
       return render_to_response('super_user_details.html',{})


@csrf_exempt
def back_page(request):

       print "Iam in super user........"
       
                       
       return render_to_response('secondpage.html',{})

@csrf_exempt
def details_owner(request):
       owner=Login.objects.all()
       result=[]
       result1=[]
       first=[]
       invoice=[]
       
       print "am here"
       res="Ascending"
       res1="None"
      #print "sdafsdfdsfsdf",request.POST['desc']

       for i in owner:
                first.append(i.user_id)
                
                temp = int(i.user_id)
                names=(i.email_id)
                first.append(i.email_id)
                first.append(i.user_password)
                data=Property_Details.objects.filter(submitted_by=names)
                if(data):
                        count=Property_Details.objects.filter(submitted_by=names).count()
                        for d in data:
                            invoice.append(str(d.invoice_no))
                            
                else:
                        count=0
                first.append(count)
                invoice1=','.join(invoice)
                first.append(invoice1)

                result.append(first)
                invoice=[]
                first=[]

       return render_to_response('owner_details_next.html',{"property":result})

@csrf_exempt
def owner_property(request):

       owner_id=request.POST["newID"]
       print "iddddddddd",owner_id
       result=[]
       result1=[]
       first=[]
       names=[]
       print "Haiiiiiiiiiiiiiiiiiiiiiii"
       data=Login.objects.filter(user_id=owner_id)
       if(data):
               print "Helloooooooooo"
               for d in data:
                       print "Haiiiiiiiiiiiiiiiiiiiiiii"
                       email=d.email_id
                       print "email...",email
                       new_data=Property_Details.objects.filter(submitted_by=email)
                       if(new_data):
                               print "Iam innnnnnnnnnnnnnnnnnnnnn"

                               for n in new_data:
                                        first.append(n.property_id)
                                        
                                        temp = int(n.property_id)
                                        names.append(n.property_name)
                                       
                                        #print "namesEERRRRERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR",names
                                        first.append(n.property_name)
                                        address=n.address
                                        first.append(n.address)
                                        result.append(first)
                                        first=[]
       
       return render_to_response('my_property_owner.html',{"property":result})

@csrf_exempt
def user_details(request):

        user=Login.objects.filter(user_type="User")
        result=[]
        result1=[]
        first=[]
        names=[]
        if(user):
                for u in user:
                      first.append(u.user_id)
                
                      temp = int(u.user_id)
                      names=(u.email_id)
                      first.append(u.email_id)
                      first.append(u.user_password) 
                      result.append(first)
                      first=[]  

        return render_to_response('user_details.html',{"property":result})

@csrf_exempt
def edit_property(request):

        ownerID=request.POST["ownerID"]
        print "ownerID",ownerID
        prop_id=request.POST["newID"]
        print "prop_id",prop_id
        non_work1=[]
        holi1=[]
        rate_halfday_event=0
        rate_daily_event=0
        halfday_event=0
        daily_event=0
        capacity_event=0
        room_no_event=0
        rate_half_day_train=0
        rate_full_day_train=0
        halfday_train=0
        fullday_train=0
        capacity_train=0
        room_no_train=0
        hourly_meet=0
        daily_meet=0
        weekly_meet=0
        rate_hr_meet=0
        rate_daily_meet=0
        rate_weekly_meet=0
        capacity_meet=0
        room_no_meet=0
        rate_hr_plug=0
        hourly_plug=0
        rate_daily_plug=0
        daily_plug=0
        rate_weekly_plug=0
        weekly_plug=0
        rate_monthly_plug=0
        monthly_plug=0
        capacity_plug=0
        Start_Up=0
        Coffee_Shop=0
        result_meet=[]
        result_train=[]
        result_event=[]
        first=[]
        
        data=Property_Details.objects.filter(property_id=prop_id)
        if(data):
                for d in data:
                        property_name=d.property_name
                        address=d.address
                        loc=d.location
                        lat=d.latitude
                        lon=d.longitude
                        description=d.description
                        default=d.default_type
                        p_type=d.property_type
                        p_type1=p_type.split(",")
                        p_type_p=0
                        p_type_t=0
                        p_type_e=0
                        p_type_m=0
                        print "p_type1...",p_type1
                        for pt in p_type1:
                                print "ptptptptptptptptptptptvv",pt
                                if "Plug & Play" in pt:
                                        p_type_p="checked"
                                if "Training Room" in pt:
                                        p_type_t="checked"
                                if "Meeting_Room" in pt:
                                        p_type_m="checked"
                                if "Event_Hall" in pt:
                                        p_type_e="checked"

                        if 'Plug & Play' in p_type1:
                                my_val_plug = "Plug & Play"
                                print "pppppppppppppppppppppppppp",my_val_plug
                        else:
                                my_val_plug = "none"
                                print "pppppppppppppppppppppppppp",my_val_plug

                        if "Training Room" in p_type1:
                                my_val_Training = "Training Room"
                        else:
                                my_val_Training = "none"
                        if "Event_Hall" in p_type1:
                                my_val_event = "Event_Hall"
                        else:
                                my_val_event = "none"

                        if "Meeting_Room" in p_type1:
                                my_val_meeting = "Meeting_Room"
                                print "pppppppppppppppppppppppppp",my_val_meeting
                        else:
                                my_val_meeting = "none"
                                print "pppppppppppppppppppppppppp",my_val_meeting

                        
                        start_time=d.operating_strattime
                        end_time=d.operating_stoptime
                        
                        non_working=Non_Working_Days.objects.filter(property_id=prop_id)
                        for n in non_working:
                                non_work1.append(n.Non_Working_days)
                                non_work=','.join(non_work1)
                        holiday=Holidays.objects.filter(property_id=prop_id)
                        for h in holiday:
                                holi1.append(h.holidays)
                                holi=','.join(holi1)

                        if "Plug & Play" in p_type1:
                                sub_type=d.property_subtype
                                if (("Start Up/Co-Working" in sub_type) or ("Co-Working" in sub_type )or ("Start Up" in sub_type)) :
                                      Start_Up="checked"
                                if "Coffee Shop" in sub_type:
                                       Coffee_Shop="checked"

                                
                                plug_n_play=Shared_area.objects.filter(property_id=prop_id)
                                if(plug_n_play):
                                        for plug in plug_n_play:
                                                if(plug.rate_hr > 0):
                                                        rate_hr_plug=plug.rate_hr
                                                        hourly_plug="checked"
                                                if(plug.rate_daily > 0):
                                                        rate_daily_plug=plug.rate_daily
                                                        daily_plug="checked"
                                                if(plug.rate_weekly > 0):
                                                        rate_weekly_plug=plug.rate_weekly
                                                        weekly_plug="checked"
                                                if(plug.rate_monthly > 0):
                                                        rate_monthly_plug=plug.rate_monthly
                                                        monthly_plug="checked"
                                                capacity_plug=plug.capacity

                        if "Training Room" in p_type1:
                                
                                train1=Training_room.objects.filter(property_id=prop_id)
                                if(train1):
                                        for tr1 in train1:
                                                room_no=int(tr1.training_room_no)
                                                train=Training_room.objects.filter(property_id=prop_id,training_room_no=room_no)
                                                
                                                for tr in train:
                                                        if(tr.rate_half_day > 0):
                                                                rate_half_day_train=tr.rate_half_day
                                                                halfday_train="checked"
                                                        if(tr.rate_full_day > 0):
                                                                rate_full_day_train=tr.rate_full_day
                                                                fullday_train="checked"
                                                        capacity_train=tr.capacity
                                                        room_no_train=tr.training_room_no
                                                        first.append(halfday_train)
                                                        first.append(fullday_train)
                                                        first.append(rate_half_day_train)
                                                        first.append(rate_full_day_train)
                                                        first.append(capacity_train)
                                                        first.append(room_no_train)
                                                        result_train.append(first)
                                                        first=[]

                        if "Meeting_Room" in p_type1:
                        
                                meeting1=Meeting_room.objects.filter(property_id=prop_id)
                                if(meeting1):
                                        for meet1 in meeting1:
                                                room_no=int(meet1.room_no)

                                                meeting=Meeting_room.objects.filter(property_id=prop_id,room_no=room_no)
                                                for meet in meeting:
                                                
                                                        if(meet.rate_hr > 0):
                                                                rate_hr_meet=meet.rate_hr
                                                                hourly_meet="checked"
                                                        if(meet.rate_daily > 0):
                                                                rate_daily_meet=meet.rate_daily
                                                                daily_meet="checked"
                                                        if(meet.rate_weekly > 0):
                                                                rate_weekly_meet=meet.rate_weekly
                                                                weekly_meet="checked"
                                                        capacity_meet=meet.capacity
                                                        room_no_meet=meet.room_no
                                                        first.append(hourly_meet)
                                                        first.append(rate_hr_meet)
                                                        first.append(daily_meet)
                                                        first.append(rate_daily_meet)
                                                        first.append(capacity_meet)
                                                        first.append(room_no_meet)
                                                        first.append(weekly_meet)
                                                        first.append(rate_weekly_meet)
                                                        result_meet.append(first)
                                                        first=[]


                        if "Event_Hall" in p_type1:
                                
                                eventhall=Event_Halls.objects.filter(property_id=prop_id)
                                if(eventhall):
                                        for event1 in eventhall:
                                                room_no=event1.room_no
                                                eventhall0=Event_Halls.objects.filter(property_id=prop_id,room_no=room_no)
                                                for event in eventhall0:
                                                        if(event.rate_halfday > 0):
                                                                rate_halfday_event=event.rate_halfday
                                                                halfday_event="checked"
                                                        if(event.rate_daily > 0):
                                                                rate_daily_event=event.rate_daily
                                                                daily_event="checked"
                                                        capacity_event=event.capacity
                                                        room_no_event=event.room_no

                                                        first.append(halfday_event)
                                                        first.append(daily_event)
                                                        first.append(rate_halfday_event)
                                                        first.append(rate_daily_event)
                                                        first.append(capacity_event)
                                                        first.append(room_no_event)
                                                        result_event.append(first)
                                                        first=[]
                        Wifi=0
                        AC=0
                        Library=0
                        Printer=0
                        Printer=0
                        Projection=0
                        Coffee=0
                        Charging=0
                        Chair=0
                        Seat=0
                        Projector=0
                        Water=0
                        Changing=0
                        Locker=0
                        Stationary=0
                        PC=0
                        facility=Property_Facility.objects.filter(property_id=prop_id)
                        if(facility):
                                for f in facility:
                                       if(f.Wifi):
                                               Wifi="checked"
                                       if(f.AC):
                                               AC="checked"
                                       if(f.Library):
                                               Library="checked"
                                       if(f.Printer):
                                               Printer="checked"
                                       if(f.Projection_screen):
                                               Projection="checked"
                                       if(f.Coffee_tea):
                                               Coffee="checked"
                                       if(f.Charging_Point):
                                               Charging="checked"
                                       if(f.Chair=="ND"):
                                               Chair="checked"
                                       if(f.Seat_up=="common"):
                                               Seat="checked"
                                       if(f.Projector):
                                               Projector="checked"
                                       if(f.Water_bottle):
                                               Water="checked"
                                       if(f.Changing_room):
                                               Changing="checked"
                                       if(f.Locker):
                                               Locker="checked"
                                       if(f.Stationary=="basic"):
                                               Stationary="checked"
                                       if(f.PC_or_Laptop):
                                               PC="checked"
                
                                                
                                                
                        

        
        
##        prop_id=request.POST["newID"]
##	print "prop id..",prop_id

        return render_to_response('edit_propertypage.html',{"ownerID":ownerID,"prop_id":prop_id,"property_name":property_name,
                                                            "address":address,"loc":loc,"lat":lat,"lon":lon,"default":default,
                                                            "p_type_p":p_type_p,"p_type_t":p_type_t,"p_type_e":p_type_e,
                                                            "p_type_m":p_type_m,"start_time":start_time,"end_time":end_time,
                                                            "non_work":non_work,"holi":holi,"description":description,
                                                            "PC":PC,"Stationary":Stationary,"Locker":Locker,"Changing":Changing,
                                                            "Water":Water,"Projector":Projector,"Seat":Seat,"Chair":Chair,
                                                            "Charging":Charging,"Coffee":Coffee,"Projection":Projection,
                                                            "Printer":Printer,"Library":Library,"AC":AC,"Wifi":Wifi,
                                                            "my_val_plug":my_val_plug,"my_val_Training":my_val_Training,
                                                            "my_val_event":my_val_event,"my_val_meeting":my_val_meeting,
                                                            "result_meet":result_meet,"result_train":result_train,"result_event":result_event,
                                                            "rate_halfday_event":rate_halfday_event,"rate_daily_event":rate_daily_event,
                                                            "halfday_event":halfday_event,"daily_event":daily_event,
                                                            "capacity_event":capacity_event,"room_no_event":room_no_event,
                                                            "rate_half_day_train":rate_half_day_train,"rate_full_day_train":rate_full_day_train,
                                                            "halfday_train":halfday_train,"fullday_train":fullday_train,
                                                            "capacity_train":capacity_train,"room_no_train":room_no_train,
                                                            "capacity_plug":capacity_plug,"rate_hr_plug":rate_hr_plug,
                                                            "rate_daily_plug":rate_daily_plug,"rate_weekly_plug":rate_weekly_plug,
                                                            "rate_monthly_plug":rate_monthly_plug,"hourly_plug":hourly_plug,
                                                            "daily_plug":daily_plug,"weekly_plug":weekly_plug,
                                                            "monthly_plug":monthly_plug,"Start_Up":Start_Up,"Coffee_Shop":Coffee_Shop})

def upload_image(images,pid):
        pathlist=[]
        temp_image=images
        print "temp_image",temp_image
        for lis in temp_image:
                print "list",lis
                pathrelative="/static/"+str(pid)+"/"+str(lis)
                print "Ppathrel",pathrelative
                path=os.getcwd() + pathrelative
                print "pathsjdjjfj",path
                d = os.path.dirname(path)
                print "D",d


                if not os.path.exists(d):
                        os.makedirs(d)
                with open(path, 'wb+') as fileName:
                        for chunk in lis.chunks():
                                fileName.write(chunk)
                                #pathcomp=d+pathrelative
                                pathlist.append(pathrelative)
	pathlist=list(set(pathlist))
        return pathlist





		
