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

push = ({'id':'sms', 'name':'短信'}, {'id':'weixin', 'name':'微信'})

class msg_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'消息'
			,path = '/msg/msg'
			,width = '600'
			,height = '600'
			#,add = False
			,edit = False
			,full = True
			,search = (('farm_id-select-','type_id-select-','content-input-mlike'), (u'选择农场',u'硬件类型', u'消息内容'))
			,thead = (u'所属农场', u'消息类型', u'接收人', u'消息内容', u'是否已读',u'发送时间')
			,tbody = ('farm','type', 'user', 'content', 'status', 'cdate')
			,state = False
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.data['common']['search_type_id-select-'] = self.service('common').list('msg_type')
		self.commonList('msg')
		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']
				htype = self.service('common').one('msg_type', id=value['hardware_type'])
				self.data['list'][key]['type'] = htype['name']

				user = self.service('common').one('farm_user', id=value['uid'])
				self.data['list'][key]['user'] = user['username']

				if value['status'] == True:
					self.data['list'][key]['status'] = '已读'
				else:
					self.data['list'][key]['device'] = '未读'
		self.commonView('list')

class msg_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/msg/msg'
			,label = (u'所属农场',u'发送用户',u'消息类型',u'消息内容',u'消息链接',u'推送')
			,update = ('farm_id-select--uid','uid-checkbox--farm_id','msg_type-select-required','content-editor-required','link-input-','push-checkbox-')
			,update_msg_type = self.service('common').list('msg_type')
			,update_farm_id = Demeter.config['setting']['farmList']
			,update_uid = self.service('common').list('farm_user')
			,update_push = push
		)
		self.commonOne('msg')
		self.commonView('update')
	@Web.auth
	def post(self):
		# 发消息
		self.commonUpdate('msg')
	@Web.auth
	def delete(self):
		self.commonDelete('msg')

class type_path(Load):
	@Web.auth
	def get(self):
		self.common(
			name = u'消息类型'
			,path = '/msg/type'
			,width = '600'
			,height = '300'
			,search = (('name-input-mlike',), (u'类型名称',))
			,thead = (u'类型名称',u'类型key', u'更新时间')
			,tbody = ('name', 'key', 'cdate')
			,state = False
		)
		self.commonList('msg_type')
		self.commonView('list')

class type_update_path(Load):
	@Web.auth
	def get(self):
		self.common(
			path = '/msg/type'
			,label = (u'类型名称', u'类型key',u'推送')
			,update = ('name-input-required', 'key-input-required','push-checkbox-')
			,update_push = push
		)
		self.commonOne('msg_type')
		self.commonView('update')
	@Web.auth
	def post(self):
		self.commonUpdate('msg_type')
	@Web.auth
	def delete(self):
		self.commonDelete('msg_type')