<?php

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

             $_data ["area_name"]=$this->input->post('area_name');           
             if($_data ["area_name"]){
                 $_row = $this->area_model->get_area_name($_data ["area_name"]);
                 if($_row){
                     echo'{"success":false,"message":"添加失败,库区名已经存在！"}';exit;
                 }
             }else{
                     echo'{"success":false,"message":"非法访问"}';exit;
             }
             if($this->area_model->add_area($_data)){
                 echo'{"success":true,"message":"添加成功"}';
             }else{
                 echo'{"success":false,"message":"添加失败"}';
             }

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

    
    
    public function get_one_area(){
            $_id=$this->input->post('id');
        if(isset($_id)){
            if($this->area_model->get_one_area($_id)){
                $_row = $this->area_model->get_one_area($_id);
                $_row = json_encode($_row);
                echo '['.$_row.']';
            }else{
                echo'{"success":false,"message":"库区不存在"}';
            }
        }else{
            echo'{"success":false,"message":"非法访问"}';
        }
     
     }
     
}