<?php defined('SYSPATH') or die('No direct script access.'); 

        class Controller_Team extends Controller { 
    
            public function action_teamstatus()
            {
            
            $me = Session::instance()->get("fbUserId");






    	    $team = ORM::factory("team")->where('id','=',$me)->find();
	    //;
		$data = array("team" => $team);
                
                $view = View::factory("team",$data);  
                $this->request->response = $view;  
            }

public function action_fileupload()
        {
       $me = Session::instance()->get("fbUserId");
        $team = ORM::factory("team")->where('id','=',$me)->find();
        if($_POST){
            move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "./images/".$team->id.".jpg");
        }
        $data = array("team" => $team);
        $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
        $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }
       
        public function action_changestadiumcapacity()
        {
        $me = Session::instance()->get("fbUserId");
       
         $stadium = ORM::factory("stadium")->where('id','=',$me)->find();
        $team = ORM::factory("team")->where('id','=',$me)->find();
        $economy = ORM::factory("economy")->where('id','=',$me)->find();
        if($_POST){
            $new = $_POST["capacity"];
            $stadium->capacity += $new;
            $stadium->save();
            $economy->total_money -= $new*10;
            $economy->weekly_total_outgoing += $new*10;
            $economy->save();
        }
            $data = array("stadium" => $stadium, "team" => $team);
       
            $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
            $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }
       
        public function action_changestadiumname()
        {
        $me = Session::instance()->get("fbUserId");
       
         $stadium = ORM::factory("stadium")->where('id','=',$me)->find();
        $team = ORM::factory("team")->where('id','=',$me)->find();
        $economy = ORM::factory("economy")->where('id','=',$me)->find();
        $economy->total_money -= 2000;
        $economy->weekly_total_outgoing += 2000;
        $economy->save();
        if($_POST){
           
            $stadium->stadium_name = $_POST["stadiumname"];
            $stadium->save();
        }
            $data = array("stadium" => $stadium, "team" => $team);
       
            $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
            $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }
       
       
       
        public function action_changeteamname()
        {
        $me = Session::instance()->get("fbUserId");
       
        $team = ORM::factory("team")->where('id','=',$me)->find();
        if($_POST){
           
            $team->team_name = $_POST["teamname"];
            $team->save();
        }
            $data = array("team" => $team);
       
            $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
            $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }
       
        public function action_changecoachnumber()
        {
        $me = Session::instance()->get("fbUserId");
          $economy = ORM::factory("economy")->where('id','=',$me)->find();
       
         $staff = ORM::factory("staf")->where('id','=',$me)->find();
        $team = ORM::factory("team")->where('id','=',$me)->find();
        if($_POST){
            $new = $_POST["number"];
           $economy->total_money -= abs(($staff->coach_number - $new)) * 1000;
           $economy->weekly_total_outgoing += abs(($staff->coach_number - $new)) * 1000;
$economy->save();
            $staff->coach_number = $new;
            $staff->save();
        }
            $data = array("staff" => $staff, "team" => $team);
       
            $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
            $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }
        public function action_changephysionumber()
        {
        $me = Session::instance()->get("fbUserId");
       
         $staff = ORM::factory("staf")->where('id','=',$me)->find();
        $team = ORM::factory("team")->where('id','=',$me)->find();
        if($_POST){
           
            $staff->physio_number = $_POST["number"];
            $staff->save();
        }
            $data = array("staff" => $staff, "team" => $team);
       
            $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
            $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }
       
        public function action_changeticketprice()
        {
        $me = Session::instance()->get("fbUserId");
       
         $stadium = ORM::factory("stadium")->where('id','=',$me)->find();
        $team = ORM::factory("team")->where('id','=',$me)->find();
        if($_POST){
            $new = $_POST["price"];
            if($new <10)
            $stadium->ticket_price = $new;
            $stadium->save();
        }
            $data = array("stadium" => $stadium, "team" => $team);
       
            $view = View::factory("team", $data);  //viewin çaðrýldýðý yer
            $this->request->response = $view;  // .= eklenmesini saðlýyor.
        }


            public function action_createteam()
            {
		$me = Session::instance()->get("fbUserId");
		if($_POST){
			$q=rand(0,86);
			//throw new Exception($name1);
			$name1 = ORM::factory('name')->where('id','=',$q)->find();
			$q=rand(0,86);
			$name2 = ORM::factory('name')->where('id','=',$q)->find();
			
			$name = $name1->name." ".$name2->name;
			
			
			
			$team = ORM::factory('team');
			$team->id = $me;
			$team->economy_id = $me;
			$team->stadium_id = $me;
			$team->staff_id = $me;
			$team->team_name = $_POST["teamname"];
			//throw new Exception($name);
			$team->save();
                        copy("./images/defaultlogo.jpg","./images/".$team->id.".jpg");
			$tactic_number=array(0,1,2,4,5,6,7,9,10,11,13,99,99,99,99,99);
			for($i=0;$i<16;$i++){
				$q=rand(0,86);
				$name1 = ORM::factory('name')->where('id','=',$q)->find();
				$q=rand(0,86);
				$name2 = ORM::factory('name')->where('id','=',$q)->find();
				$name = $name1->name." ".$name2->name;
				
				$player = ORM::factory('player');
				//$team->id = $me;
				$player->name = $name;
				$player->age = rand(17,27);
                                $q = rand(0,10);
                                if($q>2)
				$player->prefferedFoot = "right";
                                else
                                $player->prefferedFoot = "left";
				$player->influence = rand(1,6);
				$player->nationality = rand(1,6);
				$player->specialities = rand(1,6);
				$player->goalkeeping = rand(1,6);
				$player->defending = rand(1,6);
				$player->playmaking = rand(1,6);
				$player->winger = rand(1,6);
				$player->scoring = rand(1,6);
				$player->technique = rand(1,6);
				$player->passing = rand(1,6);
				$player->experience = 1;
				$player->stamina = rand(1,6);
				$player->form = rand(1,6);
				$player->teamid = $me;
				$player->sale = 0;
				$player->value = $player->goalkeeping + $player->defending + $player->playmaking + $player->winger + $player->scoring + $player->technique + $player->passing;
                                if($player->goalkeeping>$player->playmaking && $player->goalkeeping > $player->scoring && $player->goalkeeping > $player->defending)
                                $player->value = $player->value * $player->goalkeeping * 100 + $player->value * $player->passing * 10 + $player->value * $player->winger * 10 + $player->value * $player->technique * 10;
                                elseif($player->playmaking >= $player->goalkeeping && $player->playmaking >= $player->scoring && $player->playmaking >= $player->defending)
                                $player->value = $player->value * $player->playmaking * 100 + $player->value * $player->passing * 10 + $player->value * $player->winger * 10 + $player->value * $player->technique * 10;
                                elseif($player->scoring >= $player->playmaking && $player->scoring >= $player->goalkeeping && $player->scoring >= $player->defending)
                                $player->value = $player->value * $player->scoring * 100 + $player->value * $player->passing * 10 + $player->value * $player->winger * 10 + $player->value * $player->technique * 10;
                                elseif($player->defending >= $player->playmaking && $player->defending >= $player->goalkeeping && $player->defending >= $player->scoring)
                                $player->value = $player->value * $player->defending* 100 + $player->value * $player->passing * 10 + $player->value * $player->winger * 10 + $player->value * $player->technique * 10;
                                else
                                $player->value = $player->value * 100 + $player->value * $player->passing * 10 + $player->value * $player->winger * 10 + $player->value * $player->technique * 10;
				$player->position = $tactic_number[$i];
				$player->save();	
			}
			$economy = ORM::factory('economy');
			$economy->id = $me;
			$economy->total_money = 50000;
			$economy->save();
			
			$stadium = ORM::factory('stadium');
			$stadium->id = $me;
			$stadium->capacity = 3000;
			$stadium->stadium_name = $_POST["stadiumname"];
			$stadium->save();
			
			$league = ORM::factory('league');
			$league->id = 1;
			$league->league_name = "La Liga";
			$league->save();
			
			$staff = ORM::factory('staf');
			$staff->id = $me;
			$staff->coach_number = 1;
			$staff->physio_number = 1;
			$staff->save();
			
			$training = ORM::factory('training');
			$training->id = $me;
			$training->training_type = 1;
			$training->training_effect = 95;
			$training->save();

			
			$team = ORM::factory("team")->where('id','=',$me)->find();
			$data = array("team" => $team);
			
			$view = View::factory("team",$data);  
			$this->request->response = $view;  
		}
		else{
	        $team = ORM::factory("team")->where('id','=',$me)->find();
	    //throw new Exception($team[0]->id);
	
		$data = array("create" => $team);
                
                $view = View::factory("team",$data); 
                $this->request->response = $view;
		}
            }
	    public function action_league(){
		$me = Session::instance()->get("fbUserId");
	    $league = ORM::factory("league")->where('id','=',1)->find_all()->as_array();
	    	    $team = ORM::factory("team")->where('id','=',$me)->find();
	    //throw new Exception($team[0]->id);
		
	    //throw new Exception($team[0]->id);
		$data = array("league" => $league[0],"team" => $team);
                
                $view = View::factory("team",$data); 
                $this->request->response = $view; 	
		
	    }
	    public function action_stadium(){
		$me = Session::instance()->get("fbUserId");
	    $stadium = ORM::factory("stadium")->where('id','=',$me)->find_all()->as_array();
	     $team = ORM::factory("team")->where('id','=',$me)->find();
	    //throw new Exception($team[0]->id);
		$data = array("stadium" => $stadium[0], "team" => $team);
                
                $view = View::factory("team",$data);  
                $this->request->response = $view;  	
		
	    }
	    public function action_staff(){
		$me = Session::instance()->get("fbUserId");
		//throw new exception($me);
	    $staff = ORM::factory("staf")->where('id','=',$me)->find_all()->as_array();
	     $team = ORM::factory("team")->where('id','=',$me)->find();
	    //throw new Exception($team[0]->id);
		$data = array("staff" => $staff[0] , "team" => $team);
                
                $view = View::factory("team",$data); 
                $this->request->response = $view;  		
		
	    }
	    public function action_economy(){
		$me = Session::instance()->get("fbUserId");
	    $economy = ORM::factory("economy")->where('id','=',$me)->find_all()->as_array();
	     $team = ORM::factory("team")->where('id','=',$me)->find();
	    //throw new Exception($team[0]->id);
		$data = array("economy" => $economy[0] ,"team" => $team);
                
                $view = View::factory("team",$data);  
                $this->request->response = $view;  	
		
	    }

        }
	/*
        $me	  = Auth::instance()->get_user();
        $sale = ORM::factory('sale');
        $sale->product_type = $new_sale["product_type"];
        $sale->header = $new_sale["header"];
        $sale->department_id = $me->department_id;
        $sale->university_id = $me->university_id;
        $sale->category_id = $new_sale["category"]; 
        $sale->user_id = $me; 
        $sale->time = gen::now();
        $sale->save();
        $sale = ORM::factory("sale")->where('user_id','=',$me)
                                    ->where('header','=',$new_sale["header"])
                                    ->where('product_type','=',$new_sale["product_type"])
                                    ->find();
        $sale->searchid = $sale->id;
        $sale->save();*/
?>