<?php

class Position extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('manage/position_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('Manage');
        $_data ['url'] = $this->url;
        $_data['area']= $this->get_area();
        $this->load->view('manage/position',$_data); 
    }
    
     public function add_position(){

             $_data ["position_name"]=$this->input->post('position_name');
             $_data ["area_id"]=$this->input->post('area_id');
             if($_data ["position_name"]){
                 $_row = $this->position_model->get_position_name($_data ["position_name"]);
                 if($_row){
                     echo'{"success":false,"message":"添加失败,储位名已经存在！"}';exit;
                 }
             }else{
                     echo'{"success":false,"message":"非法访问"}';exit;
             }
             if($this->position_model->add_position($_data)){
                 echo'{"success":true,"message":"添加成功"}';
             }else{
                 echo'{"success":false,"message":"添加失败"}';
             }

     }     
    
     public function update_position(){
         $_id=$this->input->post('position_id');
         if(isset($_id)){
             $_data ["position_name"]=$this->input->post('position_name');
             $_data ["area_id"]=$this->input->post('area_id');
             if($this->position_model->update_position($_id,$_data)){
                 echo'{"success":true,"message":"修改成功"}';
             }else{
                 echo'{"success":false,"message":"修改失败"}';
             }
         }else{
             echo'{"success":false,"message":"非法访问"}';
         }           
         
     }
   
    
    public function get_position(){
       $_row = $this->position_model->get_position();
       $_row=array_values($_row);
       $_str='['.substr(json_encode($_row),1,-1).']';
       echo $_str;
    }

   
    public function get_one_position(){
            $_id=$this->input->post('id');
        if(isset($_id)){
            if($this->position_model->get_one_position($_id)){
                $_row = $this->position_model->get_one_position($_id);
                $_row = json_encode($_row);
                echo '['.$_row.']';
            }else{
                echo'{"success":false,"message":"储位不存在"}';
            }
        }else{
            echo'{"success":false,"message":"非法访问"}';
        }
     
     }
     public function get_area(){
         return $this->position_model->get_area();
     }     
     public function get_area_tree(){
         $_row=$this->get_area();
         foreach ($_row as $_key=>$_value){
             $_rows[$_key]->id=$_value->area_id;
             $_rows[$_key]->text=$_value->area_name;
         }
         $_row=array_values($_rows);
         $_str='['.substr(json_encode($_row),1,-1).']';
         echo $_str;
     }
     
     
}