<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\User;
use \App\Helper;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\DataTables;

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if(auth()->check()){
                Helper::checkLevel(['admin']);
            }
            else{
                return redirect('/login');
            }
            return $next($request);
        });
    }

    public function mass(Request $request) {
      $request->validate([
          'angka' => 'required|numeric',
          'periode' => 'required|in:hour,day,week,month'
      ]);
      $the_s = '';
      if(request('angka') > 1) {
        $the_s = 's';
      }
      $time = request('angka').' '.request('periode').$the_s;
      $user = User::all();
      foreach($user as $u) {
        // if (time() <= strtotime($u->expired_date)) {
        //   $newexp = date('Y-m-d', strtotime('+'.$time, strtotime($u->expired_date)));
        //   User::where('id', '=', $u->id)->update(['expired_date' => $newexp]);
        // } else {
        //   $newexp = date('Y-m-d', strtotime('+'.$time));
        //   User::where('id', '=', $u->id)->update(['expired_date' => $newexp]);
        // }
        $newexp = date('Y-m-d', strtotime('+' . $time, strtotime($u->expired_date)));
        User::where('id', '=', $u->id)->update(['expired_date' => $newexp]);
      }
      $message = ['alert' => 'Sukses menambah '.$time.' ke Seluruh member'];
      session()->flash('message', $message);
      return redirect()->action('AdminController@massView');
    }

    public function massView() {
      return view('admin.mass-extends');
    }

    public function transfer(Request $request){
      $username = auth()->user()->username;
      $request->validate([
          'jumlah' => 'required|numeric',
          'user' => "required|not_in:{$username}"
      ]);
      $price = request('jumlah');
      $user = request('user');
      if(auth()->user()->saldo < $price){
        $message = ['Alert' => 'Saldo tidak mencukupi'];
        session()->flash('message', $message);
        return redirect()->back();
      }
      $users = User::where('username', '=', $user)->first();
      if ($users->count() == 0) {
        $message = ['Alert' => 'Error, user tidak terdeteksi di database'];
        session()->flash('message', $message);
        return redirect()->back();
      }
      Helper::subtractSaldo(auth()->user()->id, $price);
      Helper::addSaldo($users->id, $price);
      Helper::saveUserBalanceHistory(auth()->user()->id, $price, 'debit', 'Transfer Saldo kepada akun '.$users->level.' | "'.$users->username.'"', 'complete');
      Helper::saveUserBalanceHistory($users->id, $price, 'deposit', 'Penambahan saldo dari '.auth()->user()->level.' | "'.auth()->user()->username.'"', 'complete');
      $u = User::where('username', '=', $users->username)->first();
      $message = [
        'Message' => '<strong>Transfer Saldo berhasil!</strong>',
        'username' => $u->username,
        'jumlah dikirim' => $price,
        'saldo sekarang' => $u->saldo,
        'dikirim oleh' => auth()->user()->username,
        'Date' => date('Y-m-d'),
        'status' => 'Berhasil'
      ];
      session()->flash('message', $message);
      return redirect()->action('AdminController@transferView');
    }

    public function transferView(){
      $user = User::where('id', '!=' , auth()->user()->id)->get();
      return view('admin.transfer', compact('user'));
    }

    public function decrease(Request $request){
      $username = auth()->user()->username;
      $request->validate([
          'jumlah' => 'required|numeric',
          'user' => "required|not_in:{$username}"
      ]);
      $price = request('jumlah');
      $user = request('user');
      $users = User::where('username', '=', $user)->first();
      if ($users->count() == 0) {
        $message = ['Alert' => 'Error, user tidak terdeteksi di database'];
        session()->flash('message', $message);
        return redirect()->back();
      }
      Helper::subtractSaldo($users->id, $price);
      Helper::saveUserBalanceHistory($users->id, $price, 'debit', 'Pengurangan saldo oleh '.auth()->user()->level.' | "'.auth()->user()->username.'"', 'complete');
      $u = User::where('username', '=', $users->username)->first();
      $message = [
        'Message' => 'Pengurangan Saldo berhasil!',
        'username' => $u->username,
        'jumlah dikirim' => $price,
        'saldo sekarang' => $u->saldo,
        'dikirim oleh' => auth()->user()->username,
        'Date' => date('Y-m-d'),
        'status' => 'Berhasil'
      ];
      session()->flash('message', $message);
      return redirect()->action('AdminController@decreaseView');
    }

    public function decreaseView(){
      $user = User::where('id', '!=' , auth()->user()->id)->get();
      return view('admin.decrease', compact('user'));
    }
    
    public function IndexService(){
        dd('hello');
        $service = DB::table('services')->orderBy('sid', 'ASC')->paginate(10);
        return view('admin.service', compact('service'));
    }
}
