<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 */

if(!defined('__CORE_DIR')){
    exit("Access Denied");
}

/*我的关注*/
class Ctl_Ucenter_Mystaff extends Ctl_Ucenter
{
     public function index($page = 1) 
    {
        $order = $this->GP('order');
        if($order){
            $this->pagedata['order'] = $order;
        }
        $this->tmpl = 'ucenter/mystaff/index.html';
    }
    
	public function staff_list($page = 1) 
    {
         $filter = array();
         $filter['uid'] = $this->uid;
         $page= max(intval($this->GP('page')), 1);
         $pager['limit'] = $limit = 10;
         $count = 0;
         $catess = K::M('cate/cate')->fetch_all();
         foreach($catess as $k=>$val){
                if($val['parent_id'] != 0){
                    $cates[$k] = $val;
                }
            }
         if (!$lists = K::M('member/mystaff')->items($filter, null, $page, $limit, $count)) {
            $lists= array();
        }else{
            foreach($lists as $k=>$val){
                $staff_ids[$val['staff_id']] = $val['staff_id'];
            }
            $staffs = K::M('staff/staff')->items_by_ids($staff_ids);
            $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']][] = $cates[$val['cate_id']]['title'];
                }
            }
            $lng = $this->GP('lng');
            $lat = $this->GP('lat');
            if(!$lng || !$lat){
                $lng = $this->request['UxLocation']['lng'];
                $lat = $this->request['UxLocation']['lat'];
            }
            foreach($staffs as $k=>$val){
                if($cate_lists[$val['staff_id']]){
                    $staffs[$val['staff_id']]['skill'] = $cate_lists[$val['staff_id']];
                }else{
                    $staffs[$val['staff_id']]['skill'] = array();
                }
                $staffs[$k]['dateline'] = date('Y-m-d',$val['dateline']);
                $staffs[$k]['rate'] = round($val['praise_num']/$val['comments'],2)*100;
                $staffs[$k]['d'] = K::M('helper/round')->getdistances($val['lng'], $val['lat'], $lng, $lat);  //距离
            }
            $items = array();
            foreach($staffs as $k=>$val){
                $items[$k] = $this->filter_fields('staff_id,city_id,mobile,orders,name,face,d,rate,skill,title,lat,lng,score,comments,praise_num,year_num', $val);
            }
        }
        $this->msgbox->set_data('data', array('items' => array_values($items)));
    }
    
    
    public function cancle($staff_id, $uid)
    {
        if($staff_id && $uid){
           if(K::M('member/mystaff')->del($uid, $staff_id)){

                $this->tmpl = 'ucenter/mystaff.html';
           }else{
                $this->msgbox->add('取消关注失败！', 302);
           }
        }else{
            $this->msgbox->add('参数错误！', 301);
        }
        $this->tmpl = 'ucenter/mystaff.html';
    }
}