<?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"] . " AND status = 1";
        $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');
            $data["page_key"] = $page_key;
            $member_id = $_SESSION["member_id"];
            if ($member_id > 0 && $page_key != '') {
                //echo 'yes'; 
                $mysql = "SELECT id AS page_id, * 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;
                    $data['page_id'] = $drow->page_id;
                    // now let us get the items for the page



                    $mysql = "SELECT * FROM members_page_item WHERE member_id = " . $_SESSION["member_id"] . " AND page_id = " . $data['page_id'];
                    $r = $this->db->query($mysql);
                    $data["page_card_items"] = $r->result();

                    $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"];
            $data["page_id"] = $this->input->get('page_id');
            $data["page_key"] = $this->input->get('page_key');

            /*    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 myprofile() {
        $data = [];
        $this->renderMemberPages('view_myprofile', $data);
    }

    public function configure() {
        //  echo 'yes'; view_mypage

        $data = array();
        $this->load->model('combo_model');
        $this->combo_model->defaultComboMessage = "Select Start Template [Optional]";
        $data["page_template"] = $this->combo_model->getPageTemplate("page_template", '');




        $mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"] . " AND status = 1";
        $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"];
            $data['page_id'] = trim($this->input->post('page_id'));


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

            //    exit();
        }
        $this->configure();
    }

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

            $data['member_id'] = $_SESSION["member_id"];
            $data['session'] = $_SESSION["sessionid"];
            $data['page_id'] = trim($this->input->get('page_id'));


            if ($data['page_id'] != '' && $data['member_id'] != '') {

                $mysql = "SELECT * FROM members_pages WHERE member_id = " . $_SESSION["member_id"] . " AND id = " . $data['page_id'];
                $r = $this->db->query($mysql);
                $data["pagei"] = $r->result()[0];

                //  print_r($data);

                $this->load->view('member/pages/manage_share', $data);

                //        $out = [];
                //       $this->coregrade_webapi(COREGRADE_ACCOUNT_ADDCARD, $data, $out);
            }
            //    exit();
        }
    }

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

            $data['member_id'] = $_SESSION["member_id"];
            $data['session'] = $_SESSION["sessionid"];
            $data['page_id'] = trim($this->input->get('page_id'));


            if ($data['page_id'] != '' && $data['member_id'] != '') {
                $mysql = "UPDATE members_pages SET status = 0 WHERE status = 1 AND member_id =" . $data['member_id'] . " AND id=" . $data['page_id'];
                $this->db->query($mysql);
                echo "Deleted";
            }
            //    exit();
        }
    }

    public function addNotecard() {
        $data = array();

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

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

//define('COREGRADE_ACCOUNT_ADDPAGECARD', 11029);
            if ($data['detail'] != '' && $data['title'] != '') {
                $out = [];
                $this->coregrade_webapi(COREGRADE_ACCOUNT_ADDPAGECARD, $data, $out);
            }


            //    exit();
        }


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

    public function upload() {
        set_time_limit(5000);
        
        $page_item_id = 0;
        $data = [];
        $page_key = "";
        if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload') {

            $page_key = $data['page_key'] = trim($this->input->post('page_key'));

            $data['title'] = "This s the ttitle" . rand(100, 9999);
            $data['detail'] = "This s the ttitle, This s the ttitle" . rand(100, 9999);
            $data['member_id'] = $_SESSION["member_id"];
            $data['session'] = $_SESSION["sessionid"];
            $data['sessionid'] = $_SESSION["sessionid"];
            $data['item_type'] = 'VIDEOCARD';
            $data['page_id'] = trim($this->input->post('page_id'));
            if ($data['detail'] != '' && $data['title'] != '') {
                $out = [];
                $this->coregrade_webapi(COREGRADE_ACCOUNT_ADDPAGECARD, $data, $out);
                $page_item_id = $out["page_item_id"];
            }
            //   print_r($data);
            if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK) {

                // get details of the uploaded file
                $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
                $fileName = $_FILES['uploadedFile']['name'];
                $fileSize = $_FILES['uploadedFile']['size'];
                $fileType = $_FILES['uploadedFile']['type'];
                $fileNameCmps = explode(".", $fileName);
                $fileExtension = strtolower(end($fileNameCmps));

                $newFileName = md5(time() . $fileName) . '.' . $fileExtension;
                $allowedfileExtensions = array('jpg', 'gif', 'png', 'mp4');
                if (in_array($fileExtension, $allowedfileExtensions)) {

                    // directory in which the uploaded file will be moved
                    $uploadFileDir = '/home/uploads/' . $_SESSION['myfolder']; // str_pad($_SESSION["member_id"], 10, "0", STR_PAD_LEFT);

                    if (!is_dir($uploadFileDir)) {
                        mkdir($uploadFileDir, 0777);
                    }

                    $uploadFileDir = '/home/uploads/' . $_SESSION['myfolder'] . "/page";
                    if (!is_dir($uploadFileDir)) {
                        mkdir($uploadFileDir, 0777);
                    }


                    $mypath = "testdir/subdir/test";
                    mkdir($mypath, 0777, TRUE);



                    $dest_path = $uploadFileDir . "/" . $newFileName;

                    if (move_uploaded_file($fileTmpPath, $dest_path)) {
                        $message = 'File is successfully uploaded.';
                        $x = [];
                        $x["filename"] = $fileName;
                        $x["filesize"] = $fileSize;
                        $x["filetype"] = $fileType;
                        $x["ext"] = $fileExtension;
                        $x["page_item_id"] = $page_item_id;
                        $x["page_id"] = $data['page_id'];
                        $x['member_id'] = $_SESSION["member_id"];
                        $x['session'] = $_SESSION["sessionid"];
                        $x['sessionid'] = $_SESSION["sessionid"];
                    } else {
                        $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
                    }
                }
                //   print_r($_FILES);
                //  echo $message;
                //  echo base_url("images/icons/edit.png");
            }
        }

        redirect("/member/page?pgname=$page_key", 'refresh');
    }

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

}
