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

namespace App\Controller;

use App\Controller\AppController;

class ShareController extends AppController
{
    var $helpers = array( 'Html' );
    public function initialize()
    {
        parent::initialize();

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

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

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

    public function add()
    {
        $this->loadModel('Member');
        $member = $this->Member->find('list', array( 'keyField' => 'mem_no', 'valueField' => 'mem_no' ));
        $this->set('member',$member);
        $share = $this->Share->newEntity();
        if ($this->request->is('post')) {
            $share = $this->Share->patchEntity($share, $this->request->getData());
            if ($this->Share->save($share)) {
                $this->Flash->success(__(' share has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to add share.'));
        }
       
        $this->set('share', $share);
    }

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

    $this->set('share', $share);
}
public function delete($mem_no)
{
    $this->request->allowMethod(['post', 'delete']);
    $share = $this->Share->findByMem_no($mem_no)->firstOrFail();
    $this->Share->updateAll(
        array('is_deleted' => "1"),
        array('mem_no' => $mem_no)
    );
    return $this->redirect(['action' => 'index']);
}
}
?>