﻿Ext.namespace('Ext.caiyee');

Ext.caiyee.TopicReply = Ext.extend(Object, {
	constructor : function(config) {
		Ext.apply(this, config);

		this.events = [];
		this.initialConfig = config || {};
		
		this.init();
	},
	      //创建模板
	tpl_reply:new Ext.XTemplate(
		'<div class="vedio_all" >',
				'<div class="vedio_person">',
					'<ul>',
						'<li style="float:left; width:auto; padding:3px 10px;">财益网友&nbsp;&nbsp;&nbsp;{nick}</li>',
						'<li style="float:left;width:150px; padding:3px 10px;">{created}&nbsp;&nbsp;时说：</li>',
						'<li style="float:right;padding:3px 10px;">{floor}楼</li>',
					'</ul>',
				'</div>',
				'<span>{content}</span>',
			'</div>'
	),
	
	tpl_reply_item:new Ext.XTemplate(
	'<div class="reply_wz"><dl><dd><span><h1>{nick}</h1><h1># {floor}</h1><h2>{time}</h2></span><p>{content}</p></dd></dl></div>'
		),
	tpl_reply_commit:new Ext.XTemplate(
			'<div class="veido_reply">',
				'<p style="font-weight:bold; color:#000000;">我来说两句</p>',
			'</div>',
			'<div style="float:left; padding:10px 0 0 3px;">',
				'<div class="commentAction" style="display:block">',
					'<textarea id="txt_msg" name="content" ></textarea>',
					'<div class="action">',
						'<div class="submit"><button id="bt_comment" name="bt_comment" >发表评论</button>',
					'</div>',
				'</div>'
		),
	tpl_reply_reg:new Ext.XTemplate(
			'<div class="vedio_all" >',
				'<div class="vedio_person">',
					'<ul>',
						'<li style="float:left; width:auto; padding:3px 10px;">财益网友&nbsp;&nbsp;&nbsp;{nick}</li>',
						'<li style="float:left;width:150px; padding:3px 10px;">{{r.created}}&nbsp;&nbsp;时说：</li>',
						'<li style="float:right;padding:3px 10px;"></li>',
					'</ul>',
				'</div>',
				'<span>{{r.content.content}}</span>',
			'</div>'
		),
		tpl_reply_line:new Ext.XTemplate(
		'<div style="padding:0 0 0 200px;color:#555555;font-family:宋体，黑体,Verdana,palatino,Arial,geneva,sans-serif;font-size:13px;line-height:25px;clear:both;">登陆后可以发表对本文章的评论</div>'
		),
	data:[],
	url:'',
	logout: true,

	getLogstate : function(response, opts){
		var code = Ext.decode(response.responseText)['code'];
		if (code != -1){
			this.logout = false;
			}
	},

	onRenderReply : function(response, opts){
		//数据源
		var el = Ext.get('al');
		this.data = Ext.decode(response.responseText)['data'];
		this.url = window.location.href;
		//重写绑定模板
		//this.tpl_reply.overwrite(el, [this.topic_pv]);
		if (this.logout){
			for(var i=0; i<data_length; i++){
				this.data[i]['content']['floor'] = data_length-i;
				this.data[i]['content']['created'] = this.data[i].created;
				this.tpl_reply_reg.insertAfter(el, {hrf:this.url});
			};
			this.tpl_reply_line.insertAfter(el,[]);
		}
		else{
			this.tpl_reply_commit.insertAfter(el);
			this.submit = new Ext.Button({
				x:20,
				y:10,
				width:80,
				text:'发表',
				renderTo:Ext.get('reply_commit')
				});
			Ext.fly('bt_comment').on('click', this.onCommitReply, this);
			};
		var data_length = this.data.length;
		for(var i=0; i<data_length; i++){
			this.data[i]['content']['floor'] = data_length-i;
			this.data[i]['content']['created'] = this.data[i].created;
			this.tpl_reply.insertAfter(el,this.data[i]['content']);
		};
	},

	onCommitReply : function(userInfo, content){
		var tid = Ext.getDom('topic_id').title;
		var content_c = Ext.getDom('txt_msg').value;
		if (content_c === ''){
			alert('不能提交空的回复');
			return;
		}
		var year = 0;
		var month = 0;
		var day = 0;
		var currentDate = "";
		var time = new Date;
		year = time.getFullYear();
		month = time.getMonth()+1;
		day = time.getDate();
		currentDate = year+ "-";
		if (month >=10 ){
			currentDate = currentDate + month + "-";
		}else{
			currentDate = currentDate + "0" + month + "-";
		}
		if (day >=10){
			currentDate = currentDate + day;
		}else{
			currentDate = currentDate + "0" + day;
		}
		var el_reply = Ext.get('al');
		var reply_data = {
			nick:"您",
			floor:"0",
			created:currentDate,
			content:content_c
		};
		this.tpl_reply.insertAfter(el_reply, reply_data);
		var params = {opt:'save', content:content_c, topic_id: tid};

		url = '/public/topic_reply_a';
		Ext.Ajax.request({
			url: url,
			method: 'POST',
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				alert('发表成功！');
			},
			failure: function(response, opts) {
				alert('failure');
			},
			params: params
			});
	},

	init : function(){
		Ext.Ajax.request({
			url: '/public/topic_reply_a',
			method: 'POST',
			success: this.getLogstate.createDelegate(this, [], true),
			failure: function(response, opts) {
					alert('failure');
					}
			});
		var tid = Ext.getDom('topic_id').title;
		var params = {opt:'get', topic_id: tid};
		Ext.Ajax.request({
			url: '/public/get_topic_reply_a',
			method: 'GET',
			success: this.onRenderReply.createDelegate(this, [], true),
			failure: function(response, opts) {
				alert('failure');
			},
			params: params
			});
	}
});

Ext.onReady(function(){
	var topic_pv = Ext.decode(Ext.get('topic_pv').dom.firstChild.nodeValue);
	var app = new Ext.caiyee.TopicReply({
		topic_pv: topic_pv
	});
	return;
});
