# -*- 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('/')
		self.data['farm'] = self.service('common').one('farm', id=self.data['info']['farm_id'])
		self.data['product'] = self.service('common').one('farm_product', id=self.data['info']['product_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['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['growth'] = self.service('common').list('origin_growth', search={'batch_id':self.data['info']['id']}, order='date asc,cdate asc')

		#self.data['device'] = self.service('common').one('device_info', id=self.data['info']['device_camera_id'])

		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']):
				method = self.service('common').one('farm_work_method', id=value['method_id'])
				value['method'] = method['name']
				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)
		
		self.data['info']['devices'] = tuple(eval(self.data['info']['devices']))
		search = {}
		search['id--ins'] = self.data['info']['devices']
		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")