<?php

namespace App\Http\Controllers\SyS;


use App\Http\Controllers\Api\KasController;
use App\Http\Controllers\Api\KasSLLController;
use App\Http\Controllers\Controller;
use App\Mail\MailInfo;
use App\Models\PaymentMethod;
use App\Models\ShoppingUser;
use App\Models\UserShop;
use App\Repositories\ContractPDFRepository;
use App\Services\CustomerPriority;
use App\Services\Shop;
use App\User;
use Auth;
use Illuminate\Support\Facades\Mail;
use Request;


class AdminToolsController extends Controller
{
    protected $userRepo;

    public function __construct()
    {
        $this->middleware('sysadmin');

    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
       dd('index');
    }

    public function customers()
    {

        $shopping_users = ShoppingUser::where('member_id', '=', NULL)->where('auth_user_id', '=', NULL)->get();
        $data = [
            'values' => $shopping_users,
            'text' => '',
        ];

        return view('sys.admin.customers', $data);
    }


    public function customerStore()
    {
        $data = Request::all();
        $ret = "";
        if($data['action'] === 'makePaymentMethodsDefault'){
            $users = User::where('payment_methods', '=', NULL)->get();
            //$users = User::all();
            foreach ($users as $user){
                $user->payment_methods = PaymentMethod::getDefaultAsArray();
                $user->save();
            }
        }
        if($data['action'] === 'checkForAll'){
            $shopping_users = CustomerPriority::checkForAll();
        }

        if($data['action'] === 'checkContractPDF'){
            //create PDF
            $user = User::findOrFail(80);

            $pdf = new ContractPDFRepository($user);
            $pdf->_set('disk', 'user');
            $pdf->_set('dir', '/'.$user->id.'/documents/');
            $pdf->_set('user_id', $user->id);
            $pdf->_set('identifier', 'contract');
            $pdf->createContractPDF();

        }



        if(strpos($data['action'], 'checkOne_') !== false){
            $id = (int) str_replace('checkOne_', '', $data['action']);
            $shopping_user = ShoppingUser::findOrFail($id);
            $ret = CustomerPriority::checkOne($shopping_user);
        }
        \Session()->flash('alert-success', $ret);
        return back();
    }




    public function cronjobs()
    {
        //$user_shops = UserShop::all();
        $text = "";
        $values = [
            'check_payments_account' => route('cron_jobs_action', ['check_payments_account', 'key'])
        ];
        $data = [
            'values' => $values,
            'text' => $text,
        ];

        return view('sys.admin.cronjobs', $data);
    }
    public function cronjobsStore()
    {
        $data = Request::all();
        \Session()->flash('alert-save', true);
        return back();
    }


    public function domainSSL()
    {
        $user_shops = UserShop::all();
        $text = "";

        $kas = new KasController();
        $domain = 'mivita.care';

        $ssl = KasSLLController::getApiSSLParameter();

        $subdomains = $kas->action('get_subdomains');
        foreach ($subdomains as $subdomain){
            $text .= $subdomain['subdomain_name']." - ".$subdomain['ssl_certificate_sni']."\n";

            if($subdomain['ssl_certificate_sni'] !== "Y"){
                $pra = array(
                    'hostname' => $subdomain['subdomain_name'],
                );
                $pra = array_merge($pra, $ssl);
                $value = $kas->action('update_ssl', $pra);
                $text .= $value."\n";
            }
        }
        $data = [
            'values' => $user_shops,
            'text' => $text,
        ];

        return view('sys.admin.domain-ssl', $data);



    }
    public function domainSSLStore()
    {
        $data = Request::all();
        \Session()->flash('alert-save', true);
        return back();
    }

    public function shoppingOrders()
    {
        $shopping_users = ShoppingUser::all();
        $data = [
            'values' => $shopping_users,
            'text' => '',
        ];
        return view('sys.admin.shopping-orders', $data);

    }
    public function shoppingOrdersStore()
    {
        //first run
        $data = Request::all();

        if($data['action'] === 'first_run'){
            $shopping_users = ShoppingUser::whereHas('shopping_order', function($q) {
                $q->where('txaction', 'paid')->OrWhere('txaction', 'appointed');
            })->get();

            $order_email = [];
            $order_number = [];

            foreach ($shopping_users as $shopping_user){
                $order_email[$shopping_user->billing_email] = isset($order_email[$shopping_user->billing_email]) ? $order_email[$shopping_user->billing_email] + 1 : 1;
                if($shopping_user->number) {
                    $order_number[$shopping_user->number] = isset($order_number[$shopping_user->number]) ? $order_number[$shopping_user->number] + 1 : 1;
                    $shopping_user->orders = $order_number[$shopping_user->number];
                }else {
                    $shopping_user->orders = $order_email[$shopping_user->billing_email];
                }
                $shopping_user->save();

            }
            \Session()->flash('alert-save', true);
        }

        if($data['action'] === 'next_run'){
           Shop::userOrders();
            \Session()->flash('alert-save', true);
        }
        return back();

    }



}