<?php

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

class Home extends MY_Controller {

    public function screen($slug = null) {
        Ci::set_layout_file('main_doorprize');
        $absen = Absen_model::get_dbo()->where('slug', $slug)->first();
        if (!$absen) {
            $this->session->set_flashdata('danger', 'absen tidak tersedia');
            Ci::view('home/screen_not_available');
        } else {
            Ci::view(__METHOD__, [
                'hide_banner' => true,
                'absen' => $absen
            ]);
        }
    }

    public function ajax_screen($slug = null) {
        $absen = Absen_model::get_dbo()->where('slug', $slug)->first();
        $last_absen = Visitor_absen_model::get_dbo('visitor_absen')
            ->select(
                'visitor_absen.*',
                'visitor_profile.name as nama_visitor',
                'visitor_profile.optic as nama_optic',
                'visitor_profile.table as table_number',
                'visitor_profile.card_name'
            )->leftJoin('user_profile as visitor_profile', 'visitor_absen.visitor_id', '=', 'visitor_profile.user_id')
            ->where('visitor_absen.absen_id', $absen->id)
            ->orderBy('visitor_absen.created_at', 'DESC')
            ->first();
        exit(json_encode([
            'status' => 1,
            'name' => $last_absen->nama_visitor,
            'html' => Ci::partials(__METHOD__, ['last_absen' => $last_absen, 'absen' => $absen])
        ]));
    }

//    public function index() {
//        require_once('vendor/autoload.php');
//
//        $client = new \GuzzleHttp\Client();
//
//        $response = $client->request('POST', 'https://chatapps.8x8.com/api/v1/subaccounts/EarworksConsulting_0pPEB_hq/messages', [
//            'body' => '{"user":{"msisdn":"+6281385189757","country":"ID"},"content":{"sms":{"encoding":"AUTO"},"text":"ehe"},"clientMessageId":"123","type":"Text"}',
//            'headers' => [
//                'accept' => 'application/json',
//                'authorization' => 'Bearer G2MJzYSWw6QjbIIh4U9yyqH3Y4E9pkGMSePL9ZhBQ',
//                'content-type' => 'application/json',
//            ],
//        ]);
//
//        echo $response->getBody();
//    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
//    public function index() {
//        if ($this->user) {
//            redirect('dashboard/index');
//        }
//       
//        Ci::view(__METHOD__);
//    }
//
//    public function view() {
//        $productModel = Ci::model('model_appproduct');
//        $product = $productModel->find();
//        Ci::view(__METHOD__, ['product' => $product]);
//    }
//
//    public function view_winner() {
//        Ci::set_metatag('refresh', '5');
//        $productModel = Ci::model('model_appproduct');
//        $product = $productModel->find();
//        $kuponModel = Ci::model('model_apporder');
//        $kupon = $kuponModel->get_screen_winner();
//        Ci::view(__METHOD__, ['product' => $product, 'kupon' => $kupon]);
//    }
//    public function ajax_view_winner(){
//        $kuponModel = Ci::model('model_apporder');
//        $kupon = $kuponModel->get_screen_winner();
//        echo Ci::partials(__METHOD__, ['kupon' => $kupon]);
//        exit;
//    }
//    public function winner(){
//        $model = Ci::model('model_apporder');
//        Ci::view(__METHOD__);
//    }
//    public function ajax_winner(){
//        $model = Ci::model('model_apporder');
//        $data = $model->get_last_winner();
//        $page = Ci::partials(__METHOD__, ['win_coupon' => $data]);
//        exit(json_encode([
//            'status' => (Ci::post('last_id') == $data['ordId']) ? 2 : 1,
//            'newId' => $data['ordId'],
//            'html' => $page
//        ]));
//    }
//    public function ajax_get_order() {
//        $model = Ci::model('model_apporder');
//        $data = $model->get_screen_order();
//        $arranged = [];
//        $limit_per_product = [];
////        $limit_all_product = 20;
//        foreach ($data as $res) {
//            if (!isset($limit_per_product[$res['productId']])) {
//                $limit_per_product[$res['productId']] = 1;
//            } else {
//
//                $limit_per_product[$res['productId']] = $limit_per_product[$res['productId']] + 1;
//            }
////            if ($limit_per_product[$res['productId']] <= $limit_all_product) {
//            $arranged[$res['productId']][$res['ordId']] = $res;
////            }
//        }
//        exit(json_encode($arranged));
//    }
//
//    public function ajax_get_winner() {
//        $model = Ci::model('model_apporder');
//        $data = $model->get_screen_winner();
//        $arranged = [];
//        $limit_per_product = [];
////        $limit_all_product = 20;
//        foreach ($data as $k => $res) {
//            $order = $k+1;
//            if (!isset($limit_per_product[$res['productId']])) {
//                $limit_per_product[$res['productId']] = 1;
//            } else {
//
//                $limit_per_product[$res['productId']] = $limit_per_product[$res['productId']] + 1;
//            }
//            $res['ordId'] = $order.$res['ordId'];
////            if ($limit_per_product[$res['productId']] <= $limit_all_product) {
//            $arranged[$res['productId']][$res['ordId']] = $res;
////            }
//        }
//        exit(json_encode($arranged));
//    }
//
//    public function import_sales() {
//        Ci::helper('form');
//        $excel = Ci::library('excel');
//        $model = Ci::model('model_usrmaster');
////        $data = $excel->readexcel('file');
//        if (isset($_FILES['fileExcel'])) {
//            $data = $excel->readexcel($_FILES['fileExcel']['tmp_name']);
//            if ($data) {
//                foreach ($data as $res) {
//                    $model->fill([
//                        'userPassword' => $res['D'],
//                        'userEmail' => $res['C'],
//                        'userName' => $res['C'],
//                        'userStatus' => 1,
//                        'userState' => 1,
//                        'profileName' => $res['B'],
//                        'profileEmail' => $res['C'],
//                        'profilePhone' => '',
//                        'profileAddress' => '',
//                        'profilePostCode' => '',
//                        'profileCity' => '',
//                        'authorityListId' => [
//                            0 => 3
//                        ],
//                    ]);
//                    $model->save_update();
//                    unset($model->userId);
//                }
//            }
//        }
//        echo Ci::partials(__METHOD__);
//    }
//
//    public function import_user() {
//        Ci::helper('form');
//        $excel = Ci::library('excel');
//        $model = Ci::model('model_usrmaster');
////        $data = $excel->readexcel('file');
//        if (isset($_FILES['fileExcel'])) {
//            $data = $excel->readexcel($_FILES['fileExcel']['tmp_name']);
//            if ($data) {
//                foreach ($data as $res) {
//                    $this->db->where('profileName', $res['E']);
//                    $query = $this->db->get('UsrProfile');
//                    $sales = $query->row_array();
//                    $model->fill([
//                        'userName' => $res['F'],
//                        'userEmail' => $res['D'],
//                        'profileEmail' => $res['D'],
//                        'userPassword' => $res['G'],
//                        'userMktId' => ($sales) ? $sales['userId'] : null,
//                        'userStatus' => 1,
//                        'userState' => 3,
//                        'profileName' => $res['C'],
//                        'profileCompany' => $res['B'],
//                        'authorityListId' => [
//                            0 => 2
//                        ],
//                    ]);
//                    $model->register_public();
//                    unset($model->userId);
//                }
//            }
//        }
//        echo Ci::partials(__METHOD__);
//    }
//
//    public function asd() {
//        Ci::helper('form');
//        $excel = Ci::library('excel');
//        $model = Ci::model('model_usrmaster');
////        $data = $excel->readexcel('file');
//        if (isset($_FILES['fileExcel'])) {
//            $data = $excel->readexcel($_FILES['fileExcel']['tmp_name']);
//            if ($data) {
//                foreach ($data as $res) {
//                    $this->db->insert('UsrProfile', [
//                        'userId' => $res['B'],
//                        'profileName' => $res['D'],
//                        'profileCompany' => $res['C'],
//                    ]);
//                }
//            }
//        }
//        echo Ci::partials(__METHOD__);
//    }
}
