<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Referral extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('referral');
        $data['title'] = 'arifahpulsa';
        $data['halaman'] = 'Referral';

        $this->template->load('includes/header', 'view_referral', $data);
    }

    function aksi_data()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('hadiah');

        $db_arifahpulsa_api = $this->load->database('db_arifahpulsa_api', TRUE);
        $query = $db_arifahpulsa_api->query("SELECT * FROM tbl_mst_bonus 
                                          ORDER BY mst_bonus_id DESC ");

        echo "<table id='example1' class='table table-bordered table-striped'>";
        echo    "<thead>
                    <tr>
                        <th>NO</th>
                        <th>AKSI</th>
                        <th>KODE</th>
                        <th>NAMA</th>
                        <th>POIN</th>
                        <th>ADMIN</th>
                        <th>HARGA</th>
                        <th>KETERANGAN</th>
                        <th>NOTASI</th>
                    </tr>
                </thead
                <tbody>";

        $no = 1;
        foreach ($query->result() as $row) {
            $aksi = "<div class='btn-group'>
                        <button type='button' class='btn btn-danger'>PILIH</button>
                        <button type='button' class='btn btn-danger dropdown-toggle dropdown-icon' data-toggle='dropdown'>
                        <span class='sr-only'>Toggle Dropdown</span>
                        <div style='position: relative;float: none;' class='dropdown-menu' role='menu'>
                            <a class='dropdown-item' onclick=aksi_edit_modal(this)>EDIT</a>
                        </div>
                        </button>
                    </div>";

            //ISI
            if (strlen($row->mst_bonus_keterangan) > 100) {
                $words = explode(' ', $row->mst_bonus_keterangan);
                $limitedWords = array_slice($words, 0, 15);
                $mst_bonus_keterangan = implode(' ', $limitedWords);
            } else {
                $mst_bonus_keterangan = $row->mst_bonus_keterangan;
            }


            echo        "<tr>";
            echo            "<td>" . $no++ . "</td>";
            echo            "<td>$aksi </td>";
            echo            "<td id='tbl_mst_bonus_kode'>$row->mst_bonus_kode</td>";
            echo            "<td id='tbl_mst_bonus_nama'>$row->mst_bonus_nama</td>";
            echo            "<td id='tbl_mst_bonus_poin'>" . number_format($row->mst_bonus_poin, 0, ',', '.') . "</td>";
            echo            "<td id='tbl_mst_bonus_admin'>$row->mst_bonus_admin</td>";
            echo            "<td id='tbl_mst_bonus_harga'>" . number_format($row->mst_bonus_harga, 0, ',', '.') . "</td>";
            echo            "<td id='tbl_mst_bonus_keterangan'>$mst_bonus_keterangan</td> ";
            echo            "<td id='tbl_mst_bonus_notasi'>$row->mst_bonus_notasi</td>";
            echo        "</tr>";
        }
        echo        "</tbody>";
        echo    "</table>";
        echo "<script>
              
                $(document).ready(function () {
                    var table = $('#example1').DataTable({
                        lengthChange : true, 
                        autoWidth: true,
                        paging: true,
                        buttons: ['copy', 'print'],
                    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
                
                  
                });
                
            </script>";
    }

    function aksi_edit()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('hadiah');

        $db_arifahpulsa_api = $this->load->database('db_arifahpulsa_api', TRUE);

        $mst_bonus_kode = $this->input->post("mst_bonus_kode");
        $mst_bonus_nama = $this->input->post("mst_bonus_nama");
        $mst_bonus_poin = $this->input->post("mst_bonus_poin");
        $mst_bonus_admin = str_replace(str_split('\\/:*?"<>|,.-+'), "", $this->input->post("mst_bonus_admin"));
        $mst_bonus_harga = str_replace(str_split('\\/:*?"<>|,.-+'), "", $this->input->post("mst_bonus_harga"));
        $mst_bonus_notasi = $this->input->post("mst_bonus_notasi");
        $mst_bonus_keterangan = $this->input->post("mst_bonus_keterangan");

        $db_arifahpulsa_api->query("UPDATE tbl_mst_bonus set
                            mst_bonus_nama='" . str_replace("'", "\'", $mst_bonus_nama) . "',
                            mst_bonus_poin='$mst_bonus_poin',
                            mst_bonus_admin='$mst_bonus_admin',
                            mst_bonus_harga='$mst_bonus_harga',
                            mst_bonus_notasi='$mst_bonus_notasi',
                            mst_bonus_keterangan='$mst_bonus_keterangan'
                            where mst_bonus_kode='$mst_bonus_kode'");

        $data['edit_status'] = '0';
        echo json_encode($data);
    }
}
