<?php
namespace App\Controller\Admin01;

use Stripe\Stripe;

/**
 * Users Controller
 *
 * @property \App\Model\Table\UsersTable $Users
 *
 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class UsersController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Http\Response|void
     */
    public function index()
    {
        $users = $this->paginate($this->Users);

        $this->set(compact('users'));
    }

    /**
     * View method
     *
     * @param string|null $id User id.
     * @return \Cake\Http\Response|void
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $user = $this->Users->get($id, [
            //'contain' => ['Addresses', 'Lines', 'Sales']
            'contain' => ['Addresses']
        ]);

        $this->set('user', $user);
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->admin01_success(__('The user has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->admin01_error(__('The user could not be saved. Please, try again.'));
        }
        $this->set(compact('user'));
    }

    
    public function edit($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            
            if (in_array('password', $this->request->data) && $this->request->data['password'] == '') {
                unset($this->request->data['password']);
            }
            
            $user = $this->Users->patchEntity($user, $this->request->getData());
            
            if ($this->Users->save($user)) {
                $this->Flash->admin01_success(__('The user has been saved.'));

                return $this->redirect($this->referer());
            }
            $this->Flash->admin01_error(__('The user could not be saved. Please, try again.'));
        }
        $this->set(compact('user'));
    }
    
    public function addresses($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => ['Addresses']
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->admin01_success(__('The user has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->admin01_error(__('The user could not be saved. Please, try again.'));
        }
        
        $address = $this->Users->Addresses->newEntity();
        $this->set(compact('user','address'));
    }
    
    public function createStripeCustomer($id = null)
    {
        
    }
    
    public function stripeTest($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => ['Addresses']
        ]);
        
        //debug(env('STRIPE_API_SKEY'));die;
        
//        \Stripe\Stripe::setApiKey(env('STRIPE_API_SKEY'));
//
//        \Stripe\InvoiceItem::create([
//            'amount' => 1000,
//            'currency' => 'usd',
//            'customer' => 'cus_4fdAW5ftNQow1a',
//            'description' => 'One-time setup fee',
//        ]);
        
        
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->admin01_success(__('The user has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->admin01_error(__('The user could not be saved. Please, try again.'));
        }
        
        $address = $this->Users->Addresses->newEntity();
        $this->set(compact('user','address'));
    }

    /**
     * Delete method
     *
     * @param string|null $id User id.
     * @return \Cake\Http\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $user = $this->Users->get($id);
        if ($this->Users->delete($user)) {
            $this->Flash->admin01_success(__('The user has been deleted.'));
        } else {
            $this->Flash->admin01_error(__('The user could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
