<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class SaleGoodController extends Controller
{
	/**
	*用户卖货
	*
	 */
	public function sales()
	{
		$openid=$_GET['openid'];
		$village_id = $_GET['village_id'];
		$choice=I('choice');
		$good_id=I('good_id');
		$else=I('else');
		$num=I('num');
		$img_1=$_GET['img_1'];
		$img_2=$_GET['img_2'];
		$name=I('name');
		$tel=I('tel');
        $address=I('address');
        $date_time=date('Y-m-d',time());
		 // 手机号码正则
        $pattern = "/^1[34578]{1}\d{9}$/";
        $matchMobile = preg_match($pattern, $tel);  //匹配正确返回 1,错误返回 0
        if (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid有误");
        }elseif (!village_id || village_id == "undefined" || village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        }elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "姓名有误");
        }elseif (!$tel || $tel == "undefined" || $tel == "null") {
            $data = array("status" => -1, "msg" => "电话有误");
        } elseif (!$address || $address == "undefined" || $address == "null") {
            $data = array("status" => -1, "msg" => "地址有误");
        } elseif ($matchMobile == 0) {
            $data = array("status" => -1, "msg" => "请填写正确手机号码");
        } elseif ($num == "undefined" || $num == "null") {
            $data = array("status" => -1, "msg" => "数量有误");
        } elseif ($num < 1) {
            $data = array("status" => -1, "msg" => "请输入数量");
        } else {
            // 检索是否有未审核申请
            $applyNum = M("purchase")->where("`openid`='{$openid}' AND `status`=1")->count();
            if ($applyNum > 0) {
                $data = array("status" => -1, "msg" => "您有未审核申请");
            } else {
                if ($choice == 1){
                    $addData = array(
                        'openid'=>$openid,
                        'village_id'=>$village_id,
                        'good_id'=>$good_id,                
                        'num'=>$num,
                        'img_1'=>$img_1,
                        'img_2'=>$img_2,
                        'name'=>$name,
                        'tel'=>$tel,
                        'address'=>$address,
                        'create_time'=> $date_time
                    );
                } elseif ($choice == 2){
                    $addData=array(
                        'openid'=>$openid,
                        'village_id'=>$village_id,
                        'good_id'=>0,  
                        'else'=>$else,              
                        'num'=>$num,
                        'img_1'=>$img_1,
                        'img_2'=>$img_2,
                        'name'=>$name,
                        'tel'=>$tel,
                        'address'=>$address,
                        'create_time'=>$date_time
                    );
                }               
                $result=M('purchase')->add($addData);      
                if ($result){
                    $data = array(
                        "status" => 1,
                        "msg" => "申请成功"                   
                    );
                } else {
                    $data=array(
                        "status" => -1,
                        "msg" => "系统繁忙"
                    );                                 
                }
            }
        }
        apiReturn($data);		
	}

    /**
     * 通过村ID查询收购商品列表
     */
    
    public function purchase_list()
    {
        $village_id = I('village_id');
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID错误");
        }else{
            $Model=M("");
            $res=$Model->table('am_purchase_goods')->where("`village_id`='$village_id'")->select();
            foreach ($res as $key => $value) {
                $res[$key]['pic_goods'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$value['pic_goods']}";
            }
            unset($key, $value);
            if ($res) {
                $data=array(
                    "status" => 1,
                    "result"=>$res
                );
            }else if(empty($res)){
                $res1=$Model->table('am_purchase_goods')->where("`purchase_type`=0")->select();
                foreach ($res1 as $key => $value) {
                    $res1[$key]['pic_goods'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$value['pic_goods']}";
                }
                unset($key, $value);
                $data=array(
                    "status" => 1,
                    "result"=>$res1
                );
            }else{
                $data=array(
                    "status" => -1,
                    "msg"=>"系统繁忙"
                );
            }
        }
       apiReturn($data); 
    }
    /**
     * 返回openid查找的收购申请列表
     */
    public function byOpenid()
    {
        $openid = I('openid');
         if (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid错误");
        } else {
            $where = array(
                'openid' => $openid,
            );            
            $res=M('purchase')
                ->where($where)
                ->order("`id` DESC")
                ->select();
            foreach ($res as $key => $value) {
                if ($value['good_id'] > 0) {
                    // 商品
                    $row = M("purchaseGoods")->where("`id`={$value['good_id']}")->find();
                    $res[$key]['goods_name'] = $row['goods_name'];
                    $defaultPic = $row['pic_goods'];
                } else {
                    $res[$key]['goods_name'] = $value['else'];
                    $defaultPic = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Public/img/torch.png";
                }
                $res[$key]['img_1'] = $value['img_1'] ? "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$value['img_1']}":$defaultPic;
                $res[$key]['img_2'] = $value['img_2'] ? "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$value['img_2']}":$defaultPic;
            }
            unset($key, $value);
            if($res){
                 $data=array(
                    "status" => 1,
                    "result"=>$res
                    );
             } else {
                 $data=array(
                     "status" => 1,
                    "result"=> array()
                    );
             }
        }
         apiReturn($data); 
    }

    /**
     * 村商品价格设置
     */
    public function vilGoodsPrice()
    {
    	$village_id =I('village_id');
    	$good_id = I('good_id');
    	$price = I('price');
    	 if (!$good_id || $good_id == "undefined" || $good_id == "null") {
            $data = array("status" => -1, "msg" => "商品ID有误");
        }elseif (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        }elseif (!$price || $price == "undefined" || $price == "null" || $price <= 0) {
            $data = array("status" => -1, "msg" => "价格有误");
        } else {
        	$dataAll = array(
        		'good_id'=>$good_id,
        		'village_id'=>$village_id,
        		'price'=>$price,
        		'priceSn'=>'AM'.$village_id.time().$good_id,
        		'return_time'=>time()
        		);
        		$res = M('villages_price')->add($dataAll);
        		if ($res){
        		 $data = array(
                    "status" => 1,
                    "msg" => "添加成功"                   
                );
        		} else {
        			$data=array(
        				"status" => -1,
                    	"msg" => "系统繁忙，请稍后在试"
        				);					               
        		} 
        }
        	apiReturn($data);
    }

    /**
     * 根据village_id,good_id反馈价格
     */
    public function returnPrice()
    {
    	$village_id =I('village_id');
    	$good_id = I('good_id');
    	if (!$good_id || $good_id == "undefined" || $good_id == "null") {
            $data = array("status" => -1, "msg" => "商品ID有误");
        }elseif (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        }elseif($good_id && $village_id){
        	$price = M('villages_price')->where("`village_id`={$village_id} AND `good_id`={$good_id}")->field("`price`,`priceSn`")->find();
        	if ($price){
        		 $data = array(
                    "status" => 1,
                    "result" => $price                   
                );
        		} else {
        		$data = array(
        				"status" => -1,
                    	"msg" => "您查找的商品不存在"
        				);					               
        		} 

        }
          apiReturn($data);
    }  
}