<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 * $Id$
 */
if(!defined('__CORE_DIR')){
    exit("Access Denied");
}
class Ctl_XiChe_Ucenter_Car extends Ctl_Xiche_Ucenter
{
    
    public function index()
    {
        $gid = $this->GP('gid');
        if($gid){
            $this->pagedata['gid'] = $gid;
        }
        $addr_id = $this->GP('addr_id');
        if($addr_id){
            $this->pagedata['addr_id'] = $addr_id;
        }
        $filter = array();
        $filter['uid'] = $this->uid;
        $res = K::M('xiche/car')->items($filter,array('car_id'=>'desc'));
        $this->pagedata['res'] = $res;
        $brand = K::M('xiche/car/brand')->items($where,array('py'=>'asc'),1,2000);
        $this->pagedata['brand'] = $brand;
        $this->tmpl = 'xiche/ucenter/car/index.html';
        
    }
    
    
    public function sub(){
        
        $brand_id = (int)$this->GP('brand_id');
        if(!$brand_id){
            $this->msgbox->add('错误',201);
        }else{
            $filter = array();
            $filter['parent_id'] = $brand_id;
            
            $r = K::M('xiche/car/brand')->items($filter,array('brand_id'=>'desc'));
            if($r){
                $this->msgbox->add('成功');
                $this->msgbox->set_data('r',$r);
                $this->pagedata['r'] = $r;
            }else{
                $this->msgbox->add('错误',202);
            }
        }
        
    }
    
    
    public function add()
    {
        
        $gid = (int)$this->GP('gid');
        if($gid){
            $this->pagedata['gid'] = $gid;
        }
        
        if($paizhao_sf = $this->GP('paizhao_sf')){
            
            if($attach = $_FILES['car_photo']){
                if($a = K::M('magic/upload')->upload($attach, 'car')){
                    $data['photo'] = $a['photo'];
                }
            }
            $brand_id = $this->GP('brand_id');
            $paizhao_hao = $this->GP('paizhao_hao');
            $color = $this->GP('color');
            $data['paizhao_sf'] = $paizhao_sf;
            $data['brand_id'] = $brand_id;
            $data['paizhao_hao'] = $paizhao_hao;
            $data['color'] = $color;
            $data['uid'] = $this->uid;

            if($car_id = K::M('xiche/car')->create($data)){
                $this->msgbox->add('添加车辆成功');
                if($gid){
                   $this->msgbox->set_data("forward", $this->mklink('xiche/order/goods:index',null,array('gid'=>$gid,'car_id'=>$car_id)));
                }else{
                   $this->msgbox->set_data("forward", $this->mklink('xiche/ucenter/car'));
                }
                
            }
            
        }else{
            $chepai = K::M('data/data')->chepai(); //返回车牌省份开头
            $checolor = K::M('data/data')->checolor(); //返回车颜色
            $where = array();
            $where['parent_id'] = 0;

            $brand = K::M('xiche/car/brand')->items($where,array('py'=>'asc'),1,2000);

            foreach($brand as $k=>$val){
              $s = K::M('xiche/car/brand')->find(array('parent_id'=>$val['brand_id']));
              $brand[$k]['sub'] = 1;
            }
            
            
            $list = array();
            foreach($brand as $k=>$val){
                $list[$val['py']][] = $val; 
            }

  
            $this->pagedata['list'] = $list;
            $this->pagedata['chepai'] = $chepai;
            $this->pagedata['checolor'] = $checolor;
            $this->tmpl = 'xiche/ucenter/car/add.html';
                 
        }
        

    }
 
    
    
    public function del_car(){
        $car_id = (int)$this->GP('car_id');
        if(!$car_id){
            $this->msgbox->add('错误的车辆!',211);
            $this->msgbox->response();
        }else if(!$addr =K::M('xiche/car')->detail($car_id)){
            $this->msgbox->add('错误的车辆!',211);
            $this->msgbox->response();
        }else if(!$this->uid){
            $this->msgbox->add('您还没有登录或登录超时!',101);
            $this->msgbox->response();
        }else if($addr['uid'] != $this->uid){
            $this->msgbox->add('不可操作别人的车辆!',213);
            $this->msgbox->response();
        }else{
            $del = K::M('xiche/car')->delete(array('car_id'=>$car_id));
            if($del){
                $this->msgbox->add('删除成功!');
            }else{
                $this->msgbox->add('删除失败!',215);
                $this->msgbox->response();
            }
        }
    }
    

    
}