<?php namespace Place\Lib;
use Dever;
use Place;
class Address
{
    # 获取默认地址
    public function getDefault($type = 1)
    {
        $where['uid'] = Place::$uid;
        $where['type'] = $type;
        $info = Dever::db('address', 'place')->find($where);
        if (!$info) {
            unset($where['type']);
            $info = Dever::db('address', 'place')->find($where);
        }
        if ($info && Dever::project('area')) {
            $info = $this->handleInfo($info);
        }
        return $info;
    }

    # 获取某个收货地址
    public function getInfo($id, $uid)
    {
        $where['id'] = $id;
        $where['uid'] = $uid;
        $info = Dever::db('address', 'place')->find($where);
        if ($info && Dever::project('area')) {
            $info = $this->handleInfo($info);
        }
        return $info;
    }

    # 获取地址列表
    public function getList()
    {
        $where['uid'] = Place::$uid;
        $where['type'] = array('<', 3);
        $data = Dever::db('address', 'place')->select($where);
        if ($data && Dever::project('area')) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->handleInfo($v);
            }
        }
        return $data;
    }

    public function handleInfo($data)
    {
        if (is_array($data['area'])) {
            $data['area'] = implode(',', $data['area']);
        }
        $data['area_string'] = Dever::load('data', 'area')->string($data['area']);
        $data['province_name'] = $data['city_name'] = $data['county_name'] = '';

        if ($data['county_id']) {
            $info = Dever::db('county', 'area')->find($data['county_id']);
            if ($info) {
                $data['county_name'] = $info['name'];
            }
        }
        if ($data['city_id']) {
            $info = Dever::db('city', 'area')->find($data['city_id']);
            if ($info) {
                $data['city_name'] = $info['name'];
            }
        }
        if ($data['province_id']) {
            $info = Dever::db('province', 'area')->find($data['province_id']);
            if ($info) {
                $data['province_name'] = $info['name'];
            }
        }
        $data['full'] = $data['name'] . ',' . $data['phone'] . ',' . $data['area_string'] . ',' . $data['address'];
        return $data;
    }

    # 添加或者更新地址
    public function update($id, $type = 1, $phone, $name, $province = '', $city = '', $county = '', $address = '')
    {
        $update['uid'] = Place::$uid;
        if ($name) {
            $update['name'] = $name;
        }
        if ($phone) {
            $update['phone'] = $phone;
        } else {
            Dever::alert('请输入联系方式');
        }
        if ($province) {
            $update['province_id'] = $province;
        } else {
            Dever::alert('请选择省份');
        }
        if ($city) {
            $update['city_id'] = $city;
        } else {
            Dever::alert('请选择城市');
        }
        if ($county) {
            $update['county_id'] = $county;
        } else {
            Dever::alert('请选择区县');
        }
        if ($province && $city && $county) {
            $update['area'] = $province . ',' . $city . ',' . $county;
        }
        $update['type'] = $type;
        if ($address) {
            $update['address'] = $address;
        }
        if ($type == 1) {
            Dever::db('address', 'place')->update(array('type' => 1), array('type' => 2));
        }
        if ($id) {
            Dever::db('address', 'place')->update(array('id' => $id, 'uid' => Place::$uid), $update);
        } else {
            $id = Dever::db('address', 'place')->insert($update);
        }
        return $id;
    }

    # 删除
    public function delete($id)
    {
        return Dever::db('address', 'place')->delete(array('id' => $id, 'uid' => Place::$uid));
    }
}