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

namespace App\Controller;

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

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

    public function index()
    {
        $insurances = $this->Insurance->find('all', ['conditions' => ['Insurance.is_deleted' => 0]] );
         $result = array();
            foreach($insurances 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('insurances'));
    }

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

    public function add()
    {
        $this->loadModel('Member');
        $member = $this->Member->find('list', array( 'valueField' => function ($row) {
            return $row['mem_no'] . '-' . $row['mem_name'];
        }, 'keyField' => 'mem_no'  ));
        $this->set('member',$member);
        if ($this->request->is('post')) {
        $insurance = TableRegistry::get("Insurance");
        $query = $insurance->query();
        $result = $query->insert(['mem_no', 'insurance','added_on'])
        ->values([
            'mem_no' => $this->request->getData('mem_no') ,
            'insurance' => $this->request->getData('insurance'),
            'added_on' => $this->request->getData('added_on')
        ])
                ->execute();
        return $this->redirect(['action' => 'index']);
        }
    }
    // Add the following method.

public function edit($mem_no)
{
    $this->loadModel('Member');
        $member = $this->Member->find('list', array( 'valueField' => function ($row) {
            return $row['mem_no'] . '-' . $row['mem_name'];
        }, 'keyField' => 'mem_no'  ));
        $this->set('member',$member);
        $this->loadModel('Insurance');
        $query = $this->Insurance->find('all', ['conditions' => ['Insurance.is_deleted' => 0,'Insurance.mem_no'=>$mem_no]] );
        $insurance  = $query->first(); 
    if ($this->request->is(['post', 'put'])) {
        $this->Insurance->patchEntity($insurance, $this->request->getData());
        if ($this->Insurance->save($insurance)) {
            $this->Flash->success(__('insurance has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update insurance.'));
    }

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