jiyiri.register_namespace('jiyiri.page.cart');

jiyiri.require_once('jiyiri.page.common.userorderoper');
/* jiyiri.require_once('jiyiri.helper.htmlelementhelper.htmlelementhelper'); */
/* jiyiri.require_once('jiyiri.ui.secondregionselector'); */
/* jiyiri.require_once('jiyiri.helper.validatehelper.data_validate'); */


(
function()
{
	 var CartUIManager = Class.create();
	 CartUIManager._instance = null;
	 CartUIManager.GetInstance = function()
	 {
		if(CartUIManager._instance==null)
		{
			CartUIManager._instance = new CartUIManager();
		}
		return CartUIManager._instance;
	 };



	 CartUIManager.prototype =
	 {
		initialize:function()
		{


		 	this._receiver_name_ctl = $("receiver_name");
		 	this._receiver_phone_ctl = $("receiver_phone");
		 	this._receiver_province_ctl = $('receiver_province');
		 	this._receiver_city_ctl = $('receiver_city');
		 	this._receiver_address_ctl = $("receiver_address");


		 	this._orderer_name_ctl = $("orderer_name");
		 	this._orderer_phone_ctl = $("orderer_phone");
			this._orderer_remark_ctl = $("order_remark");

		 	this._zhifubao_pay_style_ctl = $('zhifubao_paystyle');
		 	this._wangyin_pay_style_ctl = $('wangyin_paystyle');
		 	//this._yinhang_pay_style_ctl = $('yinhang_paystyle');

		 	this._order_address_button_ctl = $('order_address');
		 	this._good_address_button_ctl = $( 'good_address' );
		 	this._order_address_id = null;


		 	this._receive_time_ctl = new TimeSelectorCtl();
		 	this._receive_time_area = $("receive_time_area");

		 	this._error_notice_area = $("errorarea");
		 	this._error_notice_content = $('errorcontent');

		 	this._order_address_button_ctl.onclick = jiyiri.helper.eventhelper.EventHelper.create_event_function(this, 'on_click_order_address' );
		 	this._good_address_button_ctl.onclick = jiyiri.helper.eventhelper.EventHelper.create_event_function(this, 'on_click_good_address' );
		},
		init_show:function(){

			/* 清楚错误区域 */
			this._clean_error();

			/* 预计送达时间 */
			this._receive_time_area.appendChild(this._receive_time_ctl.draw());
			/* 预计送达时间设置成2天后 */
			var now = new Date();
			now.setTime( now.getTime() + (24 * 60 * 60 * 1000 * 4));
			var receiver_year = now.getFullYear();
			var receiver_month = now.getMonth() + 1;
			var receiver_day = now.getDate();
			this._receive_time_ctl.on_select_time( receiver_year,receiver_month,receiver_day );


			/* 地址 */
			jiyiri.ui.SecondRegionSelector.GetInstance().InitialControl('receiver_province','receiver_city');
			jiyiri.ui.SecondRegionSelector.GetInstance().DrawProvinceSelectControl();
			jiyiri.ui.SecondRegionSelector.GetInstance().DrawCitiesSelectControl();

			/* 默认支付方式 */
			this.set_pay_style(jiyiri.page.common.PayStyle.WangYin);

		},
		set_pay_style:function( pay_style ){
			switch( pay_style )
			{
				case jiyiri.page.common.PayStyle.ZhiFuBao:
					this._zhifubao_pay_style_ctl.className = "radioOn";
				 	this._wangyin_pay_style_ctl.className = "radioOff";
				 	//this._yinhang_pay_style_ctl.className = "radioOff";
					break;
				case jiyiri.page.common.PayStyle.WangYin:
					this._zhifubao_pay_style_ctl.className = "radioOff";
				 	this._wangyin_pay_style_ctl.className = "radioOn";
				 	//this._yinhang_pay_style_ctl.className = "radioOff";
					break;
				case jiyiri.page.common.PayStyle.YinHang:
					this._zhifubao_pay_style_ctl.className = "radioOff";
				 	this._wangyin_pay_style_ctl.className = "radioOff";
				 	//this._yinhang_pay_style_ctl.className = "radioOn";
					break;
			}
		},
		get_pay_style:function(){
			var rtn;
			if( this._zhifubao_pay_style_ctl.className == "radioOn" )
			{
				rtn = jiyiri.page.common.PayStyle.ZhiFuBao;
			}
			else if( this._wangyin_pay_style_ctl.className == "radioOn" )
			{
				rtn = jiyiri.page.common.PayStyle.WangYin;
			}
			else if( this._yinhang_pay_style_ctl.className == "radioOn" )
			{
				rtn = jiyiri.page.common.PayStyle.YinHang;
			}
			return rtn;
		},
		init_data_show:function(receiver_name , receiver_phone , receiver_year , receiver_month , receiver_day , receiver_address , receiver_province , receiver_city , orderer_name , orderer_phone , pay_style){

			this._receiver_name_ctl.value = receiver_name;
			this._receiver_phone_ctl.value = receiver_phone;
			this._receiver_address_ctl.value = receiver_address;
			this._orderer_name_ctl.value = orderer_name;
			this._orderer_phone_ctl.value = orderer_phone;

			this._receive_time_ctl.on_select_time( receiver_year,receiver_month,receiver_day );

			jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._receiver_province_ctl,receiver_province);
			jiyiri.ui.SecondRegionSelector.GetInstance().DrawCitiesSelectControl();
			jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._receiver_city_ctl,receiver_city);


		},
		on_click_order_address:function(){
			var adress_url =  __APP__ + '/Cart/OrderAddress_Tiny';
            jiyiri.ui.Dialog.GetInstance().show(adress_url);
		},
		on_click_good_address:function(){
			var adress_url =  __APP__ + '/Cart/GoodAddress_Tiny';
            jiyiri.ui.Dialog.GetInstance().show(adress_url);
		},
		on_select_order_address:function( address_id , receiver_name , receiver_phone , receiver_province , receiver_city , receiver_address ){
			this._order_address_id = address_id;
			this._receiver_name_ctl.value = receiver_name;
			this._receiver_phone_ctl.value = receiver_phone;
			this._receiver_address_ctl.value = receiver_address;

			jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._receiver_province_ctl,receiver_province);
			jiyiri.ui.SecondRegionSelector.GetInstance().DrawCitiesSelectControl();
			jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._receiver_city_ctl,receiver_city);
		},
		do_submit:function(){
			this._clean_error();
			var is_valid = this._validate_data();
			if( is_valid )
			{
				this._do_submit();
			}
		},
		_do_submit:function()
		{
			var receiver_name = $F("receiver_name");
			var receiver_phone = $F("receiver_phone");

			var receiver_province = $F("receiver_province");
			var receiver_city = $F("receiver_city");
			var receiver_address = $F("receiver_address");

			var receiver_year = this._receive_time_ctl.get_year();
			var receiver_month = this._receive_time_ctl.get_month();
			var receiver_day = this._receive_time_ctl.get_day();

			var orderer_name = $F("orderer_name");
			var orderer_phone = $F("orderer_phone");
			var order_remark = $F("order_remark");

			var order_address_id = this._order_address_id;

			var pay_style = this.get_pay_style();


			this._do_create_cart_order(receiver_name , receiver_phone , receiver_year , receiver_month , receiver_day , receiver_address , receiver_province , receiver_city , orderer_name , orderer_phone , pay_style , order_address_id , order_remark);
		},
		_validate_data:function()
		{
			var rtn = true;

			var receiver_name = $F("receiver_name");
			var receiver_phone = $F("receiver_phone");

			var receiver_province = $F("receiver_province");
			var receiver_city = $F("receiver_city");
			var receiver_address = $F("receiver_address");

			var receiver_year = this._receive_time_ctl.get_year();
			var receiver_month = this._receive_time_ctl.get_month();
			var receiver_day = this._receive_time_ctl.get_day();

			var orderer_name = $F("orderer_name");
			var orderer_phone = $F("orderer_phone");

			var order_address_id = this._order_address_id;

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_name ))
			{
				this._show_error( "收卡人姓名不能为空" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_phone ))
			{
				this._show_error( "收卡人电话不能为空" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_year ) || receiver_year == "选择年" || receiver_year == 0)
			{
				this._show_error( "请选择送达年" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_month ) || receiver_month == "选择月" || receiver_month == 0)
			{
				this._show_error( "请选择送达月" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_day ) || receiver_day == "选择日" || receiver_month == 0)
			{
				this._show_error( "请选择送达日" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_province ) || receiver_province == "选择省")
			{
				this._show_error( "请选择送达省" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_city ) || receiver_city == "选择市")
			{
				this._show_error( "请选择送达市" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( receiver_address ))
			{
				this._show_error( "请填写详细地址" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( orderer_name ))
			{
				this._show_error( "请填写你的姓名" );
				return false;
			}

			if(!jiyiri.helper.validatehelper.DataValidater.CheckEmpty( orderer_phone ))
			{
				this._show_error( "请填写你的电话" );
				return false;
			}

			return true;

		},
		_do_create_cart_order:function( receiver_name , receiver_phone , receiver_year , receiver_month , receiver_day , receiver_address , receiver_province , receiver_city , orderer_name , orderer_phone , pay_style , order_address_id , order_remark){

			var post_data = new jiyiri.helper.net.Parameter();

			post_data.add_param('receiver_name',receiver_name);
			post_data.add_param('receiver_phone',receiver_phone);
			post_data.add_param('receiver_year',receiver_year);
			post_data.add_param('receiver_month',receiver_month);
			post_data.add_param('receiver_day',receiver_day);
			post_data.add_param('receiver_address',receiver_address);
			post_data.add_param('receiver_province',receiver_province);
			post_data.add_param('receiver_city',receiver_city);
			post_data.add_param('orderer_name',orderer_name);
			post_data.add_param('orderer_phone',orderer_phone);
			post_data.add_param('pay_style',pay_style);
			post_data.add_param('order_address_id',order_address_id);
			post_data.add_param('order_remark' , order_remark);


			jiyiri.helper.net.Ajax.post_tp( 'Cart','doMakeOrder',post_data , jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_create_make_order_callback_function' ));

		},
		_create_make_order_callback_function:function(response)
		{
			var rst = jiyiri.helper.net.Ajax.parse_tp_return(response.responseText);
			if(!rst || !rst.status)
			{
				this._show_error( rst.info );
			}
			else
			{
				var order_id = rst.info;
				location.href =  __APP__ + "/Cart/MakeOrderSuccess/order_id/" + order_id;
			}
		},
		_show_error:function( error )
		{
			this._error_notice_area.style.display = "block";
			this._error_notice_content.innerHTML = error;

		},
		_clean_error:function()
		{
			this._error_notice_content.innerHTML = "";
			this._error_notice_area.style.display = "none";
		}
	 }

	 var TimeSelectorCtl = Class.create();
	 TimeSelectorCtl.prototype  =
	 {
		 initialize:function(){

		 	this._year_ctl;
		 	this._month_ctl;
		 	this._day_ctl;

		 	this._base_time_delay_day = 2;
		 	this._top_time_delay_day = 62;

		 	this._now_time = new Date();
		 	this._base_time = new Date();
		 	this._base_time.setTime( this._now_time.getTime() + 24 * 60 * 60 * 1000 * this._base_time_delay_day );
		 	this._top_time = new Date();
		 	this._top_time.setTime( this._now_time.getTime() + 24 * 60 * 60 * 1000 * this._top_time_delay_day );

		 },
		 init_data:function( year , month , day ){
			 this._year = year;
			 this._month = month;

		 },
		 draw:function(){
			 var time_div = document.createElement("div");
			 this._year_ctl = document.createElement("select");
			 this._month_ctl = document.createElement("select");
			 this._day_ctl = document.createElement("select");

			 this._year_ctl.onchange = jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_year_ctl_change');
		     this._month_ctl.onchange = jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_month_ctl_change');
		     this._day_ctl.onchange = jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_day_ctl_change');

			 this._init_year_ctl_data();
			 this._init_month_ctl_data();
			 this._init_day_ctl_data();

			 time_div.appendChild( this._year_ctl);
			 time_div.appendChild( this._month_ctl);
			 time_div.appendChild( this._day_ctl);

			 return time_div;
		 },
		 _init_year_ctl_data:function(){

			var html_helper = jiyiri.helper.htmlelementhelper.HTMLElementHelper
			html_helper.CleanOption(this._year_ctl);
			html_helper.add_option(this._year_ctl ,'选择年', 0);

		    for(var i=this._base_time.getFullYear();i<=this._top_time.getFullYear();i++)
		    {
		    	html_helper.add_option(this._year_ctl, i + "年", i);
		    }

		    this._year_ctl.value = 0;

		 },
		 _init_month_ctl_data:function(){

			 var html_helper = jiyiri.helper.htmlelementhelper.HTMLElementHelper;

			 var minMonth = 1;
		     var maxMonth = 12;

		     if( this._year_ctl.value == 0 || (this._year_ctl.value != this._base_time.getFullYear() && this._year_ctl.value != this._top_time.getFullYear()) )
		     {
		         minMonth = 1;
		         maxMonth = 12;
		     }
		     else if(this._base_time.getFullYear() == this._top_time.getFullYear())
		     {
		         minMonth = this._base_time.getMonth() + 1;
		         maxMonth = this._top_time.getMonth() + 1;
		     }
		     else if(this._year_ctl.value == this._base_time.getFullYear() )
		     {
		            minMonth = this._base_time.getMonth() + 1;
		            maxMonth = 12;
		     }
		     else if(this._year_ctl.value == this._top_time.getFullYear())
		     {
		            minMonth = 1;
		            maxMonth = this._top_time.getMonth() + 1;
		     }
		    var oldmonth = this._month_ctl.value;

		    html_helper.CleanOption(this._month_ctl );
		    html_helper.add_option(this._month_ctl ,'选择月', 0);
		    for(var i=minMonth ;i<=maxMonth; i++)
		    {
		    	html_helper.add_option(this._month_ctl, i + "月", i);
		    }

		    this._month_ctl.value = oldmonth;

		 },
		 _init_day_ctl_data:function(){
			 var minDay = 1;
			 var maxDay = 31;
			 var html_helper = jiyiri.helper.htmlelementhelper.HTMLElementHelper
			 var now = new Date();

			 if( this._month_ctl.value == (this._base_time.getMonth() + 1) )
			 {
				 minDay = this._base_time.getDate();
			 }
			 else
			 {
				 minDay = 1;
			 }


			 if( this._month_ctl.value == (this._top_time.getMonth() + 1))
		     {
		         maxDay = this._top_time.getDate();
		     }
			 else if( this._month_ctl.value == 2 )
		     {
		         maxDay = 29;
		     }else{
		         maxDay = 31;
		     }


			 var oldday = this._day_ctl.value;
		     html_helper.CleanOption(this._day_ctl );
		     html_helper.add_option(this._day_ctl ,'选择日', 0);
		     for(var i=minDay;i<=maxDay;i++)
		     {
		    	 //hack 2月份 14-20 号 去掉 因为过年回家了
		    	 if( this._month_ctl.value == 2 && i >= 14 && i <= 20 ){
		    		 continue;
		    	 }
		    	 //end hack

 		       	 html_helper.add_option(this._day_ctl, i + "日", i);
		     }
		     this._day_ctl.value = oldday;
		 },
		 _on_year_ctl_change:function(){
			 this._init_month_ctl_data();
			 this._init_day_ctl_data();
		 },
		 _on_month_ctl_change:function(){
			 this._init_day_ctl_data();
		 },
		 _on_day_ctl_change:function(){

		 },
		 on_select_time:function( year , month , day){
			 jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._year_ctl,year);
			 this._on_year_ctl_change();
			 jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._month_ctl,month);
			 this._on_month_ctl_change();
			 jiyiri.helper.htmlelementhelper.HTMLElementHelper.select_option(this._day_ctl,day);
		 },
		 get_year:function(){
			 return this._year_ctl.value;
		 },
		 get_month:function(){
			 return this._month_ctl.value;
		 },
		 get_day:function(){
			 return this._day_ctl.value;
		 }
	 }



	 /* 命名空间注册 */
	jiyiri.page.cart.CartUIManager = CartUIManager;
}
)();
