<?php

/**
 * 供货商管理控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Admin\Controller;
class SuppliersController extends BaseController
{	
	public function _initialize(){
		parent::_initialize ();
		$admin = M('Admin')->field('id,username')->select();
		$this->assign('admin',$admin);
     /*   $is_auth=check_use_auth("Invoice");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }*/
   	}

    /**
     * 店铺的供货商列表
     */
    public function index_shops(){
        $id=I('get.id');
        $this->assign('id',$id);
        $suppliers=M('Suppliers')->where('shop_id = '.$id)->select();
        $this->assign('list',$suppliers);
        $this->display();
    }
	/**
     * 供货商列表
     */
    public function index(){
		$postdata = I('post.');

		$keyword = $postdata['keyword'];
        $map = array();
        if ($keyword) {
            $whereor['id'] = array('like', '%' . $keyword . '%');
            $whereor['name'] = array('like', '%' . $keyword . '%');
            $whereor['phone'] = $keyword;
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
		
		if($postdata['data_from'])
			$map['shen_time'][] = array('egt',strtotime($postdata['data_from']));
		if($postdata['data_to'])
			$map['shen_time'][] = array('elt',strtotime($postdata['data_to']));
		
		
		if($postdata['district']){
			$map['district'] = $postdata['district'];
		}else{
			if($postdata['city_id']){
				$map['city_id'] = $postdata['city_id'];
			}else{
				if($postdata['province']){
					$map['province'] = $postdata['province'];
				}
			}
		}
		$postdata['postion'] = $postdata['province'].','.$postdata['city_id'].','.$postdata['district'];
		
        $list = $this->lists('Suppliers', $map);
        $this->assign('list', $list);
		$this->assign($postdata);
        $this->display();
    }
    /**
     * 删除供货商
     */
    public function _before_delete(){exit();
		$id=I('id');
		if(isset($id)&&is_array($id)){
			foreach($id as $k=>$v){
				$result=check_delete($v,5);
				if(!$result['status'])unset($id[$k]);
			}
			if(empty($id)){$this->error($result['info']);exit;}
		}else{
			$result=check_delete($id,5);
			if(!$result['status']){$this->error($result['info']);exit;}
		}
		$this->id=$id;
	}
	
	/**
     * 验证手机号是否存在
     */
    public function checkmobile(){
		$data_post = I('post.');
        $where['phone'] = $data_post['phone'];
		$where['id'] = array('neq',$data_post['id']);
        $is_mobile = M('Suppliers')->where($where)->count();
        if($is_mobile){
			$return['status']=0;
			$return['data'] = "该手机号已存在";
			$this->ajaxReturn($return);
        }
    }

    /**
     * 添加
     */
    public function insert(){
        $model = D(CONTROLLER_NAME);
        // if (false === $model->create()) {
            // $this->error($model->getError());
        // }
        // $suppliers_id = $model->add();
        $postdata = I('post.');
		
$postdata['areas'] = implode(',',$postdata['regions']);
$postdata['password'] = md5($postdata['password']);
$postdata['payment'] = md5($postdata['payment']);
$postdata['xiang'] = $postdata['town'];
$postdata['village'] = $postdata['community'];
$postdata['publish_wait'] = 1;

$this->check_post($postdata, 0);
$postdata = $this->format_post($postdata);
$postdata['point'] = $postdata['x'].','.$postdata['y'];
$model->create($postdata);
	
$result = $model->add();
// echo $model->getLastsql();exit;
if ($result) {
	$url = U(CONTROLLER_NAME . '/index');
	$this->success(L('ADD_OK'), $url);
} else {
	$this->error(L('ADD_ERROR'), $url);
}
unset($postdata);

    }

    /**
     * 修改信息
     */
    public function update(){
		$model = D(CONTROLLER_NAME);
        $id = I('post.id');
        $postdata = I('post.');
	//判断密码是否修改
	if ($postdata['password']) {
		if ($postdata['password'] != $postdata['repassword']) {
			$this->error('两次输入的密码不相同');
		}
		$postdata['password'] = md5($postdata['password']);
	} else {
		unset($postdata['password']);
	}
	//判断支付密码是否修改
	if ($postdata['payment']) {
		$postdata['payment'] = md5($postdata['payment']);
	} else {
		unset($postdata['payment']);
	}
	//判断手机号是否存在
	if ($postdata['phone']) {
		$where_p['phone'] = $postdata['phone'];
		$where_p['id'] = array('neq',$id);
		$is_mobile = M('Suppliers')->where($where_p)->count();
		if($is_mobile){
			$this->error('该手机号已存在');
		}
	}
	$postdata['areas'] = implode(',',$postdata['regions']);
	$postdata['xiang'] = $postdata['town'];
	$postdata['village'] = $postdata['community'];
	$postdata['publish_wait'] = 1;
	$postdata['point'] = $postdata['x'].','.$postdata['y'];
	
	$this->check_post($postdata, $id);
	$postdata = $this->format_post($postdata);
	$data = $model->create($postdata);
		
        $where_s['id'] = $id;
        $info = M('Suppliers')->where($where_s)->save($data);
        if ($info !== false) {
            $url = U(CONTROLLER_NAME . '/index');
            $this->success(L('EDIT_OK'), $url);
        } else {
            $this->error(L('EDIT_ERROR'), $url);
        }
    }

    /**
     * 编辑
     */
    public function edit(){
        $id = I('get.id');
        if (!$id) $this->error(L('PARAM_ERR'));
        $edit = M('Suppliers');
        $edits = $edit->field(true)->find($id);
		
		$edits['point'] = explode(',',$edits['point']);
		$edits['x'] = $edits['point'][0];
		$edits['y'] = $edits['point'][1];
		
		$edits['postion'] = $edits['province'].','.$edits['city_id'].','.$edits['district'];
		
		$edits['region'] = explode(',',$edits['areas']);
		
        $this->assign('id', $id);
        $this->assign($edits);
        $this->assign('edit', $edits);
        $this->display();
    }
    //审核列表
    public function audit_list(){
        $sess = session('admin_info');
        $map=array();
        // $map['is_type'] = 0;
        $keyword=I('post.keyword');
        $id=I('post.supplier_id');
        if($id)$map['id']=$id;
        if($keyword){
            $whereor['name']=array('like','%'.$keyword.'%');
            $map['_complex'] = $whereor;
        }
        $map['status']=0;
        
        if($sess['xian_id'] != 0){
            $map['district'] = $sess['xian_id'];
        }elseif($sess['city_id'] != 0){
            $map['city'] = $sess['city_id'];
        }
        
        $list = $this->lists('Suppliers',$map);
        $this->assign('list',$list);
        $this->display();
    }
	
	
	/**
     * 格式化提交的数据
     * @param array $postdata
     * @return array
     */
	public function format_post($postdata = array()){
		$postdata['phone'] = intval($postdata['phone']);
        $postdata['shen_time'] = NOW_TIME;
		$postdata['add_time'] = NOW_TIME;
        return $postdata;
    }
	/**
     * 检查提交的数据是否合法
     * @param array $postdata
     * @param $id 店铺id
     */
    public function check_post($postdata = array(), $id = 0){
        if (empty($postdata)) $this->error("非法数据");
        if ($postdata['name'] == '') $this->error("供货商名称不得为空");
        if ($postdata['phone'] == '') $this->error("联系电话不得为空");
       // if ($postdata['areas'] == '') $this->error("配送区域不得为空");
		
		if($id != 0){
			$where['id'] = array('neq',$id);
		}
		$where['phone'] = $postdata['phone'];
        $is_mobile = M('Suppliers')->where($where)->count();
		if($is_mobile){
			$this->error("联系电话已存在");
		}
    }
	
	
    public function audit_view(){
        $id = I('get.id');
        if (!$id) $this->error(L('PARAM_ERR'));
        $edit = M('Suppliers');
        $edits = $edit->field(true)->find($id);
        $this->assign('id', $id);
        $this->assign($edits);
        $this->assign('edit', $edits);
        $this->display();
    }
}
?>