<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Userzu extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjAdmin');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
	}


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


	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."userzu order by CS_ID asc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/userzu/index/');
	    $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['userzu'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

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

	public function add()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_Name']='';
            $data['CS_Cion_30']='0';
            $data['CS_Cion_180']='0';
            $data['CS_Cion_365']='0';
            $data['CS_Quanx']='';
            $this->load->view('admin/userzu_edit',$data);
        }

	public function edit()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/admin/index/'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."userzu 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_Cion_30']=$row[0]->CS_Cion_30;
                 $data['CS_Cion_180']=$row[0]->CS_Cion_180;
                 $data['CS_Cion_365']=$row[0]->CS_Cion_365;
                 $data['CS_Quanx']=$row[0]->CS_Quanx;
            }
            $this->load->view('admin/userzu_edit',$data);
        }


	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('42');
            $data['CS_Name']=$this->input->post('CS_Name');
            $data['CS_Cion_30']=$this->input->post('CS_Cion_30');
            $data['CS_Cion_180']=$this->input->post('CS_Cion_180');
            $data['CS_Cion_365']=$this->input->post('CS_Cion_365');
            $data['CS_Quanx']=$this->input->post('CS_Quanx');

            if(empty($data['CS_Cion_30'])) $data['CS_Cion_30']=10;
            if(empty($data['CS_Cion_180'])) $data['CS_Cion_180']=50;
            if(empty($data['CS_Cion_365'])) $data['CS_Cion_365']=100;

            $ids=$this->input->post('CS_ID');
            if(!empty($data['CS_Quanx'])){
                $data['CS_Quanx']=implode(',', $data['CS_Quanx']);
            }
            if(empty($data['CS_Name'])) $this->CsdjAdmin->Admin_Url('<font color=red>名称、不能为空！</font>','javascript:history.back();');

            if($ids==0 || empty($ids)){
                 $this->CsdjDB->get_insert('userzu',$data);
            }else{
                 $this->CsdjDB->get_update('userzu',$ids,$data);
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/userzu/index/'),'ok');
        }

	public function del()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/admin/index/'));

            $sql="SELECT * FROM ".CS_SqlPrefix."user where cs_vip=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该会员组下有会员，请先移除会员！</font>',site_url('admin/userzu/'));
            }

            $this->db->query("delete from ".CS_SqlPrefix."userzu where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }
}

