<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * UserCards Controller
 *
 * @property \App\Model\Table\UserCardsTable $UserCards
 *
 * @method \App\Model\Entity\UserCard[] paginate($object = null, array $settings = [])
 */
class UserCardsController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Http\Response|void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Users', 'Organizes']
        ];
        $userCards = $this->paginate($this->UserCards);

        $this->set(compact('userCards'));
        $this->set('_serialize', ['userCards']);
    }

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

        $this->set('userCard', $userCard);
        $this->set('_serialize', ['userCard']);
    }

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

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user card could not be saved. Please, try again.'));
        }
        $users = $this->UserCards->Users->find('list', ['limit' => 200]);
        $organizes = $this->UserCards->Organizes->find('list', ['limit' => 200]);
        $this->set(compact('userCard', 'users', 'organizes'));
        $this->set('_serialize', ['userCard']);
    }

    /**
     * Edit method
     *
     * @param string|null $id User Card id.
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $userCard = $this->UserCards->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $userCard = $this->UserCards->patchEntity($userCard, $this->request->getData());
            if ($this->UserCards->save($userCard)) {
                $this->Flash->success(__('The user card has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user card could not be saved. Please, try again.'));
        }
        $users = $this->UserCards->Users->find('list', ['limit' => 200]);
        $organizes = $this->UserCards->Organizes->find('list', ['limit' => 200]);
        $this->set(compact('userCard', 'users', 'organizes'));
        $this->set('_serialize', ['userCard']);
    }

    /**
     * Delete method
     *
     * @param string|null $id User Card 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']);
        $userCard = $this->UserCards->get($id);
        if ($this->UserCards->delete($userCard)) {
            $this->Flash->success(__('The user card has been deleted.'));
        } else {
            $this->Flash->error(__('The user card could not be deleted. Please, try again.'));
        }

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

    public function createCards()
    {
        $this->viewBuilder()->layout('blank');
        $this->loadModel('TempUserCards');
        $this->loadModel('MasterOrganizations');
        if ($this->request->is('post')) {
            // pr($this->Auth);die;
            // pr($this->request->data());die;
            $birthdate = explode("/", $this->request->data['UserCards']['birthdate']);
            $birthdate = $birthdate['2'].'-'.$birthdate['1'].'-'.$birthdate['0'];
            $TempUserCards = $this->TempUserCards->find('all', [
                'conditions' => [
                    'organize_id' => $this->request->data['UserCards']['organize_id'],
                    'card_code' => $this->request->data['UserCards']['employee'],
                    'birthdate' => $birthdate
                ]
            ])->toArray();
             $TempUserCards = $this->TempUserCards->get($TempUserCards[0]['id']);

           // pr($TempUserCards);die;
          //  pr($this->TempUserCards->get($TempUserCards[0]['id']));die;
            $userCard = $this->UserCards->newEntity();
            //pr($userCard);die;
            // $userCard = $this->UserCards->patchEntity($userCard, $TempUserCards);
            $userCard = $TempUserCards;
            $userCard['user_id'] = 1;
            $userCard['id'] = null;
            // pr($userCard->toArray());die;

            if ($this->UserCards->save($userCard)) {
                $this->Flash->success(__('The user card has been saved.'));
                die;
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user card could not be saved. Please, try again.'));

            // pr($TempUserCards);die;
        }
        
    }
    public function checkOrg()
    {
        $this->autoRender = false;
        if ($this->request->is('post')) {
            $this->loadModel('MasterOrganizations');
            $MasterOrganizations = $this->MasterOrganizations->find('all', [
                'fields' => [
                    'id','org_code',
                    'org_name_th'
                ], 'conditions' => [
                    'org_code' => $this->request->data['organize_code']
                ]
            ])->toArray();
            if(!empty($MasterOrganizations)){
                $data = json_encode($MasterOrganizations);
                echo $data;
            }else{
                echo 'false';
            }
        }
    }
}