<?php

namespace Active\Lib;

use Dever;
Class Manage {
	#全额退款
	public function refund_api($id){
		$data = Dever::db('active/order')->find($id);
		$param = array
        (
            'project_id' => 6,
            'channel_id' => 1,
            'system_source' => 1,
            'account_id' => 8,
            'order_id' => $data['order_num'],
            'refund_cash' => $data['price'],
            //'refund_order_id' => '',
        );
        if($data['status'] != 2){
        	Dever::alert('此状态下不可退款');
        }

        if ($data['price'] > 0) {
            $result = Dever::load('pay/api.refund', $param);

            if (!$result) {
                # 退款失败，抛出错误
                //throw new \Exception('退款失败');
                Dever::alert('退款失败，请联系管理员');
            }else{ 
                $admin = Dever::load('manage/auth.data');
                $time = time();
            	Dever::db('active/order')->update(array('where_id'=>$id,'set_status'=>6,'set_refund_admin'=>$admin['id'],'set_rdate'=>$time));
            }
        }
  		return 'reload';
	}

    public function show($id){
        $data = Dever::db('active/order')->find($id);
        $refund_admin = '-';
        $rdate = '-';
        if ($data && isset($data['refund_admin']) && $data['refund_admin'] && $data['refund_admin']>0 && $data['rdate']) {
            $admin = Dever::load('manage/admin-find',$data['refund_admin']);
            $refund_admin = $admin['username'];
            $rdate = date('Y-m-d H:i',$data['rdate']);
        }
        $html = $refund_admin.'<br/>'.$rdate;
        // print_R($html);die;
        return $html;
    }
}