<?php
namespace app\admin\controller;

use app\model\Admins;
use app\model\Follows;
use app\model\Orders;
use support\Request;

class FollowController extends base
{
    public function index(Request $request) {
        $admin = $request->get('admin');
        $sn = $request->get('sn');
        $self = $request->get('self');
        $times = $request->get('times');

        $query = Follows::with(['admin','orders'])->order('id', 'desc');

        if($request->admin->is_super == 0 || $self) {
            $query->where('admin_id', $request->admin->id);
        }

        if(!empty($admin)) {
            $admin_id = Admins::where('username', $admin)->value('id');
            $query->where('admin_id', $admin_id ?? 0);
        }

        if(!empty($sn)) {
            $order_id = Orders::where('sn', $sn)->value('id');
            $query->where('order_id', $order_id ?? 0);
        }

        if(!empty($times) && count($times)) {
            $query->whereBetween('create_time', [strtotime($times[0]),strtotime($times[1])]);
        }
        
        $list = $query->paginate($request->get('limit', 30));

        return $this->success($list->append(['status_name']));
    }
}