<?php
namespace app\api\controller\user;
use app\models\store\StoreOrderCartInfo;
use app\admin\model\user\User;
use app\models\store\StoreOrder;
use app\models\user\UserCode;
use app\models\user\UserCodeInfo;
use app\Request;
use crmeb\services\{UtilService,JsonService};
class CodeController
{
    private function init($request)
    {
        $userinfo = $request->user();
        if(intval($userinfo['level'])<6)
        {
            return app('json')->fail('非总代不能进入');
        }
    }
    public function index(Request $request)
    {
        $this->init($request);
        $user = $request->user();
        $data['user'] = $user;
        $data['user_code'] = UserCode::where('uid',$request->uid())->where('code',$user['code'])->find();
       return app('json')->successful($data);

    }
    public function goods(Request $request)
    {
        $this->init($request);
        $user = $request->user();
        $list = UserCodeInfo::where('uid',$request->uid())->where('code',$user['code'])->select()->toArray();
        foreach ($list as &$v)
        {
            $v['add_time_txt'] = date("Y-m-d",$v['add_time']);
        }
        return app('json')->successful($list);
    }
    public function order(Request $request)
    {
        $where = UtilService::postMore([
            ['page',1],
            ['limit',20],
            ['type',1],
        ],$request);
        $this->init($request);
        $user = $request->user();
        switch ($where['type'])
        {
            case 1:
                $count = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',0)->where('refund_status', 0)->count();
                $data = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',0)->where('refund_status', 0)->page($where['page'],$where['limit'])->select()->toArray();
                break;
            case 2:
                $count = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',1)->where('refund_status', 0)->count();
                $data = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',1)->where('refund_status', 0)->page($where['page'],$where['limit'])->select()->toArray();
                break;
            case 3:
                $count = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',2)->where('refund_status', 0)->count();
                $data = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',2)->where('refund_status', 0)->page($where['page'],$where['limit'])->select()->toArray();
                break;
            case 4:
                $count = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',3)->where('refund_status', 0)->count();
                $data = StoreOrder::where('paid',1)->where('code',$user['code'])->where('status',3)->where('refund_status', 0)->page($where['page'],$where['limit'])->select()->toArray();
                break;
        }
        foreach ($data as &$v)
        {
            $v['user'] = User::where('uid',$v['uid'])->field('uid,real_name,nickname,avatar');
            $v['goods_name'] = StoreOrderCartInfo::getProductNameList($v['id']);
            $v = StoreOrder::tidyOrder($v, true);
        }
        return app('json')->successful(compact('count','data'));
    }
    public function users(Request $request)
    {
        $this->init($request);
        $user = $request->user();
        return app('json')->successful(User::where('code',$user['code'])->order('team_cts desc')->page(input('page',1),input('limit',100))->field('uid,real_name,nickname,avatar,level,team_cts')->select()->toArray());
    }
}