<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 * $Id: index.ctl.php 14351 2015-07-22 01:25:14Z wanglei $
 */
if(!defined('__CORE_DIR')){
    exit("Access Denied");
}
class Ctl_XiChe_Comment extends Ctl
{
    
    public function index($page=1)  //评价列表
    {
        
        $staff_id = $this->GP('staff_id');
        $order_id = $this->GP('order_id');
        
        $filter = $pager = array();
        $pager['page'] = max(intval($page), 1);
        $pager['limit'] = $limit = 5;
        $orderby = array('id'=>'desc');

        $filter['staff_id'] = $staff_id;

        if($items = K::M('staff/comment')->items($filter, $orderby, $page, $limit, $count)){
            $pager['count'] = $count;
            $pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
        
        $staffs = K::M('staff/staff')->items();
        $this->pagedata['aunts'] = $staffs;

        if($order_id){
            $this->pagedata['order_id'] = $order_id;
        }
        
        $this->pagedata['staff_id'] = $staff_id;
        $this->pagedata['items'] = $items;
        $this->pagedata['pager'] = $pager;
        
        //查询出该阿姨一共服务成功的次数
        $count = K::M('order/order')->count(array('staff_id'=>$staff_id,'order_status'=>3));
        $this->pagedata['count'] = $count;

        $this->tmpl = 'xiche/comment/index.html';
       
    }
    
 
    
    
    public function add() //评价提交
    {
       $staff_id = $this->GP('staff_id');
       $order_id = $this->GP('order_id');
       $staff = K::M('staff/staff')->detail($staff_id);
       $this->pagedata['staff'] = $staff;
       $this->pagedata['order_id'] = $order_id;
       $this->tmpl = 'xiche/comment/add.html';
        
    }
    
    public function handle()
    {
        $staff_id = $this->GP('staff_id');
        $text = $this->GP('text');
        $star = $this->GP('star');
        $order_id = $this->GP('order_id');
        if(!$staff_id){
            $this->msgbox->add('错误',201);
            $this->msgbox->response();
        }else if(!$order_id){
            $this->msgbox->add('错误',200);
            $this->msgbox->response();
        }else if(!$text){
            $this->msgbox->add('评价内容没有填写',202);
            $this->msgbox->response();
        }else if(!$star){
            $this->msgbox->add('没有选择星级',203);
            $this->msgbox->response();
        }else if(!$this->uid){
            $this->msgbox->set_data('login',0);
            $this->msgbox->add('您没有登录',301);
            $this->msgbox->response();
        }
 
        $add_time = K::M('staff/comment')->find(array('uid'=>$this->uid,'staff_id'=>$staff_id,'order_id'=>$order_id),array('id'=>'desc'));
        if($add_time){
            $this->msgbox->add('您已经评价过了！',216);
            $this->msgbox->response();
        }

        $data = array(
            'staff_id'=>$staff_id,
            'uid'=>$this->uid,
            'order_id'=>$order_id,
            'star'=>$star,
            'text'=>$text,
            'type'=>'xiche'
        );
        $create = K::M('staff/comment')->create($data);
        if($create){
            $this->msgbox->add('评价成功！');
            $this->msgbox->response();
        }else{
            $this->msgbox->add('评论失败！',204);
            $this->msgbox->response();
        }
    }
    
   
    
}