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

class Master_token_pln extends CI_Controller
{
    /**
     * This is default constructor of the class
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln');
        $data['title'] = 'arifahpulsa';
        $data['halaman'] = 'Master Token PLN';

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



    function aksi_data()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln');

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

        $query = $db_arifahpulsa_api->query("SELECT * FROM tbl_mst_token_pln 
                                        ORDER BY mst_token_pln_urutan ASC ");

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

        $no = 1;
        foreach ($query->result() as $row) {
            if ($row->mst_token_pln_status == '0') {
                $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>
                            <a class='dropdown-item' onclick=aksi_hapus('" . $row->mst_token_pln_kode . "')>HAPUS</a>
                            <div class='dropdown-divider'></div>
                            <a class='dropdown-item' onclick=aksi_tidak_aktif('" . $row->mst_token_pln_kode . "')>SET TIDAK AKTIF</a>
                        </div>
                        </button>
                    </div>";
                $status = "<div class='btn btn-success'>AKTIF</div>";
            } else {
                $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>
                           <a class='dropdown-item' onclick=aksi_hapus('" . $row->mst_token_pln_kode . "')>HAPUS</a>
                            <div class='dropdown-divider'></div>
                            <a class='dropdown-item' onclick=aksi_aktif('" . $row->mst_token_pln_kode . "')>SET AKTIF</a>
                        </div>
                        </button>
                    </div>";
                $status = "<div class='btn btn-danger'>TIDAK AKTIF</div>";
            }


            echo        "<tr>";
            echo            "<td>" . $no++ . "</td>";
            echo            "<td>$aksi </td>";
            echo            "<td id='tbl_mst_token_pln_status'>$status</td>";
            echo            "<td id='tbl_mst_token_pln_kode'>$row->mst_token_pln_kode</td>";
            echo            "<td id='tbl_mst_token_pln_nama'>$row->mst_token_pln_nama</td>";
            echo            "<td id='tbl_mst_token_pln_admin'>" . number_format($row->mst_token_pln_admin, 0, ',', '.') . "</td> ";
            echo            "<td id='tbl_mst_token_pln_harga'>" . number_format($row->mst_token_pln_harga, 0, ',', '.') . "</td> ";
            echo            "<td id='tbl_mst_token_pln_total'>" . number_format($row->mst_token_pln_admin + $row->mst_token_pln_harga, 0, ',', '.') . "</td> ";
            echo            "<td id='tbl_mst_token_pln_poin'>$row->mst_token_pln_poin</td>";
            echo            "<td id='tbl_mst_token_pln_keterangan'>$row->mst_token_pln_keterangan</td> ";
            echo            "<td id='tbl_mst_token_pln_notasi'>$row->mst_token_pln_notasi</td>";
            echo            "<td id='tbl_mst_token_pln_urutan'>$row->mst_token_pln_urutan</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_reload()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln');

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

        $url = "https://api.digiflazz.com/v1/price-list";

        $header = array(
            'Content-Type: text/plain',
        );

        $data = array(
            'cmd' => 'prepaid',
            'username' => $this->config->item('digiflazz_api_username'),
            'sign' => md5($this->config->item('digiflazz_api_username') . $this->config->item('digiflazz_api_key') . 'pricelist')
        );

        $json = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        curl_close($ch);
        $response = json_decode($result, true)['data'];

        foreach ($response as $data) {
            //DIGIFLAZZ
            $buyer_sku_code = $data['buyer_sku_code'];
            $price = $data['price'];
            $seller_product_status = $data['seller_product_status'];
            $buyer_product_status = $data['buyer_product_status'];

            if ($seller_product_status === true && $buyer_product_status === true) {
                //AKTIF
                $tbl_mst_token_pln[] = array(
                    'mst_token_pln_kode' => $buyer_sku_code,
                    'mst_token_pln_harga' =>  $price,
                    'mst_token_pln_status' =>  '0'
                );
            } else {
                //TIDAK AKTIF
                $tbl_mst_token_pln[] = array(
                    'mst_token_pln_kode' => $buyer_sku_code,
                    'mst_token_pln_harga' =>  $price,
                    'mst_token_pln_status' =>  '1'
                );
            }
        }
        //UPDATE
        $db_arifahpulsa_api->update_batch('tbl_mst_token_pln', $tbl_mst_token_pln, 'mst_token_pln_kode');
    }

    function aksi_edit()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln/edit');

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

        $mst_token_pln_kode = $this->input->post("mst_token_pln_kode");
        $mst_token_pln_nama = $this->input->post("mst_token_pln_nama");
        $mst_token_pln_keterangan = $this->input->post("mst_token_pln_keterangan");
        $mst_token_pln_notasi = $this->input->post("mst_token_pln_notasi");
        $mst_token_pln_harga = str_replace(str_split('\\/:*?"<>|,.-+'), "", $this->input->post("mst_token_pln_harga"));
        $mst_token_pln_admin = str_replace(str_split('\\/:*?"<>|,.-+'), "", $this->input->post("mst_token_pln_admin"));
        $mst_token_pln_poin = $this->input->post("mst_token_pln_poin");
        $mst_token_pln_urutan = $this->input->post("mst_token_pln_urutan");

        $db_arifahpulsa_api->query("UPDATE tbl_mst_token_pln set
                                        mst_token_pln_nama='" . str_replace("'", "\'", $mst_token_pln_nama) . "',
                                        mst_token_pln_keterangan='" . str_replace("'", "\'", $mst_token_pln_keterangan) . "',
                                        mst_token_pln_notasi='" . str_replace("'", "\'", $mst_token_pln_notasi) . "',
                                        mst_token_pln_harga='$mst_token_pln_harga',
                                        mst_token_pln_admin='$mst_token_pln_admin',
                                        mst_token_pln_poin='$mst_token_pln_poin',
                                        mst_token_pln_urutan='$mst_token_pln_urutan'
                                        where  mst_token_pln_kode='$mst_token_pln_kode'");
    }

    function aksi_tambah()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln/tambah');

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

        $mst_token_pln_kode = $this->input->post("mst_token_pln_kode");

        $mst_token_pln_nama = $this->input->post("mst_token_pln_nama");
        $mst_token_pln_keterangan = $this->input->post("mst_token_pln_keterangan");
        $mst_token_pln_notasi = $this->input->post("mst_token_pln_notasi");
        $mst_token_pln_harga = str_replace(str_split('\\/:*?"<>|,.-+'), "", $this->input->post("mst_token_pln_harga"));
        $mst_token_pln_admin = str_replace(str_split('\\/:*?"<>|,.-+'), "", $this->input->post("mst_token_pln_admin"));
        $mst_token_pln_poin = $this->input->post("mst_token_pln_poin");
        $mst_token_pln_urutan = $this->input->post("mst_token_pln_urutan");

        $query = $db_arifahpulsa_api->query("SELECT * from tbl_mst_token_pln where mst_token_pln_kode='$mst_token_pln_kode'");

        if ($query->num_rows() > 0) {
            $data['tambah_status'] = '1';
            echo json_encode($data);
        } else {
            $db_arifahpulsa_api->query("insert into tbl_mst_token_pln (
                                mst_token_pln_kode,
								mst_token_pln_nama, 
								mst_token_pln_keterangan, 
								mst_token_pln_notasi, 
								mst_token_pln_harga,
								mst_token_pln_admin,
								mst_token_pln_poin,
								mst_token_pln_urutan) values (
                                '$mst_token_pln_kode', 
								'" . str_replace("'", "\'", $mst_token_pln_nama) . "', 
								'" . str_replace("'", "\'", $mst_token_pln_keterangan) . "',
								'" . str_replace("'", "\'", $mst_token_pln_notasi) . "',
								'$mst_token_pln_harga',
								'$mst_token_pln_admin', 
								'$mst_token_pln_poin', 
								'$mst_token_pln_urutan')");
            $data['tambah_status'] = '0';
            echo json_encode($data);
        }
    }

    function aksi_hapus($mst_token_pln_kode)
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln/hapus');

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

        $db_arifahpulsa_api->query("DELETE from tbl_mst_token_pln where mst_token_pln_kode='$mst_token_pln_kode'");
    }

    function aksi_tidak_aktif($mst_token_pln_kode)
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln/edit');

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

        $db_arifahpulsa_api->query("UPDATE tbl_mst_token_pln set
                                        mst_token_pln_status='1'
                                        where  mst_token_pln_kode='$mst_token_pln_kode'");
    }

    function aksi_aktif($mst_token_pln_kode)
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('master/master_token_pln/edit');

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

        $db_arifahpulsa_api->query("UPDATE tbl_mst_token_pln set
                                        mst_token_pln_status='0'
                                        where  mst_token_pln_kode='$mst_token_pln_kode'");
    }
}
