<?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->library('session'); //加载session
			    $this->load->model('CsdjDB');
			    $this->load->model('CsdjSkins');
			    $this->load->model('CsdjUser');
				$this->match=require_once(CSCMSPATH.'lib/Cs_Match_Arr.php');
	}

	public function index()
	{
		        $id = intval($this->uri->segment(3));   //ID
				if($id==0 || $id>CS_Match_ID) $id=CS_Match_ID;
                $data='';
				$query=$this->db->query("select * from ".CS_SqlPrefix."match where CS_SID=".$id." order by CS_ID desc");
			    $data['user']=$query->result();

				$nian=$this->match[$id]['nian'];
				$yue=$this->match[$id]['yue'];

                $data['bmtime']=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,$yue,9,$nian)));
                $data['hxtime']=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,$yue,11,$nian)));
                $data['pmtime']=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,$yue,18,$nian)));
                $data['jstime']=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,$yue,31,$nian)));

				if(date('Y-m-d')==date("Y-m-d H:i:s",mktime(0,0,0,$yue,31,$nian))){
                      $data['zt']=5;
				}elseif(time()<$data['jstime'] && time()>$data['pmtime']){
                      $data['zt']=4;
				}elseif(time()<$data['jstime'] && time()>$data['hxtime']){
                      $data['zt']=3;
				}elseif(time()<$data['jstime'] && time()>$data['bmtime']){
                      $data['zt']=2;
                }elseif(time()<$data['jstime']){
                      $data['zt']=1;
				}

                if(time()>$data['hxtime']){
                      //上传歌曲数量
					  $musiccount=$this->db->query("select CS_ID from ".CS_SqlPrefix."dance where CS_SID=".$id."")->num_rows();

                      //获取数量会员
			          $data['nums1']=$query->num_rows();
			          $data['nums2']=$musiccount;
			          $data['nums3']=$data['nums1']-$musiccount;
				}

                $data['match']=$this->match[$id];
                $this->load->get_templates();
				$template=$this->load->view('cs-match-'.$id.'.html',$data,true);

			    $Mark_Text=str_replace("{cscms:title}",Web_Name."第".$id."届串烧大赛 - ".Web_Url,$template);
			    $Mark_Text=str_replace("{cscms:keywords}",Web_Name."第".$id."届串烧大赛",$Mark_Text);
			    $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,'');
                $this->CsdjSkins->skins($Mark_Text,$data);
	}

	public function add()
	{
                if(!$this->CsdjUser->User_Login(1)){
		             exit("20001");//未登入
                }
                $sid=intval($this->input->post('sid')); //SID
				if($sid==0 || $sid!=CS_Match_ID) exit('20002');

                $bmjstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,CS_Match_Yue,9,date("Y"))));
                if(time()>$bmjstime) exit('20003');

                $bmkstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,(CS_Match_Yue-1),9,date("Y"))));
                if(time()<$bmkstime) exit('20005');

                $row=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."match where cs_sid=".CS_Match_ID." and cs_uid='".$this->session->userdata('cs_id')."'")->row(); 
				if($row){
                       exit('20004');
				}
				echo '<div class="apply"><ul><li>报名参加本次串烧大赛，需要3000积分作为报名费。点击立即参加按钮后,系统会从您的账户中自动扣除。</li><li>点击立即参加按钮后，如想退出本次赛，只需不上传任何作品即可，但不返还任何报名费用。</li></ul><div class="button"><button id="comIn" class="promptly" type="button" cid="'.CS_Match_ID.'">立即参加</button><button id="comCen" class="pend" type="button">暂不参加</button></div></div><script type="text/javascript">activityLib.doComApplyInit();</script>';
	}

	public function add_save()
	{
                if(!$this->CsdjUser->User_Login(1)){
		             exit("20001");//未登入
                }
                $sid=intval($this->input->post('sid')); //SID
				if($sid==0 || $sid!=CS_Match_ID) exit('20002');

                $bmjstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,CS_Match_Yue,9,date("Y"))));
                if(time()>$bmjstime) exit('20003');

                $bmkstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,(CS_Match_Yue-1),9,date("Y"))));
                if(time()<$bmkstime) exit('20005');

                $row=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."match where cs_sid=".CS_Match_ID." and cs_uid='".$this->session->userdata('cs_id')."'")->row(); 
				if($row){
                       exit('20004');
				}

                $rowu=$this->db->query("SELECT CS_ID,CS_Cion,CS_DJID FROM ".CS_SqlPrefix."user where cs_id=".$this->session->userdata('cs_id')."")->row();
				if($rowu->CS_Cion<3000){
                       exit('20005');
				}
				if($rowu->CS_DJID!=1){
                       exit('20006');
				}
				//扣分
				$edit['CS_Cion']=$rowu->CS_Cion-3000;
				$this->CsdjDB->get_update('user',$this->session->userdata('cs_id'),$edit);

				//增加
				$add['CS_UID']=$this->session->userdata('cs_id');
				$add['CS_SID']=$sid;
				$this->CsdjDB->get_insert('match',$add);

				exit('10000');
	}

	public function dance_add()
	{
                if(!$this->CsdjUser->User_Login(1)){
		             exit("20001");//未登入
                }

                $bmjstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,CS_Match_Yue,9,date("Y"))));
                if(time()>$bmjstime) exit('20007');

                $bmkstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,(CS_Match_Yue-1),9,date("Y"))));
                if(time()<$bmkstime) exit('20006');

                $row=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."match where cs_sid=".CS_Match_ID." and cs_uid='".$this->session->userdata('cs_id')."'")->row(); 
				if(!$row){
                       exit('20008');
				}

                $rowu=$this->db->query("SELECT CS_ID,CS_DJID FROM ".CS_SqlPrefix."user where cs_id=".$this->session->userdata('cs_id')."")->row();
				if($rowu->CS_DJID!=1){
                       exit('20002');
				}

                $rowd=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."dance where cs_uid=".$this->session->userdata('cs_id')." and cs_sid=".CS_Match_ID."")->row();
				if($rowd){
                       exit('20005');
				}
			    $result=$this->db->query("select CS_ID,CS_Name from ".CS_SqlPrefix."class where CS_SID=1 and CS_FID=1");
			    $str='<option value="0" id="defultType">请选所属类型</option>';
			    foreach ($result->result() as $row2) {
                        $str.='<option value="'.$row2->CS_ID.'">'.$row2->CS_Name.'</option>';
				}
				echo '<div class="upload">
				<ul>
				<li>
				<div class="dA_title">舞曲名字：</div>
				<div class="dA_input"><input id="rdance_name" class="input_normal" type="text" maxlength="255" value="" name="dance_name" style="width: 290px;"></div>
				<div id="mdance_name" class="input_msg">作为舞曲名称，此项必须填写。</div>
				</li>
				<li>
				<div class="dA_title">舞曲类型：</div>
				<div class="dA_input"><select id="rclass_id" class="select_normal" name="class_id" style="width: 110px;">'.$str.'</select></div>
				<div id="mclass_id" class="input_msg">舞曲所属的类型，请正确选择。</div>
				</li>
				<li>
				<div class="dA_title">上传路径：</div>
				<div class="dA_input">
				<input id="uploadname" class="input_normal" type="text" style="width:290px;" disabled="disabled"/>
				<div id="uploaddanceid" style="display: none;"></div>
				<span class="button2" id="upButton"><span>
				<input id="file_upload" width="100" type="file" height="32" name="file_upload">
				</span>
				</span>
				</div>
				<div id="mdanceurl" class="input_msg">选择上传舞曲。</div>
				</li>
				<li><div class="dA_title">上传进度：</div><div class="dA_progress"><div class="progressBox"><div class="progressBg"><div id="idCompleted"></div></div><div id="idProgress">0%</div></div><div class="progressBox"><span id="spnFileSize"></span><span id="spnSpeed"></span><span id="spnRemainingTime"></span></div></div></li></ul><div class="button"><button id="btnUpload" onclick="activityLib.shareAdd();" class="save" type="button">上传保存</button><button onclick="$.dialog.get(\'danceupload\').close();" class="no_withhold" type="button">取消发布</button></div></div><input type="hidden" id="filePath" name="filePath" /><input type="hidden" id="fileSize" name="fileSize" /><script type="text/javascript">activityLib.danceAddInit(\''.$this->session->userdata('cs_name').'\',\''.md5(CS_Encryption_Key.$this->session->userdata('cs_name')).'\');activityLib.checkShareAddInit();</script>';
	}

	public function dance_save()
	{
                if(!$this->CsdjUser->User_Login(1)){
		             exit("20001");//未登入
                }

                $music['cs_name']=strip_tags($this->input->post('name', TRUE));   //名称
                $music['cs_cid']=intval($this->input->post('cid', TRUE));   //分类 
                $music['cs_rmb']=8;   //金币
				$music['cs_laiy']='原创';
                $music['cs_content']='';   //歌词/介绍
                $music['cs_playurl']=$this->CsdjSkins->str_checkhtml($this->input->post('playurl', TRUE));   //播放地址
                $music['cs_dx']=$this->CsdjSkins->str_checkhtml($this->input->post('dx', TRUE));   //歌曲大小
                $music['cs_did']=1;  
                $music['cs_dts']=0;  
                $music['cs_sid']=CS_Match_ID;  
                $music['CS_DownUrl1']=$music['cs_playurl'];
                $music['CS_UID']=$this->session->userdata('cs_id');
                $music['CS_User']=$this->session->userdata('cs_name');
                $music['CS_AddTime']=date('Y-m-d H:i:s');
				$music['CS_YID']=1;

                if(empty($music['cs_name']) || $music['cs_cid']==0 || empty($music['cs_playurl'])){
                     exit('10002');
				}

                $bmjstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,CS_Match_Yue,9,date("Y"))));
                if(time()>$bmjstime) exit('20007');

                $bmkstime=strtotime(date("Y-m-d H:i:s",mktime(0,0,0,(CS_Match_Yue-1),9,date("Y"))));
                if(time()<$bmkstime) exit('20006');

                $row=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."match where cs_sid=".CS_Match_ID." and cs_uid='".$this->session->userdata('cs_id')."'")->row(); 
				if(!$row){
                       exit('20008');
				}

                $rowu=$this->db->query("SELECT CS_ID,CS_DJID FROM ".CS_SqlPrefix."user where cs_id=".$this->session->userdata('cs_id')."")->row();
				if($rowu->CS_DJID!=1){
                       exit('20002');
				}

                $rowd=$this->db->query("SELECT CS_ID FROM ".CS_SqlPrefix."dance where cs_uid=".$this->session->userdata('cs_id')." and cs_sid=".CS_Match_ID."")->row();
				if($rowd){
                       exit('20006');
				}

                $res=$this->CsdjDB->get_insert('dance',$music);
                if(intval($res)>0){
                     exit('10000');
				}else{
                     exit('歌曲入库失败');
				}
	}
}
