<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lw 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()
	{
 	        $user = $this->input->get_post('user');
 	        $page = $this->input->get('page');
            if(empty($page)) $page=1;

	        $sql_string = "SELECT * FROM ".CS_SqlPrefix."lw_data where 1=1";
			if(!empty($user)){
				$uid=$this->CsdjSkins->getziduan('user','cs_id',$user,'cs_name');
				if(intval($uid)>0){
	                $sql_string.= " and (CS_Uida=".$uid." or CS_Uidb=".$uid.")";
				}
			}
			$sql_string.=" order by CS_AddTime desc";
            
	        $query = $this->db->query($sql_string); 
	        $total = $query->num_rows();

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

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

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

	        $sql_string = "SELECT * FROM ".CS_SqlPrefix."lw where 1=1";
			if($cid>0){
				if($cid==100){
      			    $sql_string.=" and cs_cid=0";
				}else{
      			    $sql_string.=" and cs_cid=".$cid."";
				}
			}
			$sql_string.=" order by CS_ID desc";
            
	        $query = $this->db->query($sql_string); 
	        $total = $query->num_rows();

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

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

	public function add()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_Name']="";
            $data['CS_CID']=0;
            $data['CS_Pic']="";
            $data['CS_RMB']=1;
            $this->load->view('admin/lw_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/lw/lists'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."lw where cs_id=".$id."";
	        $row=$this->CsdjDB->get_all($sql);

            $data['CS_ID']=$row[0]->CS_ID;
            $data['CS_Name']=$row[0]->CS_Name;
            $data['CS_CID']=$row[0]->CS_CID;
            $data['CS_Pic']=$row[0]->CS_Pic;
            $data['CS_RMB']=$row[0]->CS_RMB;

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

	public function save()
	{
            $id=intval($this->input->post('CS_ID', TRUE));

            $data['CS_Name']=$this->input->post('CS_Name', TRUE);
            $data['CS_CID']=intval($this->input->post('CS_CID', TRUE));
            $data['CS_Pic']=$this->input->post('CS_Pic', TRUE);
            $data['CS_RMB']=intval($this->input->post('CS_RMB', TRUE));

			if(empty($data['CS_Name']) || empty($data['CS_Pic']) || $data['CS_RMB']==0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>数据不完整！</font>','javascript:history.back();');
			}

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

	public function del()
	{

	        $ids=$this->input->post('CS_ID');
			if(empty($ids)){
				$ids=$this->uri->segment(4);
                if(empty($ids)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');
                $this->db->query("delete from ".CS_SqlPrefix."lw where CS_ID=".$ids."");
			}else{
                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."lw_data where CS_ID in (".$ids.")");
			}
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
	}
}