<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
/**
 * Feedbacks Controller
 *
 * @property \App\Model\Table\FeedbacksTable $Feedbacks
 *
 * @method \App\Model\Entity\Feedback[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */

 require_once(ROOT . DS  .'vendor' . DS  . 'S3.php');
class FeedbacksController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Http\Response|void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Orders']
        ];
        $feedbacks = $this->paginate($this->Feedbacks);

        $this->set(compact('feedbacks'));
    }

    /**
     * View method
     *
     * @param string|null $id Feedback id.
     * @return \Cake\Http\Response|void
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $feedback = $this->Feedbacks->get($id, [
            'contain' => ['Orders']
        ]);

        $this->set('feedback', $feedback);
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $feedback = $this->Feedbacks->newEntity();
        if ($this->request->is('post')) {
            $feedback = $this->Feedbacks->patchEntity($feedback, $this->request->getData());
            if ($this->Feedbacks->save($feedback)) {
                $this->Flash->success(__('The feedback has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The feedback could not be saved. Please, try again.'));
        }
        $orders = $this->Feedbacks->Orders->find('list', ['limit' => 200]);
        $this->set(compact('feedback', 'orders'));
    }

    /**
     * Edit method
     *
     * @param string|null $id Feedback id.
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $feedback = $this->Feedbacks->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $feedback = $this->Feedbacks->patchEntity($feedback, $this->request->getData());
            if ($this->Feedbacks->save($feedback)) {
                $this->Flash->success(__('The feedback has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The feedback could not be saved. Please, try again.'));
        }
        $orders = $this->Feedbacks->Orders->find('list', ['limit' => 200]);
        $this->set(compact('feedback', 'orders'));
    }

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

        return $this->redirect(['action' => 'index']);
    }
    public function feedbackSubmit()
    {
        $request =  $this->request->data;
        if ($this->request->is('post') && !empty($request['project_id'])) {
            $feedback = $this->Feedbacks->newEntity();
            $request['uploaded_by']=11;
            $request['status']=1;
            if(!empty($request['attachFile']) && $request['attachFile'][0]['name'] ){
                foreach($request['attachFile'] as $file){
                    $s3 = new \S3(awsAccessKey, awsSecretKey);
                    $res = $s3->putObjectFile($file['tmp_name'], bucketName, 'workolics/feedback/'.$file['name'], \S3::ACL_PUBLIC_READ,[],$file['type']);
                    $attachment['attachment'][]='feedback/'.$file['name'];
                }
                $files = json_encode($attachment);
                $request['attachment'] = $files;
            }else{
                unset($request['attachment']);
            }
            $feedback = $this->Feedbacks->patchEntity($feedback, $request);

            if ($this->Feedbacks->save($feedback)) {
                $this->loadModel('Projects');
                $projects = TableRegistry::get("Projects");
                $query = $projects->query();
                $result = $query->update()
                        ->set(['status' => 'Feedback'])
                        ->where(['id' => $request['project_id']])
                        ->execute();
                $this->Flash->success(__('The feedback has been saved.'));
            }else {
                $this->Flash->error(__('The feedback could not be saved. Please, try again.'));
            }
        }
        exit;
        return $this->redirect(['action' => 'index']);
        
    }
}
