# -*- 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
	@Web.setting
	def get(self):
		self.set(
			name = u'消息'
			,path = '/msg/msg'
			,width = '600'
			,height = '600'
			,add = False
			,edit = False
			,full = True
			,search = (('type_id-select-','content-input-mlike'), (u'硬件类型', u'消息内容'))
			,thead = ( u'消息类型', u'消息内容', u'是否已读',u'发送时间')
			,tbody = ('type', 'content', 'status', 'cdate')
			,state = False
		)
		self.data['common']['search_type_id-select-'] = self.service('common').list('msg_type')
		self.list('msg')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				htype = self.service('common').one('msg_type', id=value['type_id'])
				self.data['list'][key]['type'] = htype['name']

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

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

class msg_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			path = '/msg/msg'
			,label = (u'发送用户',u'消息类型',u'消息内容',u'消息链接',u'推送')
			,update = ('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_uid = self.service('common').list('farm_user')
			,update_push = push
		)
		self.one('msg')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		# 发消息
		self.update('msg')
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('msg')

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

class type_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			path = '/msg/type'
			#,label = (u'类型名称', u'类型key',u'推送')
			#,update = ('name-input-required', 'key-input-required','push-checkbox-')
			,label = (u'类型名称', u'类型key')
			,update = ('name-input-required', 'key-input-required')
			,update_push = push
		)
		self.one('msg_type')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.update('msg_type')
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('msg_type')
