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

class c_modules extends CI_Controller {
  function __construct(){
    parent::__construct();    
    $this->load->model('m_masters');
        $this->load->helper(array('url','html','form'));
        $this->load->library('session');
    ini_set('max_execution_time', 0);    
    ini_set('memory_limit', '20000M');
    date_default_timezone_set("Asia/Jakarta");

    $this->data = array(
      'intervalinactnotpickedup' => '3',
      'intervalinterested' => '30'
    );

     // if(empty($this->session->userdata("username")))
   //       {
   //         redirect(base_url(),'refresh');
   //       }
  }
  public function index()
  {
    if (empty($this->session->userdata('username')))
    { 
      redirect('c_login');
    }
    else
    {
      $data['title'] = "Main Modules";
      $this->load->view('v_modules',$data); 
      // echo $this->session->userdata('username') . ' - ' . $this->session->userdata('privilege') . ' - ' . $this->session->userdata('privileges') ;
    }
  }

  public function SignOut()
  { 
    session_unset();
    session_destroy();
    redirect('c_login');
  }

  public function settimerangelock()
  { 
    if(empty($this->session->userdata('username')))
    {
          echo "0";
    }
    else
    {
      $datefrom = $this->input->post("datefrom");
      $datefrom = date('Y-m-d', strtotime($datefrom));
      $datefrom = $datefrom . " 00:00:00";

      $dateto = $this->input->post("dateto");
      $dateto = date('Y-m-d', strtotime($dateto));
      $dateto = $dateto . " 23:59:59";

      $querygettotaldata = "select count(1) as datacount from (
      select a.*,b.maxcalldate from members_call a left join members_call_maxcalldate b on a.username = b.username and a.`group` = b.`group` where length(phoneno)>5 and last_deposit <> '' and last_deposit <= '".$dateto. "' and last_deposit >= '" .$datefrom . "'  order by maxcalldate , last_deposit
      )semuanya";
      $datacount = $this->m_masters->querying($querygettotaldata)->result()[0]->datacount;
      // echo $querygettotaldata . '<br>';

      $querystring = "update timerangelock set datefrom = '".$datefrom ."', dateto ='".$dateto."', initialdata = ".$datacount . ",lock_date = now()";
      $this->m_masters->querying($querystring);
      // echo $querystring . '<br>';


      $querystring = "insert into timerangelockhistory values ('".$datefrom."','".$dateto."', ".$datacount . ",now())";
      $this->m_masters->querying($querystring);
      // echo $querystring . '<br>';

      echo "Time Lock Success!";
    }  
  }


  public function preview()
  { 
    if(empty($this->session->userdata('username')))
    {
          echo "0";
    }
    else
    {
      $datefrom = $this->input->post("datefrom");
      $datefrom = date('Y-m-d', strtotime($datefrom));
    //   $datefrom = $datefrom . " 00:00:00";

      $dateto = $this->input->post("dateto");
      $dateto = date('Y-m-d', strtotime($dateto));
    //   $dateto = $dateto . " 00:00:00";


      // $querystring = "select b.tele, b.group, a.pendingcall from (
      //   select tele, count(1) as pendingcall  from members_call a  where tele<> '' and  last_deposit <> '' and last_deposit < concat(DATE_SUB(date(now()), INTERVAL 30 DAY), ' 00:00:00') and calldate = '' group by tele 
      //   )a
      //   inner join teles b on a.tele = b.tele
      //   order by a.pendingcall desc";

      // $data['reportresult'] = $this->m_masters->querying($querystring)->result();
      
      $querystring = "call reportcrm ('".$datefrom . "','".$dateto."')";
      $data['reportresult'] = $this->m_masters->querying($querystring)->result();
      
    //   echo $data['reportresult'][0]->currdate;
      $this->load->view('v_reports_dg',$data);

        
    }  
  }


  public function nextalert()
  { 
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $phoneno = $this->input->post("phoneno");
      $response = $this->input->post("response");
      $group = $this->input->post("group");
      if($response == 'blank')
      {
        echo "Pick Response!";
      }
      else
      { 
        $querystring = "update members_call set calldate = now(), response = '" .$response . "' where phoneno = '".$phoneno . "' and `group` = '" . $group . "'";
        // echo $querystring;
        $this->m_masters->querying($querystring);
        $this->inquiretelemarket();
      }
    }
  }

  public function inquiretelemarket()
    {
        if(empty($this->session->userdata('username')))
        {
          echo "0";
        }
        else
        {
          $errmsg = "";
          $DBName = "work";
          $data['website'] = $this->session->userdata('privilege');
                // $querysp = "select a.* from members_call a  where a.tele = '".$this->session->userdata('username'). "' and last_deposit <> '' and last_deposit <= concat(cast(date_sub(date_format(now(),'%Y-%m-%d'), INTERVAL 21 DAY) as char), ' 00:00:00') and last_deposit >= concat(cast(date_sub(date_format(now(),'%Y-%m-%d'), INTERVAL 60 DAY) as char), ' 00:00:00')  and calldate = '' order by last_deposit desc limit 1" ;
                
          $querystring = "select datefrom from timerangelock";
          $datefrom = $this->m_masters->querying($querystring)->result()[0]->datefrom;

          $querystring = "select dateto from timerangelock";
          $dateto = $this->m_masters->querying($querystring)->result()[0]->dateto;

                // $querysp = "select a.*,b.maxcalldate from members_call a left join members_call_maxcalldate b on a.username = b.username and a.`group` = b.`group` where a.tele = '".$this->session->userdata('username'). "' and length(phoneno)>5 and last_deposit <> '' and last_deposit <= concat(cast(date_sub(date_format(now(),'%Y-%m-%d'), INTERVAL 21 DAY) as char), ' 00:00:00') and last_deposit >= concat(cast(date_sub(date_format(now(),'%Y-%m-%d'), INTERVAL 60 DAY) as char), ' 00:00:00') and calldate = '' order by maxcalldate , last_deposit limit 1";

          $querysp = "select a.*,b.maxcalldate from members_call a left join members_call_maxcalldate b on a.username = b.username and a.`group` = b.`group` where a.tele = '".$this->session->userdata('username'). "' and length(phoneno)>5 and last_deposit <> '' and last_deposit <= '".$dateto. "' and last_deposit >= '" .$datefrom . "' and calldate = '' order by maxcalldate , last_deposit desc limit 1";
              // echo $querysp;
            $data['listphoneno'] = $this->m_masters->querying($querysp);
            $data['listscounts'] = $data['listphoneno']->num_rows();
            $data['listphonenoarr'] = $data['listphoneno']->result();
            
            if($this->session->userdata('privileges') <> 'Admin')
            {
                if($data['listscounts'] == 0 ) 
                { 

                  echo '<div class="alert alert-info alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <h5><i class="icon fas fa-info"></i> Info!</h5>
                            There is no more work to be done!
                          </div>' ;
            
                }
                else
                {

                    $this->load->view('v_ILK_dg',$data);
                }
            }
        }
    }
 
 
  
//   public function uploadmemberstotele()
//   {
//     $errmsg = '';
//     $now = DateTime::createFromFormat('U.u', microtime(true));
//     $currdatetime =  $now->format("m-d-Y H:i:s.u");
    
    
//     $imagecurrdatetime = str_replace(" ","_",$currdatetime);
//     $imagecurrdatetime = str_replace("-","_",$imagecurrdatetime);
//     $imagecurrdatetime = str_replace(":","_",$imagecurrdatetime);
//     $imagecurrdatetime = str_replace(".","_",$imagecurrdatetime);

//     if (empty($_FILES['filemember']['name']))
//     {
//       $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
//                   <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
//                   <h5><i class="icon fas fa-check"></i>Danger</h5>
//                     Please Upload File Members To Tele!
//                 </div>';
      
//       echo $errmsg;
//     }

//     // echo $_FILES['filemember']['name'];

//     else
//     {

//       $path = './assets/excels/';
//       require_once APPPATH . "/third_party/PHPExcel.php";
//       $config['upload_path'] = $path;
//       $config['allowed_types'] = 'xlsx|xls';
//       $config['remove_spaces'] = TRUE;
//       $config['max_execution_time'] = 259200;
//       $config['max_input_time'] = 259200;
//       $this->load->library('Upload', $config);
//       $this->upload->initialize($config);

//       if (!$this->upload->do_upload('filemember')) 
//       {
//           $error = array('error' => $this->upload->display_errors());
//       }

//       else
//       {
      
        
//         try
//         {

//           $querystring = "insert into memberstotelestorage select * from memberstotele";
//           $this->m_masters->querying($querystring);

//           $querystring = "delete from memberstotele";
//           $this->m_masters->querying($querystring);


//           // echo "a";
//           $inputFileName = $path . $_FILES['filemember']['name'];   
//           $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
//           $objReader = PHPExcel_IOFactory::createReader($inputFileType);
//           $objPHPExcel = $objReader->load($inputFileName);
//           $highestrow = $objPHPExcel->getActiveSheet()->getHighestRow();
//           $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray('A2:M'.$highestrow, true, true, true);
//           $i = 1;
//           foreach ($allDataInSheet as $value)
//           {
//                 // echo $value['C'] . "-";
//                 // echo $value['B'] . "<br>";      
//                 if($i > 1)
//                 {
//                   // echo $value['B'] . "<br>";
//                   $username[$i] = $value['D'];
//                   $group[$i] = $value['F'];
//                   $tele[$i] = $value['G'];
//                   $creator[$i] = $this->session->userdata('username');
//                   $username[$i] = str_replace(",","",$username[$i]);
//                   $username[$i] = str_replace("/","",$username[$i]);
//                   $username[$i] = str_replace("\\","",$username[$i]);

//                   // echo $username[$i] . "<br>";


//                   $querystring = "insert into memberstotele values('" . $username[$i] . "','" .$group[$i] . "','".$tele[$i]. "','" . $creator[$i] . "',now())";

//                   $this->m_masters->querying($querystring);
//                 }
//                 $i = $i + 1;

//           }


        
//         } catch (Exception $e)
//         {
//             die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
//                             . '": ' .$e->getMessage());
//         }

//         unlink($path . $_FILES['filemember']['name']);
//         echo "Finish Uploading for Members To Tele!";
      
//       }
//     }
//   }
  
  
  public function distributelembarkerja()
  {
    $querystring = "call distribute()";
    $this->m_masters->querying($querystring);

    $querystring = "select datefrom from timerangelock";
    $datefrom = $this->m_masters->querying($querystring)->result()[0]->datefrom;
    // echo $datefrom . '<br>';

    $querystring = "select dateto from timerangelock";
    $dateto = $this->m_masters->querying($querystring)->result()[0]->dateto;
    // echo $dateto . '<br>';
    
    
    $querystring = "select count(1) as datacount from (
      select a.*,b.maxcalldate from members_call a left join members_call_maxcalldate b on a.username = b.username and a.`group` = b.`group` where length(phoneno)>5 and last_deposit <> '' and last_deposit <= '".$dateto. "' and last_deposit >= '" .$datefrom . "'   order by maxcalldate , last_deposit
      )semuanya";
    $availabledata = $this->m_masters->querying($querystring)->result()[0]->datacount;
    // echo $querystring . '<br>';
    

    $queryinsert = "insert into workdatedatacount values (now(),'" .$availabledata. "')";
    $this->m_masters->querying($queryinsert);
    // echo $queryinsert . '<br>';

    echo "Data Cleansed and Successfully Distributed to Tele";
  }
  
  
  

  public function senddatatotele()
  {
    $group = $this->input->post("group");
    $match = $this->input->post("match");
    
    if ($match == "match") 
    {
        $tablename = "members_call";
        $tablename2 = "members_called";
    }
    else
    {
        $tablename = "members_call_ext";
        $tablename2 = "members_called_ext";
    }

    
    // echo $group;
    if($group == 'All')
    {
        $querystring = "call membercleansing()";
        $this->m_masters->querying($querystring);
        
        $querystring = "insert into " .$tablename2 ." select * from " .$tablename ." ";
        $this->m_masters->querying($querystring);
        // echo $querystring;
        
        $querystring = "delete from " .$tablename ;
        $this->m_masters->querying($querystring); 
        // echo $querystring;
        
        $querystring = "insert into " .$tablename ." select username, `group`, phoneno, last_deposit,'','',nama_rekening, '',uploaddate from members";
        $this->m_masters->querying($querystring);
        
        $querystring = "insert into " .$tablename ." select username, `group`, phone, lastlogin,'','',bankaccountname, '',uploaddate from members_NUSAWL";
        $this->m_masters->querying($querystring);
        
        $querystring = "insert into " .$tablename ." select loginid, `group`, contactnumber, lastlogintime,'','',memname, '',uploaddate from members_ZAPPORT";
        $this->m_masters->querying($querystring);
        
        $querystring = "insert into " .$tablename ." select username, `group`, telpon, date,'','',name,'',uploaddate from members_PAY4D";
        $this->m_masters->querying($querystring);
        // echo $querystring;
    }
    
    else
    {
        $querystring = "insert into " .$tablename2 ." select * from " .$tablename ." where `group` = '".$group."'";
        $this->m_masters->querying($querystring);
        // echo $querystring;
        
        $querystring = "delete from " .$tablename ." where `group` = '".$group."'";
        $this->m_masters->querying($querystring); 
        // echo $querystring;
     
        $querygetwl = "select wl from groups where `group` = '".$group."'";
        $wl = $this->m_masters->querying($querygetwl)->result()[0]->wl;
        
        if($wl == 'NEXUS')
        {
            $querystring = "call membercleansing()";
            $this->m_masters->querying($querystring);
            
            $querystring = "insert into " .$tablename ." select username, `group`, phoneno, last_deposit,'','',nama_rekening, '',uploaddate from members where `group` = '".$group."'";
            $this->m_masters->querying($querystring);
            // echo $querystring;
        }
        
        else if($wl == 'NUSAWL')
        {
            $querystring = "insert into " .$tablename ." select username, `group`, phone, lastlogin,'','',bankaccountname, '',uploaddate from members_NUSAWL where `group` = '".$group."'";
            $this->m_masters->querying($querystring);
        }
        
        else if($wl == 'ZAPPORT')
        {
            $querystring = "insert into " .$tablename ." select loginid, `group`, contactnumber, lastlogintime,'','',memname, '',uploaddate from members_ZAPPORT where `group` = '".$group."'";
            $this->m_masters->querying($querystring);
            
            
        }
        else if($wl == 'PAY4D')
        {
            $querystring = "insert into " .$tablename ." select username, `group`, telpon, date,'','',name,'',uploaddate from members_PAY4D where `group` = '".$group."'";
            $this->m_masters->querying($querystring);
        }
        
            
    }

    $querystring = "delete from " .$tablename ." where `group` = '" .$group."' and LENGTH(last_deposit) <> 19";
            $this->m_masters->querying($querystring);
    echo "Data Cleansed !";
  }



  public function vipsenddatatotele()
  {
    $group = $this->input->post("group");
    $match = $this->input->post("match");
    
    if ($match == "match") 
    {
        $tablename = "members_call_vip";
    }
    else
    {
        $tablename = "members_call_ext_vip";
    }

    if($group == 'All')
    {
        $querystring = "call vipmembercleansing()";
        $this->m_masters->querying($querystring);
        
    }
    
    // else
    // {
    //     $querygetwl = "select wl from groups where `group` = '".$group."'";
    //     $wl = $this->m_masters->querying($querygetwl)->result()[0]->wl;
        
    //     if($wl == 'NEXUS')
    //     {
    //         $querystring = "call vipmembercleansing()";
    //         $this->m_masters->querying($querystring);    
    //     }
    // }
    $querystring = "delete from " .$tablename ." where LENGTH(phoneno) < 8";
            $this->m_masters->querying($querystring);
    echo "Data Cleansed !";
  }




  

  public function uploadmembers()
  {

    $group = $this->input->post('group');
    $errmsg = '';
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $currdatetime =  $now->format("m-d-Y H:i:s.u");
    
    
    $querygetwl = "select wl from groups where `group` = '".$group."'";
    $wl = $this->m_masters->querying($querygetwl)->result()[0]->wl;
    

    $imagecurrdatetime = str_replace(" ","_",$currdatetime);
    $imagecurrdatetime = str_replace("-","_",$imagecurrdatetime);
    $imagecurrdatetime = str_replace(":","_",$imagecurrdatetime);
    $imagecurrdatetime = str_replace(".","_",$imagecurrdatetime);

    if (empty($_FILES['filemember']['name']) || $group == 'blank')
    {
      $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Danger</h5>
                    Please Choose Group and Upload File!
                </div>';
      
      echo $errmsg;
    }

    // echo $_FILES['filemember']['name'];

    else
    {
      
      $path = './assets/excels/';
      require_once APPPATH . "/third_party/PHPExcel.php";
      $config['upload_path'] = $path;
      $config['allowed_types'] = 'xlsx|xls|csv';
      $config['remove_spaces'] = TRUE;
      $config['max_execution_time'] = 259200;
      $config['max_input_time'] = 259200;
      $this->load->library('upload', $config);
      $this->upload->initialize($config);

      if (!$this->upload->do_upload('filemember')) 
      {
          $error = array('error' => $this->upload->display_errors());
      }

      else
      { 
        if($wl == 'NEXUS')
        {
            try
            {
              $querystring = "delete from members where `group` = '".$group . "'";
              $this->m_masters->querying($querystring);
              
              $inputFileName = $path . $_FILES['filemember']['name'];   
              $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
              $objReader = PHPExcel_IOFactory::createReader($inputFileType);
              $objPHPExcel = $objReader->load($inputFileName);
              $highestrow = $objPHPExcel->getActiveSheet()->getHighestRow();
              $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray('A2:M'.$highestrow, true, true, true);
              $i = 1 ;
              
            //   echo $inputFileName;
              
            //   print_r($allDataInSheet);
              foreach ($allDataInSheet as $value)
              {
                    // echo $value['C'] . "-";
                    // echo $value['B'] . "<br>";      
                    if($i > 1)
                    {
                      // echo $value['B'] . "<br>";
                      $regisdate[$i] = $value['B'];
                      $username[$i] = $value['C'];
                      $fullname[$i] = $value['D'];
                      $phoneno[$i] = $value['F'];
                      $status[$i] = $value['G'];
                      $lastlogin[$i] = $value['H'];
                      $referralcode[$i] = $value['J'];
                      $ownreferralcode[$i] = $value['K'];
                      $sourceinformation[$i] = $value['L'];
                      $lastdeposit[$i] = $value['M'];
                      $tele[$i] = '';
                      $creator[$i] = $this->session->userdata('username');
                      $username[$i] = str_replace(",","",$username[$i]);
                      $username[$i] = str_replace("/","",$username[$i]);
                      $username[$i] = str_replace("\\","",$username[$i]);
    
                    //   echo $username[$i] . "<br>";
                    
                    // $querystring = "insert into tracker values ('" .$i . "')";
                    // $this->m_masters->querying($querystring);
                        
                        

                      $querystring = "insert into members values('" . $regisdate[$i] . "','" .$username[$i] . "','".$fullname[$i]. "','" . $group . "','" . $phoneno[$i] . "','" .$status[$i]. "','" . $lastlogin[$i]. "','" . $lastdeposit[$i]. "','" . $referralcode[$i] . "','". $ownreferralcode[$i] . "','" . $sourceinformation[$i] . "','" . $creator[$i] . "',now())";
    
                      $this->m_masters->querying($querystring);
                    // echo $querystring;
                    }
                    // echo $i;
                    $i = $i + 1;
              }
            
            } catch (Exception $e)
            {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                                . '": ' .$e->getMessage());
            }
    
            
            $querystring = "insert into members_storage select * from members where `group` ='".$group. "'";
              $this->m_masters->querying($querystring);
            
            $querystring = "select a.`group`, b.datacount from groups a 
            left join 
            (
                select `group`, count(1) datacount from members where uploaddate >= concat(curdate(), ' 00:00:00')
                group by `group`
            ) b on a.`group` = b.`group`
            where b.datacount is null";
            $pendinggroup = $this->m_masters->querying($querystring)->result();
    
            unlink($path . $_FILES['filemember']['name']);
            echo "Finish Uploading for group : '".$group. "'! <br>";
            echo "Today pending group(s) : <br>";
    
            for($runner = 0;$runner < count($pendinggroup);$runner++)
            {
              echo $pendinggroup[$runner]->group . "<br>";
            }
        
        }
        
        else if ($wl == 'NUSAWL')
        {
            
            try
            {
              $querystring = "delete from members_NUSAWL where `group` = '".$group . "'";
              $this->m_masters->querying($querystring);
              
              $inputFileName = $path . $_FILES['filemember']['name'];   
              $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
              $objReader = PHPExcel_IOFactory::createReader($inputFileType);
              $objPHPExcel = $objReader->load($inputFileName);
              $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
              $i = 0;
              
            //   echo $inputFileName;
              
            //   print_r($allDataInSheet);
              foreach ($allDataInSheet as $value)
              {
                    // echo $value['C'] . "-";
                    // echo $value['B'] . "<br>";      
                    if($i >= 1)
                    {
                      // echo $value['B'] . "<br>";
                      $customerid[$i] = $value['A'];
                      $username[$i] = $value['B'];
                      $name[$i] = $value['C'];
                      $level[$i] = $value['D'];
                      $registerdate[$i] = $value['E'];
                      $bankaccountname[$i] = $value['F'];
                      $bankaccount[$i] = $value['G'];
                      $bankcode[$i] = $value['H'];
                      $email[$i] = $value['I'];
                      $phone[$i] = $value['J'];
                      $ipaddress[$i] = $value['K'];
                      $signature[$i] = $value['L'];
                      $signaturecookies[$i] = $value['M'];
                      $lastlogin[$i] = $value['N'];
                      
                      $tele[$i] = '';
                      $creator[$i] = $this->session->userdata('username');
                      
                      $username[$i] = str_replace(",","",$username[$i]);
                      $username[$i] = str_replace("/","",$username[$i]);
                      $username[$i] = str_replace("\\","",$username[$i]);
    
    
                      $phone[$i] = str_replace("-","",$phone[$i]);
                      $phone[$i] = str_replace("+62","",$phone[$i]);
                      
                      $registerdate[$i] = str_replace("/","-",$registerdate[$i]);
                      if(substr($registerdate[$i],9,1) == ' ')
                      {
                          $registerdate[$i] = substr($registerdate[$i], 0, 8) . "0".substr($registerdate[$i],8,10);
                      }
                      $lastlogin[$i] = str_replace("/","-",$lastlogin[$i]);
                      if(substr($lastlogin[$i],9,1) == ' ')
                      {
                          $lastlogin[$i] = substr($lastlogin[$i], 0, 8) . "0".substr($lastlogin[$i],8,10);
                      }
    
                      $querystring = "insert into members_NUSAWL values('" . $customerid[$i] . "','" .$username[$i] . "','".$name[$i]. "','" . $level[$i] . "','" . $registerdate[$i] . "','" .$bankaccountname[$i]. "','" . $bankaccount[$i]. "','" . $bankcode[$i]. "','" . $email[$i]."','". $phone[$i] . "','". $ipaddress[$i] . "','" . $signature[$i] . "','" .$signaturecookies[$i] . "','". $lastlogin[$i]. "','". $group. "','"
                      . $creator[$i] . "',now())";
    
                      $this->m_masters->querying($querystring);
                    // echo $querystring;
                    }
                    // echo $i;
                    $i = $i + 1;
              }
            
            } catch (Exception $e)
            {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                                . '": ' .$e->getMessage());
            }
    
            
            $querystring = "insert into members_storage_NUSAWL select * from members_NUSAWL where `group` ='".$group. "'";
              $this->m_masters->querying($querystring);
            
            $querystring = "select a.`group`, b.datacount from groups a 
            left join 
            (
                select `group`, count(1) datacount from members_NUSAWL where uploaddate >= concat(curdate(), ' 00:00:00')
                group by `group`
            ) b on a.`group` = b.`group`
            where b.datacount is null";
            $pendinggroup = $this->m_masters->querying($querystring)->result();
    
            unlink($path . $_FILES['filemember']['name']);
            echo "Finish Uploading for group : '".$group. "'! <br>";
            echo "Today pending group(s) : <br>";
    
            for($runner = 0;$runner < count($pendinggroup);$runner++)
            {
              echo $pendinggroup[$runner]->group . "<br>";
            }
            
            
        }
        
        else if ($wl == 'ZAPPORT')
        {
            try
            {
              $querystring = "delete from members_ZAPPORT where `group` = '".$group . "'";
              $this->m_masters->querying($querystring);
              
              $inputFileName = $path . $_FILES['filemember']['name'];   
              $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
              $objReader = PHPExcel_IOFactory::createReader($inputFileType);
              $objPHPExcel = $objReader->load($inputFileName);
              $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
              $i = 0;
              
            //   echo $inputFileName;
              
            //   print_r($allDataInSheet);
              foreach ($allDataInSheet as $value)
              {
                    // echo $value['C'] . "-";
                    // echo $value['B'] . "<br>";      
                    if($i >= 1)
                    {
                      // echo $value['B'] . "<br>";
                      $grouporigin[$i] = $value['A'];
                      $merchant[$i] = $value['B'];
                      $loginid[$i] = $value['C'];
                      $playid[$i] = $value['D'];
                      $memname[$i] = $value['E'];
                      $currency[$i] = $value['F'];
                      $bankid[$i] = $value['G'];
                      $referralid[$i] = $value['H'];
                      $contactnumber[$i] = $value['I'];
                      $membergroup[$i] = $value['J'];
                      $transfermode[$i] = $value['K'];
                      $showpromotionlist[$i] = $value['L'];
                      $status[$i] = $value['M'];
                      $errorcount[$i] = $value['N'];
                      $email[$i] = $value['O'];
                      $datecreated[$i] = $value['P'];
                      $lastupdate[$i] = $value['Q'];
                      $lastlogintime[$i] = $value['R'];
                      $lastloginip[$i] = $value['S'];
                      $loginlocation[$i] = $value['T'];
                      
                      
                      $tele[$i] = '';
                      $creator[$i] = $this->session->userdata('username');
                      
                      $loginid[$i] = str_replace(",","",$loginid[$i]);
                      $loginid[$i] = str_replace("/","",$loginid[$i]);
                      $loginid[$i] = str_replace("\\","",$loginid[$i]);
    
                      $contactnumber[$i] = str_replace("-","",$contactnumber[$i]);
                      
                      $contactnumber[$i] = str_replace("+62","",$contactnumber[$i]);
                      

    
                      $datecreated[$i] = substr($datecreated[$i],6,4) ."-". substr($datecreated[$i],0,2) . "-". substr($datecreated[$i],3,2) . substr($datecreated[$i],10,9) ;
                      $lastupdate[$i] = substr($lastupdate[$i],6,4) ."-". substr($lastupdate[$i],0,2) . "-". substr($lastupdate[$i],3,2) . substr($datecreated[$i],10,9);
                      $lastlogintime[$i] = substr($lastlogintime[$i],6,4) ."-". substr($lastlogintime[$i],0,2) . "-". substr($lastlogintime[$i],3,2). substr($datecreated[$i],10,9);
    
                      $querystring = "insert into members_ZAPPORT values('" . $grouporigin[$i] . "','" .$merchant[$i] . "','".$loginid[$i]. "','" . $playid[$i] . "','" . $memname[$i] . "','" .$currency[$i]. "','" . $bankid[$i]. "','" . $referralid[$i]. "','" . $contactnumber[$i]."','". $membergroup[$i] . "','". $transfermode[$i] . "','" . $showpromotionlist[$i] . "','" .$status[$i] . "','". $errorcount[$i]. "','". 
                      $email[$i]."','". $datecreated[$i] . "','".
                      $lastupdate[$i]."','". $lastlogintime[$i] . "','".
                      $lastloginip[$i]."','". $loginlocation[$i] . "','".
                      
                      $group. "','"
                      . $creator[$i] . "',now())";
    
                      $this->m_masters->querying($querystring);
                    // echo $querystring;
                    }
                    // echo $i;
                    $i = $i + 1;
              }
            
            } catch (Exception $e)
            {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                                . '": ' .$e->getMessage());
            }
    
            
            $querystring = "insert into members_storage_ZAPPORT select * from members_ZAPPORT where `group` ='".$group. "'";
              $this->m_masters->querying($querystring);
            
            $querystring = "select a.`group`, b.datacount from groups a 
            left join 
            (
                select `group`, count(1) datacount from members_ZAPPORT where uploaddate >= concat(curdate(), ' 00:00:00')
                group by `group`
            ) b on a.`group` = b.`group`
            where b.datacount is null";
            $pendinggroup = $this->m_masters->querying($querystring)->result();
    
            unlink($path . $_FILES['filemember']['name']);
            echo "Finish Uploading for group : '".$group. "'! <br>";
            echo "Today pending group(s) : <br>";
    
            for($runner = 0;$runner < count($pendinggroup);$runner++)
            {
              echo $pendinggroup[$runner]->group . "<br>";
            }
            
            
        }
        
        else if ($wl == 'PAY4D')
        {

            try
            {
              $querystring = "delete from members_PAY4D where `group` = '".$group . "'";
              $this->m_masters->querying($querystring);
              
              $inputFileName = $path . $_FILES['filemember']['name'];   
              $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
              $objReader = PHPExcel_IOFactory::createReader($inputFileType);
              $objPHPExcel = $objReader->load($inputFileName);
              $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
              $i = 0;
              
            //   echo $inputFileName;
              
            //   print_r($allDataInSheet);
              foreach ($allDataInSheet as $value)
              {
                    // echo $value['C'] . "-";
                    // echo $value['B'] . "<br>";      
                    if($i >= 1)
                    {
                      // echo $value['B'] . "<br>";
                      $username[$i] = $value['B'];
                      $name[$i] = $value['C'];
                      $email[$i] = $value['D'];
                      $telpon[$i] = $value['E'];
                      $credit[$i] = $value['F'];
                      $referal[$i] = $value['G'];
                      $date[$i] = $value['H'];

                      
                      $tele[$i] = '';
                      $creator[$i] = $this->session->userdata('username');
                      
                      $username[$i] = str_replace(",","",$username[$i]);
                      $username[$i] = str_replace("/","",$username[$i]);
                      $username[$i] = str_replace("\\","",$username[$i]);
    
                     $telpon[$i] = str_replace("-","",$telpon[$i]);
                     $telpon[$i] = str_replace("+62","",$telpon[$i]);
                     $telpon[$i] = str_replace(" ","",$telpon[$i]);
                     if (substr($telpon[$i],0,2) <> '08')
                     {
                        $telpon[$i] = "62" . $telpon[$i];
                     } 
                    //   $date[$i] = substr($date[$i],6,4) ."-". substr($date[$i],0,2) . "-". substr($date[$i],3,2) . " 00:00:00" ;

    
                      $querystring = "insert into members_PAY4D values('" . $username[$i] . "','" .$name[$i] . "','".$email[$i]. "','" . $telpon[$i] . "','" . $credit[$i] . "','" .$referal[$i]. "','" . $date[$i]. "','".
                      $group. "','"
                      . $creator[$i] . "',now())";
    
                      $this->m_masters->querying($querystring);
                    // echo $querystring;
                    }
                    // echo $i;
                    $i = $i + 1;
              }
            
            } catch (Exception $e)
            {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                                . '": ' .$e->getMessage());
            }
    
            
            $querystring = "insert into members_storage_PAY4D select * from members_PAY4D where `group` ='".$group. "'";
              $this->m_masters->querying($querystring);
            
            $querystring = "select a.`group`, b.datacount from groups a 
            left join 
            (
                select `group`, count(1) datacount from members_PAY4D where uploaddate >= concat(curdate(), ' 00:00:00')
                group by `group`
            ) b on a.`group` = b.`group`
            where b.datacount is null";
            $pendinggroup = $this->m_masters->querying($querystring)->result();
    
            unlink($path . $_FILES['filemember']['name']);
            echo "Finish Uploading for group : '".$group. "'! <br>";
            echo "Today pending group(s) : <br>";
    
            for($runner = 0;$runner < count($pendinggroup);$runner++)
            {
              echo $pendinggroup[$runner]->group . "<br>";
            }
            
            
        }
        
        
      }
    }
  }

  public function vipuploadmembers()
  {

    $group = $this->input->post('group');
    $errmsg = '';
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $currdatetime =  $now->format("m-d-Y H:i:s.u");
    
    
    $querygetwl = "select wl from groups where `group` = '".$group."'";
    $wl = $this->m_masters->querying($querygetwl)->result()[0]->wl;
    

    $imagecurrdatetime = str_replace(" ","_",$currdatetime);
    $imagecurrdatetime = str_replace("-","_",$imagecurrdatetime);
    $imagecurrdatetime = str_replace(":","_",$imagecurrdatetime);
    $imagecurrdatetime = str_replace(".","_",$imagecurrdatetime);

    if (empty($_FILES['filemember']['name']) || $group == 'blank')
    {
      $errmsg = $errmsg .'<div class="alert alert-danger alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i>Danger</h5>
                    Please Choose Group and Upload File!
                </div>';
      
      echo $errmsg;
    }

    // echo $_FILES['filemember']['name'];

    else
    {
      
      $path = './assets/excels/';
      require_once APPPATH . "/third_party/PHPExcel.php";
      $config['upload_path'] = $path;
      $config['allowed_types'] = 'xlsx|xls|csv';
      $config['remove_spaces'] = TRUE;
      $config['max_execution_time'] = 259200;
      $config['max_input_time'] = 259200;
      $this->load->library('upload', $config);
      $this->upload->initialize($config);

      if (!$this->upload->do_upload('filemember')) 
      {
          $error = array('error' => $this->upload->display_errors());
      }

      else
      { 
        if($wl == 'NEXUS')
        {
            try
            {
              $querystring = "delete from members_vip where `group` = '".$group . "'";
              $this->m_masters->querying($querystring);
              
              $inputFileName = $path . $_FILES['filemember']['name'];   
              $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
              $objReader = PHPExcel_IOFactory::createReader($inputFileType);
              $objPHPExcel = $objReader->load($inputFileName);
              $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
              $i = 0;

              foreach ($allDataInSheet as $value)
              {
   
                    if($i >= 1)
                    {
                      // echo $value['B'] . "<br>";
                      $username[$i] = $value['C'];
                      $fullname[$i] = $value['D'];
                      $email[$i] = $value['E'];
                      $contactno[$i] = $value['F'];
                      $whatsapp[$i] = $value['G'];
                      $referralcode[$i] = $value['H'];
                      $ownreferralcode[$i] = $value['I'];
                      $registrationdate[$i] = $value['J'];
                      $lastlogindate[$i] = $value['K'];
                      $totaldeposit[$i] = $value['L'];
                      $totalwithdrawal[$i] = $value['M'];
                      $creator[$i] = $this->session->userdata('username');
                      $username[$i] = str_replace(",","",$username[$i]);
                      $username[$i] = str_replace("/","",$username[$i]);
                      $username[$i] = str_replace("\\","",$username[$i]);

                      $totaldeposit[$i] = str_replace(",","",$totaldeposit[$i]);

                      $totalwithdrawal[$i] = str_replace(",","",$totalwithdrawal[$i]);


                      $querystring = "insert into members_vip values('" . $group . "','" .$username[$i] . "','".$fullname[$i]. "','" . $email[$i] . "','" . $contactno[$i] . "','" .$whatsapp[$i]. "','" . $referralcode[$i]. "','" . $ownreferralcode[$i]. "','" . $registrationdate[$i] . "','". $lastlogindate[$i] . "','" . $totaldeposit[$i] . "','" .$totalwithdrawal[$i] . "','". $creator[$i] . "',now())";
    

                      // echo $username[$i] . "  :   ". $totaldeposit[$i].'<br>';

                      $this->m_masters->querying($querystring);

                    }
                    $i = $i + 1;
              }
            
            } catch (Exception $e)
            {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                                . '": ' .$e->getMessage());
            }
            unlink($path . $_FILES['filemember']['name']);
            echo "Finish Uploading for group : '".$group. "'! <br>";
        
        }
        
      }
    }
  }

  public function getcallviphistory()
  {
    $phoneno=$this->input->post("phoneno");
    $group=$this->input->post("group");
    $username=$this->input->post("username");
    $querystring = "select calldate, tele, keterangan from members_call_vip_logs where phoneno = '".$phoneno."' and username = '".$username ."' and `group` = '".$group. "' order by calldate desc";
    $hist = $this->m_masters->querying($querystring)->result();
    if (count($hist)<0)
    {
      $show = '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              No Chat History Found
                            </div>';
    }
    else
    {
      for($i = 0; $i < count($hist) ; $i++)
      {
        $show = $show ."Called  by  : ".$hist[$i]->tele ." at : " .$hist[$i]->calldate;
        $show = $show . "\nKeterangan : ".$hist[$i]->keterangan."\n\n";
      }
    }
        
    echo $show;
    // echo count($hist). " ".$hist[]
    // echo "ads";
  }





  public function inquirevipmembers()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "0";
    }
    else
    {
      $group = $this->session->userdata("privilege");
      $phoneno = $this->input->post('phonenoselector');
      $username = $this->input->post('usernameselector');
      $namarekening = $this->input->post('namarekeningselector');
      $sortingselector1 = $this->input->post('sortingselector1');
      $sortingselector2 = $this->input->post('sortingselector2');
      $sortingselector3 = $this->input->post('sortingselector3');
      $phoneno = substr($phoneno, (strlen($phoneno) - 8),8);
      $phonenolength = strlen($phoneno);
      
      $querystring = "select *, case 
        when DATEDIFF(now(), last_deposit) <= 10 Then 'Green'
        when DATEDIFF(now(), last_deposit) >= 11 and DATEDIFF(now(), last_deposit) <= 21  Then 'Orange'
        when DATEDIFF(now(), last_deposit) >= 22 Then 'Red'
        END
       as alert from members_call_vip where `group` = '".$group ."'";

      if ($phoneno == '')
      {
        $querystring = $querystring . " and phoneno like '%%' ";
      }
      else
      {
        $querystring = $querystring . " and right(phoneno ,".$phonenolength . ") = '" . $phoneno . "' ";
      }


      $querystring = $querystring . " and username like '%" .$username . "%'";
 
      $querystring = $querystring . " and nama_rekening like '%" .$$namarekening . "%'";
    
      $querystring = $querystring . " order by ".$sortingselector1 ;

      $querystring = $querystring . " ,".$sortingselector2 ;

      $querystring = $querystring . " ,".$sortingselector3 ;

      $querystring = $querystring . " limit 5";

      // echo $querystring;
      $data['queryresult'] = $this->m_masters->querying($querystring)->result();

      if(count($data['queryresult']) == 0)
      {
        echo "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              No Data To Show!
                            </div>';

      }

      else
      {
        $this->load->view("v_ILKV_dg",$data);
      }
    }

  }


  public function main()
  {
      if(empty($this->session->userdata('username')))
      {
        echo "0";
      }
      else
      {

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

        if($modulecode == 'IT')
        {
          $querystring = "select privilege from privileges";
          $data['privilegelist'] = $this->m_masters->querying($querystring)->result();

          $querystring = "select `group` from groups";
          $data['grouplist'] = $this->m_masters->querying($querystring)->result();
        }
        
        if($modulecode == 'IG')
        {
          $querystring = "select wl from wls";
          $data['wllist'] = $this->m_masters->querying($querystring)->result();
        }

        if($modulecode == 'IUP' || $modulecode == 'IUPV')
        {
          $querystring = "select `group` from groups";
          $data['grouplist'] = $this->m_masters->querying($querystring)->result();
        }


        
        if($modulecode == 'DC')
        {
          $querystring = "select `group` from groups";
          $data['grouplist'] = $this->m_masters->querying($querystring)->result();
        }

        if($modulecode == 'ILKV')
        {
          $data['group'] = $this->session->userdata("privilege");
        }

        if($modulecode == 'TRL')
        {

          $querystring = "select * from timerangelock";
          $data['currtimerangelock'] = $this->m_masters->querying($querystring)->result();
        }
            $this->load->view("v_".$modulecode,$data);
      }

      
  }

  public function savevipcall()
  {
    if(empty($this->session->userdata('username')))
    {
      echo "session";

    }

    else
    {
      $errmsg = "";
      $response = $this->input->post("response");
      $phoneno = $this->input->post("phoneno");
      $username = $this->input->post("username");
      $group = $this->input->post("group");
      $keterangan = $this->input->post("keterangan");
      $telename = $this->session->userdata("username");



      if($response == '')
      {
        // $errmsg = "error";
        echo "error"; 
      }

      else 
      {
        $querystring = "insert into members_call_vip_logs values ('". $username."','".$group ."','".$phoneno."',now(),'".$telename."','".$response . "','".$keterangan . "')";
        $this->m_masters->querying($querystring);
        // echo $querystring;
        // $errmsg = "Saved Callback for ".$phoneno ;

        $querystring = "update members_call_vip set last_call_date = now() where username = '".$username . "' and `group` = '".$group. "' and phoneno = '" . $phoneno . "'" ;
        $this->m_masters->querying($querystring);

        $errmsg = "Saved Callback for ".$phoneno ;

        echo $errmsg;
      }

    }

    
  }



  public function inquiretele()
  {
    $errmsg = "";
    $DBName = "teles";
    $tele = $this->input->post('tele');
    $group = $this->input->post('group');
    $privilege = $this->input->post('privilege');
    if ($privilege == 'blank')
    {
      $privilege = '';
    }
    if ($group == 'blank')
    {
      $group = '';
      $querygroup = "";
    }
    else
    {
      $querygroup = " and `group` = '".$group."' ";
    }

    $data['querycheck'] = "select * from teles where tele like '%". $tele ."%'".$querygroup . " and privilege like '%".$privilege."%'" ;
    // echo $data['querycheck'];


    $data['lists'] = $this->m_masters->querying($data['querycheck']);
    $data['listscounts'] = $data['lists']->num_rows();
    if($data['listscounts'] == 0) 
    {
      echo '<div class="alert alert-warning alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                  There is no said tele!
                </div>';
    }
    else
    {
        $data['listtele'] = $data['lists']->result();
        $this->load->view('v_IT_dg',$data);
        
    }
    
    // echo $data['querycheck'];
  }


  public function submittele()
  {
    $errmsg = "";
    $DBName = "operators";
    $tele = $this->input->post('tele');
    $group = $this->input->post('group');
    $password = $this->input->post('password');
    $confpassword = $this->input->post('confpassword');
    $privilege = $this->input->post('privilege');

    // echo "asdf";
    if($tele == "")
    {
      $errmsg = $errmsg. "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Please fill Tele!
                            </div>';
    }
    if($group == "blank")
    {
      $errmsg = $errmsg . "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Please choose Group !
                            </div>';
    }
    if($privilege == "blank")
    {
      $errmsg = $errmsg . "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Please choose Privilege!
                            </div>';
    }
    if($password == "")
    {
      $errmsg = $errmsg. "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Password cannot be empty!
                            </div>';
    }
    if($password <> $confpassword)
    {
      $errmsg = $errmsg . "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Password and Confirm Password does not match!
                            </div>';
    }

    if ($errmsg == "")
    {
      $data['querycheck'] = "select * from teles where tele = '". $tele ."'" ;
      $data['lists'] = $this->m_masters->querying($data['querycheck']);
      $data['listscounts'] = $data['lists']->num_rows();
      if($data['listscounts'] >= 1) 
      {
        echo '<div class="alert alert-warning alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                      Tele already exists!
                    </div>';
      }

      else
      {
        $querystring = "insert into teles values ('".$tele. "','".$group. "','".$privilege . "', md5('".$password ."'),'".$this->session->userdata('username')."',now())";
        // echo $querystring;  
        $this->m_masters->querying($querystring);

        echo '<div class="alert alert-success alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-check"></i> Success!</h5>
                      Successfully Submitted!
                    </div>';
      }
    }
    else
    {
      echo $errmsg ;
    }
  }

  public function deletetele()
  {
    $errmsg = "";
    $DBName = "teles";
    $tele = $this->input->post('tele');
    if($tele == "")
    {
      $errmsg = $errmsg. "<br>". '<div class="alert alert-warning alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                              Please write tele name to delete!
                            </div>';
    }
    if ($errmsg == "")
    {
      $data['querycheck'] = "select * from teles where tele = '". $tele ."'" ;
      $data['lists'] = $this->m_masters->querying($data['querycheck']);
      $data['listscounts'] = $data['lists']->num_rows();
      if($data['listscounts'] == 0) 
      {
        echo '<div class="alert alert-warning alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                      There is no said tele!
                    </div>';
      }
      else
      {
          $querydelete = "delete from teles where tele = '" .$tele ."'";
          $this->m_masters->querying($querydelete);
          echo '<div class="alert alert-success alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-check"></i> Success!</h5>
                      Deletion Successful!
                    </div>';
      }
    }
    else
    {
      echo $errmsg ;
    }
  }

  public function inquiregroup()
  {
    $errmsg = "";
    $DBName = "groups";
    $group = $this->input->post('group');
    $gsecret = $this->input->post('gsecret');
    $wl = $this->input->post('wl');
    
    
    $data['querycheck'] = "select * from groups where `group` like '%".$group . "%' and gsecret like '%".$gsecret . "%'" ;
    
    if ($wl <> 'blank')
    {
        
        $data['querycheck'] = $data['querycheck'] . " and wl = '" .$wl . "'";    
    }
    // echo $data['querycheck'];
    $data['lists'] = $this->m_masters->querying($data['querycheck']);
    $data['listscounts'] = $data['lists']->num_rows();
    if($data['listscounts'] == 0) 
    {
      echo '<div class="alert alert-warning alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                  There is no said group!
                </div>';
    }
    else
    {
        $data['listgroup'] = $data['lists']->result();
        $this->load->view('v_IG_dg',$data);
    }
  }

  public function deletegroup()
  {
    $errmsg = "";
    $DBName = "groups";
    $group = $this->input->post('group');
    if($group == "")
    {
      echo '<div class="alert alert-warning alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                      Please select Group to delete!
                    </div>';
    }
    if ($errmsg == "")
    {
      $data['querycheck'] = "select * from groups where `group` = '". $group ."'" ;
      $data['lists'] = $this->m_masters->querying($data['querycheck']);
      $data['listscounts'] = $data['lists']->num_rows();
      if($data['listscounts'] == 0) 
      {
        echo '<div class="alert alert-warning alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                      There is no said Group!
                    </div>';
      }
      else
      {
          $querydelete = "delete from groups where `group` = '" .$group ."'";
          $this->m_masters->querying($querydelete);
          
          echo '<div class="alert alert-success alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-check"></i> Success!</h5>
                      Deletion Successful!
                    </div>';
      }
    }
    else
    {
      echo $errmsg ;
    }
  }

  public function submitgroup()
  {
    $errmsg = "";
    $DBName = "groups";
    $group = $this->input->post('group');
    $gsecret = $this->input->post('gsecret');
    $wl = $this->input->post('wl');
    
    
    if($group == "")
    {
      $errmsg = $errmsg. "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Please fill Group!
                            </div>';
    }
    
    if($gsecret == "")
    {
      $errmsg = $errmsg. "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Please fill Google Secret!
                            </div>';
    }
    
    if($wl == "blank")
    {
      $errmsg = $errmsg. "<br>". '<div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                              <h5><i class="icon fas fa-ban"></i> Alert!</h5>
                              Please fill Whitelabel!
                            </div>';
    }

    if ($errmsg == "")
    {
      $data['querycheck'] = "select * from groups where `group` like '%".$group . "%' and gsecret like '%".$gsecret . "%'" ;
    
      if ($wl <> 'blank')
      {
            
        $data['querycheck'] = $data['querycheck'] . " and wl = '" .$wl . "'";    
      }
      $data['lists'] = $this->m_masters->querying($data['querycheck']);
      $data['listscounts'] = $data['lists']->num_rows();
      if($data['listscounts'] >= 1) 
      {
        echo '<div class="alert alert-warning alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                      Group already exists!
                    </div>';
      }

      else
      {
        
        $querystring = "insert into groups values ('".$group . "','".$wl."','".$gsecret. "','".$this->session->userdata('username'). "',now())";
        $this->m_masters->querying($querystring); 
        
        echo '<div class="alert alert-success alert-dismissible">
                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                      <h5><i class="icon fas fa-check"></i> Success!</h5>
                      Successfully Submitted!
                    </div>';
            // echo $data['querystring'];
      }
    }
    else
    {
      echo $errmsg ;
    }
  }


}