<?php
namespace App\Controller;

use App\Controller\AppsController;
use Cake\I18n\I18n;
use Cake\ORM\TableRegistry;
use DateTime;
use Cake\ORM\Query;
use Cake\Routing\Router;

/**
 * Teams Controller
 *
 * @property \App\Model\Table\TeamsTable $Teams
 */
class ContentsController extends AppsController
{

    
    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        return $this->redirect(['action' => 'add','action' => 'add']);
        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('user_permission', $user_permissions);
        $this->viewBuilder()->layout("custom_layout");
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
            $category_id = 0;
            $sub_category_id = 0;
        if ($this->request->is('post')) {
            $category_id = $this->request->data['category_id'];
            $sub_category_id = $this->request->data['sub_category_id'];
			$page = $this->request->data['content']['page'];
			$html = $this->request->data['content']['html'];

            $folderName = '';
            $folderName = $this->getCategoryNameById($category_id);
            
            $myfile = fopen(WWW_ROOT.$folderName.'/'.$page.".html", "w") or die("Unable to open file!");
            fwrite($myfile, $html);
            fclose($myfile);
            $this->Flash->success(__('The contents has been saved.'));
            // return $this->redirect(['action' => 'add','prev' => $page]);
            return $this->redirect(['action' => 'add']);
        }
        
        $getPrev = '';
        if(isset($_GET['prev']) && $_GET['prev']){
            $getPrev = $_GET['prev'];
        }else{
            $i = 0;
            foreach(json_decode(TEAM_AGE_CATEGORY) as $key => $value){
                if($i==0){
                    $getPrev = $key;
                    $i++;
                }else{
                    break;
                }
            }
        }

        $categoriesTable = TableRegistry::get('Categories');
        $categories = $categoriesTable->find('list')->where([
            'Categories.status' => 1,
            'Categories.type' => 2
        ]);
        $this->set(compact('categories'));

        $subCategoriesTable = TableRegistry::get('SubCategories');
        $subCategories = $subCategoriesTable->find('list')->where([
            'SubCategories.status' => 1
        ]);
        $this->set(compact('subCategories'));

        $formsTable = TableRegistry::get('Forms');
        $formsData = $formsTable->find('all')
        ->select(['short_name','long_name'])
        ->where([
            'Forms.status' => 1
        ]);
        
        $forms = array();
        if(!empty($formsData)){
            foreach ($formsData as $key => $value) {
                $forms[$value->long_name] = $value->short_name;
            }
        }
        $this->set(compact('forms'));
        
        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('user_permission', $user_permissions);
        $this->set('prev', $getPrev);
        $base_url = Router::url('/');
        $this->set('base_url', $base_url);
        $this->set('category_id', $category_id);
        $this->set('sub_category_id', $sub_category_id);
        $this->viewBuilder()->layout("custom_layout");
    }
	
	/**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function show()
    {
            $category_id = 0;
            $sub_category_id = 0;
        if ($this->request->is('post')) {
            $category_id = $this->request->data['category_id'];
            $sub_category_id = $this->request->data['sub_category_id'];
			$page = $this->request->data['content']['page'];
			$html = $this->request->data['content']['html'];

            $folderName = '';
            $folderName = $this->getCategoryNameById($category_id);
            
            $myfile = fopen(WWW_ROOT.$folderName.'/'.$page.".html", "w") or die("Unable to open file!");
            fwrite($myfile, $html);
            fclose($myfile);
            $this->Flash->success(__('The contents has been saved.'));
            // return $this->redirect(['action' => 'add','prev' => $page]);
            return $this->redirect(['action' => 'add']);
        }
        
        $getPrev = '';
        if(isset($_GET['prev']) && $_GET['prev']){
            $getPrev = $_GET['prev'];
        }else{
            $i = 0;
            foreach(json_decode(TEAM_AGE_CATEGORY) as $key => $value){
                if($i==0){
                    $getPrev = $key;
                    $i++;
                }else{
                    break;
                }
            }
        }

        $categoriesTable = TableRegistry::get('Categories');
        $categories = $categoriesTable->find('list')->where([
            'Categories.status' => 1
        ]);
        $this->set(compact('categories'));

        $subCategoriesTable = TableRegistry::get('SubCategories');
        $subCategories = $subCategoriesTable->find('list')->where([
            'SubCategories.status' => 1
        ]);
        $this->set(compact('subCategories'));

        $formsTable = TableRegistry::get('Forms');
        $formsData = $formsTable->find('all')
        ->select(['short_name','long_name'])
        ->where([
            'Forms.status' => 1
        ]);
        
        $forms = array();
        if(!empty($formsData)){
            foreach ($formsData as $key => $value) {
                $forms[$value->long_name] = $value->short_name;
            }
        }
        $this->set(compact('forms'));
        
        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('user_permission', $user_permissions);
        $this->set('prev', $getPrev);
        $base_url = Router::url('/');
        $this->set('base_url', $base_url);
        $this->set('category_id', $category_id);
        $this->set('sub_category_id', $sub_category_id);
        $this->viewBuilder()->layout("custom_layout");
    }

    public function loadformcontent()
    {
        $this->autorender = false; 
        $category_id = $this->request->data['cid'];
        $form_name = $this->request->data['form_name'];

        $base_url = Router::url('/');
        $base_url = 'https://'.$_SERVER['SERVER_NAME'].$base_url;
        $folderName = '';
        $folderName = $this->getCategoryNameById($category_id);
        $fileName = $form_name.".html";

        $file_dir = $folderName.'/'.$fileName;
        $fileContent = file_get_contents($file_dir);

        $txtJson = json_encode($fileContent);
        $this->response->type('json');
        $this->response->body($txtJson);
        return $this->response;
    }
}
