<?php
namespace App\Controller;
use App\Controller\AppController;

use Cake\Core\Configure;
use Cake\Http\Client;

class ProfilesController extends AppController
{

    public function index(){

        $this->viewBuilder()->layout('blank');

        /*******************************/

	    $id = $this->Auth->user('id');
	
        $this->loadModel('Users');
        $responseUserProfile = $this->Users->get($id, [
                'contain' => []
            ]);
        $username = $responseUserProfile['username'];

        $this->loadModel('UserPersonals');
        $responseUserPersonal = $this->UserPersonals->find('all', [
            'conditions' => [
                
                'user_id' => $id ]
            ])->first();

        //********DropdownCountry********
        $this->loadModel('MasterCountries');
        $Country = $this->MasterCountries->find('list', [
            'conditions' => [
                'is_used' => true],
            'keyField' => 'id',
            'valueField' => 'country_name_th'
        ]);
        if(!empty($Country)) $Country = $Country->toArray();

        //********DropdownProvince********
        $this->loadModel('MasterProvinces');
        $Province = $this->MasterProvinces->find('list', [
            'conditions' => [
                'is_used' => true],
            'keyField' => 'id',
            'valueField' => 'province_name_th'
        ])
        ->where(['master_country_id =' => $responseUserPersonal['master_country_id']])
        ->order(['province_name_th']);
            // pr($Province);die;
        if(!empty($Province)) $Province = $Province->toArray();

            $res = $responseUserPersonal['birthdate'];
        if($res){
            $dateNow = @$res->i18nFormat('dd/MM/yyyy');
        }else{
            $dateNow = "";
        }

    /**
     * 
     * Function show user card
     * @Data 01/06/2018
     * @author  Prasong.p
     * 
     */

        $this->loadModel('UserCards');
        $UserCards = $this->UserCards->find('all')
         ->select($this->UserCards)
        ->select(
            'morg.org_name_th'
        )
        ->join([             
            'morg' => [
                'table' => 'master.master_organizations',
                'type' => 'INNER',
                'conditions' => [
                    'morg.id = userCards.organize_id'
                ],
            ]
        ])
        ->where([
            'userCards.user_id' => $id   
        ])
        ->order(['userCards.id' => 'ASC'])
        ->toArray();

    
        //$this->set(compact('UserCards'));


     
        //------- save update profile ----
        if ($this->request->is(['patch', 'post', 'put'])) {
            $data = $this->request->data;

            // pr($this->request->data);die;
            $this->loadModel('UserPersonals');
            $userPersonals = $this->UserPersonals->find('all',
                [      
                    'limit' => 1,
                    'conditions' => [
                        'user_id' => $data['Users']['user_id']
                    ]
                ]
            )->first();
            // pr($this->request->data['email']);die;
 
            if($userPersonals['email'] == $this->request->data['email']){
                $chkemail = true;
            }else{
                $useremails = $this->UserPersonals->find('all',
                [      
                    'limit' => 1,
                    'conditions' => [
                        'email' => $data['email']
                    ]
                ]
                )->first();

                if(empty($useremails)){
                    $chkemail = true;
                }else{
                    $chkemail = false;
                }
            }

            if($chkemail == true){
            
                $birthdate = explode("/", $this->request->data['UserPersonals']['birthdate']);
                $birthdate = $birthdate[2].'-'.$birthdate[1].'-'.$birthdate[0];
                // pr($userPersonals);die;
                $userPersonals = $this->UserPersonals->patchEntity($userPersonals, $data['UserPersonals']);
                $userPersonals['firstname_th'] = $this->request->data['UserPersonals']['firstname_th'];
                $userPersonals['lastname_th'] = $this->request->data['UserPersonals']['lastname_th'];
                $userPersonals['firstname_en'] = $this->request->data['UserPersonals']['firstname_th'];
                $userPersonals['lastname_en'] = $this->request->data['UserPersonals']['lastname_th'];
                $userPersonals['email'] = $this->request->data['email'];
                $userPersonals['birthdate'] = $birthdate;
                $userPersonals['user_id'] = $data['Users']['user_id'];
                $userPersonals['master_country_id'] = $this->request->data['master_country_id'];
                $userPersonals['master_province_id'] = $this->request->data['master_province_id'];
                $userPersonals['modified_by'] = $id;
                // pr($userPersonals);die;
                
                if ($this->UserPersonals->save($userPersonals)) {
                    $this->Flash->success(__('Update Complete.'));
                    return $this->redirect(['action' => 'index']);
                }
            }else{
                $this->Flash->error(__('Email Address already used by others.'));
            }
            $this->Flash->error(__('Not update. Please, try again.'));
        }
         //-------end  save update ----

        $this->set(compact('userPersonals', 'responseUserProfile',  'dateNow', 'username', 'Country', 'Province', 'responseUserPersonal','UserCards'));
        $this->set('_serialize', ['userPersonals', 'responseUserProfile', 'dateNow', 'username', 'Country', 'Province', 'responseUserPersonal','UserCards']);
    }

    
}

