<?php
namespace Clue\Lib;

use Dever;
Class Manage 
{
	#销售员
	public function getSale($id)
	{
		$data = Dever::db('clue/salepeople')->find($id);
		if ($data) {
			$data['group'] = explode(',',$data['group']);
			$group = Dever::db('manage/group')->find($data['group'][0]);
			$admin = Dever::db('manage/admin')->find($data['group'][1]);
			$html = array();
			$html['name'] = $admin['username'].'<br/>'.$admin['email'].'<br/>'.$group['name'];
			return $html;
		}
	}

	#线索列表导出
	public function out_info($data)
	{
		$header = array('客户姓名','手机号','客户等级','跟进次数','所在城市','跟进人','跟进状态','最近跟进时间');
		$body = array();
		$config_type = Dever::db('clue/info')->config['config_type'];
		foreach ($data as $k => $v) {
			$user_set = Dever::db('clue/user_set')->find($v['userset_id']);
			$userset_name = '';
			if ($user_set) {
				$userset_name = $user_set['name'];
			}
			$num = '';
			if ($v['num'] && $v['num'] > 0) {
				$num = $v['num'];
			} else {
				$num = Dever::db('clue/follow')->getCount(array('info_id'=>$v['id']));
			}
			$area = '';
			if ($v['area']) {
				$area = Dever::load("area/api.string", $v['area']);
				if (strpos($area,',')!=false) {
					$area = str_replace(',', '/', $area);
				}
			}
			$branch = '';
			if ($v['branch_id']) {
				$v['branch_id'] = explode(',',$v['branch_id']);
				if ($v['branch_id'][0] > 0) {
					$branch = Dever::load('clue/lib/info')->group_admin($v['company_id'],$v['branch_id'][0],$v['branch_id'][1]);
				}
			}
			$type = '';
			$type = Dever::status($config_type,$v['type']);
			$gdate = '';
			if ($v['gdate']) {
				$gdate = date('Y-m-d H:i',$v['gdate']);
			}
			$d = array
			(
				$v['name'],
				$v['mobile'],
				$userset_name,
				$num,
				$area,
				$branch,
				$type,
				$gdate,
			);
			$body[] = $d;
		}
		$file = Dever::input('excel_name');
    	return Dever::excelExport($body, $header, $file);
	}
	public function out_muban_api()
	{
		$header = array('客户姓名*','手机号*','客户等级*','微信号','QQ号','所在城市','性别','年龄','行业','线索状态（默认为未分派）');
		$body = array();
		
		$file = "下载模板";
    	return Dever::excelExport($body, $header, $file);
	}
	public function updateImport($id,$name,$data) {
		$where = array();
		$source = Dever::param('source',$data);
		if ($source) {
			$sou = Dever::db('clue/source')->find(array('name'=>$source));
			if ($sou){
				$where['source'] = $sou['id'];
			}
		}
		$file = Dever::param('file',$data);
		$file = Dever::local($file);
		$res = Dever::excelImport($file,0);
		if ($res) {
			foreach ($res as $k => $v) {
				if ($k >= 2) {
					$where['name'] = $v['A'];
					$where['mobile'] = $v['B'];
					$where['clue_num'] = Dever::load('clue/lib/info')->getClueNum();
					if ($v['C']) {
						$userset = Dever::db('clue/user_set')->find(array('name'=>$v['C']));

						if ($userset ) {
							$where['userset_id'] = $userset['id'];
						}
					}
					if ($v['D']) {
						$where['wechat'] = $v['D'];
					}
					if ($v['E']) {
						$where['qq'] = $v['E'];
					}
					$provice = '';
					$city_id = '';
					if ($v['F']) {
						$area = explode('/',$v['F']);
						if ($area[0]) {
							$pro = Dever::db('area/province')->find(array('name'=>$area[0]));
							if ($pro) {
								$province = $pro['id'];
							}
						}
						if(isset($area[1]) && $area[1]) {
							$city = Dever::db('area/city')->find(array('name'=>$area[1]));
							if ($city) {
								$city_id = ','.$city['id'];
							}
						}
						$where['area'] = $province.$city_id;
					}
					if ($v['G']) {
						if ($v['G'] == '未知') {
							$where['sex'] = 1;
						} else if ($v['G'] == '男') {
							$where['sex'] = 2;
						} else if ($v['G'] == '女') {
							$where['sex'] = 3;
						}
					}
					if ($v['H']) {
						$where['age'] = $v['H'];
					}
					if ($v['I']) {
						$where['trade'] = $v['I'];
					}
					if ($v['J']) {
						if ($v['J'] == '未分派') {
							$where['status_id'] = 1;
						} elseif ($v['J'] == '初期接触阶段') {
							$where['status_id'] = 2;
						} elseif ($v['J'] == '潜在客户洽谈') {
							$where['status_id'] = 3;
						} elseif ($v['J'] == '定金已付阶段') {
							$where['status_id'] = 4;
						} elseif ($v['J'] == '成交完成') {
							$where['status_id'] = 5;
						} elseif ($v['J'] == '取消关闭') {
							$where['status_id'] = 6;
						} elseif ($v['J'] == '搁置中	') {
							$where['status_id'] = 7;
						}
					} else {
						$where['status_id'] = 1;
					}
					
					$group_id = '';
					$admin_id = '';
					$company = Dever::load('manage/company.get');
					$admin = Dever::load("manage/auth.info");
					$admin = Dever::db('manage/admin')->find($admin['id']);
					if ($admin) {
						$admin['group'] = explode(',',$admin['group']);
						foreach ($admin['group'] as $k =>$v) {
							$group = Dever::db('manage/group')->find(array('company_id'=>$company,'id'=>$v));
						}
						if ($group) {
							$group_id = $group['id'];
							$where['follow_group_id'] = $group['id'];
							$where['create_group_id'] = $group['id'];
						}
						$admin_id = $admin['id'];
						$where['follow_admin_id'] = $admin['id'];
						$where['create_admin_id'] = $admin['id'];
						$where['follow'] = $group_id.','.$admin_id;
					}
					$where['out_type'] = 2;
					$info = Dever::db('clue/info')->find(array('mobile'=>$where['mobile'],'clear'=>true));
					$where['clear'] = true;
					if (!$info && $where) {

						Dever::db('clue/info')->insert($where);
					}
					
				}
			}

		}
	}
	/*
	public function updateImport($id,$name,$data) {
		$where = array();
		$source = Dever::param('source',$data);
		if ($source) {
			$sou = Dever::db('clue/source')->find(array('name'=>$source));
			if ($sou){
				$where['source'] = $sou['id'];
			}
		}
		$file = Dever::param('file',$data);
		$file = Dever::local($file);
		$res = Dever::excelImport($file,0);
		if ($res) {
			foreach ($res as $k => $v) {
				if ($k >= 2) {
					$where['name'] = $v['A'];
					$where['mobile'] = $v['B'];
					$where['clue_num'] = Dever::load('clue/lib/info')->getClueNum();
					if ($v['C']) {
						$userset = Dever::db('clue/user_set')->find(array('name'=>$v['C']));

						if ($userset ) {
							$where['userset_id'] = $userset['id'];
						}
					}
					$where['num'] = $v['D'];
					$provice = '';
					$city_id = '';
					if ($v['E']) {
						$area = explode('/',$v['E']);
						if ($area[0]) {
							$pro = Dever::db('area/province')->find(array('name'=>$area[0]));
							if ($pro) {
								$province = $pro['id'];
							}
						}
						if(isset($area[1]) && $area[1]) {
							$city = Dever::db('area/city')->find(array('name'=>$area[1]));
							if ($city) {
								$city_id = ','.$city['id'];
							}
						}
						$where['area'] = $province.$city_id;
					}
					if ($v['F']) {
						$group_id = '';
						$admin_id = '';
						#跟进人
						$f=explode('/',$v['F']);
						if (isset($f[0]) && $f[0]) {
							$group = Dever::db('manage/group')->find(array('name'=>$f[0]));
							if ($group){
								$group_id = $group['id'];
								$where['follow_group_id'] = $group['id'];
								$where['create_group_id'] = $group['id'];
							}
						}
						if (isset($f[1]) && $f[1]) {
							$f[1] = trim($f[1]);
							$admin = Dever::db('manage/admin')->find(array('username'=>$f[1]));
							if ($admin){
								$admin_id = ','.$admin['id'];
								$where['follow_admin_id'] = $admin['id'];
								$where['create_admin_id'] = $admin['id'];
							}
						}
						$where['follow'] = $group_id.$admin_id;
					}
					if ($v['G']) {
						#跟进状态
						if ($v['G'] == '待跟进') {
							$where['type'] = 1;
						}
						if ($v['G'] == '未跟进') {
							$where['type'] = 2;
						}
						if ($v['G'] == '已跟进') {
							$where['type'] = 3;
						}
						if ($v['G'] == '已完成') {
							$where['type'] = 4;
						}
						if ($v['G'] == '已关闭') {
							$where['type'] = 5;
						}
					}
					if ($v['H']) {
						#跟进时间
						$where['gdate'] = strtotime($v['H']);
					}
					$where['out_type'] = 2;
					$info = Dever::db('clue/info')->find(array('mobile'=>$where['mobile'],'clear'=>true));
					// $where['clear'] = true;
					if (!$info && $where) {

						Dever::db('clue/info')->insert($where);
					}
					
				}
			}

		}
	}*/
	public function group($id)
	{
		$data = Dever::db('manage/group')->find($id);
		return $data['name'];
	}
	
}