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

farm = {
	'id' : int(Demeter.config['setting']['farm'])
}

class farm_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			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'ID', u'农场名称', u'状态', u'功能列表') #表头
			,tbody = ('id','name', 'status', 'func') #表内容
			,state = False #启用回收站
		)
		menu = (
			{'name':'基础资料', 'url':'/farm/info'}
			,{'name':'用户管理', 'url':'/farm/user'}
			,{'name':'产品管理', 'url':'/farm/product'}
			,{'name':'地块管理', 'url':'/work/land'}
			,{'name':'农事管理', 'url':'/work/work'}
			,{'name':'批次管理', 'url':'/origin/batch'}

			,{'name':'网关管理', 'url':'/device/gateway'}
			,{'name':'设备管理', 'url':'/device/info'}

			#,{'name':'生成本地控制器', 'url':'/package/download'}
			
			#,{'name':'打印机管理', 'url':'/device/printer'}
			#,{'name':'消息提醒', 'url':'/msg/msg'}
			#,{'name':'网关日志', 'url':'/device/log'}
			)
		control = {'name':'本地控制', 'url':'/package/download'}
		self.list('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]['status'] = '云端'
				if value['eros_status'] == 1:
					self.data['list'][key]['status'] = '本地：' + value['eros_server']
				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.show('list')

class farm_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			path = '/farm/farm'
			,label = (u'农场名称',)
			,update = ('name-input-required',)
		)
		self.one('farm')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		id = self.update('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
	@Web.setting
	def get(self):
		id = self.input('farm')
		param = '?farm=' + id
		self.set(
			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-请输入农场介绍')
			,update_eros_status = Demeter.service('core', 'setting').eros_status()
		)
		self.one('farm', id=id)
		self.show('set')
	@Web.auth
	@Web.setting
	def post(self):
		id = self.input('id')
		info = self.service('common').one('farm', id=id)
		if info and info['eros_status'] == 1:
			self.out('本地服务已开启，无法通过云端修改农场资料')
			return
		self.update('farm')

class product_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			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.list('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.show('list')

class product_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			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 = self.service('common').list('farm', search={'eros_status':2})
		)
		self.one('farm_product')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		#self.data['update']['farm_id'] = farm['id']
		self.update('farm_product')
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('farm_product')


class user_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			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.list('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.show('list')

class user_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			path = '/farm/user'
			,label = (u'所属农场',u'账号姓名',u'手机号',u'安全码')
			,update = ('farm_id-select-required','username-input-required','mobile-input-phone','password-password-')
			,update_farm_id = self.service('common').list('farm', search={'eros_status':2})
		)
		self.one('farm_user')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		#self.data['update']['farm_id'] = farm['id']
		self.update('farm_user', '手机号已经被注册', mobile=self.data['update']['mobile'])
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('farm_user')