<?php
namespace Clue\Lib;

use Dever;
Class Salepeople
{
	private $default = array
    (
        'value' => -1,
        'name' => '不选择',
    );
    private $search_default = array
    (
        'value' => -1,
        'name' => '部门',
    );
	public function get_api()
    {
        $type = Dever::input('type', 2);
        # 联动总数，默认到部门
        $level_total = Dever::input('level_total', 1);

        # 当前联动级别
        $level_num = Dever::input('level_num');

        # 一般为id
        $level_id = Dever::input('level_id');

        # 是否是搜索列表页
        $level_search = Dever::input('level_search');
        if ($level_search) {
            $default = $this->search_default;
            if ($level_num == 1) {
                $default['name'] = '部门选择';
            } elseif ($level_num == 2) {
                $default['name'] = '组员选择';
            } 
        } else {
            $default = $this->default;
        }
        if ($level_num == 1) {
            $data = $this->group();

        } elseif ($level_num == 2) {
        	$data = $this->group_admin($level_id);
        } 

        if (!$data) {
            Dever::alert('error');
        }

        if ($level_search || $level_num >= 1) {
            array_unshift($data, $default);
        }
        if ($level_num == 2) {
            unset($data[0]);
        }
        $result['level_total'] = $level_total;
        $result['list'] = $data;
        return $result;
    }
    public function group(){
        $auth = Dever::load("manage/auth.info");
        $company = Dever::load('manage/company.get');
        $admin = Dever::db('manage/admin')->find($auth['id']);
        $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) {
            $data[] = Dever::db('clue/salepeople')->find(array('group_id'=>$group['id']));
        }
        // print_R($data);die;
    	// $data = Dever::db('clue/salepeople')->getState();
    	if ($data) {
            // $group = Dever::db('manage/group')->find(array('id'=>$data['group_id']));
            // $data[]['name'] = $group['name'];
            // $data[]['value'] = $data['group_id'];
            // print_R($data);die;
    		foreach ($data as $k => $v) {
	    		$data[$k]['value'] = $v['group_id'];
	    		$group = Dever::db('manage/group')->find(array('id'=>$v['group_id']));
	    		$data[$k]['name'] = $group['name'];
	    	}
    	} else {
    		$data = array();
    		$data['value'] = -1;
    	}
    	// print_R($data);die;
    	return $data;
    }
    public function group_admin($level_id){
    	$data = Dever::db('clue/salepeople')->state(array('group_id'=>$level_id));
    	foreach ($data as $k =>$v) {
    		$group = explode(',',$v['group']);
    		if (isset($group[1]) && $group[1]) {
    			$admin = Dever::db('manage/admin')->find($group[1]);
    			if ($admin) {
    				$data[$k]['value'] = $admin['id'];
    				$data[$k]['name'] = $admin['username'];
    			}
    		}
    	}
    	return $data;
    }
}