#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    demeter web page
    name:main.py
    author:rabin
"""
from __load__ import *

class Origin(object):
	@staticmethod
	def init(self, search={}):
		id = self.input('id')
		if not id:
			self.redirect('/')
		if 'work' in self.request.uri:
			self.data['cur'] = 'work'
		elif 'env' in self.request.uri:
			self.data['cur'] = 'env'
		elif 'farm' in self.request.uri:
			self.data['cur'] = 'farm'
		else:
			self.data['cur'] = 'index'

		self.data['info'] = self.service('common').one('origin_batch', id=id)
		if not self.data['info']:
			self.redirect('/')
		if self.data['info']['pic']:
			pic = self.data['info']['pic'].split(',')
			self.data['info']['pic'] = pic[0]
		self.data['farm'] = self.service('common').one('farm', id=self.data['info']['farm_id'])
		if self.data['info']['csdate'] > self.data['info']['zzdate']:
			self.data['zhou'] = self.data['info']['csdate'] - self.data['info']['zzdate']
		else:
			self.data['zhou'] = self.data['info']['zzdate'] - self.data['info']['csdate']
		self.data['zhou'] = self.data['zhou']/64800
		self.data['info']['zzdates'] = Demeter.date(self.data['info']['zzdate'], '%Y-%m-%d')
		self.data['info']['csdates'] = Demeter.date(self.data['info']['csdate'], '%Y-%m-%d')
		self.data['path'] = 'mobile/'
		self.data['url'] = Origin.url('id=' + id + '#menu')

	@staticmethod
	def url(param):
		config = {}
		config['index'] = '/origin?' + param;
		config['work'] = '/origin/work?' + param;
		config['env'] = '/origin/env?' + param;
		config['farm'] = '/origin/farm?' + param;
		return config

class index_path(Load):
	@Web.setting
	def get(self):
		Origin.init(self)

		self.data['list'] = self.service('common').list('farm_work_land_pic', search={'farm_id':self.data['info']['farm_id'], 'land_id':self.data['info']['land_id']}, page={'num':10})
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				self.data['list'][key]['cdates'] = Demeter.date(value['cdate'], '%Y-%m-%d')

		self.data['pic'] = {}
		if self.data['info']['pic_id']:
			self.data['pic'] = self.service('common').one('device_info', id=self.data['info']['pic_id'])
			if self.data['pic']:
				self.data['pic']['cdates'] = Demeter.date(self.data['pic']['cdate'], '%Y-%m-%d')

		self.view(self.data['path'] + "origin/index.html")

class work_path(Load):
	@Web.setting
	def get(self):
		Origin.init(self)
		self.data['work'] = self.service('common').list('farm_work', search={'farm_id':self.data['info']['farm_id']}, order='workdate asc,cdate asc')

		self.data['cate'] = {}
		self.data['list'] = []

		if self.data['work']:
			for key, value in enumerate(self.data['work']):
				value['workdates'] = Demeter.date(value['workdate'], '%Y.%m.%d')

				if value['category_id'] not in self.data['list']:
					self.data['list'].append(value['category_id'])
					self.data['cate'][value['category_id']] = self.service('common').one('farm_work_category', id=value['category_id'])

				if 'work' not in self.data['cate'][value['category_id']]:
					self.data['cate'][value['category_id']]['work'] = []
				self.data['cate'][value['category_id']]['work'].append(value)

		self.view(self.data['path'] + "origin/work.html")

class env_path(Load):
	@Web.setting
	def get(self):
		Origin.init(self)
		search = {}
		search['farm_id'] = self.data['info']['farm_id']
		search['gateway_id'] = self.data['info']['gateway_id']
		search['hardware_type'] = 2
		self.data['list'] = self.service('common').list('device_info', search=search)

		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				self.data['list'][key] = self.stat(value)

		self.view(self.data['path'] + "origin/env.html")

	def stat(self, value):
		method = 'maxmin'
		value['type'] = self.service('common').one('device_type', id=value['type_id'])
		model = Demeter.model('data', 'tsdb')
		value['group'] = model.dateConfig()

		search = {}
		search['device'] = value['id']
		#search['group'] = '1'
		search['start'] = Demeter.date(self.data['info']['zzdate'])
		search['end'] = Demeter.date(self.data['info']['csdate'])
		value['search'] = search
		value['stat'] = model.getData(method, search, value)
		return value

class farm_path(Load):
	@Web.setting
	def get(self):
		Origin.init(self)


		self.view(self.data['path'] + "origin/farm.html")