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

	        $sql_string = "SELECT * FROM ".CS_SqlPrefix."match where 1=1";
			if($sid>0){
      			$sql_string.=" and cs_sid=".CS_Match_ID."";
			}
			if(!empty($user)){
				$uid=$this->CsdjSkins->getziduan('user','cs_id',$user,'cs_name');
				if(intval($uid)>0){
	                $sql_string.= " and CS_UID=".$uid."";
				}
			}
			$sql_string.=" order by CS_ID desc";
            
	        $query = $this->db->query($sql_string); 
	        $total = $query->num_rows();

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

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

	public function setting()
	{
            $this->load->view('admin/match_config');
	}

	public function save()
	{
		    $CS_Match_ID = intval($this->input->post('CS_Match_ID', TRUE));
		    $CS_Match_Nian = $this->input->post('CS_Match_Nian', TRUE);
		    $CS_Match_Yue = $this->input->post('CS_Match_Yue', TRUE);
		    $CS_Match_1_Name = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_1_Name', TRUE));
			$CS_Match_1_Nums = intval($this->input->post('CS_Match_1_Nums', TRUE));
			$CS_Match_1_Jiang = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_1_Jiang', TRUE));
			$CS_Match_1_Rmb = intval($this->input->post('CS_Match_1_Rmb', TRUE));

		    $CS_Match_2_Name = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_2_Name', TRUE));
			$CS_Match_2_Nums = intval($this->input->post('CS_Match_2_Nums', TRUE));
			$CS_Match_2_Jiang = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_2_Jiang', TRUE));
			$CS_Match_2_Rmb = intval($this->input->post('CS_Match_2_Rmb', TRUE));

		    $CS_Match_3_Name = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_3_Name', TRUE));
			$CS_Match_3_Nums = intval($this->input->post('CS_Match_3_Nums', TRUE));
			$CS_Match_3_Jiang = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_3_Jiang', TRUE));
			$CS_Match_3_Rmb = intval($this->input->post('CS_Match_3_Rmb', TRUE));

		    $CS_Match_4_Name = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_4_Name', TRUE));
			$CS_Match_4_Nums = intval($this->input->post('CS_Match_4_Nums', TRUE));
			$CS_Match_4_Jiang = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_4_Jiang', TRUE));
			$CS_Match_4_Rmb = intval($this->input->post('CS_Match_4_Rmb', TRUE));

		    $CS_Match_5_Name = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_5_Name', TRUE));
			$CS_Match_5_Nums = intval($this->input->post('CS_Match_5_Nums', TRUE));
			$CS_Match_5_Jiang = $this->CsdjSkins->str_checkhtml($this->input->post('CS_Match_5_Jiang', TRUE));
			$CS_Match_5_Rmb = intval($this->input->post('CS_Match_5_Rmb', TRUE));


	        $strs="<?php"."\r\n";

	        $strs.="define(\"CS_Match_ID\",".$CS_Match_ID.");  \r\n";
	        $strs.="define(\"CS_Match_Nian\",\"".$CS_Match_Nian."\");  \r\n";
	        $strs.="define(\"CS_Match_Yue\",\"".$CS_Match_Yue."\");  \r\n\r\n";

	        $strs.="define(\"CS_Match_1_Name\",\"".$CS_Match_1_Name."\");  \r\n";
	        $strs.="define(\"CS_Match_1_Nums\",".$CS_Match_1_Nums.");  \r\n";
	        $strs.="define(\"CS_Match_1_Jiang\",\"".$CS_Match_1_Jiang."\"); \r\n ";
	        $strs.="define(\"CS_Match_1_Rmb\",".$CS_Match_1_Rmb.");  \r\n\r\n";

	        $strs.="define(\"CS_Match_2_Name\",\"".$CS_Match_2_Name."\");  \r\n";
	        $strs.="define(\"CS_Match_2_Nums\",".$CS_Match_2_Nums.");  \r\n";
	        $strs.="define(\"CS_Match_2_Jiang\",\"".$CS_Match_2_Jiang."\");  \r\n";
	        $strs.="define(\"CS_Match_2_Rmb\",".$CS_Match_2_Rmb.");  \r\n\r\n";

	        $strs.="define(\"CS_Match_3_Name\",\"".$CS_Match_3_Name."\");  \r\n";
	        $strs.="define(\"CS_Match_3_Nums\",".$CS_Match_3_Nums.");  \r\n";
	        $strs.="define(\"CS_Match_3_Jiang\",\"".$CS_Match_3_Jiang."\");  \r\n";
	        $strs.="define(\"CS_Match_3_Rmb\",".$CS_Match_3_Rmb.");  \r\n\r\n";

	        $strs.="define(\"CS_Match_4_Name\",\"".$CS_Match_4_Name."\");  \r\n";
	        $strs.="define(\"CS_Match_4_Nums\",".$CS_Match_4_Nums.");  \r\n";
	        $strs.="define(\"CS_Match_4_Jiang\",\"".$CS_Match_4_Jiang."\");  \r\n";
	        $strs.="define(\"CS_Match_4_Rmb\",".$CS_Match_4_Rmb.");  \r\n\r\n";

	        $strs.="define(\"CS_Match_5_Name\",\"".$CS_Match_5_Name."\");  \r\n";
	        $strs.="define(\"CS_Match_5_Nums\",".$CS_Match_5_Nums.");  \r\n";
	        $strs.="define(\"CS_Match_5_Jiang\",\"".$CS_Match_5_Jiang."\");  \r\n";
	        $strs.="define(\"CS_Match_5_Rmb\",".$CS_Match_5_Rmb.");  \r\n";

			$match=require_once(CSCMSPATH.'lib/Cs_Match_Arr.php');
            $match[$CS_Match_ID]['id']=$CS_Match_ID;
            $match[$CS_Match_ID]['nian']=$CS_Match_Nian;
            $match[$CS_Match_ID]['yue']=$CS_Match_Yue;
            $match[$CS_Match_ID]['name1']=$CS_Match_1_Name;
            $match[$CS_Match_ID]['nums1']=$CS_Match_1_Nums;
            $match[$CS_Match_ID]['jiang1']=$CS_Match_1_Jiang;
            $match[$CS_Match_ID]['rmb1']=$CS_Match_1_Rmb;
            $match[$CS_Match_ID]['name2']=$CS_Match_2_Name;
            $match[$CS_Match_ID]['nums2']=$CS_Match_2_Nums;
            $match[$CS_Match_ID]['jiang2']=$CS_Match_2_Jiang;
            $match[$CS_Match_ID]['rmb2']=$CS_Match_2_Rmb;
            $match[$CS_Match_ID]['name3']=$CS_Match_3_Name;
            $match[$CS_Match_ID]['nums3']=$CS_Match_3_Nums;
            $match[$CS_Match_ID]['jiang3']=$CS_Match_3_Jiang;
            $match[$CS_Match_ID]['rmb3']=$CS_Match_3_Rmb;
            $match[$CS_Match_ID]['name4']=$CS_Match_4_Name;
            $match[$CS_Match_ID]['nums4']=$CS_Match_4_Nums;
            $match[$CS_Match_ID]['jiang4']=$CS_Match_4_Jiang;
            $match[$CS_Match_ID]['rmb4']=$CS_Match_4_Rmb;
            $match[$CS_Match_ID]['name5']=$CS_Match_5_Name;
            $match[$CS_Match_ID]['nums5']=$CS_Match_5_Nums;
            $match[$CS_Match_ID]['jiang5']=$CS_Match_5_Jiang;
            $match[$CS_Match_ID]['rmb5']=$CS_Match_5_Rmb;
			$this->arr_file_edit($match);

            //写文件
            if (!write_file('./csdj/lib/Cs_Prize.php', $strs)){
                          $this->CsdjAdmin->Admin_Url('<font color=red>操作失败，文件没有写入权限！</font>',site_url('admin/match/setting'));
            }else{
                          $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，修改成功！</font>',site_url('admin/match/setting'),'ok');
            }
	}

	public function del()
	{

	        $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."match where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
	}

	//写入新数组到文件
	public function arr_file_edit($arr){
	     $file=CSCMSPATH.'lib/Cs_Match_Arr.php';
	     if(is_array($arr)){
		        $con = var_export($arr,true);
	     } else{
		        $con = $arr;
	     }
	     $strs="<?php if (!defined('FCPATH')) exit('No direct script access allowed');".PHP_EOL;
	     $strs.="return $con;";
	     $strs.="?>";
         return write_file($file, $strs);
	}
}