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

class farm_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'农场' #中文名
			,path = '/farm/farm' #路径
			,width = '600' # 新增页面的宽度
			,height = '600' # 新增页面的高度
			,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':'/device/gateway'}
			,{'name':'设备管理', 'url':'/device/info'}
			,{'name':'农事管理', 'url':'/work/work'}
			,{'name':'农事分类', 'url':'/work/category'}
			,{'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
	@Web.setting
	def get(self):
		self.common(
			path = '/farm/farm'
			,label = (u'农场名称',u'农场地址',u'联系人',u'联系电话')
			,update = ('name-input-required','address-input-required','master_name-input-required','tel-input-required')
		)
		self.commonOne('farm')
		self.commonView('update')
	@Web.auth
	@Web.setting
	def post(self):
		id = self.commonUpdate('farm')
		Demeter.config['setting']['farmList'] = self.service('common').list('farm')

class info_path(Load):
	@Web.auth
	@Web.setting
	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'农场介绍')
			,update = ('name-input-required','pic-pic-required-1000px*300~450px','tel-input-required','address-input-required','master_name-input-required',u'info-text-required-请输入农场介绍')
		)
		self.commonOne('farm', id=Demeter.config['setting']['farm'])
		self.commonView('set')
	@Web.auth
	@Web.setting
	def post(self):
		self.commonUpdate('farm', id=Demeter.config['setting']['farm'])

class user_path(Load):
	@Web.auth
	@Web.setting
	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'用户名称', u'绑定列表', u'用户手机号', u'更新时间')
			,tbody = ('farm','username','wechat', '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'])
				id = str(farm['id'])
				self.data['list'][key]['farm'] = farm['name']
				param = '?farm=' + id + '&search_uid-input-=' + str(value['id'])
				self.data['list'][key]['wechat'] = '<a href="/farm/user_wechat'+param+'">点此查看</a>'
		self.commonView('list')

class user_update_path(Load):
	@Web.auth
	@Web.setting
	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
	@Web.setting
	def post(self):
		self.commonUpdate('farm_user', '手机号已经被注册', mobile=self.data['update']['mobile'])
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('farm_user')

class user_wechat_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'微信绑定列表'
			,path = '/farm/user_wechat'
			,width = '600'
			,height = '400'
			,add = False
			,edit = False
			,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','uid-input-'), (u'日期范围',u'开始时间',u'截止时间','选择农场',u'用户id'))
			,thead = (u'所属农场', u'微信昵称', u'微信openid', u'更新时间', u'封禁')
			,tbody = ('farm','nickname','openid', 'cdate', 'ban')
			,state = False
		)

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

		self.commonList('farm_user_wechat')
		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 value['status'] == True:
					self.data['list'][key]['ban'] = '<a href="javascript:;" alt="点此恢复" onclick="oper(this,\''+value['id']+'\', \'/farm/user_wechat_update\', \'False\')">已封禁</a>'
				else:
					self.data['list'][key]['ban'] = '<a href="javascript:;" alt="点此封禁" onclick="oper(this,\''+value['id']+'\', \'/farm/user_wechat_update\', \'True\')">封禁</a>'
		self.commonView('list')

class user_wechat_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			path = '/farm/user_wechat'
			,label = (u'微信昵称',)
			,update = ('nickname-input-required',)
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_user_wechat')
		self.commonView('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.commonUpdate('farm_user_wechat')
	@Web.auth
	@Web.setting
	def delete(self):
		id = self.input('id')
		update = {}
		update['status'] = self.input('status', True)
		self.service('common').update('farm_user_wechat', id, update)
		#self.commonDelete('farm_user_wechat')