<?php

namespace app\controller\order;
use Overtrue\Pinyin\Pinyin;
use think\facade\Db;
use think\Request;
class ChargeOrder
{
    
     public function charge_order(Request $request)
    {

        $params = $request->param();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $where = [];
        if(isset($params['phone']) && !empty($params['phone'])){
            $where[] = ['u.phone','=',$params['phone']];
        }
        $list =  Db::table('zxc_charge_order o')
        ->leftJoin('zxc_user u', 'o.openid = u.openid')
        ->leftJoin('charge_station s', 's.charge_station_id = o.charge_station_id')
        ->field('o.*, u.phone,s.charge_station_name')
        ->where($where)
        ->order('o.order_id desc')
        ->paginate([
            'list_rows' => $pageSize,
            'page' => $page,
            'query' =>$params
        ]);
        $list->each(function($item, $key) {
            $item['type_text'] = $item['type'] == 1 ? '即充即退' : '个人钱包';
            return $item;
        });
        return json($list);
    }
    
    public function recharge_order(Request $request){
        $params = $request->param();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $where = [];
        $list =  Db::table('zxc_recharge o')
        ->leftJoin('zxc_user u', 'o.openid = u.openid')
        ->field('o.*, u.phone')
        ->order('o.id desc')
        ->paginate([
            'list_rows' => $pageSize,
            'page' => $page,
            'query' =>$params
        ]);
        $list->each(function($item, $key) {
            $item['trade_state'] = '成功';
            return $item;
        });
        return json($list);
    }
    
    public function refund_order(Request $request){
        $params = $request->param();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $where = [];
        $list =  Db::table('zxc_refund o')
        ->leftJoin('zxc_user u', 'o.openid = u.openid')
        ->field('o.*, u.phone')
        ->order('o.id desc')
        ->paginate([
            'list_rows' => $pageSize,
            'page' => $page,
            'query' =>$params
        ]);
        $list->each(function($item, $key) {
            $item['status'] = '成功';
            $item['refund_total'] = $item['refund_total']/100;
            return $item;
        });
        return json($list);
    }
    
}