<?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_Mcenter_Order extends Ctl_XiChe_Mcenter
{

  
    // 订单-抢新单
    public function rob($page)
    {   
        $type_name = '抢单';
        $this->pagedata['type_name']=$type_name;
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        //$orderby = array('order_id'=>'desc');
        $staff_lat = $_COOKIE['lat'];
        $staff_lng = $_COOKIE['lng'];
        
        if($staff_lat && $staff_lng){
          $orderby = " (ABS(lng - '{$staff_lng}') +  ABS(lat - '{$staff_lat}') ) asc";  
        }else{
          $orderby = array('order_id'=>'desc');
        }

        
        $filter['staff_id'] = 0;
        $filter['order_status'] = 2;
        $filter['fuwu_time'] = '>:'.$this->system->sdaytime;
        if($items = K::M('xiche/order')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
   
        foreach($items as $k=>$val){
            if($aunt_lat && $aunt_lng){
               if($val['lat']!='' && $val['lng']!=''){
                    $items[$k]['km'] = K::M('staff/staff')->getdistance($val['lng'],$val['lat'],$aunt_lng,$aunt_lat);  //距离
                } 
            }
        }

       
        $goods = K::M('xiche/goods')->items();
        $this->pagedata['goods']=$goods;
        
        $this->pagedata['items'] = $items;
        $this->pagedata['pager'] = $pager;
        $this->tmpl = 'xiche/mcenter/order/rob.html';
    }

    // 订单-待处理
    public function wait($page)
    {
        $type_name = '待处理';
        $this->pagedata['type_name']=$type_name;
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        //$orderby = array('order_id'=>'desc');
        $staff_lat = $_COOKIE['lat'];
        $staff_lng = $_COOKIE['lng'];
        
        if($staff_lat && $staff_lng){
          $orderby = " (ABS(lng - '{$staff_lng}') +  ABS(lat - '{$staff_lat}') ) asc";  
        }else{
          $orderby = array('order_id'=>'desc');
        }

        $filter['staff_id'] = 0;
        $filter['order_status'] = 3;

        if($items = K::M('xiche/order')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
   
        foreach($items as $k=>$val){
            if($aunt_lat && $aunt_lng){
               if($val['lat']!='' && $val['lng']!=''){
                    $items[$k]['km'] = K::M('staff/staff')->getdistance($val['lng'],$val['lat'],$aunt_lng,$aunt_lat);  //距离
                } 
            }
        }

       
        $goods = K::M('xiche/goods')->items();
        $this->pagedata['goods']=$goods;
        
        $this->pagedata['items'] = $items;
        $this->pagedata['pager'] = $pager;
    	$this->tmpl = 'xiche/mcenter/order/rob.html';
    }

    // 订单-已完成
    public function finish($page)
    {
        $type_name = '已完成';
        $this->pagedata['type_name']=$type_name;
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        //$orderby = array('order_id'=>'desc');
        $staff_lat = $_COOKIE['lat'];
        $staff_lng = $_COOKIE['lng'];
        
        if($staff_lat && $staff_lng){
          $orderby = " (ABS(lng - '{$staff_lng}') +  ABS(lat - '{$staff_lat}') ) asc";  
        }else{
          $orderby = array('order_id'=>'desc');
        }

        $filter['order_status'] = 4;

        if($items = K::M('xiche/order')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
   
        foreach($items as $k=>$val){
            if($aunt_lat && $aunt_lng){
               if($val['lat']!='' && $val['lng']!=''){
                    $items[$k]['km'] = K::M('staff/staff')->getdistance($val['lng'],$val['lat'],$aunt_lng,$aunt_lat);  //距离
                } 
            }
        }

       
        $goods = K::M('xiche/goods')->items();
        $this->pagedata['goods']=$goods;
        
        $this->pagedata['items'] = $items;
        $this->pagedata['pager'] = $pager;
    	$this->tmpl = 'xiche/mcenter/order/rob.html';
    }

    
    
    
    // 订单-今日任务
    public function today($page)
    {   
        $type_name = '今日任务';
        $this->pagedata['type_name']=$type_name;
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        //$orderby = array('order_id'=>'desc');
        $staff_lat = $_COOKIE['lat'];
        $staff_lng = $_COOKIE['lng'];
        
        if($staff_lat && $staff_lng){
          $orderby = " (ABS(lng - '{$staff_lng}') +  ABS(lat - '{$staff_lat}') ) asc";  
        }else{
          $orderby = array('order_id'=>'desc');
        }
        
        $td = strtotime(date('Y-m-d', time()));
        $tmd = strtotime(date('Y-m-d', time()+86400));

        
        $filter['staff_id'] = $this->staff['staff_id'];
        $filter['order_status'] = 3;
        $filter['fuwu_time'] = $td."~".$tmd;
        if($items = K::M('xiche/order')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
   
        foreach($items as $k=>$val){
            if($aunt_lat && $aunt_lng){
               if($val['lat']!='' && $val['lng']!=''){
                    $items[$k]['km'] = K::M('staff/staff')->getdistance($val['lng'],$val['lat'],$aunt_lng,$aunt_lat);  //距离
                } 
            }
        }

       
        $goods = K::M('xiche/goods')->items();
        $this->pagedata['goods']=$goods;
        
        $this->pagedata['items'] = $items;

        $this->pagedata['pager'] = $pager;
        $this->tmpl = 'xiche/mcenter/order/rob.html';
    }
    
    
    
    // 订单-今日完成
    public function today_finish($page)
    {   
        $type_name = '今日完成';
        $this->pagedata['type_name']=$type_name;
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        //$orderby = array('order_id'=>'desc');
        $staff_lat = $_COOKIE['lat'];
        $staff_lng = $_COOKIE['lng'];
        
        if($staff_lat && $staff_lng){
          $orderby = " (ABS(lng - '{$staff_lng}') +  ABS(lat - '{$staff_lat}') ) asc";  
        }else{
          $orderby = array('order_id'=>'desc');
        }
        
        $td = strtotime(date('Y-m-d', time()));
        $tmd = strtotime(date('Y-m-d', time()+86400));

        
        $filter['staff_id'] = $this->staff['staff_id'];
        $filter['order_status'] = 4;
        $filter['fuwu_time'] = $td."~".$tmd;
        if($items = K::M('xiche/order')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
   
        foreach($items as $k=>$val){
            if($aunt_lat && $aunt_lng){
               if($val['lat']!='' && $val['lng']!=''){
                    $items[$k]['km'] = K::M('staff/staff')->getdistance($val['lng'],$val['lat'],$aunt_lng,$aunt_lat);  //距离
                } 
            }
        }

       
        $goods = K::M('xiche/goods')->items();
        $this->pagedata['goods']=$goods;
        
        $this->pagedata['items'] = $items;

        $this->pagedata['pager'] = $pager;
        $this->tmpl = 'xiche/mcenter/order/rob.html';
    }
    
    
    
     // 订单-今日完成
    public function all_finish($page)
    {   
        $type_name = '总计完成';
        $this->pagedata['type_name']=$type_name;
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        //$orderby = array('order_id'=>'desc');
        $staff_lat = $_COOKIE['lat'];
        $staff_lng = $_COOKIE['lng'];
        
        if($staff_lat && $staff_lng){
          $orderby = " (ABS(lng - '{$staff_lng}') +  ABS(lat - '{$staff_lat}') ) asc";  
        }else{
          $orderby = array('order_id'=>'desc');
        }

        
        $filter['staff_id'] = $this->staff['staff_id'];
        $filter['order_status'] = 4;

        if($items = K::M('xiche/order')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
   
        foreach($items as $k=>$val){
            if($aunt_lat && $aunt_lng){
               if($val['lat']!='' && $val['lng']!=''){
                    $items[$k]['km'] = K::M('staff/staff')->getdistance($val['lng'],$val['lat'],$aunt_lng,$aunt_lat);  //距离
                } 
            }
        }

       
        $goods = K::M('xiche/goods')->items();
        $this->pagedata['goods']=$goods;
        
        $this->pagedata['items'] = $items;

        $this->pagedata['pager'] = $pager;
        $this->tmpl = 'xiche/mcenter/order/rob.html';
    }
    
    
    
    
    // 订单详情
    public function detail($order_id)
    {   
        $order_id=$this->GP('order_id');
        if(!$order_id){
            $this->msgbox->add('错误的订单',201);
        }
        $order = K::M('xiche/order')->detail($order_id);
        if(!$order){
            $this->msgbox->add('错误的订单',201);
        }else{
            $this->pagedata['order'] = $order;
            //查询所有服务
            $goods = K::M('xiche/goods')->items();
            $this->pagedata['goods'] = $goods;

            //查询下单次数
            $order_count = K::M('xiche/order')->count(array('uid'=>$order['uid']));
            $this->pagedata['order_count'] = $order_count;
            //查询附加项目
            $other = K::M('xiche/order/product')->items(array('order_id'=>$order_id));
            $this->pagedata['other'] = $other;
            //查询评价详情
            $comment = K::M('staff/comment')->items(array('order_id'=>$order['order_id'],'staff_id'=>$staff['staff_id']));
            $this->pagedata['comment'] = $comment;
            
            $photo = K::M('xiche/order/photo')->find(array('order_id'=>$order['order_id']));
            $this->pagedata['photo'] = $photo;
        }
        
        $this->tmpl = 'xiche/mcenter/order/detail.html';
    }
    
    
    public function upload_photo(){
        $order_id = $this->GP('order_id');
        if(!$order_id){
            $this->msgbox->add('错误的订单',201);
            $this->msgbox->response();
        }else if(!$order = K::M('xiche/order')->detail($order_id)){
            $this->msgbox->add('错误的订单',201);
            $this->msgbox->response();
        }else if(!$this->staff){
            $this->msgbox->add('没有登录！',202);
            $this->msgbox->response();
        }else{
            
            $data = array();
            
            $data['staff_id'] = $this->staff['staff_id'];
            $data['order_id'] = $order['order_id'];
            
            if($attach1 = $_FILES['car_photo1']){
                if($a = K::M('magic/upload')->upload($attach1, 'car')){
                    $data['before'] = $a['photo'];
                }
            }
            if($attach2 = $_FILES['car_photo2']){
                if($a = K::M('magic/upload')->upload($attach2, 'car')){
                    $data['after'] = $a['photo'];
                }
            }
            
            //检查是否有照片记录,没有则创建一条再修改.
             $photo = K::M('xiche/order/photo')->find(array('order_id'=>$order['order_id']));
             if(!$photo){
          
                 $add = K::M('xiche/order/photo')->create($data);//创建照片记录
                 if($add){
                     $this->msgbox->add('上传成功1！');
                     $this->msgbox->response();
                 }else{
                     $this->msgbox->add('上传失败！');
                     $this->msgbox->response();
                 }
             }else{
                 
                 
                 if($attach1){
                     $update = K::M('xiche/order/photo')->updates($photo['photo_id'],array('before'=>$data['before']));
                 }else if($attach2){
                     $update = K::M('xiche/order/photo')->updates($photo['photo_id'],array('after'=>$data['after']));
                 }
                 
                 if($update){
        
                     $this->msgbox->add('上传成功2！');
                     $this->msgbox->response();
                 }else{
                     $this->msgbox->add('上传失败！');
                     $this->msgbox->response();
                 }
                 
             }
             
        }
        
        
    }



    //  抢单
    public function  qiang()
    {

        $order_id = $this->GP('order_id');

        if(empty($order_id)){
            $this->msgbox->add('订单不存在', 212);
            $this->msgbox->response();
        }else if(!$detail = K::M('xiche/order')->detail($order_id)){
            $this->msgbox->add('订单不存在', 212);
            $this->msgbox->response();
        }else if($detail['staff_id'] !=0){
            $this->msgbox->add('订单已被抢', 212);
            $this->msgbox->response();
        }else if($detail['order_status'] !=2){
            $this->msgbox->add('订单状态不正确', 212);
            $this->msgbox->response();
        }else if($detail['order_status'] ==3){
            $this->msgbox->add('订单已被抢', 212);
            $this->msgbox->response();
        }

        // 更新订单状态
        if(false !== K::M('xiche/order')->update($order_id,array('order_status'=>3,'staff_id'=>$this->staff['staff_id']))){

            //订单取消成功，增加订单记录
            $order_log_data = array(
                'order_id'=>$order_id,
                'log'=>'订单被接单',
            );
   
            $log = K::M('xiche/order/log')->create($order_log_data);
            //更新模版消息
            /*$u = K::M('member/member')->detail($detail['uid']);
            if ($u['wx_openid']) {
                //获取模版消息配置
                $wx_config = $this->system->config->get('wx_config');
                $config = $this->system->config->get('site');
                $a = array('title'=>'恭喜,预约成功，请保持电话畅通', 'items' => array('OrderSn' => $detail['order_id'], 'OrderStatus' => '订单已被接单'), 'remark' =>'恭喜,您的订单于'.date('Y-m-d H:i:s',time()).'已被接单！');
                $url = K::M('helper/link')->mklink('xiche/mcenter/order:detail_wait', array($detail['order_id']),array(),  'www');
                K::M('weixin/wechat')->wechat_client($config)->sendTempMsg($u['wx_openid'], $wx_config['order_id'], $url, $a);   
            }*/
            $this->msgbox->add('抢单成功');
            $this->msgbox->response();
         }else{
            $this->msgbox->add('抢单失败', 212);
            $this->msgbox->response();
         }
   }
   
   
   
   
   
   //  抢单
    public function end()
    {

        $order_id = $this->GP('order_id');

        if(empty($order_id)){
            $this->msgbox->add('订单不存在', 212);
            $this->msgbox->response();
        }else if(!$detail = K::M('xiche/order')->detail($order_id)){
            $this->msgbox->add('订单不存在', 212);
            $this->msgbox->response();
        }else if($detail['order_status'] !=3){
            $this->msgbox->add('订单状态错误', 212);
            $this->msgbox->response();
        }else if($detail['order_status'] ==4){
            $this->msgbox->add('订单已', 212);
            $this->msgbox->response();
        }

        // 更新订单状态
        if(false !== K::M('xiche/order')->update($order_id,array('order_status'=>4,'staff_id'=>$this->staff['staff_id']))){

            //订单成功，增加订单记录
            $order_log_data = array(
                'order_id'=>$order_id,
                'log'=>'订单已完成',
            );
   
            $log = K::M('xiche/order/log')->create($order_log_data);
            //更新模版消息
            /*$u = K::M('member/member')->detail($detail['uid']);
            if ($u['wx_openid']) {
                //获取模版消息配置
                $wx_config = $this->system->config->get('wx_config');
                $config = $this->system->config->get('site');
                $a = array('title'=>'恭喜,预约成功，请保持电话畅通', 'items' => array('OrderSn' => $detail['order_id'], 'OrderStatus' => '订单已被接单'), 'remark' =>'恭喜,您的订单于'.date('Y-m-d H:i:s',time()).'已被接单！');
                $url = K::M('helper/link')->mklink('xiche/mcenter/order:detail_wait', array($detail['order_id']),array(),  'www');
                K::M('weixin/wechat')->wechat_client($config)->sendTempMsg($u['wx_openid'], $wx_config['order_id'], $url, $a);   
            }*/
            $this->msgbox->add('洗车完成');
            $this->msgbox->response();
         }else{
            $this->msgbox->add('完成失败', 212);
            $this->msgbox->response();
         }
   }
   
   
   
   
   
   
   
   
}