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

class work_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'农事'
			,path = '/work/work'
			,width = '600'
			,height = '600'
			,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'更新时间')
			,tbody = ('farm','workdate', 'category', 'land', 'amount', 'cdate')
			,state = True
		)
		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')

		farm_list = {}
		for value in self.data['common']['search_farm_id-select-']:
			farm_list[value['id']] = value

		gateway = {}
		for value in self.data['common']['search_category_id-select-']:
			gateway[value['id']] = value

		farm_work_land = {}
		for value in self.data['common']['search_land_id-select-']:
			farm_work_land[value['id']] = value

		self.commonList('farm_work')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				self.data['list'][key]['land'] = '未知'
				farm = farm_list[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']
				if value['land_id'] in farm_work_land:
					land = farm_work_land[value['land_id']]
					if land:
						self.data['list'][key]['land'] = land['name']
		self.commonView('list')

class work_update_path(Load):
	@Web.auth
	@Web.setting
	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','amount-input-required','land_id-select-required','pic-pic-required','content-text-required')
			,update_category_id = self.service('common').list('farm_work_category')
			,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
	@Web.setting
	def post(self):
		self.commonUpdate('farm_work')
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('farm_work')

class category_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'劳作方式'
			,path = '/work/category'
			,width = '600'
			,height = '250'
			,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'方式名称'))
			,thead = (u'方式名称', u'更新时间')
			,tbody = ('name', 'cdate')
			,state = True
		)
		self.commonList('farm_work_category')
		self.commonView('list')

class category_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			path = '/work/category'
			,label = (u'方式名称',)
			,update = ('name-input-required',)
		)
		self.commonOne('farm_work_category')
		self.commonView('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.commonUpdate('farm_work_category')
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('farm_work_category')


class land_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'地块'
			,path = '/work/land'
			,width = '600'
			,height = '250'
			,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 = True
		)
		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
	@Web.setting
	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
	@Web.setting
	def post(self):
		self.commonUpdate('farm_work_land')
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('farm_work_land')