<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends Member_Controller {

    public function index() {

        $data = array();

        $mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"];
        $r = $this->db->query($mysql);
        $data["page_card"] = $r->result();



        $this->renderMemberPages('view_dash', $data);
    }

    public function page() {


        $data = array();
        
        
         $page_items = array(
            ['NOTECARD', 'Note Card', 'btn-primary'],
            ['VIDEOCARD', 'Video Content', 'btn-danger'],
            ['ACTIONITEM', 'Action Item', 'btn-secondary'],
            ['FORUMITEM', 'Forum Item', 'btn-success'],
            ['TASKCARD', 'Task', 'btn-danger'],
            ['RESOURCES', 'Resources', 'btn-light'],
            ['RESEARCH', 'Research', 'btn-info']
        );
         
        $data['page_items'] = $page_items;
         
       $page_toindex = true;

        if ($_SERVER['REQUEST_METHOD'] == 'GET') {

            $page_key = $this->input->get('pgname');
            $member_id = $_SESSION["member_id"];
            if ($member_id > 0 && $page_key != '') {
                //echo 'yes'; 
                $mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"] . " AND page_key='$page_key'";
                $r = $this->db->query($mysql);
                $drow = $r->row();

                if (isset($drow)) {
                    $data['title'] = $drow->title;
                    $data['description'] = $drow->description;
                    $data['what_page'] = $drow->page_mode;
                    $this->renderMemberPages('view_mypage', $data);
                   $page_toindex = false;
                }
            }
        }
       if( true == $page_toindex ){
             $this->index();
       }
      
        
    }

    public function viewCardAddAction(){
        ///member/viewCardAddAction?proc=PROCESS&action_name="+action_name+"&member_id=" + member_id + "&limit=" + limit + "&offset=" + offset
        $data =[];
        
          if ($_SERVER['REQUEST_METHOD'] == 'GET') {

            $action_name = $this->input->get('action_name');
            $member_id = $_SESSION["member_id"];
        /*    if ($member_id > 0 && $page_key != '') {
                //echo 'yes'; 
                $mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"] . " AND page_key='$page_key'";
                $r = $this->db->query($mysql);
                $drow = $r->row();

                if (isset($drow)) {
                    $data['title'] = $drow->title;
                    $data['description'] = $drow->description;
                    $data['what_page'] = $drow->page_mode;
                    $this->renderMemberPages('view_mypage', $data);
                   $page_toindex = false;
                }
            }
            */
            $this->load->view('member/addpages/'.$action_name, $data);
        }
        
        
        
     //   echo "Yes i got to the controller";
    }
    public function configure() {
        //  echo 'yes'; view_mypage

        $data = array();


        $mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"];
        $r = $this->db->query($mysql);
        $data["page_card"] = $r->result();


        //$mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"];
        // $r = $this->db->query($mysql);
        //  $data["page_card"] = $r->result();
        $this->renderMemberPages('view_configure', $data);
    }

    public function addcard() {

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            $data['description'] = trim($this->input->post('description'));
            $data['title'] = trim($this->input->post('title'));
            $data['member_id'] = $_SESSION["member_id"];
            $data['session'] = $_SESSION["sessionid"];



            if ($data['description'] != '' && $data['title'] != '') {
                $out = [];
                $this->coregrade_webapi(COREGRADE_ACCOUNT_ADDCARD, $data, $out);
            }

            //    exit();
        }





        $this->configure();
    }

    public function mycalendar() {
        $data = array();
        $this->renderMemberPages('view_mycalendar', $data);
    }

}
