<?php
namespace Clue\lib;
use Dever;
Class Info
{
	#更新次数
	public function getNum($id)
	{
		$html = array();
		$info = Dever::db('clue/info')->find($id);
		
		if ($info['gdate']) {
			$info['gdate'] = '<br/>'.date('Y.m.d H:i',$info['gdate']);
		} else {
			$info['gdate'] = '';
		}
		if ($info['area']) {
			$area = Dever::load("area/api.string", $info['area']);
			if (strpos($area,',')!=false) {
				$area = str_replace(',', '/', $area);
			}
		}
		if ($info['follow'] ) {
			$branch = explode(',',$info['follow']);
			// print_R($branch);die;
			$html['people'] = $this->group_admin($info['company_id'],$branch[0],$branch[1]);
		}
		
		$html['area'] = $area;
		$html['time'] = date('Y.m.d H:i',$info['cdate']).$info['gdate'];
		$html['user'] = $info['name'].'<br/>'.$info['mobile'];
		$data= Dever::db('clue/follow')->getCount(array('info_id'=>$id));
		$follow = Dever::db('clue/follow')->find(array('info_id'=>$id));
		if ($follow){
			if ($follow['xdate']) {
				$html['xdate'] = date('Y.m.d H:i',$follow['xdate']);
			}
		}
		if ($info['num'] && $info['num'] > 0) {
			$html['num'] = $info['num'];
		} else {
			$html['num'] = $data;
		}
		return $html;

	}
	public function insertInfo($id,$name,$data)
	{
		$mobile = Dever::param('mobile',$data);
		$info = Dever::db('clue/info')->find(array('mobile'=>$mobile));
		if ($info){
			Dever::alert('手机号已存在，不能重复提交');
		}
	}
	#更新线索表
	public function updateInfo($id,$name,$data)
	{
		Dever::config('base')->hook = true;
		$status_id = Dever::param('status_id',$data);
		$follow = Dever::param('follow',$data);
		$where['wechat'] = Dever::param('wechat',$data);
		$where['name'] = Dever::param('name',$data);
		$where['status_id'] = Dever::param('status_id',$data);
		$where['userset_id'] = Dever::param('userset_id',$data);
		$info = Dever::db('clue/info')->find($id);
		$where['where_id'] = $id;
		if (!$info['day'] || $info['day'] <=0) {
			$where['day'] = time();
		}
		if ($where['status_id'] == 1) {
				$where['type'] = 1;
			} elseif ($where['status_id'] == 2) {
				$where['type'] = 2;
			} elseif ($where['status_id'] > 2 && $where['status_id'] <= 4) {
				$where['type'] = 3;
			} else if ($where['status_id'] == 5) {
				$where['type'] = 4;
			} else if ($where['status_id'] == 6) {
				$where['type'] = 5;
			} else if ($where['status_id'] == 7) {
				$where['type'] = 6;
			}

		if ($follow && $follow[0]>0 ) {
			$fgroup_admin = implode(',',$follow);
			$follow_info = Dever::db('clue/follow')->find(array('info_id'=>$id,'follow_admin_id'=>$fgroup_admin));
			if ($follow_info) {
				$where['type'] = 3;
			} else {
				$where['type'] = 2;
				if ($where['status_id'] == 3 || $where['status_id'] == 4){
					$where['type'] = 3;
				} elseif ($where['status_id'] == 7) {
					$where['type'] = 6;
				} elseif($where['status_id'] == 5) {
					$where['type'] = 4;
				} elseif ($where['status_id'] == 6) {
					$where['type'] = 5;
				}
			}
			$where['follow'] = $follow;
			if ($info['follow_group_id'] && $info['follow_group_id']!=$follow[0]) {
				$where['follow_group_id'] = $follow[0];
			} elseif (!$info['follow_group_id'] || $info['follow_group_id']<=0) {
				$where['follow_group_id'] = $follow[0];
			}
			if ($info['follow_admin_id'] && $info['follow_admin_id']!=$follow[1]) {
				$where['follow_admin_id'] = $follow[1];
			} elseif (!$info['follow_group_id'] || $info['follow_admin_id']<=0) {
				$where['follow_admin_id'] = $follow[1];
			}
			if (!$info['create_group_id'] || $info['create_group_id']<=0) {
				$where['create_group_id'] = $follow[0];
			} 
			if (!$info['create_admin_id'] || $info['create_admin_id']<=0) {
				$where['create_admin_id'] = $follow[1];
			} 
			
		}
		if ($status_id && ($status_id == 5 || $status_id == 6)) {
			$where['fdate'] = time();
		}
		if ($info) {
			if ($follow && $follow[0]<0) {
				$where['type'] = 2;
				if ($where['status_id'] == 3 || $where['status_id'] == 4){
					$where['type'] = 3;
				} elseif ($where['status_id'] == 7) {
					$where['type'] = 6;
				} elseif($where['status_id'] == 5) {
					$where['type'] = 4;
				} elseif ($where['status_id'] == 6) {
					$where['type'] = 5;
				}
				$auth = Dever::load("manage/auth.info");
				$admin_group = Dever::db('manage/admin_group')->all(array('admin_id'=>$auth['id']));
				foreach ($admin_group as $k => $v) {
					$admin[$v['group_id']] = $v['group_id'];
				}
				foreach ($admin as $k1 =>$v1) {
					$group = Dever::db('manage/group')->find(array('company_id'=>$info['company_id'],'id'=>$v1));
					if ($group) {
						$where['follow'] = $group['id'].','.$auth['id'];
						if ($info['follow_group_id'] && $info['follow_group_id']!=$group['id']) {
							$where['follow_group_id'] = $group['id'];
						} elseif (!$info['follow_group_id'] || $info['follow_group_id'] <=0) {
							$where['follow_group_id'] =  $group['id'];
						}
						if ($info['follow_admin_id'] && $info['follow_admin_id']!=$auth['id']) {
							$where['follow_admin_id'] = $auth['id'];
						} elseif (!$info['follow_group_id'] || $info['follow_group_id'] <=0) {
							$where['follow_admin_id'] =  $auth['id'];
						}
						if (!$info['create_group_id'] || $info['create_group_id']<=0) {
							$where['create_group_id'] = $group['id'];
						} 
						if (!$info['create_admin_id'] || $info['create_admin_id']<=0) {
							$where['create_admin_id'] = $auth['id'];
						} 
					}
				}
				// $w['admin_id'] = $where['follow'];
				// $w['info_id'] = $id;
				// if ($where['type'] == 3) {
				// 	$w['status'] = 3;
				// } else {
				// 	$w['status'] = 2;
				// }
				// $ids = Dever::db('clue/follow')->insert($w);
				// if ($ids) {
				// 	$where['follow_id'] = $ids;
				// } 
			}
			if (isset($where['follow']) && $where['follow']) {
				$where['follow'] = $where['follow'];
			} else {
				$where['follow'] = $info['follow'];
			}
			$w['follow_admin_id'] = $where['follow'];
				$w['info_id'] = $id;
				// if ($where['type'] == 3) {
				// 	$w['status'] = 3;
				// } else {
				// 	$w['status'] = 2;
				// }
				$w['status'] = 2;
				$ids = Dever::db('clue/follow')->insert($w);
				if ($ids) {
					$where['follow_id'] = $ids;
				} 
			if (!$info['clue_num']) {
				$where['clue_num'] = $this->getClueNum();
			}
			Dever::db('clue/info')->update($where);
		}
	}
	#获取线索编号
	public function getClueNum()
	{
	    $where['clue_num'] = Dever::order('X');
	    $state = Dever::db('clue/info')->one($where);
	    if (!$state) {
	        return $where['clue_num'];
	    } else {
	        return $this->getClueNum();
	    }
	}
	#通过跟进人信息修改线索表
	public function updateFollow($id,$name,$data)
	{
		Dever::config('base')->hook = true;
		$follow = Dever::db('clue/follow')->find($id);
		$info = Dever::db('clue/info')->find($follow['info_id']);
		if ($info) {
			$where['where_id'] = $info['id'];
			$where['follow_id'] = $id;
			$where['gdate'] = time();
			$where['day']= $follow['xdate'];
			$where['num'] = $info['num']+1;
			$where['type'] = 3;
			Dever::db('clue/info')->update($where);
			$w['where_id'] = $id;
			$w['follow_admin_id'] = $info['follow'];
			Dever::db('clue/follow')->update($w);
		}

		$auth = Dever::load('manage/auth.info');
		
		$admin = Dever::db('manage/admin')->find($auth['id']);

		$group_id = explode(',',$admin['group']);
		if ($group_id) {
			foreach($group_id as $k => $v) {
				$group = Dever::db('manage/group')->find(array('company_id'=>$info['company_id'],'id'=>$v));
			}
		}
		$branch_id = explode(',',$info['follow']);
		$i['crate_admin_id'] = $admin['id'];
		$i['crate_group_id'] = $group['id'];
		$i['crate_group_admin'] = $admin['id'].','.$group['id'];
		$i['follow_id'] = $id;
		$i['follow_group_admin'] = $info['follow'];
		$i['follow_group_id'] = $branch_id[0];
		$i['follow_admin_id'] = $branch_id[1];
		$i['info_id'] = $info['id'];
		Dever::db('clue/info_log')->insert($i);
	}
	#线索详情页
	public function show_api()
	{
		$id = Dever::input('id');
		$data['info'] = Dever::db('clue/info')->find($id);
		$admin = Dever::db('manage/admin')->find($data['info']['audit_admin']);
		$data['info']['admin_name'] = '';
		if ($admin) {
			$data['info']['admin_name'] = $admin['username'];
		}
		
		$data['info']['source_name'] = '';
		if ($data['info']['source']) {
			$source = Dever::db('clue/source')->find($data['info']['source']);
			if ($source) {
				$data['info']['source_name'] = $source['name'];
			}
		}
		$sex = Dever::db('clue/info')->config['config_sex'];
		$data['info']['sex_name'] = Dever::status($sex,$data['info']['sex']);
		$data['info']['area_name'] = '';
		if ($data['info']['area']) {
			$area = Dever::load("area/api.string", $data['info']['area']);
			if ($area) {
				$area = explode(',',$area);
				if (isset($area[1]) && $area[1]) {
					$data['info']['area_name'] = $area[0].'/'.$area[1];
				} else {
					$data['info']['area_name'] = $area[0];
				}
			}
		}
		// guanli.uat.churenyiliao.com/package/manage/?l=project/database/list&project=work&table=info&menu=work&search_option_state=1&search_option_dever_auth=2&menu_id=347
		$data['info']['status_name'] = '';
		if($data['info']['status_id']) {
			$status = Dever::db('clue/sale_status')->find($data['info']['status_id']);
			if ($status) {
				$data['info']['status_name'] = $status['name'];
			}
		}
		$userset = Dever::db('clue/user_set')->find($data['info']['userset_id']);
		if ($userset) {
			$data['info']['userset_name'] = $userset['name'];
		}
		$data['follow'] = Dever::db('clue/follow')->getAll(array('info_id'=>$id));
		$config_type = Dever::db('clue/follow')->config['config_type'];
		$people = array();
		foreach ($data['follow'] as $k => $v) {
			$admin_id = explode(',',$v['follow_admin_id']);
			$data['follow'][$k]['group_name'] = '';
			if (isset($admin_id[0]) && $admin_id[0]) {
				$group = Dever::db('manage/group')->find(array('id'=>$admin_id[0],'company_id'=>$data['info']['company_id']));
				if ($group) {
					$data['follow'][$k]['group_name'] = $group['name'];
				}
			}
			$data['follow'][$k]['admin_name'] = '';
			if (isset($admin_id[1]) && $admin_id[1]) {
				$admin = Dever::db('manage/admin')->find(array('id'=>$admin_id[1]));
				if ($admin) {
					$data['follow'][$k]['admin_name'] = $admin['username'];
				}
			}
			if ($data['follow'][$k]['admin_name']) {
				$data['follow'][$k]['group_name'] = $data['follow'][$k]['group_name'].' / '.$data['follow'][$k]['admin_name'];
			}
			$data['follow'][$k]['type_name'] = Dever::status($config_type,$v['type']);
			$data['follow'][$k]['cdate'] = date('Y-m-d H:i',$v['cdate']);
			$data['follow'][$k]['xdate'] = date('Y-m-d H:i',$v['xdate']);
			if ($v['pic']) {
				$data['follow'][$k]['pic'] = explode(',',$v['pic']);
			} else {
				$data['follow'][$k]['pic'] = '';
			}
			
			
			if (!$v['desc']) {
				unset($data['follow'][$k]);
			}
		}
		$data['info']['people'] = '';
		$branch = explode(',',$data['info']['follow']);
		$data['info']['people'] = $this->group_admin($data['info']['company_id'],$branch[0],$branch[1],1);
		
		$follow = 0;
		$follow = Dever::db('clue/follow')->getCount(array('info_id'=>$id));
		if ($data['info']['num'] && $data['info']['num']>0) {
			$data['info']['cishu'] = $data['info']['num'].'次'; 
		} else {
			$data['info']['cishu'] = $follow.'次';
		}
		
		$f = Dever::db('clue/follow')->getOne(array('info_id'=>$id));
		// print_R($f);die;
		if ($f) {
			$data['info']['xdate'] = date('Y-m-d',$f['xdate']);
			$data['info']['day'] = floor((time()-$f['cdate'])/86400).'天';
		} else {
			$data['info']['xdate'] = '-';
			$data['info']['day'] = floor((time()-$data['info']['cdate'])/86400).'天';
		}
		$data['info']['cdate'] = date('Y-m-d H:i',$data['info']['cdate']);

		$data['xiu'] =  Dever::url('project/database/update?project=clue&table=info&where_id='.$id.'&col=wechat,qq,area,sex,age,trade,desc,remark', 'manage');
		if (Dever::load('manage/auth')->checkFunc('clue.info', 'infojichu', '基础信息修改')) {
			$data['jichu'] =  Dever::url('project/database/update?project=clue&table=info&where_id='.$id.'&col=name,mobile,source', 'manage');
		}
		
		$data['gen'] =  Dever::url('project/database/update?project=clue&table=follow&search_option_info_id='.$id, 'manage');
		$data['pai'] =  Dever::url('project/database/update?project=clue&table=info&where_id='.$id.'&col=follow', 'manage');
		$data['ke'] =  Dever::url('project/database/update?project=clue&table=info&where_id='.$id.'&col=userset_id', 'manage');
		$data['status'] =  Dever::url('project/database/update?project=clue&table=info&where_id='.$id.'&col=status_id,status_desc', 'manage');
		return Dever::render('index', $data);
	}

	public function group_admin($company_id,$group_id,$admin_id,$type=false)
	{
		$group = Dever::db('manage/group')->find(array('company_id'=>$company_id,'id'=>$group_id));
		$admin = Dever::db('manage/admin')->find($admin_id);
		if ($type == 1) {
			return $group['name'].' <br/> '.$admin['username'];
		} else {
			return $group['name'].' / '.$admin['username'];
		}
		
	}
	public function source_Num($id)
	{
		$data = Dever::db('clue/info')->state(array('source'=>$id,'out_type'=>2));
		$html = array();
		$string = '';
		
		$url = Dever::url('project/database/list?project=clue&table=info&search_option_source='.$id.'&search_option_out_type=2&[refer]', 'manage');
        if (count($data) > 0) {
            $string = '<a href="'.$url.'">（查看）</a>';
        }
		return count($data).$string;
	}
	public function insertSell($id,$name,$data) {
		$group = Dever::param('group',$data);
		
		if ($group) {
			$res = Dever::db('clue/salepeople')->find(array('group'=>$group));
			if ($res && isset($id['where_id']) && $id['where_id']){
				if ($res['id'] !=  $id['where_id']){
					Dever::alert('销售员不可重复提交');
				} 
			} elseif ($res) {
				Dever::alert('部门已存在');
			}
		}
	}
	public function updateSell($id,$name,$data)
	{
		$group = Dever::param('group',$data);
		if ($group) {
			if (isset($group[0]) && $group[0]) {
				$w['where_id'] = $id;
				$w['group_id'] = $group[0];
				Dever::db('clue/salepeople')->update($w);
			}
		}
	}
	public function back_people_api($id)
	{
		Dever::config('base')->hook = true;
		$data = Dever::db('clue/salepeople')->find($id);
		$branch = Dever::db('work/branch')->find(array('group_id'=>$data['group_id']));
		$admin_id = explode(',',$branch['group']);
		if ($data['group']!=$branch['group']) {
			$info = Dever::db('clue/info')->state(array('follow'=>$data['group']));
			if ($info) {
				foreach ($info as $k =>$v) {
					$where['where_id'] = $v['id'];
					$where['follow'] = $branch['group'];
					$where['follow_group_id'] = $branch['group_id'];
					$where['follow_admin_id'] = $admin_id[1];
					Dever::db('clue/info')->update($where);
					// print_R($v);die;
					// echo dever::sql();die;
				}
			}
		}
		return 'ok';
		// print_R($branch);die;
		// print_R($data);die;
	}
}