#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    demeter web page
    name:farm.py 农场相关页面
    author:rabin
"""
from __load__ import *

class farm_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'农场' #中文名
			,path = '/farm/farm' #路径
			,width = '600' # 新增页面的宽度
			,height = '600' # 新增页面的高度
			,edit = False
			,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'农场名称')) #搜索
			,thead = (u'农场名称', u'功能列表') #表头
			,tbody = ('name', 'func') #表内容
			,state = False #启用回收站
		)
		menu = (
			{'name':'基础资料', 'url':'/farm/info'}
			,{'name':'农场产品', 'url':'/farm/product'}
			,{'name':'网关管理', 'url':'/device/gateway'}
			,{'name':'设备管理', 'url':'/device/info'}
			,{'name':'农事管理', 'url':'/work/work'}
			,{'name':'农事分类', 'url':'/work/category'}
			,{'name':'劳作方式', 'url':'/work/method'}
			,{'name':'地块管理', 'url':'/work/land'}
			,{'name':'批次管理', 'url':'/origin/batch'}
			,{'name':'用户管理', 'url':'/farm/user'}
			,{'name':'打印机管理', 'url':'/device/printer'}
			,{'name':'消息提醒', 'url':'/msg/msg'}
			)
		self.commonList('farm')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				id = str(value['id'])
				param = '?farm=' + id + '&search_farm_id-select-=' + id
				self.data['list'][key]['func'] = ''
				for i in menu:
					self.data['list'][key]['func'] = self.data['list'][key]['func'] + '<a href="'+i['url']+''+param+'">'+i['name']+'</a>&nbsp;&nbsp;&nbsp;&nbsp;'
		self.commonView('list')

class farm_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/farm/farm'
			,label = (u'农场名称',)
			,update = ('name-input-required',)
		)
		self.commonOne('farm')
		self.commonView('update')
	@Web.auth
	def post(self):
		id = self.commonUpdate('farm')
		Demeter.config['setting']['farmList'] = self.service('common').list('farm')
		#self.gateway(id)

	def gateway(self, farm):
		gateway = self.data['update']['gateway']
		if gateway:
			#换行隔开，逗号隔开，插入到网关表里
			data = gateway.split("\r\n")
			for value in data:
				if ',' in value:
					value = value.split(',')
				else:
					value[0] = value
					value[1] = '网关' + value
				info = self.service('common').one('device_gateway', hardware_id=value[0])
				if not info:
					insert = {}
					insert['farm_id'] = farm
					insert['name'] = value[1]
					insert['hardware_id'] = value[0]
					self.service('common').update('device_gateway', None, insert)
				else:
					update = {}
					update['name'] = value[1]
					self.service('common').update('device_gateway', info['id'], update)


class info_path(Load):
	@Web.auth
	def get(self):
		id = str(Demeter.config['setting']['farm'])
		param = '?farm=' + id + '&search_farm_id-select-=' + id
		self.common(
			path = '/farm/info' + param
			,label = (u'农场名称',u'农场图片',u'农场位置',u'联系电话',u'联系地址',u'农场主姓名',u'农场主照片',u'欢迎语',u'农场介绍')
			,update = ('name-input-required','pic-pic-required-1000px*300~450px','location-input-required','tel-input-required','address-input-required','master_name-input-required','master_pic-pic-required-300px*450px','welcome-input-',u'info-text-required-请输入农场介绍')
		)
		self.commonOne('farm', id=Demeter.config['setting']['farm'])
		self.commonView('set')
	@Web.auth
	def post(self):
		self.commonUpdate('farm', id=Demeter.config['setting']['farm'])

class product_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'产品'
			,path = '/farm/product'
			,width = '600'
			,height = '500'
			,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间','选择农场',u'产品名称'))
			,thead = (u'所属农场', u'产品名称', u'图片', u'种植面积', u'平均亩产', u'更新时间')
			,tbody = ('farm','name', 'pic', 'area', 'yields', 'cdate')
			,state = True
		)

		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']

		self.commonList('farm_product')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				farm = self.service('common').one('farm', id=value['farm_id'])
				self.data['list'][key]['farm'] = farm['name']
				if self.data['list'][key]['pic']:
					self.data['list'][key]['pic'] = '<img src="'+self.data['list'][key]['pic']+'" width="200"/>'
				else:
					self.data['list'][key]['pic'] = '暂无图片'
		self.commonView('list')

class product_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/farm/product'
			,label = (u'所属农场',u'产品名称', u'种植面积', u'平均亩产', u'特点', u'使用须知', u'图片')
			,update = ('farm_id-select-required', 'name-input-required','area-input-required','yields-input-required','charact-input-required','notice-input-required','pic-pic-required')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_product')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('farm_product')
	@Web.auth
	def delete(self):
		self.commonDelete('farm_product')


class user_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'用户'
			,path = '/farm/user'
			,width = '600'
			,height = '400'
			,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间','选择农场',u'用户名称'))
			,thead = (u'所属农场', u'UID', u'用户名称', u'用户手机号', u'更新时间')
			,tbody = ('farm','id', 'username', 'mobile', 'cdate')
			,state = True
		)

		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']

		self.commonList('farm_user')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				farm = self.service('common').one('farm', id=value['farm_id'])
				self.data['list'][key]['farm'] = farm['name']
		self.commonView('list')

class user_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/farm/user'
			,label = (u'所属农场',u'账号姓名',u'手机号',u'安全码')
			,update = ('farm_id-select-required','username-input-required','mobile-input-phone','password-password-')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_user')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('farm_user', '手机号已经被注册', mobile=self.data['update']['mobile'])
	@Web.auth
	def delete(self):
		self.commonDelete('farm_user')