<?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_Staff extends Ctl
{

    public function items($params)
    {
		$cate_id = $params['cate_id'];
        $lat = $params['lat'];
        $lng = $params['lng'];
        $filter = $staff_ids = $cate_lists = array();
         $filter['cate_id'] = $cate_id;
         $filter['name'] = "LIKE:%".$params['keyword']."%";
         $page = max((int)$params['page'], 1);
         $pager['limit'] = $limit =  $params['limit'] ? $params['limit'] :10;
         $count = 0;
         if($params['SouthWlat']&&$params['NorthElat']&&$params['SouthWlng']&&$params['NorthElng']){
            $filter['lat'] = $params['SouthWlat'].'~'.$params['NorthElat'];   //左下纬度和右上纬度
            $filter['lng'] = $params['SouthWlng'].'~'.$params['NorthElng'];   //左下经度和右上经度
         }
         $catess = K::M('cate/cate')->fetch_all();
         foreach($catess as $k=>$val){
                if($val['parent_id'] != 0){
                    $cates[$k] = $val;
                }
            }
            //print_r($cates);die;
         if (!$items = K::M('staff/staff')->items($filter, null, $page, $limit, $count)) {
            $items= array();
        }else{
            foreach($items as $k=>$val){
                $staff_ids[$val['staff_id']] = $val['staff_id'];
            }
            $attrs = K::M('staff/attr')->items(array('staff_id'=>$staff_ids));
            foreach ($attrs as $k=>$val){
                if(!$cates[$val['cate_id']]){
                    $cate_lists[$val['staff_id']] = array();
                }else{
                    $cate_lists[$val['staff_id']][] = $this->filter_fields('cate_id,title,photo,deposit,parent_id',$cates[$val['cate_id']]);
                }
            }
            if(!$lng || !$lat){
                $lng = $this->request['UxLocation']['lng'];
                $lat = $this->request['UxLocation']['lat'];
            }
            foreach($items as $k=>$val){
                if(!$cate_lists[$val['staff_id']]){
                    $items[$val['staff_id']]['skill'] = array();
                }else{
                    $items[$val['staff_id']]['skill'] = $cate_lists[$val['staff_id']];
                }
            }
            foreach ($items as $k=>$val){
                $items[$k] = $this->filter_fields('staff_id,city_id,mobile,orders,name,intro,face,skill,title,lat,lng,score,comments,praise_num,year_num', $val);
            }
        }
        $this->msgbox->set_data('data', array('items' =>array_values($items)));
    }
    
    
     public function detail($params)
   {
      $staff_id = $params['staff_id'];
      if(!$staff_id){
        $this->msgbox->add('参数错误！', 212);
      }else{
        $detail = K::M('staff/staff')->detail($staff_id);
        if(!$detail){
          $this->msgbox->add('该师傅不存在！', 212);
        }else{
            if(!$result = K::M('member/mystaff')->find(array('uid'=>$this->uid,'staff_id'=>$staff_id))){
               $collect_status = 0;
            }else{
               $collect_status = 1;
            }
        }
      }
       $this->msgbox->set_data('data', array('collect_status'=>$collect_status));
   }
   
   public function comments($params){
        $staff_id = $params['staff_id'];
        $filter = $comment_ids = array();
         $filter['staff_id'] = $staff_id;
         $page = max((int)$params['page'], 1);
         $pager['limit'] = $limit = 10;
         $staff = K::M('staff/staff')->detail($staff_id);
         $count = 0;
         if (!$items = K::M('staff/comment')->items($filter, array('comment_id'=>'desc'), $page, $limit, $count)) {
            $items= array();
        }else{
            $comment_ids = $uids = array();
            foreach ($items as $k=>$val){
                $comment_ids[$val['comment_id']] = $val['comment_id'];
                $uids[$val['uid']] = $val['uid'];
            }
            $comment_photos = K::M('staff/photo')->items(array('comment_id'=>$comment_ids));
            $members = K::M('member/member')->items_by_ids($uids);
            foreach($items as $k=>$val){
                foreach($comment_photos as $kk=>$v){
                    if($val['comment_id'] == $v['comment_id']){
                        $items[$k]['pics'][] = $v;
                    }
                }
                $items[$k]['face'] = $members[$val['uid']]['face'];
                $items[$k]['nickname'] = $members[$val['uid']]['nickname'];
            }
            foreach($items as $k=>$val){
                if(!$items[$k]['pics']){
                    $items[$k]['pics'] = array();
                }
            }
        }
        $this->msgbox->set_data('data', array('items' =>array_values($items),'count'=>$staff['comments']));
   }
   
   
}
