<?php
namespace app\admin\controller;

use think\Db;

class WxOrdersRefund extends Auth
{

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function index(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','sort','trim');      //排序列
            $sort = input('sord','asc','trim');        //排序方式
            $order = $sortRow.' '.$sort.' ,id desc';

            $order_sn = input('order_sn','','trim');
            if($order_sn){
                $ids = Db::name('wx_orders')
                    ->where('org_id',$this->orgId)
                    ->where('order_sn',$order_sn)
                    ->column('id');
                if(empty($ids)){
                    $map[]=['order_id','=',-1];
                }else{
                    $map[]=['order_id','in',$ids];
                }
            }

            $status = input('status','','trim');
            if($status != ''){
                $map[] = ['status','=',$status];

            }


            $cate_id = input('cate_id','','trim');
            if($cate_id != ''){
               $goodIds = Db::name('wx_goods')
                   ->where('cate_id',$cate_id)
                   ->column('id');
               if(empty($goodIds)){
                   $map[] = ['id','=',-1];
               }else{
                   $map[] = ['goods_id','in',$goodIds];
               }
            }
            $b = input('begin','','trim');
            $e = input('end','','trim');
            if($b){
                $b = date('Y-m-d 00:00:00',strtotime($b));
                $map[] = ['create_time','>=',$b];

            }
            if($e){
                $e = date('Y-m-d 23:59:59',strtotime($e));
                $map[] = ['create_time','<=',$e];

            }
            $map[] = ['org_id','=',cur_org_id()];
            $map[] = ['del','=',0];
            $map = empty($map) ? true: $map;

            //数据查询
            $lists = Db::name('wx_orders_refund')
                ->where($map)->limit($start,$length)->order($order)->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['status_name'] =isset(model('WxOrders')->rStatus[$v['status']])?model('WxOrders')->rStatus[$v['status']]:'';
                $lists[$k]['user_name'] = Db::name('user')
                    ->where('id',$v['user_id'])
                    ->value('real_name');

                $lists[$k]['goods_name'] = Db::name('wx_goods')
                    ->where('id',$v['goods_id'])
                    ->value('title');
                $lists[$k]['order_sn'] = Db::name('wx_orders')
                    ->where('id',$v['order_id'])
                    ->value('order_sn');
            }
            //数据返回
            $totalCount = Db::name('wx_orders_refund')->where($map)->count();
            $totalPage = ceil($totalCount/$length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{
            $cate = model('WxGoodsCate')->lists($this->orgId);
            $status = model('WxOrders')->rStatus;
            $this->assign('status',$status);
            $this->assign('cate',$cate);
            $this->assign('meta_title','订单退款列表');
            return $this->fetch();
        }
    }
    public function details($id,$goodsId){
        if(!$id){
            $this->error('参数错误');
        }
        $ret = model('WxOrders')->goodsRefundDetail($this->orgId,$id,$goodsId);
        $this->assign('info',$ret);
        $this->assign('meta_title','退款详情');
        return $this->fetch();
    }

    /**
     * 退款
     */
    public function option($id = 0){
        if(request()->isPost()){
            $res = model('WxOrders')->refundOrder($this->userId);
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error(model('WxOrders')->getError());
            }
        }else{
            $pay = Db::name('wx_orders_refund')->where('id',$id)->find();
            $money = $pay['amount'];
            $this->assign('money',$money);
            $this->assign('pay',$pay);
            $this->assign('id',$id);
            return $this->fetch();
        }
    }
    public function goodsRefund($id,$goodsId=0,$type=0){
        if(!$id){
            $this->error('参数错误');
        }
        if(request()->isPost()){
            $res = model('WxOrders')->orderRefund($this->userId,$this->orgId);
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error(model('WxOrders')->getError());
            }
        }else{
            $ret = model('WxOrders')->checkoutApplyRefund($this->userId,$this->orgId,$type,$id,$goodsId);
            $this->assign('info',$ret);
            $this->assign('id',$id);
            $this->assign('meta_title','商品申请退款');
            return $this->fetch();
        }


    }
}