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

class Farm(object):
	@staticmethod
	def init(self, search={}):
		if 'edit' in self.request.uri:
			self.data['farm_cur'] = 'edit'
		elif 'product' in self.request.uri:
			self.data['farm_cur'] = 'product'
		elif 'env' in self.request.uri:
			self.data['farm_cur'] = 'env'
		else:
			self.data['farm_cur'] = 'index'
		self.data['url'] = Farm.url()

	@staticmethod
	def url():
		config = {}
		config['info'] = '/'
		config['edit'] = '/main/edit'
		config['product'] = '/main/product'
		config['env'] = '/main/env'
		return config

class index_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		mobile = Demeter.checkMobile(self.request)
		if mobile:
			self.redirect('/device')
			return

		Farm.init(self)
		self.view("farm/index.html")

	@Web.auth
	@Web.setting
	def post(self):
		id = self.data['setting']['farm']
		update = {}
		update['info'] = self.input('update_info')
		if not update['info']:
			self.out(u'请输入农场介绍')
			return
		state = self.service('common').update('farm', id, update)
		self.out('修改成功')

class edit_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		Farm.init(self)
		self.view("farm/edit.html")

	@Web.auth
	@Web.setting
	def post(self):
		id = self.data['setting']['farm']
		update = {}
		update['name'] = self.input('update_name')
		update['pic'] = self.input('update_pic')
		update['location'] = self.input('update_location')
		update['master_name'] = self.input('update_master_name')
		update['master_pic'] = self.input('update_master_pic')
		update['tel'] = self.input('update_tel')
		update['address'] = self.input('update_address')
		update['welcome'] = self.input('update_welcome')
		if not update['name']:
			self.out(u'请输入名称')
			return
		if not update['pic']:
			self.out(u'请选择图片')
			return
		if not update['location']:
			self.out(u'请输入位置')
			return
		if not update['master_name']:
			self.out(u'请输入姓名')
			return
		if not update['master_pic']:
			self.out(u'请选择图片')
			return
		if not update['tel']:
			self.out(u'请输入联系电话')
			return
		if not update['address']:
			self.out(u'请输入联系地址')
			return
		state = self.service('common').update('farm', id, update)
		self.out('修改成功')

	@Web.auth
	@Web.setting
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.service('common').rDelete('farm', id)
		self.out('yes', {'state':state})

class product_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		Farm.init(self)
		self.data['list'] = self.service('common').list('farm_product', search={'farm_id':self.data['setting']['farm']})
		self.view("farm/product.html")

class product_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		id = self.input('id')
		self.data['info'] = {}
		if id:
			self.data['info'] = self.service('common').one('farm_product', id=id)
		self.view("farm/product_update.html")

	@Web.auth
	@Web.setting
	def post(self):
		id = self.input('id')
		update = {}
		update['name'] = self.input('update_name')
		update['pic'] = self.input('update_pic')
		update['area'] = self.input('update_area')
		update['yields'] = self.input('update_yields')
		update['charact'] = self.input('update_charact')
		update['notice'] = self.input('update_notice')

		if not update['name']:
			self.out(u'请输入名称')
			return
		if not update['area']:
			self.out(u'请输入种植面积')
			return
		if not update['yields']:
			self.out(u'请输入平均亩产')
			return
		"""
		if not update['charact']:
			self.out(u'请输入特点')
			return
		if not update['notice']:
			self.out(u'请输入使用须知')
			return
		"""
		if not update['pic']:
			self.out(u'请选择图片')
			return
		state = self.service('common').update('farm_product', id, update)
		self.out('yes', {'state':state})

	@Web.auth
	@Web.setting
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.input('state', '1')
		update = {}
		if state == '1':
			update['status'] = True
		else:
			update['status'] = False
		state = self.service('common').update('farm_product', id, update)
		self.out('yes', {'state':state})

class env_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		Farm.init(self)
		self.data['list'] = self.service('common').list('farm_env', order='inorder asc,cdate desc', search={'farm_id':self.data['setting']['farm']})
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				info = self.service('common').one('device_info', id=value['device_id'])
				self.data['list'][key]['device'] = info
				info = self.service('common').one('device_type', id=value['type_id'])
				self.data['list'][key]['type'] = info
		self.view("farm/env.html")

class env_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		id = self.input('id')
		self.data['info'] = {}
		if id:
			self.data['info'] = self.service('common').one('farm_env', id=id)
		self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
		self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 2,'farm_id':self.data['setting']['farm']})
		self.view("farm/env_update.html")

	@Web.auth
	@Web.setting
	def post(self):
		id = self.input('id')
		update = {}
		update['name'] = self.input('update_name')
		update['device_id'] = self.input('update_device_id')
		update['inorder'] = self.input('update_inorder', 0)

		if not update['name']:
			self.out(u'请输入环境名称')
			return
		if not update['device_id']:
			self.out(u'请选择设备')
			return
		if not update['inorder']:
			update['inorder'] = 0
		"""
		if update['inorder'] and not Check.number(update['inorder']):
			self.out(u'排序必须是数字')
			return
		"""
		device = self.service('common').one('device_info', id=update['device_id'])
		if not device:
			self.out(u'请选择设备')
			return
		update['type_id'] = device['type_id']
		state = self.service('common').update('farm_env', id, update)
		self.out('yes', {'state':state})

	@Web.auth
	@Web.setting
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.input('state', False)
		state = self.service('common').rDelete('farm_env', id)
		self.out('yes', {'state':state})