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

class batch_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'溯源批次'
			,path = '/origin/batch'
			,width = '600'
			,height = '600'
			,search = (('farm_id-select-','land_id-select-'), (u'选择农场',u'地块'))
			,thead = (u'所属农场', u'产品名称', u'种植时间', u'采收时间', u'设备网关', u'地块')
			,tbody = ('farm','name', 'zzdate', 'csdate', 'device', 'land')
			,state = True
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.data['common']['search_land_id-select-'] = self.service('common').list('farm_work_land')

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

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

		gateway = {}
		gateway_list = self.service('common').list('device_gateway')
		for value in gateway_list:
			gateway[value['id']] = value

		self.commonList('origin_batch')
		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']
				device = gateway[value['gateway_id']]
				self.data['list'][key]['device'] = device['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 batch_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			path = '/origin/batch'
			,label = (u'所属农场',u'产品名称', u'种植时间',u'采收时间',u'设备网关',u'地块',u'图像记录')
			,update = ('farm_id-select-required','name-input-required', 'zzdate-date-required', 'csdate-date-required','gateway_id-select-required','land_id-select-required','pic-pic-required')
			,update_gateway_id = self.service('common').list('device_gateway')
			,update_land_id = self.service('common').list('farm_work_land')
			,update_farm_id = Demeter.config['setting']['farmList']
		)
		self.commonOne('origin_batch')
		self.commonView('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.commonUpdate('origin_batch')
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('origin_batch')
