<?php
// src/Controller/ArticlesController.php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
class MemberController extends AppController
{
    public $helpers = array('Html', 'Form');
    public $components = array('RequestHandler', 'Paginator', 'Flash');


    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
         //$this->Security->config('unlockedActions', ['sectionAjax', 'section_ajax']);
         //$this->Security->config('allowedActions', ['sectionAjax', 'section_ajax']);

        if(isset($this->Security) && ($this->request->is('ajax') || $this->request['action'] == 'index')) {
			$this->Security->config('unlockedActions', [$this->request['action']]);
		}
         
    }


  /*  public $components = ['Search'];
    
    public $paginate = [
        'limit'=>25
    ];


    public $searchFields = [
        'index'=>[
            'Member'=>[
                'Member.mem_no'=>[
                    'type'=>'select',
                    'condition'=>'comma',
                    'label'=>'Division',
                    'model'=>'Division',
                    'selector'=>'getUser',
                ],
                'Member.mem_no'=>[
                    'type'=>'select',
                    'condition'=>'comma',
                    'label'=>'Section',
                    'model'=>'Section',
                    'selector'=>'getUser',
                ],
                'Member.mem_name'=>[
                    'type'=>'text',
                    'condition'=>'=',
                    'label'=>'Member Name',
                    'inputOptions'=>['style'=>'wmem_noth:50px;']
                ],  
            ]
        ],
        
    ];

        'Users.user_group_mem_no'=>[
            'type'=>'select',
            'condition'=>'comma',
            'label'=>'Group',
            'model'=>'Usermgmt.UserGroups',
            'selector'=>'getUserGroups',
        ],
        'Users.email_verified'=>[
            'type'=>'select',
            'label'=>'Email Verified',
            'options'=>[''=>'Select', '0'=>'No', '1'=>'Yes']
        ],
        'Users.active'=>[
            'type'=>'select',
            'label'=>'Status',
            'options'=>[''=>'Select', '1'=>'Active', '0'=>'Inactive']
        ],
        'Users.created1'=>[
            'type'=>'text',
            'condition'=>'>=',
            'label'=>'From',
            'searchField'=>'created',
            'inputOptions'=>['style'=>'wmem_noth:100px;', 'class'=>'datepicker']
        ],
        'Users.created2'=>[
            'type'=>'text',
            'condition'=>'<=',
            'label'=>'To',
            'searchField'=>'created',
            'inputOptions'=>['style'=>'wmem_noth:100px;', 'class'=>'datepicker']
        ]*/

    
    public function index()
    {
        $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ,'order' => 'Division.division ASC'));
        $this->set('divisions',$divisions);

        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);

       /* $division_mem_no =  52;

        $this->loadModel('MapDivisionSection');
        $sections = $this->MapDivisionSection->find('all', ['conditions' => ['MapDivisionSection.division_id' => $division_mem_no], 'contain' => ['Section'] ] );
        $ss = $sections->toArray();*/

        //$this->paginate = ['limit'=>10, 'order'=>['Member.mem_no'=>'ASC'], 'contain' => ['Division'] ];
        //$this->Search->applySearch();*/
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0]] );
        $this->set(compact('members'));

      /*  if($this->request->is('ajax')) {
            $this->viewBuilder()->layout('ajax');
            $this->render('/Element/member-index');
        }*/
    }

    public function sectionajax()
    {
        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            $division = $this->request->query('division');
            $this->loadModel('Division');
            $division = $this->Division->find('all', ['conditions' => ['Division.division' => $division ]  ]);
            $row = $division->first();
            $division_mem_no = $row['id'];
            if($division){
                $this->loadModel('MapDivisionSection');
                $sections = $this->MapDivisionSection->find('all', ['conditions' => ['MapDivisionSection.division_id' => $division_mem_no], 'contain' => ['Section'] ,'order' => array('MapDivisionSection.section_id ASC')] );
                // Iterate it
foreach($sections as $postObject) {
    //echo $postObject->section_id;
    $division = $this->request->query('section');
            $this->loadModel('Section');
            $section1[] = $this->Section->find('all', ['conditions' => ['Section.id' => $postObject->section_id ]  ]);
}
                $resultJ = json_encode(array('result' => true, 'data' => $section1));

                $this->response->type('json');
                $this->response->body($resultJ);
                return $this->response;
            }else{
                $resultJ = json_encode(array('result' => false));
                $this->response->type('json');
                $this->response->body($resultJ);
                return $this->response;
            }
        }
    }

    public function view($mem_no)
    {
        
        $member = $this->Member->findByMem_no($mem_no)->firstOrFail();
        $this->set(compact('member'));
    }

    public function add()
    {
        $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ,'order' => 'Division.division ASC' ));
        $this->set('divisions',$divisions);

        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
        
        
        $this->loadModel('Designation');
        $designation = $this->Designation->find('list', array( 'keyField' => 'designation', 'valueField' => 'designation' ));
        $this->set('designation',$designation);
        
        $this->loadModel('Caste');
        $caste = $this->Caste->find('list', array( 'keyField' => 'caste', 'valueField' => 'caste' ));
        $this->set('caste',$caste);
        
        
        $this->loadModel('Relationship');
        $relationship = $this->Relationship->find('list', array( 'keyField' => 'relationship', 'valueField' => 'relationship' ));
        $this->set('relationship',$relationship);
        
         $this->loadModel('Member');
    $mem_no = $this->Member->find()
->select(['mem_no' => 'MAX(Member.mem_no)']);
$mem_no = $mem_no->first();
$re = $mem_no->mem_no+1;
 $this->set('re',$re);
        $member = $this->Member->newEntity();
        if ($this->request->is('post')) {
            $this->Member->patchEntity($member, $this->request->getData());
             $member->join_date=date('Y-m-d',strtotime($member->join_date));
            if ($this->Member->save($member)) {
                $mem_no = $this->request->getData('mem_no');
                $this->set('mem_no',$mem_no);
                    $this->loadModel('Thrift');
                $thrift = TableRegistry::get("Thrift");
                $query = $thrift->query();
                            $result =$query->insert(['mem_no', 'thrift_amt','tot_thrift'])
                            ->values([
                                'mem_no' => $this->request->getData('mem_no') ,
                                'thrift_amt' => '1000',
                                'tot_thrift' => '0'
                            ])
                            ->execute();
                            $this->loadModel('Loan');
                            $loan = TableRegistry::get("Loan");
                            $query = $loan->query();
                                        $result =$query->insert(['mem_no'])
                                        ->values([
                                            'mem_no' => $this->request->getData('mem_no') ,
                                        ])
                                        ->execute();
                                        
                                                  $this->loadModel('Insurance');
                            $ins = TableRegistry::get("Insurance");
                            $query = $ins->query();
                                        $result =$query->insert(['mem_no'])
                                        ->values([
                                            'mem_no' => $this->request->getData('mem_no') ,
                                        ])
                                        ->execute();
                                        
                                    $this->loadModel('Share');
                $share = TableRegistry::get("Share");
                $query = $share->query();
                            $result = $query->insert(['mem_no', 'share_amt'])
                            ->values([
                                'mem_no' => $this->request->getData('mem_no') ,
                                'share_amt' => '100'
                            ])
                                    ->execute();
                                    $this->loadModel('Months');
                                    $query = $this->Months->find('all', ['conditions' => ['Months.id' => date('m')]] );
                                    $months  = $query->first(); 
                                    $payment_month= $months->month;
                                   
                                     $this->loadModel('Payment');
                                     $rec_no = $this->Payment->find()
                                 ->select(['rec_no' => 'MAX(Payment.rec_no)']);
                                 $rec_no = $rec_no->first();
                                 $re = $rec_no->rec_no;
                                 $re=$re+1;
                                 $this->loadModel('Payment');
                $pay = TableRegistry::get("Payment");
                $query = $pay->query();
                            $result = $query->insert(['mem_no', 'share_amt','tot_pay','ent_fees','payment_month','payment_year','paid_on','rec_no'])
                            ->values([
                                'mem_no' => $this->request->getData('mem_no') ,
                                'share_amt' => '100',
                                'tot_pay' => '110',
                                'ent_fees' => '10',
                                'payment_month' => $payment_month,
                                'payment_year' => date('Y'),
                                'paid_on'=>date("Y-m-d"),
                                'rec_no' => $re
                            ])
                                    ->execute();
                                     
       
    $mem_name = str_replace(' ', '%20', $this->request->getData('mem_name'));
    $mobileno = str_replace(' ', '%20', $this->request->getData('phone'));
    $mem_num = str_replace(' ', '%20', $this->request->getData('mem_no'));
    $url="http://brightsms.brighttechnologies.co.in/httpapi/httpapi?token=4fc1372211bcae6ea775183d27f176e8&sender=STUDIO&number=$mobileno&route=2&type=1&sms=Hi%20$mem_name.Welcome%20to%20our%20society.";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch);
                $this->Flash->success(__('member saved successfully'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to save member.'));
        }
    
        $this->set('member', $member);
    }
    // Add the following method.

public function edit($mem_no)
{
    $this->loadModel('Division');
    $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ));
    $this->set('divisions',$divisions);

    $this->loadModel('Section');
    $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
    $this->set('sections',$sections);
    
    $this->loadModel('Designation');
        $designation = $this->Designation->find('list', array( 'keyField' => 'designation', 'valueField' => 'designation' ));
        $this->set('designation',$designation);
        
         $this->loadModel('Caste');
        $caste = $this->Caste->find('list', array( 'keyField' => 'caste', 'valueField' => 'caste' ));
        $this->set('caste',$caste);
        
        
        $this->loadModel('Relationship');
        $relationship = $this->Relationship->find('list', array( 'keyField' => 'relationship', 'valueField' => 'relationship' ));
        $this->set('relationship',$relationship);
    
    $member = $this->Member->findByMem_no($mem_no)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
        $member = $this->Member->patchEntity($member, $this->request->getData());
        $article->mem_no = 1;
        if ($this->Member->save($member)) {
            $this->Flash->success(__('member has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update member.'));
    }

    $this->set('member', $member);
}
public function act($mem_no)
{
    $query = $this->Member->findByMem_no($mem_no)->firstOrFail();
    $this->Member->updateAll(
        array('is_deleted' => "0"),
        array('mem_no' => $mem_no)
    );
    return $this->redirect(['action' => 'active']);
}
public function deact($mem_no)
{
    $query = $this->Member->findByMem_no($mem_no)->firstOrFail();
    $this->Member->updateAll(
        array('is_deleted' => "1"),
        array('mem_no' => $mem_no)
    );
    return $this->redirect(['action' => 'deactive']);
}
public function end($mem_no)
{
    $query = $this->Member->findByMem_no($mem_no)->firstOrFail();
    $this->Member->updateAll(
        array('is_deleted' => "2"),
        array('mem_no' => $mem_no)
    );
    return $this->redirect(['action' => 'close']);
}
public function active()
{
    $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ,'order' => 'Division.division ASC' ));
        $this->set('divisions',$divisions);
        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 1]] );
        $this->set(compact('members'));

}
public function deactive()
{
    $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ,'order' => 'Division.division ASC' ));
        $this->set('divisions',$divisions);
        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0]] );
        $this->set(compact('members'));
}
public function close()
{
    $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ,'order' => 'Division.division ASC' ));
        $this->set('divisions',$divisions);
        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 2]] );
        $this->set(compact('members'));

}

public function transfer()
    {
        
        $this->loadModel('Member');
        $members = $this->Member->find('all', ['conditions' => ['Member.is_deleted' => 0]] );
        $this->set(compact('members'));
        $mem_no = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no'  ));
        $this->set('mem_no',$mem_no);
        $this->loadModel('Division');
        $divisions = $this->Division->find('list', array( 'keyField' => 'division', 'valueField' => 'division' ,'order' => 'Division.division ASC' ));
        $this->set('divisions',$divisions);

        $this->loadModel('Section');
        $sections = $this->Section->find('list', array( 'keyField' => 'section', 'valueField' => 'section' ));
        $this->set('sections',$sections);
        
    
        if ($this->request->is('post')) {
           
                $this->loadModel('Member');
                $member = TableRegistry::get("Member");
                $query = $member->query();
                $result = $query->update()
                        ->set(['division' => $this->request->getData('division'),'section'=>$this->request->getData('section')])
                        ->where(['mem_no' => $this->request->getData('mem_no')])
                        ->execute();
                $this->Flash->success(__('Member Transfered successfully'));
                return $this->redirect(['action' => 'index']);
            }
          
    }

}
?>