<?php

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

             $_data ["type_name"]=$this->input->post('type_name');           
             if($_data ["type_name"]){
                 $_row = $this->type_model->get_type_name($_data ["type_name"]);
                 if($_row){
                     echo'{"success":false,"message":"添加失败,状态名已经存在！"}';exit;
                 }
             }else{
                     echo'{"success":false,"message":"非法访问"}';exit;
             }
             if($this->type_model->add_type($_data)){
                 echo'{"success":true,"message":"添加成功"}';
             }else{
                 echo'{"success":false,"message":"添加失败"}';
             }

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

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