<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Assets extends CI_Controller {

	function __construct(){
			    parent::__construct();
        	    $this->load->helper('url');
        	    $this->load->helper('form');
                $this->load->library('session'); //加载session
			    $this->load->model('CsdjDB');
			    $this->load->model('CsdjSkins');
			    $this->load->model('CsdjUser');
	}

	public function index()
	{
			    $this->CsdjUser->User_Login();
                $data='';
	            $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
                $data['user']=$row[0];

                $this->load->get_user_templates();
			    $template=$this->load->view('assets-index.html',$data,true);
			    $template=$this->CsdjSkins->TopandBottom($template,'user');

			    $Mark_Text=str_replace("{cscms:title}","账户 - 会员管理中心",$template);
			    $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
			    $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=str_replace("[user:op]","account",$Mark_Text);


		        $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
	}

	public function cion()
	{
			    $this->CsdjUser->User_Login();

                $cid = intval($this->input->get('cid', TRUE)); 
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;
				$data['cid']=$cid;
                        
                $sqlstr="select * from ".CS_SqlPrefix."cionlist where CS_UID='".$this->session->userdata('cs_id')."'";
                if($cid==1){ //收入
                      $sqlstr.=" and CS_Cion2=0";
                }elseif($cid==2){ //消费
                      $sqlstr.=" and CS_Cion=0";
                }elseif($cid==3){ //过期
                      $sqlstr.=" and CS_SID=4";
                }
                $sqlstr.=" order by CS_AddTime desc";
			    $Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,20,20,'user/assets/cion?cid='.$cid,$page);//sql,每页显示条数
			    $result=$this->CsdjDB->db->query($Arr[2]);
			    $data['cionlist']=$result->result();

	            $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
                $data['user']=$row[0];

                $this->load->get_user_templates();
			    $template=$this->load->view('cion-list.html',$data,true);
			    $template=$this->CsdjSkins->TopandBottom($template,'user');

			    $Mark_Text=str_replace("{cscms:title}","积分账单 - 会员管理中心",$template);
			    $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
			    $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=str_replace("[user:op]","account",$Mark_Text);
		        $Mark_Text=str_replace("[cion:cid]",$cid,$Mark_Text);
			    $Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
		        $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
	}

	public function rmb()
	{
			    $this->CsdjUser->User_Login();

                $cid = intval($this->input->get('cid', TRUE)); 
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;
				$data['cid']=$cid;
                        
                $sqlstr="select * from ".CS_SqlPrefix."rmblist where CS_UID='".$this->session->userdata('cs_id')."'";
                if($cid==1){ //充值
                      $sqlstr.=" and CS_CID=1";
                }elseif($cid==2){ //消费
                      $sqlstr.=" and CS_CID=2";
                }elseif($cid==3){ //收入
                      $sqlstr.=" and CS_CID=3";
                }elseif($cid==4){ //提现
                      $sqlstr.=" and CS_CID=4";
                }
                $sqlstr.=" order by CS_AddTime desc";
			    $Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,20,20,'user/assets/rmb?cid='.$cid,$page);//sql,每页显示条数
			    $result=$this->CsdjDB->db->query($Arr[2]);
			    $data['rmblist']=$result->result();

	            $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
                $data['user']=$row[0];

                $this->load->get_user_templates();
			    $template=$this->load->view('rmb-list.html',$data,true);
			    $template=$this->CsdjSkins->TopandBottom($template,'user');

			    $Mark_Text=str_replace("{cscms:title}","金币账单 - 会员管理中心",$template);
			    $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
			    $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=str_replace("[user:op]","account",$Mark_Text);
		        $Mark_Text=str_replace("[rmb:cid]",$cid,$Mark_Text);
			    $Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
		        $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
	}

	public function gifts()
	{
			    $this->CsdjUser->User_Login();

                $cid = intval($this->input->get('cid', TRUE)); 
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;
				$data['cid']=$cid;
                        
                $sqlstr="select * from ".CS_SqlPrefix."gifts where CS_UID='".$this->session->userdata('cs_id')."'";
                if($cid==1){ //成功
                      $sqlstr.=" and CS_SID=4";
                }elseif($cid==2){ //失败
                      $sqlstr.=" and CS_SID=3";
                }elseif($cid==3){ //审核中
                      $sqlstr.=" and CS_SID=1";
                }elseif($cid==4){ //处理中
                      $sqlstr.=" and CS_SID=2";
                }
                $sqlstr.=" order by CS_AddTime desc";
			    $Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,20,20,'user/assets/gifts?cid='.$cid,$page);//sql,每页显示条数
			    $result=$this->CsdjDB->db->query($Arr[2]);
			    $data['giftslist']=$result->result();

	            $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
                $data['user']=$row[0];

                $this->load->get_user_templates();
			    $template=$this->load->view('gifts-list.html',$data,true);
			    $template=$this->CsdjSkins->TopandBottom($template,'user');

			    $Mark_Text=str_replace("{cscms:title}","兑换记录 - 会员管理中心",$template);
			    $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
			    $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=str_replace("[user:op]","account",$Mark_Text);
		        $Mark_Text=str_replace("[gifts:cid]",$cid,$Mark_Text);
			    $Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
		        $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
	}

	//领取积分
	public function dqcion()
	{
                if(!$this->CsdjUser->User_Login(1)){
		            exit("20001");//未登入
                }

                $cion=$this->CsdjSkins->get_cion($this->session->userdata('cs_id'),1);
                if($cion>0){
					 $sj=time()-86400*7;
                     $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion+".$cion." where cs_id=".$this->session->userdata('cs_id')."");
                     $this->db->query("update ".CS_SqlPrefix."cionlist set CS_SID=2 where cs_uid=".$this->session->userdata('cs_id')." and cs_addtime>".$sj."");
					 exit('10000');
                }else{
		             exit("10004");
				}
	}

	//提现
	public function tixian()
	{
             @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
             @header("Cache-Control: no-cache, must-revalidate"); 
             @header("Pragma: no-cache");

             if(!$this->CsdjUser->User_Login(1)){
                   exit('20001');
			 }
             $row=$this->db->query("SELECT CS_RMB2,CS_Name FROM ".CS_SqlPrefix."user where cs_id='".$this->session->userdata('cs_id')."'")->row(); 

	         echo '<div class="d-content" style="padding: 20px 25px;"><ul id="danceReviewModify" class="danceModify"><li><div class="dA_title">您的账号：</div><div class="dA_input">'.$row->CS_Name.'</div><div id="mdance_name" class="input_msg"></div></li><li><div class="dA_title">剩余金额：</div><div class="dA_input" id="EXMoneys">'.$row->CS_RMB2.'</div><div id="mdance_name" class="input_msg"></div></li><li><div class="dA_title">提现金额：</div><div class="dA_input"><input id="valMoney" class="input_normal" type="text" value="" name="rmb" style="width: 190px;"></div><div id="money" class="input_msg">输入您要提现的金额。</div></li><li><div class="dA_title">选择银行：</div><div class="dA_input"><select id="rtype" class="select_normal" name="type" style="width: 110px;"><option value="支付宝">支付宝</option><option value="财付通">财付通</option><option value="中国银行">中国银行</option><option value="农业银行">农业银行</option><option value="工商银行">工商银行</option><option value="建设银行">建设银行</option><option value="交通银行">交通银行</option><option value="招商银行">招商银行</option><option value="邮政银行">邮政银行</option></select></div><div id="mtype" class="input_msg">选择提现银行，请正确选择。</div></li><li><div class="dA_title" id="title-1">收款账号：</div><div class="dA_input"><input id="rkh" class="input_normal" type="text" maxlength="255" value="" name="kh" style="width: 290px;"></div><div class="input_msg" id="mkh">请填写真实，否则不能到账</div></li><li><div class="dA_title">收款姓名：</div><div class="dA_input"><input id="rmz" class="input_normal" type="text" maxlength="255" value="" name="mz" style="width: 290px;"></div><div class="input_msg" id="mmz">请填写真实的收款名字</div></li><li id="city" style="display:none;"><div class="dA_title">开户地址：</div><div class="dA_input"><input id="rcity" class="input_normal" type="text" maxlength="255" value="" name="city" style="width: 290px;"><div class="input_msg" id="mcity">您的银行开户地址。</div></li><li><div class="dA_title"></div><div id="btnUploadBox" class="dA_input"><span class="button-main"><span><button type="button" id="btnUpload" >确认提现</button></span></span><span class="button2-main"><span><button type="button" id="btnAbort1" onclick="$.dialog.get(\'exmoney\').close();">取消</button></span></span></div></li></ul><script type="text/javascript">account.doexchangeMoneyInit();</script></div>';
	}

	//提现订单
	public function tixian_save()
	{

                if(!$this->CsdjUser->User_Login(1)){
		             exit("20001");//未登入
                }

                $rmb=intval($this->input->post('rmb', TRUE));   
                $mz=$this->input->post('mz', TRUE); 
                $kh=$this->input->post('kh', TRUE);
				$city=$this->input->post('city', TRUE);
				$type=$this->input->post('type', TRUE);
                if($rmb<200) exit('10011');

                if(empty($mz) || empty($kh) || empty($type)){
                     exit('10002');
				}
                $row=$this->db->query("SELECT CS_RMB2 FROM ".CS_SqlPrefix."user where cs_id='".$this->session->userdata('cs_id')."'")->row(); 
                if($row->CS_RMB2<$rmb){
                     exit('10013');
				}
                $rowp=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."tixian where cs_uid='".$this->session->userdata('cs_id')."' and cs_sid=0")->row(); 
                if($rowp){
                     exit('10012');
				}

                $this->load->helper('string');
				$add['cs_neir']='提现银行：'.$type.'<br>提现姓名：'.$mz.'<br>提现账号：'.$kh.'<br>开户地区：'.$city;
				$add['cs_uid']=$this->session->userdata('cs_id');
				$add['cs_rmb']=intval($rmb*95/100);
				$add['cs_rmb2']=$rmb;
				$add['cs_dingdan']=date('Ymd').time().random_string('numeric',5);
				$add['cs_addtime']=date('Y-m-d H:i:s');
				$res=$this->CsdjDB->get_insert('tixian',$add);

				//写入金币账单
				$add2['cs_uid']=$this->session->userdata('cs_id');
				$add2['cs_name']='申请金币提现';
				$add2['cs_cid']=4;
				$add2['cs_did']=$res;
				$add2['cs_sid']=2;
				$add2['cs_rmb']=$rmb;
				$add2['cs_addtime']=time();
				$this->CsdjDB->get_insert('rmblist',$add2);

                $this->db->query("update ".CS_SqlPrefix."user set CS_RMB2=CS_RMB2-".$rmb." where CS_ID=".$this->session->userdata('cs_id')."");
			    //发送通知
				$msg['cs_type']='account';
				$msg['cs_name']='您刚申请了金币提现，请等待管理员给您支付';
				$msg['cs_usera']=$this->session->userdata('cs_name');
				$msg['cs_userb']='系统提示';
				$msg['cs_addtime']=date('Y-m-d H:i:s');
		        $this->CsdjDB->get_insert ('msg',$msg);
				exit('10000');
	}				
}
