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

class Work(object):
	@staticmethod
	def init(self, search={}):
		if 'set' in self.request.uri:
			self.data['work_cur'] = 'set'
		else:
			self.data['work_cur'] = 'index'
		self.data['url'] = Work.url()

	@staticmethod
	def url():
		config = {}
		config['index'] = '/work'
		config['set'] = '/work/set'
		return config

class index_path(Load):
	@Web.auth
	def get(self):
		mobile = Demeter.checkMobile(self.request)
		if mobile:
			self.redirect('/device')
			return
			
		Work.init(self)
		self.data['list'] = self.service('common').list('farm_work', search={'farm_id':self.data['setting']['farm']})
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				self.data['list'][key]['workdates'] = Demeter.date(value['workdate'], '%Y-%m-%d %H:%M')
				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.view("work/index.html")

class update_path(Load):
	@Web.auth
	def get(self):
		id = self.input('id')
		self.data['category'] = self.service('common').list('farm_work_category', search={'farm_id':self.data['setting']['farm']})
		self.data['method'] = self.service('common').list('farm_work_method', search={'farm_id':self.data['setting']['farm']})
		self.data['land'] = self.service('common').list('farm_work_land', search={'farm_id':self.data['setting']['farm']})
		self.data['info'] = {}
		if id:
			self.data['info'] = self.service('common').one('farm_work', id=id)
			if self.data['info'] and self.data['info']['workdate']:
				self.data['info']['workdate'] = Demeter.date(self.data['info']['workdate'])
		self.view("work/update.html")

	@Web.auth
	def post(self):
		id = self.input('id')
		update = {}
		update['amount'] = self.input('update_amount')
		update['category_id'] = self.input('update_category_id')
		update['method_id'] = self.input('update_method_id')
		update['land_id'] = self.input('update_land_id')
		update['pic'] = self.input('update_pic')
		update['workdate'] = self.input('update_workdate')

		if not update['category_id']:
			self.out(u'请选择劳作类别')
			return
		if not update['method_id'] or update['method_id'] == '0' or update['method_id'] == 0:
			self.out(u'请选择劳作方式')
			return
		if not update['land_id']:
			self.out(u'请选择操作地块')
			return
		if not update['workdate']:
			self.out(u'请选择农事日期')
			return
		if not update['amount']:
			self.out(u'请输入劳作用量')
			return
		if not update['pic']:
			self.out(u'请选择图片')
			return
		state = self.service('common').update('farm_work', id, update)
		self.out('yes', {'state':state})

	@Web.auth
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.input('state', False)
		state = self.service('common').delete('farm_work', id, state)
		self.out('yes', {'state':state})

class set_path(Load):
	@Web.auth
	def get(self):
		Work.init(self)
		self.data['category'] = self.service('common').list('farm_work_category', search={'farm_id':self.data['setting']['farm']})
		self.data['method'] = self.service('common').list('farm_work_method', search={'farm_id':self.data['setting']['farm']})
		self.data['land'] = self.service('common').list('farm_work_land', search={'farm_id':self.data['setting']['farm']})
		self.view("work/set.html")

class land_update_path(Load):
	@Web.auth
	def get(self):
		id = self.input('id')
		self.data['info'] = {}
		if id:
			self.data['info'] = self.service('common').one('farm_work_land', id=id)
		self.view("work/land_update.html")

	@Web.auth
	def post(self):
		id = self.input('id')
		update = {}
		update['name'] = self.input('update_name')

		if not update['name']:
			self.out(u'请输入地块名称')
			return
		state = self.service('common').update('farm_work_land', id, update)
		self.out('yes', {'state':state})

	@Web.auth
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.input('state', False)
		state = self.service('common').delete('farm_work_land', id, state)
		self.out('yes', {'state':state})

class category_update_path(Load):
	@Web.auth
	def get(self):
		id = self.input('id')
		self.data['info'] = {}
		if id:
			self.data['info'] = self.service('common').one('farm_work_category', id=id)
		self.view("work/category_update.html")

	@Web.auth
	def post(self):
		id = self.input('id')
		update = {}
		update['name'] = self.input('update_name')

		if not update['name']:
			self.out(u'请输入农事内容分类名称')
			return
		state = self.service('common').update('farm_work_category', id, update)
		self.out('yes', {'state':state})

	@Web.auth
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.input('state', False)
		state = self.service('common').delete('farm_work_category', id, state)
		self.out('yes', {'state':state})

class method_update_path(Load):
	@Web.auth
	def get(self):
		id = self.input('id')
		self.data['category'] = self.input('category')
		self.data['info'] = {}
		if id:
			self.data['info'] = self.service('common').one('farm_work_method', id=id)
		self.view("work/method_update.html")

	@Web.auth
	def post(self):
		id = self.input('id')
		update = {}
		update['name'] = self.input('update_name')
		update['category_id'] = self.input('update_category_id')

		if not update['name']:
			self.out(u'请输入农事内容名称')
			return
		if not update['category_id']:
			self.out(u'请选择农事分类')
			return
		state = self.service('common').update('farm_work_method', id, update)
		self.out('yes', {'state':state})

	@Web.auth
	def delete(self):
		id = self.input('id')
		if not id:
			self.out('no')
			return
		state = self.input('state', False)
		state = self.service('common').delete('farm_work_method', id, state)
		self.out('yes', {'state':state})