<?php

namespace Agent\Src;

use Dever;

class Area
{
    private $default = array
    (
        'value' => -1,
        'name' => '不限',
    );

    private $search_default = array
    (
        'value' => -1,
        'name' => '地区选择',
    );

	/**
     * 获取地区数据
     *
     * @return mixed
     */
    public function get()
    {
        $mid = Dever::load('passport/user')->check(false);
        if ($mid && $mid > 0) {
            $member = Dever::db('agent/member')->find($mid);
            $role = Dever::db('setting/role')->find($member['role']);
            if ($role['type'] == 2) {
                $temp = explode(',', $member['area']);
                $city = Dever::db('area/city')->find($temp[1]);
                if ($city) {
                    $city_level = $city['level_id'];
                }
                if ($role['area'] == 2 && isset($temp[2])) {
                    # 如果是区县，只能升级为更高等级的城市和区县
                    $county = Dever::db('area/county')->find($temp[2]);
                    if ($county) {
                        $county_level = $county['level'];
                    }
                } elseif ($role['area'] == 3 && isset($temp[3])) {
                    # 如果是街道，只能升级为更高等级的城市和区县
                    $town = Dever::db('area/town')->find($temp[3]);
                    if ($town) {
                        $town_level = $town['type'];
                    }
                }
            }
        }

        # 联动总数，默认到县区
        $level_total = Dever::input('level_total', 3);

        # 当前联动级别
        $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'] = '城市选择';
            } elseif ($level_num == 3) {
                $default['name'] = '县区选择';
            } elseif ($level_num == 4) {
                $default['name'] = '街道选择';
            } else {
                $default['name'] = '社区选择';
            }
        } else {
            $default = $this->default;
        }

        # 四级联动
        if ($level_num == 1) {
            $data = Dever::db('area/province')->getAll();
        } elseif ($level_num == 2) {
            $where = array('province_id' => $level_id);
            if (isset($city_level)) {
                $where['level_id'] = $city_level;
            }
            $where['status'] = 1;
            $data = Dever::db('area/city')->getAll($where);
        } elseif ($level_num == 3) {
            $where = array('city_id' => $level_id);
            if (isset($county_level)) {
                $where['level'] = $county_level;
            }
            $where['status'] = 1;
            $data = Dever::db('area/county')->getAll($where);
            /*
            if (isset($town_level)) {
                foreach ($data as $k => $v) {
                    $where = array('county_id' => $v['value']);
                    $where['type'] = $town_level;
                    $town = Dever::db('area/town')->getTotal($where);
                    if ($town <= 0) {
                        unset($data[$k]);
                    }
                }
            }*/
        } elseif ($level_num == 4) {
            $where = array('county_id' => $level_id);
            if (isset($town_level)) {
                $where['type'] = $town_level;
            }
            $where['status'] = 1;
            $data = Dever::db('area/town')->getAll($where);
        } else {
            $data = Dever::db('area/village')->getAll(array('town_id' => $level_id));
        }

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

        if ($level_search || $level_num > 1) {
            array_unshift($data, $default);
        }
        
        $result['level_total'] = $level_total;
        $result['list'] = $data;
        return $result;
    }
}
