<?php
namespace Agent\Src;

use Dever;
use Agent\Lib\Core;

class Address extends Core
{
    protected $check = false;
    # 获取默认地址
    public function getDefaultAddress($mid)
    {
        $where['mid'] = $mid;
        $where['type'] = 2;
        $data = Dever::db('agent/member_address')->one($where);

        return $data;
    }

    # 获取某个收货地址
    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;
    }

    # 获取地址列表
    public function getAddress($mid)
    {
        $where['mid'] = $mid;
        $data = Dever::db('agent/member_address')->getList($where);

        if ($data && Dever::project('area')) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->getInfo($v);
            }
        }

        return $data;
    }

    private function getInfo($data)
    {
        $data['address_info'] = $data['add_string'] = $data['address'];
        $data['province_name'] = $data['city_name'] = $data['county_name'] = '';
        if ($data['province'] && $data['city'] && $data['county']) {
            $info = Dever::db('area/province')->find($data['province']);
            $data['province_name'] = $info['name'];

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

            $info = Dever::db('area/county')->find($data['county']);
            $data['county_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'];
        }

        return $data;
    }

    # 删除和恢复
    public function del($mid, $id, $state = 2)
    {
        $info = $this->getOne($mid, $id);
        if ($info) {
            $update['where_id'] = $info['id'];
            $update['state'] = $state;
            Dever::db('agent/member_address')->update($update);
        } else {
            Dever::alert('更新失败');
        }

        return 'ok';
    }

    # 添加或者更新地址接口
    public function up()
    {
        $mid = $this->uid;
        $id = Dever::input('id');
        $type = Dever::input('type', 2);
        $province = Dever::input('province');
        $city = Dever::input('city');
        $county = Dever::input('county');
        $contact = Dever::input('contact');
        $address = Dever::input('address');
        $country = Dever::input('country');
        $mobile = Dever::input('mobile');
        $sex = Dever::input('sex');
        $house_number = Dever::input('house_number');
        $tag = Dever::input('tag');

        return $this->upAddress($id, $mid, $type, $mobile, $contact, $province, $city, $county, $address, $country, $sex, $house_number, $tag);
    }

    # 添加或者更新地址
    public function upAddress($id, $mid, $type = 2, $mobile, $contact, $province = '', $city = '', $county = '', $address = '', $country = '', $sex = '', $house_number = '', $tag = '')
    {
        if ($contact) {
            $update['contact'] = $contact;
        }
        
    	if ($country) {
    		$update['country'] = $country;
    	}

        if ($mobile) {
            $update['mobile'] = $mobile;
        }

        if ($province) {
            $update['province'] = $province;
        }

        if ($city) {
            $update['city'] = $city;
        }

        if ($county) {
            $update['county'] = $county;
        }
        
        if ($province && $city && $county) {
	        $update['area'] = $province . ',' . $city . ',' . $county;
        }

        $update['type'] = $type;

        if ($address) {
            $update['address'] = $address;
        }
        if ($sex) {
            $update['sex'] = $sex;
        }
        if ($house_number) {
            $update['house_number'] = $house_number;
        }
        if ($tag) {
            $update['tag'] = $tag;
        }

        if ($type == 2) {
            Dever::db('agent/member_address')->updateType(array('where_type' => 2, 'set_type' => 1));
        }
        
        if ($id) {
            $update['where_id'] = $id;
            Dever::db('agent/member_address')->update($update);
        } else {
            $update['mid'] = $mid;
            $id = Dever::db('agent/member_address')->insert($update);
        }



        return $id;
    }
}
