<?php
namespace Agent\Lib;

use Dever;

class Address
{
    # 获取某个收货地址
    public function getOne($mid, $id)
    {
        $where['mid'] = $mid;
        $where['id'] = $id;
        $data = Dever::db('agent/member_address')->find($where);

        if ($data && Dever::project('area')) {
            $data = $this->getInfo($data);
        }

        return $data;
    }

    private function getInfo($data)
    {
        $data['address_info'] = $data['add_string'] = $data['address'] . $data['house_number'];
        $data['province_name'] = $data['city_name'] = $data['county_name'] = '';
        if ($data['county']) {

            $info = Dever::db('area/county')->find($data['county']);
            if ($info) {
                $data['county_name'] = $info['name'];

                $data['city'] = $info['city_id'];

                $info = Dever::db('area/city')->find($data['city']);
                $data['city_name'] = $info['name'];

                $data['province'] = $info['province_id'];

                $info = Dever::db('area/province')->find($data['province']);
                $data['province_name'] = $info['name'];

                //$data['address_info'] = $data['province_name'] . $data['city_name'] . $data['county_name'] .','. $data['address'] .','. $data['house_number'];
                $data['add_string'] = $data['province_name'] . $data['city_name'] . $data['county_name'] .','. $data['address'] .','. $data['house_number'] .','. $data['contact'] .','. $data['mobile'];
            }
        }  elseif ($data['city']) {

            $info = Dever::db('area/county')->find($data['city']);
            if ($info) {
                $data['county_name'] = $info['name'];

                $data['city'] = $info['city_id'];

                $info = Dever::db('area/city')->find($data['city']);
                $data['city_name'] = $info['name'];

                $data['province'] = $info['province_id'];

                $info = Dever::db('area/province')->find($data['province']);
                $data['province_name'] = $info['name'];

                //$data['address_info'] = $data['province_name'] . $data['city_name'] . $data['county_name'] .','. $data['address'] .','. $data['house_number'];
                $data['add_string'] = $data['province_name'] . $data['city_name'] . $data['county_name'] .','. $data['address'] .','. $data['house_number'] .','. $data['contact'] .','. $data['mobile'];
            }
            
        }
        if (!$data['county_name']) {
            $city = '';
            if (strstr($data['address'], '省')) {
                $temp = explode('省', $data['address']);
                $data['province_name'] = $temp[0] . '省';
                unset($temp[0]);
                $city = $temp[1];
            } elseif (strstr($data['address'], '市')) {
                $temp = explode('市', $data['address']);
                $data['province_name'] = $temp[0] . '市';
                $city = '';
                unset($temp[0]);
                $city = implode('市', $temp);
            } elseif (strstr($data['address'], '区')) {
                $temp = explode('区', $data['address']);
                $data['province_name'] = $temp[0] . '区';

                $city = '';
                unset($temp[0]);
                $city = implode('区', $temp);
            }

            if (strstr($city, '市')) {
                $temp = explode('市', $city);
                $data['city_name'] = $temp[0] . '市';
            }
            if (isset($temp[1])) {
                $data['county_name'] = $temp[1];
            }
        }

        return $data;
    }
}
