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

namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
class ThriftController extends AppController
{
    var $helpers = array( 'Html' );
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Paginator');
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {
        $thrifts = $this->Thrift->find('all', ['conditions' => ['Thrift.is_deleted' => 0]] );
            $result = array();
    foreach($thrifts as $key=>$member){
        $mem_no= $member->mem_no;
        $this->loadModel('Member');
        $query = $this->Member->find('all', ['conditions' => ['Member.mem_no'=>$mem_no]] );
        $mem  = $query->first(); 
        $member->mem_name =$mem ? $mem->mem_name :"";
         $result[$key] = $member;
    }
        $this->set(compact('thrifts'));
    }

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

    public function add()
    {
        $this->loadModel('Member');
        $member = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no' ));
        $this->set('member',$member);
        if ($this->request->is('post')) {
            $mem_no= $this->request->getData('mem_no');
            $thrift = $this->Thrift->find('all', ['conditions' => ['Thrift.is_deleted' => 0,'Thrift.mem_no' => $mem_no]] );
          $k=0;
            foreach($thrift as $th)
            {
               $k=1;
            }
           
            if($k==1){
        $thrift = TableRegistry::get("Thrift");
        $query = $thrift->query();
                    $result = $query->update()
                            ->set(['thrift_amt' => $this->request->getData('thrift_amt')])
                            ->where(['mem_no' => $this->request->getData('mem_no')])
                            ->execute();
        $this->Flash->success(__('thrift has been added successfully.'));
        return $this->redirect(['action' => 'index']);
        }
        else{
            $thriftadd = $this->Thrift->newEntity();
            $this->Thrift->patchEntity($thriftadd,$this->request->getData());
             $this->Thrift->save($thriftadd);
             $this->Flash->success(__('thrift has been added successfully.'));
             return $this->redirect(['action' => 'index']);
        }
    }
    }
    // Add the following method.

public function edit($mem_no)
{
    $this->loadModel('Member');
        $member = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no' ));
        $this->set('member',$member);
    $thrift = $this->Thrift->findByMem_no($mem_no)->firstOrFail();
    if ($this->request->is(['post', 'put'])) {
        $this->Thrift->patchEntity($thrift, $this->request->getData());
        if ($this->Thrift->save($thrift)) {
            $this->Flash->success(__('thrift has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update thrift.'));
    }

    $this->set('thrift', $thrift);
}
public function delete($mem_no)
{
    $thrift = $this->Thrift->findByMem_no($mem_no)->firstOrFail();
   /* $this->Thrift->thrift_id = $this->Thrift->find('all', ['conditions' => ['Thrift.mem_no' => $mem_no]] );
    if ($this->Thrift->thrift_id) {
        $this->Thrift->save('is_deleted', 1);
        $this->Flash->success(__('The {0} has been deleted.', $thrift->mem_no));
        return $this->redirect(['action' => 'index']);
    }*/
    $this->Thrift->updateAll(
        array('is_deleted' => "1"),
        array('mem_no' => $mem_no)
    );
    return $this->redirect(['action' => 'index']);
}
}
?>