<?php
// +----------------------------------------------------------------------
// | Yershop 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liuwei <1010422715@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Drawback extends Admin{
    /**
     * 订单管理首页
     */
    public function index(){
		
		 if(isset($_GET['order_sn'])){
            $map['order_sn']  = array('like', '%'.(string)input('order_sn').'%');
        }
		else{
		    $map="";
		}
        $res= getLists('Drawback',$map,10,'id desc');
        $this->assign( 'res', $res);
        $this->meta_title = '订单管理';
		$this->assign('meta_title', $this->meta_title);
        return $this->fetch(); 
    }
      /**
     * 订单编辑
     */
	public function edit($id){   
	    if($_POST){
		   $user =new \app\admin\model\Drawback;
           $res=$user->validate(true)->allowField(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   $status=input('status'); 			
		       if($status==6){
		            $map["id"]=input('sales_id');
			        $save["status"]=5;//退货退款完成
			       Db::name("sales")->where($map)->update($save);
		         }
		    
		      $this->success("更新成功！",'index');
		   }else{
			   $this->success("更新失败！");
		   } 
	  }
	  else{
		 $map['id']=$id;
		 $info=Db::name("Drawback")->where($map)->find();
	     $this->assign('info', $info);
		 $type=explode("|",C('REFUND'));
		 $this->assign('type',$type);
		 $style=explode("|",C('REFUND_STYLE'));
		 $this->assign('style',$style);
		 $this->meta_title="编辑订单";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch();
	   }
	} 
	
	/**
     * 新增订单
     */
	  public function add($id=""){  
	  if($_POST){
		   $user =new \app\admin\model\Drawback;
        // 过滤post数组中的非数据表字段数据
       $res=$user->allowField(true)->save($_POST);
	   if($res){
		   $this->success("新增成功！",'index');
		}else{
			 $this->success("新增失败！");
		} 
	}
	  else{
		  $type=explode("|",C('REFUND'));
		  $this->assign('type',$type);
		  $style=explode("|",C('REFUND_STYLE'));
		  $this->assign('style',$style);
		  $this->meta_title="新增订单";
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("Drawback/edit");
	  }
	}
	public function out(){    
        $xlsName  = "Drawback";      
      
        
		$xlsCell  = array(
            array('id','编号'),
			array('order_sn','订单编号'),
            array('total_money','价格'),
            array('saleslist','订单内容'),
            array('username','联系人'),
            array('mobile','联系方式'), 
			array('total','供应商'),
            
			array('address','地址'), 
			array('status','订单状态'),
			array('express','快递名称'), 
			array('express_code','快递单号'),
			array('isdao','是否到货'), 
			array('arrive_time','到货时间'), 
			
			array('uid','用户uid')
        );
        $xlsModel = Db::name($xlsName);
        $xlsData  = $xlsModel->select();      
		 if(!$xlsData){               
                    $this->error('无数据');
                }
		exportExcel($xlsName,$xlsCell,$xlsData); 
	}
	/**
     * 删除订单
     */
	 public function del(){   
	    $id=input("id");
	    $map['id']=array("in",$id);
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$res=Db::name("Drawback")->where($map)->delete();
		if($res){
		   $this->success("删除成功！");
		}else{
			 $this->success("删除失败！");
		}
	}

}
