<?php

namespace App\Http\Controllers\AdminAPI\Admin;

# Models
use App\Models\AppDetail;

# Other Classes
use DB;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\API\BaseController as BaseController;
use Illuminate\Support\Facades\Auth;

class AppDetailController extends BaseController
{
    # Define Base view
    protected $view = 'admin.appdetail.';

     #Bind AppDetail model
    protected $appDetail;

    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                AppDetail $appDetail
                            )
    {
        $this->appDetail  = $appDetail;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try{
            $details['detail'] = $this->appDetail->first();
             #render view
             return $this->sendResponse($details, 'App Detail successfully.');
         }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return $this->sendError($ex, 'Error');
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         try{
             #validate the request
          
          
           $image = $request->file('upi_image');
            $unique = substr(str_shuffle("0123456789"), 0, 4);



            $app = $this->appDetail->where('id', 1)->first();
          
            #get the requested data in Data variable
            $data = [
                    'message'                    => $request->descripation,
                'notice'                     => $request->notice,
                'g_pay'                      => $request->g_pay,
                'mobile'                     => $request->mobile,
                'telegram'                   => $request->telegram,
                'youtube'                   => $request->youtube,
                'admin_percent'              => $request->admin_percent,
                'admin_Roullete_percent'     => $request->admin_Roullete_percent,
                'app_version'                => $request->app_version,
                'min_despoite'                => $request->min_despoite,
                'minimum_withdraw'                => $request->minimum_withdraw,
                'withdraw_time_start'                => $request->withdraw_time_start,
                'withdraw_time_end'                => $request->withdraw_time_end,
                'apk_link'                   => $request->apk_link,
                'upi_image'                  => isset($image) ? fileUploadProfile('admin/home/upiImage', $image, $unique) : (isset($request->remove_image) ? null : $app->upi_image),
                'starline_status'   => isset($request->starline_status) ? 1 : 0,
                'jackpot_status'   => isset($request->jackpot_status) ? 1 : 0,
                'min_bet_amount'                => $request->min_bet_amount,
                'max_bet_amount'                => $request->max_bet_amount,
  
                    ];
            #store the newly creted resoures
            $this->appDetail->whereId(1)->update($data);
            // #return redirect back to view
             toastr()->success('Store Create Sucess fully ...');
             return $this->sendResponse(true, 'App Detail successfully.');
         }catch(\Exception $ex)
        {
            toastr()->success($ex->getMessage());
            return $this->sendError($ex, 'Error');
        }
    }
    public function Pchangestatus(Request $request)
    {

        #get the detail of requested resources
        $citys = $this->appDetail->first();

        if ($citys->phone_pay_status == 1) {
            $status = '0';
            $g_status = '1';
        } else {
            $status = '1';
            $g_status = '0';
        }
        #get the status in data variable
        $data = [
            'phone_pay_status' => $status,
            'g_pay_status' => $g_status,
        ];

        #update status
        $this->appDetail->update($data);
        $citys = $this->appDetail->first();
        $message = $citys->phone_pay_status == 0 ? 'InActive' : 'Active';
        toastr()->success('You are successfully ' . $message);
        return $this->sendResponse(true, 'App Detail successfully.');

    }
    public function Gchangestatus(Request $request)
    {

        #get the detail of requested resources
        $citys = $this->appDetail->first();

        if ($citys->g_pay_status == 1) {
            $citys->g_pay_status = 0;
            $citys->phone_pay_status = 1;
        } else {
            $citys->g_pay_status = 1;
            $citys->phone_pay_status = 0;
        }
        $citys->save();


        $citys = $this->appDetail->first();
        $message = $citys->g_pay_status == 0 ? 'InActive' : 'Active';
        toastr()->success('You are successfully ' . $message);
        return $this->sendResponse(true, 'App Detail successfully.');
    }
}
