<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User 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('CsdjAdmin');
		$this->load->model('CsdjSkins');
	        $this->CsdjAdmin->Admin_Login();
	}

	public function index()
	{
 	    $sid = $this->input->get('sid');
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;

            if($sid==1){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_rzid=1 order by CS_AddTime desc";
            }elseif($sid==2){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_rzid=0 order by CS_AddTime desc";
            }elseif($sid==3){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_tid=1 order by CS_AddTime desc";
            }elseif($sid==4){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_tid=0 order by CS_AddTime desc";
            }elseif($sid==5){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_sid=1 order by CS_AddTime desc";
            }elseif($sid==6){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_vip=0 order by CS_AddTime desc";
            }elseif($sid==7){
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where cs_vip>0 order by CS_AddTime desc";
            }else{
	          $sql_string = "SELECT * FROM ".CS_SqlPrefix."user order by CS_AddTime desc";
            }
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/user/index/')."?sid=".$sid;
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' limit '. $per_page*($page-1) .','. $per_page;
	    $query = $this->db->query($sql_string);

	    $data['user'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/user',$data);
        }

	public function so()
	{
	    $key = $this->input->get('key');
	    $cid = $this->input->get('cid');
	    $page = $this->input->get('page');
            if(empty($page)) $page=1;
	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."user where 1=1";
            if($key){
                if($cid=="-1"){
	           $sql_string.= " and CS_ID=".$key."";
                }elseif($cid=="-3"){
	           $sql_string.= " and CS_Nichen like '%".$key."%'";
                }else{
	           $sql_string.= " and CS_Name like '%".$key."%'";
                }
            }
            if($cid=="1"){
	           $sql_string.= " and cs_sid=1";
            }elseif($cid=="2"){
	           $sql_string.= " and cs_rzid=1";
            }elseif($cid=="3"){
	           $sql_string.= " and cs_vip>0";
            }
	    $sql_string.= " order by CS_AddTime desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/user/so/')."?key=".$key."&cid=".$cid."";
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
	    $query = $this->db->query($sql_string .' limit '. $per_page*($page-1) .','. $per_page);
	    $data['user'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/user',$data);
        }

	public function edit()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/dance/lists/'));
            $sql="SELECT * FROM ".CS_SqlPrefix."user where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                 $data['CS_ID']=$row[0]->CS_ID;
                 $data['CS_Name']=$row[0]->CS_Name;
                 $data['CS_QQ']=$row[0]->CS_QQ;
                 $data['CS_Qianm']=$row[0]->CS_Qianm;
                 $data['CS_Email']=$row[0]->CS_Email;
                 $data['CS_Nichen']=$row[0]->CS_Nichen;
                 $data['CS_Pass']=$row[0]->CS_Pass;
                 $data['CS_Sex']=$row[0]->CS_Sex;
                 $data['CS_City']=$row[0]->CS_City;
                 $data['CS_Cion']=$row[0]->CS_Cion;
                 $data['CS_RMB']=$row[0]->CS_RMB;
                 $data['CS_Vip']=$row[0]->CS_Vip;
                 $data['CS_EndTime']=$row[0]->CS_EndTime;
                 $data['CS_Jinyan']=$row[0]->CS_Jinyan;
                 $data['CS_Hits']=$row[0]->CS_Hits;
                 $data['CS_Rhits']=$row[0]->CS_Rhits;
                 $data['CS_Zhits']=$row[0]->CS_Zhits;
                 $data['CS_Yhits']=$row[0]->CS_Yhits;
                 $data['CS_Zanhits']=$row[0]->CS_Zanhits;
                 $data['CS_Logo']=$row[0]->CS_Logo;
            }
            $this->load->view('admin/user_edit',$data);
        }

	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('41');
            $ids=$this->input->post('CS_ID');

                 $data['CS_QQ']=$this->input->post('CS_QQ');
                 $data['CS_Qianm']=$this->input->post('CS_Qianm');
                 $data['CS_Email']=$this->input->post('CS_Email');
                 $data['CS_Nichen']=$this->input->post('CS_Nichen');
                 $data['CS_Pass']=$this->input->post('CS_Pass');
                 $data['CS_Sex']=$this->input->post('CS_Sex');
                 $data['CS_City']=$this->input->post('CS_City');
                 $data['CS_Cion']=$this->input->post('CS_Cion');
                 $data['CS_RMB']=$this->input->post('CS_RMB');
                 $data['CS_Vip']=$this->input->post('CS_Vip');
                 $data['CS_EndTime']=$this->input->post('CS_EndTime');
                 $data['CS_Jinyan']=$this->input->post('CS_Jinyan');
                 $data['CS_Hits']=$this->input->post('CS_Hits');
                 $data['CS_Rhits']=$this->input->post('CS_Rhits');
                 $data['CS_Zhits']=$this->input->post('CS_Zhits');
                 $data['CS_Yhits']=$this->input->post('CS_Yhits');
                 $data['CS_Zanhits']=$this->input->post('CS_Zanhits');
                 if($this->input->post('dellogo')=='ok' && $this->input->post('CS_Logo')){
                     $data['CS_Logo']='';
                     //删除头像文件
		     $pic="./attachment/logo/".$this->input->post('CS_Logo');
                     if(FTP_Fun==1){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;
                            $FTPUP['passive']  = FTP_Ive;
                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file($pic);
                            $this->ftp->close();
                     }else{
	                 if(file_exists($pic)){
		            @unlink($pic);
	                 }
                     }
                 }

                 if(empty($data['CS_Cion'])) $data['CS_Cion']=0;
                 if(empty($data['CS_RMB'])) $data['CS_RMB']=0;
                 if(empty($data['CS_Jinyan'])) $data['CS_Jinyan']=0;
                 if(empty($data['CS_Hits'])) $data['CS_Hits']=0;
                 if(empty($data['CS_Rhits'])) $data['CS_Rhits']=0;
                 if(empty($data['CS_Zhits'])) $data['CS_Zhits']=0;
                 if(empty($data['CS_Yhits'])) $data['CS_Yhits']=0;
                 if(empty($data['CS_Zanhits'])) $data['CS_Zanhits']=0;

                 if(empty($data['CS_Email'])) $this->CsdjAdmin->Admin_Url('<font color=red>抱歉，Email不能为空！</font>','javascript:history.back();');

                 if($data['CS_Vip']>0 && empty($data['CS_EndTime'])){
                     $this->CsdjAdmin->Admin_Url('<font color=red>抱歉，到期时间不能为空！</font>','javascript:history.back();');
                 }

		 $row=$this->CsdjDB->get_select ('user','CS_ID','CS_Pass',''.$ids.'');
                 if($row[0]->CS_Pass!=$data['CS_Pass']){
                        $data['CS_Pass']=md5($data['CS_Pass']);
                 }
                 $this->CsdjDB->get_update('user',$ids,$data);
                 $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/user/'),'ok');

        }

	public function del()
	{
            $this->CsdjAdmin->Admin_Qx('41');
            $id = $this->security->xss_clean($this->uri->segment(4));   //ID
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

	    $row=$this->CsdjDB->get_select ('user','CS_ID','CS_Name',''.$id.'');
            IF($this->CsdjSkins->getusercount($row[0]->CS_Name,'dance')>0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该会员下有歌曲数据，请先移除歌曲！</font>',site_url('admin/dance/so/')."?user=".$row[0]->CS_Name);
            }

            IF($this->CsdjSkins->getusercount($row[0]->CS_Name,'vod')>0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该会员下有视频数据，请先移除视频！</font>',site_url('admin/vod/so/')."?user=".$row[0]->CS_Name);
            }

            IF($this->CsdjSkins->getusercount($row[0]->CS_Name,'pic')>0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该会员下有图片数据，请先移除图片！</font>',site_url('admin/pic/'));
            }

            //删除头像
	    $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$id.'');
	    if($row){
		     $biglogo="./attachment/logo/".$row[0]->CS_Logo."_big.jpg";
		     $smalllogo="./attachment/logo/".$row[0]->CS_Logo."_small.jpg";
                     if(FTP_Fun==1){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;
                            $FTPUP['passive']  = FTP_Ive;
                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file($smalllogo);
                            $this->ftp->delete_file($biglogo);
                     }else{
	                 if(file_exists($biglogo)){
		            @unlink($biglogo);
		            @unlink($smalllogo);
	                 }
                     }
	    }

            $this->db->query("delete from ".CS_SqlPrefix."funco where CS_Usera='".$row[0]->CS_Name."' or CS_Userb='".$row[0]->CS_Name."'");  //访客
            $this->db->query("delete from ".CS_SqlPrefix."fans where CS_Usera='".$row[0]->CS_Name."' or CS_Userb='".$row[0]->CS_Name."'");  //粉丝
            $this->db->query("delete from ".CS_SqlPrefix."friend where CS_Usera='".$row[0]->CS_Name."' or CS_Userb='".$row[0]->CS_Name."'");  //好友
            $this->db->query("delete from ".CS_SqlPrefix."pl where CS_User='".$row[0]->CS_Name."'");  //评论
            $this->db->query("delete from ".CS_SqlPrefix."down where CS_User='".$row[0]->CS_Name."'"); //下载
            $this->db->query("delete from ".CS_SqlPrefix."fav where CS_User='".$row[0]->CS_Name."'");  //收藏
            $this->db->query("delete from ".CS_SqlPrefix."dt where CS_User='".$row[0]->CS_Name."'");  //动态
            $this->db->query("delete from ".CS_SqlPrefix."user where CS_ID=".$id."");  //会员
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }


	public function pldel()
	{
            $this->CsdjAdmin->Admin_Qx('41');
	    $ids=$this->input->post('CS_ID');
            if(empty($ids)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');
            $id=implode(',', $ids);
            $this->db->query("delete from ".CS_SqlPrefix."user where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }
}

