<?php
namespace Smi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class SalesController extends BaseController
{
	/**
	 * 收购商品列表
	 */
	public function purchasseList()
	{   
		auth_check("sales_read");
		$num=7;
		$Model=M("");
        
         if($_GET['search']){
           $search=I('search');  
           //dump($search);die;                     
           $where['a.goods_name|b.name']=array('like',"%{$search}%"); 
           //dump($where);die;
        }
   
		$count=$Model->table('am_purchase_goods')->alias('a')
                    ->join("left join am_region_village b ON  a.village_id=b.id")
                    ->where($where)->count();
		$Page = new \Think\Page($count, $num);
        $show = $Page->show();// 分页显示输出
		$data=$Model->table('am_purchase_goods')->alias('a')
					->join("left join am_region_village b ON  a.village_id=b.id")
					->limit($Page->firstRow.','.$Page->listRows)
                    ->where($where)
                    ->order("id DESC")
					->field('a.*,b.name')->select();		
		//dump($data);die;
		$this->assign('data',$data);
		$this->assign('page',$show);
		$this->display();
	}
	//添加收购产品
	public function addGoods()
	{   
		auth_check("sales_opt");
		if (IS_POST) {
			$imgs=D('Sales')->upload();						
			$data=array(
				'purchase_type'=>I('purchase_type'),
				'village_id'=>$_POST['village_id'],
				'goods_name'=>I('goods_name'),
				'pic_goods'=>$imgs,
				'create_time'=>time()
				);
			$res=M('purchase_goods')->add($data);
			if ($res) {
				alertMsg('添加成功',"./purchasseList");
				
				
			} else {
				$this->redirect("addGoods");
			}
		} else {
            $province = M("regionProvince")->select();
            $this->assign("province", $province);
		}
		
		$this->display();
	}
	//修改收购商品
	public function updateGoods()
	{
		auth_check("sales_opt");
		if(IS_POST){
			$ids=$_POST['id'];
			$old_goods=M('purchase_goods')->where("`id`={$ids}")->getField('pic_goods'); 
			$imgs=D('Sales')->upload();		
			$data=array(
				'purchase_type'=>I('purchase_type'),
				'village_id'=>$_POST['village_id'],
				'goods_name'=>I('goods_name'),
				'pic_goods'=>$imgs,
				'create_time'=>time()
				);	
			$res=M('purchase_goods')->where("`id`={$ids}")->save($data);
			if ($res) {
				if($old_goods != $imgs){
					  @unlink("./Uploads/{$old_goods}");    // 删除旧图
				}
				alertMsg('修改成功',U('purchasseList'));								
			} else {
				alertMsg('修改失败',U('updateGoods'));					
			}			
		}
		$id=I('get.id');
		$Model=M("");
		$data=$Model->table('am_purchase_goods')->where("`id`={$id}")->find();
		//dump($data);die;
		$type=$data['purchase_type'];
		if($type == 1){			
			$vid=$data['village_id'];
			$positions=$Model->table('am_region_village')->alias('a')->where("a.id={$vid}")
						->join('left join am_region_county b ON a.county_id=b.id')
						->join('left join am_region_city c ON b.city_id=c.id')
						->join('left join am_region_province d ON c.province_id=d.id')
						->field('a.name as name1,b.name as name2,c.name as name3,d.id as id4,c.id as id3,b.id as id2,a.id as id1')->find();
						//dump($positions);die;
			$this->assign('pos',$positions);
		}
		$province = M("regionProvince")->select();
        $this->assign("province", $province);
		$this->assign('data',$data);
		$this->display();
	}
	//删除图片
	public function delImg()
	{
		$id = $_POST['id'];
		$img = M('purchase_goods')->where("`id`={$id}")->getField('pic_goods');
		$res = M('purchase_goods')->where("`id`={$id}")->setField('pic_goods',0);
		if($res){
			 @unlink("./Uploads/" . $img);
			 $this->ajaxReturn(1);
		} else {
			 $this->ajaxReturn(-1);
		}
	}
	//删除收购产品
	public function deleteGoods()
	{
		auth_check("sales_opt");
		$id=I('id');
		//dump($id);die;
		$Model=M("");
		$res=$Model->table('am_purchase_goods')->where("`id`={$id}")->delete();
		if ($res) {
			alertMsg('删除成功',U('purchasseList'));
		} else {
			 $this->redirect("purchasseList");die;
		}
	}
	  /**
     * 点击省获取市列表 ajax
     */
    public function cirtiesFromProvince()
    {
        $province_id = $_REQUEST['province_id'];
        $cities = M("regionCity")
            ->where("`province_id`={$province_id}")
            ->field("`id`,`name`")
            ->select();
        $this->ajaxReturn($cities);die;
    }

    /**
     * 点击市获取县列表 ajax
     */
    public function countiesFromCity()
    {
        $city_id = $_REQUEST['city_id'];
        $counties = M("regionCounty")
            ->where("`city_id`={$city_id}")
            ->field("`id`,`name`")
            ->select();
        $this->ajaxReturn($counties);die;
    }

    /**
     * 点击县获取村列表 ajax
     */
    
    public function villagesFromCounty()
    {
        $county_id = $_REQUEST['county_id'];
        $villages = M("regionVillage")
            ->where("`county_id`={$county_id} AND type=1")
            ->field("`id`,`name`")
            ->select();
        $this->ajaxReturn($villages);die;
    }
    /**
     * 商品货列表
     */
    public function goodsList()
    {
    	auth_check("sales_read");
    	$Model=M("");
    	$num=10;
        $where=array('good_id'=>array('gt','0'));
         if($_GET['search']){
           $search=I('search');                       
           $where['a.name|a.tel|c.name|a.create_time']=array('like',"%{$search}%");          
        }
    	$count=$Model->table('am_purchase')->alias('a')
                    ->join('left join am_purchase_goods b ON a.good_id=b.id')
                    ->join('left join am_region_village c ON a.village_id=c.id')
                    ->field('a.*,b.goods_name,c.name as vill_name')
                    ->where($where)->count();
    	$Page = new \Think\Page($count, $num);
        $show = $Page->show();// 分页显示输出
    	$data=$Model->table('am_purchase')->alias('a')
    				->join('left join am_purchase_goods b ON a.good_id=b.id')
    				->join('left join am_region_village c ON a.village_id=c.id')
    				->field('a.*,b.goods_name,c.name as vill_name')
                    ->where($where)
                    ->order("id desc")
    				->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('data',$data);
    	$this->assign('page',$show);
    	$this->display();
    }
    /**
     * 商品货详情
     */
    public function detailGoods()
    {       
    	auth_check("sales_read");
    	$id=I('id');
    	$Model=M("");
    	$data=$Model->table('am_purchase')->alias('a')->where(array('a.id'=>$id))
    				->join('left join am_purchase_goods b ON a.good_id=b.id')
    				->join('left join am_region_village c ON a.village_id=c.id')
    				->field('a.*,b.goods_name,c.name as vill_name')
    				->find();
    	$this->assign('data',$data);
    	// dump(str_replace("\n",'<br>',$data['reason']));die;
    	$this->display();
    }
     /**
     * 其他货列表
     */
    public function otherList()
    {
    	auth_check("sales_read");
    	$Model=M("");
    	$num=10;
        $where=array('good_id'=>0);
        if($_GET['search']){
           $search=I('search');                       
           $where['a.name|a.tel|c.name|a.create_time']=array('like',"%{$search}%");          
        }
    	$count=$Model->table('am_purchase')->alias('a')
                    ->join('left join am_purchase_goods b ON a.good_id=b.id')
                    ->join('left join am_region_village c ON a.village_id=c.id')
                    ->field('a.*,b.goods_name,c.name as vill_name')                   
                    ->where($where)
                    ->count();
    	$Page = new \Think\Page($count, $num);
        $show = $Page->show();// 分页显示输出

    	$data=$Model->table('am_purchase')->alias('a')
    				->join('left join am_purchase_goods b ON a.good_id=b.id')
    				->join('left join am_region_village c ON a.village_id=c.id')
    				->field('a.*,b.goods_name,c.name as vill_name')
    				->limit($Page->firstRow.','.$Page->listRows)
                    ->where($where)
                    ->order("id desc")
                    ->select();
    	$this->assign('data',$data);
    	$this->assign('page',$show);
    	$this->display();
    /**
     * 删除商品信息
     */
    }
    public function del_goods()
    {
    	auth_check("sales_opt");
    	$id=I('id');
    	$p=I('p');
    	$res=M('purchase')->where("`id`={$id}")->delete();
    	if ($res) {
    		if($p){
    			alertMsg('删除成功',U('goodsList'));
    		} else {
    			alertMsg('删除成功',U('otherList'));
    		}
			
		} else {
			if($p){
    			 $this->redirect("goodsList");die;
    		} else {
    			 $this->redirect("otherList");die;
    		}
			
		}
    }
    /**
     * 修改商品订单状态值
     */
    public function set_status()
    {
    	$data['status']=$_POST['status'];
    	$id=$_POST['id'];
        $status=M("purchase")->where("`id`={$id}")->field('status')->find();
        //查询未改变之前的状态
        session('status',$status);
        //取消原因
        $datas['reason']=$_POST['reason'];
        if($datas['reason']){
            $res1=M("purchase")->where("`id`={$id}")->save($datas);
            if($res1){
            $this->ajaxReturn(2);
        } else {
            $this->ajaxReturn(-2);
        }
        }
    	$res=M('purchase')->where("`id`={$id}")->save($data);
    	if($res){
    		$this->ajaxReturn(1);
    	}else{
    		
    		$this->ajaxReturn(-1);
    	}
    }
    /**
     * 添加取消原因
     */
    public function set_reason()
    {
      
        $id=$_POST['id'];
        //dump($id);
        $data=session('status');
        //dump($data);die;
        //$data['status']=$data;       
        $res=M('purchase')->where("`id`={$id}")->save($data);
        if($res){
            $this->ajaxReturn(1);
        } else {
            $this->ajaxReturn(-1);
        }
    }

   
    
}