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

class work_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'农事'
			,path = '/work/work'
			,width = '600'
			,height = '600'
			,add = False
			,edit = False
			,search = (('label-1','workdate-time-start','workdate-time-end','farm_id-select-','category_id-select-'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'劳作分类'))
			,thead = (u'所属农场', u'劳作时间', u'劳作分类', u'劳作方式', u'劳作地块', u'劳作用量', u'更新时间')
			,tbody = ('farm','workdate', 'category', 'method', 'land', 'amount', 'cdate')
			,state = False
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.commonList('device_gateway')

		self.data['common']['search_category_id-select-'] = self.service('common').list('farm_work_category')
		self.commonList('farm_work')
		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]['workdate'] = Demeter.date(value['workdate'])
				self.data['list'][key]['farm'] = farm['name']
				category = self.service('common').one('farm_work_category', id=value['category_id'])
				self.data['list'][key]['category'] = category['name']
				method = self.service('common').one('farm_work_method', id=value['method_id'])
				self.data['list'][key]['method'] = method['name']
				land = self.service('common').one('farm_work_land', id=value['land_id'])
				self.data['list'][key]['land'] = land['name']
		self.commonView('list')

class work_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/work/work'
			,label = (u'所属农场',u'劳作时间',u'劳作类别',u'劳作方式',u'劳作用量',u'劳作地块',u'图像记录')
			,update = ('farm_id-select-required', 'workdate-date-required','category_id-select-required-method_id','method_id-select-required--category_id','amount-input-required','land_id-select-required','pic-pic-required')
			,update_category_id = self.service('common').list('farm_work_category')
			,update_method_id = self.service('common').list('farm_work_method')
			,update_land_id = self.service('common').list('farm_work_land')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_work')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('farm_work')
	@Web.auth
	def delete(self):
		self.commonDelete('farm_work')

class method_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'劳作方法'
			,path = '/work/method'
			,width = '800'
			,height = '300'
			,add = False
			,edit = False
			,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','category_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'劳作分类',u'劳作内容'))
			,thead = (u'所属农场',u'劳作内容', u'劳作分类', u'更新时间')
			,tbody = ('farm','name', 'category', 'cdate')
			,state = False
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.data['common']['search_category_id-select-'] = self.service('common').list('farm_work_category')
		self.commonList('farm_work_method')
		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']
				category = self.service('common').one('farm_work_category', id=value['category_id'])
				self.data['list'][key]['category'] = category['name']
		self.commonView('list')

class method_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/work/method'
			,label = (u'所属农场', u'劳作内容',u'劳作分类')
			,update = ('farm_id-select-required', 'name-input-required','category_id-select-required')
			,update_category_id = self.service('common').list('farm_work_category')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_work_method')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('farm_work_method')
	@Web.auth
	def delete(self):
		self.commonDelete('farm_work_method')

class category_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'劳作分类'
			,path = '/work/category'
			,width = '600'
			,height = '300'
			,add = False
			,edit = False
			,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'分类名称'))
			,thead = (u'所属农场',u'分类名称', u'更新时间')
			,tbody = ('farm', 'name', 'cdate')
			,state = False
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.commonList('farm_work_category')
		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 category_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/work/category'
			,label = (u'所属农场', u'分类名称')
			,update = ('farm_id-select-required', 'name-input-required')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_work_category')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('farm_work_category')
	@Web.auth
	def delete(self):
		self.commonDelete('farm_work_category')


class land_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'地块'
			,path = '/work/land'
			,width = '600'
			,height = '300'
			,add = False
			,edit = False
			,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'地块名称'))
			,thead = (u'所属农场',u'地块名称', u'更新时间')
			,tbody = ('farm', 'name', 'cdate')
			,state = False
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.commonList('farm_work_land')
		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 land_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/work/land'
			,label = (u'所属农场', u'地块名称')
			,update = ('farm_id-select-required','name-input-required')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('farm_work_land')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('farm_work_land')
	@Web.auth
	def delete(self):
		self.commonDelete('farm_work_land')