<?php
/**
 * 收货地址管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */


namespace Admin\Controller;
class MemberAddressController extends BaseController{
    public function index(){
        $name = CONTROLLER_NAME;
        $map=array();
        $uid=trim(I('param.uid'));
        $keyword=trim(I('param.keyword'));
        if($uid)$map['uid']=$uid;
        if($keyword){            
            $whereor['consignee']=array('like','%'.$keyword.'%');
            $whereor['address']=array('like','%'.$keyword.'%');
            $whereor['tel']=array('like','%'.$keyword.'%');
            $whereor['mobile']=array('like','%'.$keyword.'%');
            $whereor['sign_building']=array('like','%'.$keyword.'%');
            $whereor['best_time']=array('like','%'.$keyword.'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }        
        $list = $this->lists($name,$map);
        $this->assign('list', $list);
		$this->assign('id', $uid);
        $this->display();
    }
    public function _before_add(){
        $this->assign('uid', I('uid'));
    }
    public function _before_update(){
        $_POST['province']=$_POST['area_sheng'];
        $_POST['city']=$_POST['area_shi'];
        $_POST['district']=$_POST['area_xian'];
        unset($_POST['area_sheng'],$_POST['area_shi'],$_POST['area_xian']);
    }
    public function update(){
        $name = CONTROLLER_NAME;
        $model = D($name);
        $jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME . '/index');
        $id = array_unique((array)I('id', 0));
        $id = is_array($id) ? implode(',', $id) : $id;
        $data = I('post.'); 
        if($data['status']==1){
            $map['uid']=$data['uid'];
            $mapdata['status'] = 0;
            $model->where($map)->save($mapdata);
        }
        $where['id']=$id;
        $res=$model->where($where)->save($data);
        $msg = array_merge(array('success' => L('EDIT_OK'), 'error' => L('EDIT_ERROR'), 'url' => $jumpUrl, 'ajax' => IS_AJAX), (array)$msg);
        if ($res !== false) {
            if (in_array(CONTROLLER_NAME, $this->cache_model)) {
                savecache(CONTROLLER_NAME);
            }
            $this->success($msg['success'], $msg['url'], $msg['ajax']);
        } else {
            $this->error($msg['error'], $msg['url'], $msg['ajax']);
        }
        
    }
    public function status(){//公共状态
		$mod = D(CONTROLLER_NAME);
		$id     = intval(I('id'));
		$values = $mod->where('id='.$id)->field("status,uid")->find();
        $data["status"]=($values["status"]+1)%2;
        $map['uid']=$values['uid'];
        $mapdata['status'] = 0;
        $mod->where($map)->save($mapdata);
        $res=$mod->where('id='.$id)->save($data);
		if(in_array(CONTROLLER_NAME,$this->cache_model)){savecache(CONTROLLER_NAME);}
		$this->ajaxReturn($data["status"]);
	}
}