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

class Sflw_Index_module extends CI_Module {

	function __construct(){
		    parent::__construct();
        	$this->load->helper('url');
	        $this->load->database();
            $this->load->library('session'); //加载session
	        $this->load->model('CsdjDB'); //加载数据操作模型
            $this->load->model('CsdjSkins'); //加载全局标签模型 
			require_once PLUGINDIR.'sflw/conxiu.php'; 
	        $dir = $this->uri->segment(2);
            $sql="SELECT CS_SID FROM ".CS_SqlPrefix."plugins where cs_dir='".$dir."'";
	        $row=$this->CsdjDB->get_all($sql);
	        if($row){
                      if($row[0]->CS_SID==1){
                             $this->CsdjAdmin->Admin_Url('<font color=red>该插件尚未开启！</font>',"javascript:history.back();");
                      }
            }else{
                      $this->CsdjAdmin->Admin_Url('<font color=red>该插件尚未安装！</font>',"javascript:history.back();");
            }
	}
	public function cion()
	{
	      $id='2';
			
			$sql="SELECT sum(CS_Sid) FROM ".CS_SqlPrefix."lw_list where cs_uid=".$id."";
			$rows=$this->db->query($sql);
	
			$dj=json_encode($rows->result());
	 $djs=substr($dj,'1','-1');
	 echo $djs;
			
			
			//结束
			}
			
			public function index()
	{
	      $id=$this->uri->segment(5);
			
			$sql="SELECT * FROM ".CS_SqlPrefix."lw_type where cs_cid=".$id." order by CS_Lwtime desc limit 100";
			$rows=$this->db->query($sql);
			if($rows){
			foreach($rows->result() as $rows){
	
			$list['name']=iconv("GB2312//IGNORE","UTF-8",$rows->CS_Name);
			$list['cion']=$rows->CS_Cion;
			$list['id']=$rows->CS_ID;
			$list['images']=iconv("GB2312//IGNORE","UTF-8",$rows->CS_Lwpic);
              $lwlist[]=$list;
			}
			}
			if($lwlist==''){
			$listjson['status']='0';
			$listjson['msg']=iconv("GB2312//IGNORE","UTF-8","改分类没有数据！");
			}else{
						$listjson['status']='1';
			$listjson['list']=$lwlist;
			}
		
		     echo json_encode($listjson);
			
			
			//结束
			}
			
		public function lists()
	{
	      $id=$this->uri->segment(5);
			
			$sql="SELECT * FROM ".CS_SqlPrefix."lw_type where cs_cid=".$id." order by CS_Lwtime desc limit 100";
			$rows=$this->db->query($sql);
			if($rows){
			foreach($rows->result() as $rows){
	
		  $list.='<li onclick="choosePresents(this)" onmousemove="showTips(this)" onmouseout="hideTips(this)" _name="'.$rows->CS_Name.'" _rmb="'.$rows->CS_Cion.'" _id="'.$rows->CS_ID.'" _images="'.$rows->CS_Lwpic.'"> <img src="'.$rows->CS_Lwpic.'" /><br><div class="sfcmsnames">'.$rows->CS_Name.'</div><div class="tip"><em></em><div class="tipcon"><img src="'.$rows->CS_Lwpic.'"><span>价格：'.$rows->CS_Cion.'个星币</span><p></p></div></div></li>';
			}
			}
			if($list==''){
			$listjson['status']='0';
			$listjson['list']=iconv("GB2312//IGNORE","UTF-8","没有数据！");
			}else{
						$listjson['status']='1';
			$listjson['list']=iconv("GB2312//IGNORE","UTF-8",$list);
			}
		
		     echo json_encode($listjson);
			
			
			//结束
			}
			
	         public function user()
	{   
	    $lid = $this->input->get('lid', TRUE);
		$did = $this->input->get('did', TRUE);
		$sid = $this->input->get('sid', TRUE);
	   $uid = $this->session->userdata('cs_id');
	   $row=$this->db->query("SELECT * FROM ".CS_SqlPrefix."user where cs_id='".$uid."'")->row();
	   if(!$row){
	   
	   $list['msg']=iconv("GB2312//IGNORE","UTF-8","未登录！");
	   }else{
	   $lw=$this->db->query("SELECT * FROM ".CS_SqlPrefix."lw_type where cs_id='".$lid."'")->row();
	   $user=$this->db->query("SELECT * FROM ".CS_SqlPrefix."dance where cs_id='".$did."'")->row();
	   $users=$this->db->query("SELECT * FROM ".CS_SqlPrefix."user where cs_name='".$user->CS_User."'")->row();
	   
         if(!$lw){
		
		 $list['msg']=iconv("GB2312//IGNORE","UTF-8","礼物不存在！");
		 }else{
		 $cion=$lw->CS_Cion*$sid;
		 if($cion>$row->CS_Cion){
		
		$list['msg']=iconv("GB2312//IGNORE","UTF-8","金币不足，请充值！");
		 }else{
		 
		         $updata['CS_Cion']=$row->CS_Cion-$cion;
                $this->CsdjDB->get_update ('user',$uid,$updata);
		 
		    $upqd['CS_Did']= $did;
			$upqd['CS_Lid']= $lid;
			$upqd['CS_Uid']= $uid;
			$upqd['CS_Sid']= $sid;
			$upqd['CS_Uida']= $users->CS_ID;
			$upqd['CS_Lwtime']= date('Y-m-d H:i:s');
			$this->CsdjDB->get_insert('lw_list',$upqd);
			
			
			$usercion['CS_Jifen']=$users->CS_Jifen+$cion;
                $this->CsdjDB->get_update ('user',$users->CS_ID,$usercion);
		 $list['msg']=iconv("GB2312//IGNORE","UTF-8","恭喜你，赠送成功！");
		 }//4
		 }//3
	   
	   }//1
	echo json_encode($list);
	}
	//提现
	public function zhuan()
	{
              $cion = $this->input->get('cion', TRUE);
			  $uid=$this->session->userdata('cs_id');
			  $row=$this->db->query("SELECT * FROM ".CS_SqlPrefix."user where cs_id='".$uid."'")->row();
			  
			  if($row->CS_Jifen>=$cion){
			  
			  	$usercion['CS_Jifen']=$row->CS_Jifen-$cion;
				$usercion['CS_Cion']=$row->CS_Cion+$cion;
                $this->CsdjDB->get_update ('user',$uid,$usercion);
			  
			  }
			  
        }
	
	public function admin()
	{
              header("Location: ".site_url('m/sflw/admin').""); 
        }
}
