<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Bids Controller
 *
 * @property \App\Model\Table\BidsTable $Bids
 */
class BidsController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        
        $filter =0;$searchData=array();$condition = array();
        if ($this->request->is('post')) {
            $filter =1;
           
            if(isset($this->request->data['search']) && $this->request->data['search']!=''){
                $searchData['search'] = $this->request->data['search'];
                $newCondition['Projects.topic like'] = '%'.$this->request->data['search'].'%';
                $newCondition['Freelancers.name like'] = '%'.$this->request->data['search'].'%';               
                $condition['OR'] = $newCondition;
            }
          
            $bids = $this->Bids->find('all')->contain(['Projects', 'Freelancers'])->where($condition)->toArray();
           
        }else{            
            $this->paginate = [ 'contain' => ['Projects', 'Freelancers']];
            $bids = $this->paginate($this->Bids);

        }



        $this->set(compact('bids','filter','searchData'));
        $this->set('_serialize', ['bids']);
    }

    /**
     * View method
     *
     * @param string|null $id Bid id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $bid = $this->Bids->get($id, [
            'contain' => ['Projects', 'Freelancers']
        ]);

        $this->set('bid', $bid);
        $this->set('_serialize', ['bid']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $this->loadModel('Notify');
        $bid = $this->Bids->newEntity();
        if ($this->request->is('post')) {
            $bid = $this->Bids->patchEntity($bid, $this->request->data);
            if ($this->Bids->save($bid)) {

                $newNotification = $this->Notify->newEntity();
                $this->request->data['status']=1;
                $newNotification = $this->Notify->patchEntity($newNotification,$this->request->data);
                $this->Notify->save($newNotification);
//                pr($newNotification);exit;
                $this->Flash->success(__('The bid has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The bid could not be saved. Please, try again.'));
        }
          $projects = $this->Bids->Projects->find('all', ['fields'=>['id','topic'],'limit' => 200])->toList();
        $freelancers = $this->Bids->Freelancers->find('list', ['limit' => 200]);
        $this->set(compact('bid', 'projects', 'freelancers'));
        $this->set('_serialize', ['bid']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Bid id.
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $bid = $this->Bids->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $bid = $this->Bids->patchEntity($bid, $this->request->data);
            if ($this->Bids->save($bid)) {

                $this->Flash->success(__('The bid has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The bid could not be saved. Please, try again.'));
        }
        $projects = $this->Bids->Projects->find('list', ['limit' => 200]);
        $freelancers = $this->Bids->Freelancers->find('list', ['limit' => 200]);
        $this->set(compact('bid', 'projects', 'freelancers'));
        $this->set('_serialize', ['bid']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Bid id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $bid = $this->Bids->get($id);
        if ($this->Bids->delete($bid)) {
            $this->Flash->success(__('The bid has been deleted.'));
        } else {
            $this->Flash->error(__('The bid could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
