<?php include "top.php"; ?>
<!DOCTYPE html>
<html>
   <head>
      <?php include('include/head.php'); ?>
      <title>Add Channel</title>
      <link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/jquery.dataTables.css">
      <link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/dataTables.bootstrap4.css">
      <link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/responsive.dataTables.css">
      <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
   </head>
   <body>
      <?php include('include/header.php');?>
      <?
         $custid=$_REQUEST['custid'];
         $query=f(q("SELECT * FROM `adduser`WHERE  is_cable=1 AND id='$custid'"));
         $prc=floatval($query['price'])*(0);
         $total=floatval($query['price'])+floatval($prc);



         $action=$_REQUEST['action'];
            if($action=='update'){

                $chvals=0;
                $bpvals=0;
                $alavals=$_POST['multiprice'];
                $ncfamt=0;
                $noopv=0;
                $bopv=0;
                $accv=0;
                $chtot=0;

                $totalamt=floatval($ncfamt)+floatval($bpvals)+floatval($alavals)+floatval($chvals);
                $packpr=floatval($bpvals)+floatval($alavals)+floatval($chvals);

                $delpacks=mysqli_query($con,"DELETE FROM tbl_change_package WHERE userid='$custid'");

                $res=mysqli_query($con,"INSERT INTO tbl_change_package SET userid='$custid',price='$totalamt',pay_ch='Pack Change',packpr='$packpr',ncf='$ncfamt',chp='$chvals',ala='$alavals',dbp='$bpvals',chno='$noopv',alano='$accv',bpno='$bopv',totalc='$chtot'");

                if($res){

                    $maxid=mysqli_insert_id($con);

                   

                    $alacarte=$_POST['alacarte'];
                    $delalacarte=mysqli_query($con,"DELETE FROM tbl_change_package_details WHERE customer_id='$custid' AND pac_type='4'");
                    foreach ($alacarte as $value) {
                    // echo "INSERT INTO tbl_change_package_details SET chid='$maxid',pac_type='4',cust_date=CURDATE(),pac_id='$value',customer_id='$custid'";
                         $res=mysqli_query($con,"INSERT INTO tbl_change_package_details SET chid='$maxid',pac_type='4',cust_date=CURDATE(),pac_id='$value',customer_id='$custid'");
                         $ps=getpackname($value).', '.$ps;

                    }


                        ?>
                        <script>
                           Swal.fire('Channels Updated Successfully','','success').then(function() {
    window.open('cablecust.php','_parent');
     //window.open('generatebill.php','_parent');
});
                        </script>

                    <?php
                       // echo "UPDATE `adduser` SET price='$totalamt',pay_ch='$ps' WHERE is_cable=1 AND custid='$custid'" ;
                        $upusr=mysqli_query($con,"UPDATE `adduser` SET price='$totalamt',pay_ch='$ps' WHERE is_cable=1 AND id='$custid'");

                     



                }



            }


            $getpacks=f(q("SELECT * FROM tbl_change_package WHERE userid='$custid'"));
         ?>
      <?php include('include/sidebar.php'); 
         ?>
      <div class="main-container">
         <div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
            <div class="min-height-200px">
               <div class="page-header">
                  <div class="row">
                     <div class="col-md-6 col-sm-12">
                        <div class="title">
                           <h4>Add Channel</h4>
                        </div>
                        <nav aria-label="breadcrumb" role="navigation">
                           <ol class="breadcrumb">
                              <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                              <li class="breadcrumb-item active" aria-current="page">Add Channel</li>
                           </ol>
                        </nav>
                     </div>
                     <div class="col-md-6 col-sm-12 text-right">
                     </div>
                  </div>
               </div>
               <!-- Default Basic Forms Start -->
               <div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
                  <small style="color: red;"><?=$msg?></small>
                  <div class="clearfix" style="min-height: 30px;"></div>
                  <div class="form-group row">
                     <div class="col-md-12">
                        <b>User Details</b><br>
                        <?=$query['name']?><br>
                        <b>Cust Code:</b> <?=$query['custid']?><br>
                        <b>Address:</b> <?=$query['address']?><br>
                        <b>Phone :</b> <?=$query['phone']?><br>
                        <b>Package Name :</b> <?=$query['pay_ch']?><br>
                        <b>Current Amount: &#8377;<?=$total?></b>
                     </div>
                  </div>
                  <form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>?action=update&custid=<?php echo $custid?>" >
                  <div class="form-group row">
                  
                     
                     <div class="col-md-12" style="overflow-y: scroll;height: 200px;margin-bottom:30px;">
                     <label>Channels</label><br><hr>
                     <select name="alacarte[]" id="alacarte" class="custom-select2  form-control" multiple onchange="setids()">
                     <? $count=0;
                        $getpack=mysqli_query($con,"SELECT * FROM tbl_pack WHERE ptype='4'");
                        while($arr=mysqli_fetch_array($getpack)){
                            $chkp=f(q("SELECT * FROM tbl_change_package_details WHERE pac_type='4'AND pac_id='$arr[pid]' AND customer_id='$custid'"));
                            if($chkp['pac_id']==$arr['pid']){
                            $alavalu=floatval($alavalu)+floatval($arr['prc']);
                            $count++;
                         }
                        ?>
                        <option value="<?=$arr['pid']?>" <?php if($chkp['pac_id']==$arr['pid']){?> selected <?php } ?> data="<?=$arr['prc']?>"><?=$arr['pname']?></option>
                     <!-- <input type="checkbox" name="alacarte[]" id="alapacks<?=$arr['pid']?>" class="pack pac" <?php if($chkp['pac_id']==$arr['pid']){?> checked <?php } ?> packname="<?=$arr['pname']?>" price="<?=$arr['prc']?>" data="<?=$arr['pnochannel']?>" value="<?=$arr['pid']?>" onclick="addalapacks('alapacks<?=$arr["pid"]?>')"> <?=$arr['pname']?>&nbsp;(&#8377; <?=$arr['prc']?>)<br> -->
                     <?}?>
                     </select>
                     </div>
                 
                  <?php 

                     if($getpacks['chp']==''){

                        $chp=0;
                     }else{

                        $chp=$getpacks['chp'];
                     }

                     if($getpacks['dbp']==''){

                        $dbp=0;
                     }else{

                        $dbp=$getpacks['dbp'];
                     }

                     if($getpacks['ala']==''){

                        $ala=0;
                     }else{

                        $ala=$getpacks['ala'];
                     }

                     if($getpacks['totalc']==''){

                        $totalc=0;
                     }else{

                        $totalc=$getpacks['totalc'];
                     }  

                     if($getpacks['chno']==''){

                        $noopv=0;
                     }else{

                        $noopv=$getpacks['chno'];
                     } 
                     if($getpacks['bpno']==''){

                        $bopv=0;
                     }else{

                        $bopv=$getpacks['bpno'];
                     } 
                     if($getpacks['alano']==''){

                        $accv=0;
                     }else{

                        $accv=$getpacks['alano'];
                     } 

                     if($getpacks['ncf']==''){

                        $ncfamt=0;
                     }else{

                        $ncfamt=$getpacks['ncf'];
                     } 


                  ?>
                  <div class="col-md-6">
                  
                  <p>Number Of Channels : <b id="acc"><?php echo $count?></b></p>
                  
                 
                  <p>Total : <b id="alc">&#8377;<?php echo $alavalu; ?></b></p>
                  <input type="hidden" id="cntsss" value="0" >
                  <input type="hidden" id="multirow" name="multirow" value="0" >
                  <input type="hidden" id="curprice" name="curprice" value="0" >
                  <input type="hidden" id="multiprice" name="multiprice" value="0" >
                  
                  <hr>
                  <button type="submit" class="btn btn-warning"><i class="fa fa-eye"></i> Change Now</button>
                  </div>
                  </form>
                    </div>
                  <div class="collapse collapse-box" id="basic-form1" >
                  </div>
               </div>
            </div>
            <?php include('include/footer.php'); ?>
         </div>
      </div>
      <?php include('include/script.php'); ?>
      <script src="src/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
      <script src="src/plugins/datatables/media/js/dataTables.bootstrap4.js"></script>
      <script src="src/plugins/datatables/media/js/dataTables.responsive.js"></script>
      <script src="src/plugins/datatables/media/js/responsive.bootstrap4.js"></script>
      <!-- buttons for Export datatable -->
      <script src="src/plugins/datatables/media/js/button/dataTables.buttons.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.bootstrap4.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.print.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.html5.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.flash.js"></script>
      <script src="src/plugins/datatables/media/js/button/pdfmake.min.js"></script>
      <script src="src/plugins/datatables/media/js/button/vfs_fonts.js"></script>
      <script>
         $('document').ready(function(){
            $('.data-table').DataTable({
               scrollCollapse: true,
               autoWidth: false,
               responsive: true,
               columnDefs: [{
                  targets: "datatable-nosort",
                  orderable: false,
               }],
               "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
               "language": {
                  "info": "_START_-_END_ of _TOTAL_ entries",
                  searchPlaceholder: "Search"
               },
            });
            $('.data-table-export').DataTable({
               scrollCollapse: true,
               autoWidth: false,
               responsive: true,
               columnDefs: [{
                  targets: "datatable-nosort",
                  orderable: false,
               }],
               "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
               "language": {
                  "info": "_START_-_END_ of _TOTAL_ entries",
                  searchPlaceholder: "Search"
               },
               dom: 'Bfrtip',
               buttons: [
               'copy', 'csv', 'pdf', 'print'
               ]
            });
            var table = $('.select-row').DataTable();
            $('.select-row tbody').on('click', 'tr', function () {
               if ($(this).hasClass('selected')) {
                  $(this).removeClass('selected');
               }
               else {
                  table.$('tr.selected').removeClass('selected');
                  $(this).addClass('selected');
               }
            });
            var multipletable = $('.multiple-select-row').DataTable();
            $('.multiple-select-row tbody').on('click', 'tr', function () {
               $(this).toggleClass('selected');
            });
         });
      </script>

<script type="text/javascript">

            function getXMLHTTP() {
           var x = false;
           try {
              x = new XMLHttpRequest();
           }catch(e) {
             try {
                x = new ActiveXObject("Microsoft.XMLHTTP");
             }catch(ex) {
                try {
                    req = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1) {
                    x = false;
                }
             }
          }
          return x;
        }
function getprodo(prodid)
{
   var strURL="getthings.php?action=getprice&prodid="+prodid;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
      if (req.readyState == 4)
      {
     // only if "OK"
     if (req.status == 200)
         {
        var strs=req.responseText;
       // alert(strs);
           // var curval=document.getElementById('cntsss').value;
            //curval=parseFloat(curval)+parseFloat(strs);
           // alert(curval);

        document.getElementById('curprice').value=strs;
        return strs;
       // countala();

     } else {
       alert("There was a problem while using XMLHTTP:\n" + req.statusText);
     }
       }
      }
   req.open("GET", strURL, true);
   req.send(null);
   }
}
         

        //  function addalapacks(idss){

        //         var elem=document.getElementById(cntsss);
        //         var noop=document.getElementById('alc').value;
        //         if(elem.value=='0'){
        //             noop=1;
        //         }else{
        //             noop=parseInt(noop)+1;
        //         }
        //         document.getElementById('alc').value=noop;
                

        //          document.getElementById('alc').innerHTML='&#8377; '+chtot;


        //  }

        function setids(){
//alert(idss);
// var eids=document.getElementById('multirow').value;
// var n = eids.search(idss);

// if(n=='-1'){

//     eids=eids+','+idss;
//     document.getElementById('multirow').value=eids; 
   
// }else{

//          eids=eids.replace(idss, "");
//          document.getElementById('multirow').value=eids;
      

// }
var totlp=[];
var selected = [];
var sum = 0;

    for (var option of document.getElementById('alacarte').options)
    {
        if (option.selected) {
            selected.push(option.value);
            totlp.push(option.getAttribute('data'));
                //var prodid=option.value;
                  // var strURL="getthings.php?action=getprice&prodid="+prodid;
                  // var req = getXMLHTTP();
                  // if (req)
                  // {
                  // req.onreadystatechange = function()
                  // {
                  //    if (req.readyState == 4)
                  //    {
                  // // only if "OK"
                  // if (req.status == 200)
                  //       {
                  //    var strs=req.responseText;
                       
                  //    totlp=parseFloat(totlp)+parseFloat(strs);
                     
                     
                     
                        
                  // } else {
                  //    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                  // }
                  //    }
                  //    }
                  // req.open("GET", strURL, true);
                  // req.send(null);
                  // }
                    
    }
    //alert(totlp.length);
    

//countala();

} 

var count=selected.length;
    document.getElementById('acc').innerHTML=count;
    for (let i = 0; i < totlp.length; i++) {
        sum += parseFloat(totlp[i]);
    }

    document.getElementById('alc').innerHTML='&#8377; '+ sum;

document.getElementById('multiprice').value=sum;
        }


function countala(){
    
var aa=0,bb=0,cc=0;
var count=0;
document.getElementById('curprice').value=0;
 var arr=document.getElementById('multiprice').value;
 var arry=arr.split(",");

 var arrayLength = arry.length;
for (var i = 0; i < arrayLength; i++) {

 if(arry[i]!=''){
   //alert(arry[i]);
   

   // console.log(arry[i]);
    //Do something
    count=parseFloat(count)+parseFloat(arry[i]);

        }
}
//alert(cc);



//document.getElementById('acc').innerHTML=count;


}


        


         function settotal(){

            var ncfamt=document.getElementById('ncfamt').value;
            var chvals=document.getElementById('chvals').value;
            var bpvals=document.getElementById('bpvals').value;
            var alavals=document.getElementById('alavals').value;
            var tot=parseInt(chvals)+parseInt(bpvals)+parseInt(alavals)+parseInt(ncfamt);
            document.getElementById('tott').innerHTML='&#8377; '+tot;



         }





</script>
   </body>
</html>

