<?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=$_POST['chvals'];
                $bpvals=$_POST['bpvals'];
                $alavals=$_POST['alavals'];
                $ncfamt=$_POST['ncfamt'];
                $noopv=$_POST['noopv'];
                $bopv=$_POST['bopv'];
                $accv=$_POST['accv'];
                $chtot=$_POST['chtot'];

                $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);

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

                    }

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

                    }

                    $alacarte=$_POST['alacarte'];
                    $delalacarte=mysqli_query($con,"DELETE FROM tbl_change_package WHERE customer_id='$custid' AND pac_type='4'");
                    foreach ($alacarte as $value) {
                         $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;

                    }


                        echo "<script>alert('Package has been save successfully')</script>";




                        $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>
                  <div class="form-group row">
                     <div class="col-md-4" style="overflow-y: scroll;height: 300px;">
                        <form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>?action=update&custid=<?php echo $custid?>" >
                           <input type="hidden" name="rowid" id="rowid" value="<?=$query['id']?>">
                           <input type="hidden" id="name1" value="0" name="yt">
                           <input type="hidden" id="name2" value="0" name="ytt">
                           <input type="hidden" id="name3" value="0" name="ytss">
                           <input type="hidden" name="packle" id="pacl" value="">
                           <input type="hidden" name="packleid" id="pacid" value="">
                           <label>Cable Package</label><br>
                           <hr>
                           <?

                              $chvalu=0;
                              $alavalu=0;
                              $bpackvalu=0;
                              $getpack=mysqli_query($con,"SELECT * FROM tbl_pack WHERE ptype='2'");
                              while($arr=mysqli_fetch_array($getpack)){

                                $chkp=f(q("SELECT * FROM tbl_change_package_details WHERE pac_type='2' AND pac_id='$arr[pid]' AND customer_id='$custid'"));
                                if($chkp['pac_id']==$arr['pid']){
                                $chvalu=floatval($chvalu)+floatval($arr['prc']);
                             }
                               
                              ?>
                           <input type="checkbox" name="packs[]" <?php if($chkp['pac_id']==$arr['pid']){?> checked <?php } ?> id="packs<?=$arr['pid']?>" packname="<?=$arr['pname']?>" price="<?=$arr['prc']?>" data="<?=$arr['pnochannel']?>" value="<?=$arr['pid']?>" class="cpack pac" onclick="addpacks('packs<?=$arr["pid"]?>')"> <?=$arr['pname']?>&nbsp;(&#8377; <?=$arr['prc']?>)<br>
                           <?}?>
                     </div>
                     <div class="col-md-4" style="overflow-y: scroll;height: 300px;">
                     <label>Broadcaster Package</label><br><hr>
                     <?
                        $getpack=mysqli_query($con,"SELECT * FROM tbl_pack WHERE ptype='3'");
                        while($arr=mysqli_fetch_array($getpack)){

                            $chkp=f(q("SELECT * FROM tbl_change_package_details WHERE pac_type='3' AND pac_id='$arr[pid]' AND customer_id='$custid'"));
                            if($chkp['pac_id']==$arr['pid']){
                            $bpackvalu=floatval($bpackvalu)+floatval($arr['prc']);
                         }
                        ?>
                     <input type="checkbox" name="bpacks[]" id="bpacks<?=$arr['pid']?>"  <?php if($chkp['pac_id']==$arr['pid']){?> checked <?php } ?>  packname="<?=$arr['pname']?>" price="<?=$arr['prc']?>" data="<?=$arr['pnochannel']?>" value="<?=$arr['pid']?>" class="packs pac" onclick="addbpacks('bpacks<?=$arr["pid"]?>')"> <?=$arr['pname']?>&nbsp;(&#8377; <?=$arr['prc']?>)<br>
                     <?}?>
                     </div>
                     <div class="col-md-4" style="overflow-y: scroll;height: 300px;">
                     <label>Ala Carte Channels</label><br><hr>
                     <?
                        $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']);
                         }
                        ?>
                     <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>
                     <?}?>
                     </div>
                  </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 Cable Package : <b id="noop"><?php echo $getpacks['chno']?></b></p>
                  <p>Number Of Broadcaster Package : <b id="bop"><?php echo $getpacks['bpno']?></b></p>
                  <p>Number Of Ala Carte Channel : <b id="acc"><?php echo $getpacks['alano']?></b></p>
                  <p>Total Number Of channel : <b id="tot"><?php echo $getpacks['totalc']?></b></p>
                  <p>NCF : <b id="ncc"><?php echo $getpacks['ncf']?></b></p>
                  <input type="hidden" name="nc" id="ncff" value="0">
                  <p>Cable Package Total : <b id="cpcid">&#8377;<?php echo $chvalu; ?></b></p>
                  <p>Broadcaster Package Total : <b id="pcid">&#8377;<?php echo $bpackvalu; ?></b></p>
                  <p>Ala Carte Total : <b id="alc">&#8377;<?php echo $alavalu; ?></b></p>
                  <input type="hidden" name="accc" id="aocl" value="0">
                  <input type="hidden" name="pcc" id="ptid" value="0">
                  <input type="hidden" name="pccd" id="cptid" value="0">
                  <p>Total Amount : <b id="tott"><?php echo $getpacks['price']?></b></p>
                  <input type="hidden" name="ptot" id="ptotal" value="0">


                  <input type="hidden" name="noopv" id="noopv" value="<?php echo $noopv?>">
                  <input type="hidden" name="bopv" id="bopv" value="<?php echo $bopv?>">
                  <input type="hidden" name="accv" id="accv" value="<?php echo $accv?>">

                   <input type="hidden" name="chvals" id="chvals" value="<?php echo $chvalu?>">
                   <input type="hidden" name="bpvals" id="bpvals" value="<?php echo $bpackvalu?>">
                   <input type="hidden" name="alavals" id="alavals" value="<?php echo $alavalu?>">         


                  <input type="hidden" name="chtot" id="chtot" value="<?php echo $totalc?>">

                  <input type="hidden" name="ncfamt" id="ncfamt" value="<?php echo $ncfamt?>">
                  <input type="hidden" name="ncfcnt" id="ncfcnt" value="0">
                  <hr>
                  <button type="submit" class="btn btn-warning"><i class="fa fa-eye"></i> Change Now</button>
                  </div>
                  </form>
                  <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 addpacks(ids){

            var elem=document.getElementById(ids);
                var noop=document.getElementById('noopv').value;
                if(elem.checked==true){
                    noop=parseInt(noop)+1;
                }else{
                    noop=parseInt(noop)-1;
                }
                document.getElementById('noopv').value=noop;
                document.getElementById('noop').innerHTML=noop;
                var ctot=document.getElementById('chtot').value;
                var curtot=elem.getAttribute('data');
                if(elem.checked==true){
                    ctot=parseInt(ctot)+parseInt(curtot);
                }else{
                    ctot=parseInt(ctot)-parseInt(curtot);
                }
                document.getElementById('chtot').value=ctot;
                var chtot=document.getElementById('chvals').value;

                var prc=elem.getAttribute('price');
                if(elem.checked==true){
                    chtot=parseFloat(chtot)+parseFloat(prc);
                }else{ 
                    chtot=parseFloat(chtot)-parseFloat(prc);
                }

                document.getElementById('chvals').value=chtot;

                ncftot();

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


         }


         function addbpacks(ids){

            var elem=document.getElementById(ids);
                var noop=document.getElementById('bopv').value;
                if(elem.checked==true){
                    noop=parseInt(noop)+1;
                }else{
                    noop=parseInt(noop)-1;
                }
                document.getElementById('bopv').value=noop;
                document.getElementById('bop').innerHTML=noop;
                var ctot=document.getElementById('chtot').value;
                var curtot=elem.getAttribute('data');
                if(elem.checked==true){
                    ctot=parseInt(ctot)+parseInt(curtot);
                }else{
                    ctot=parseInt(ctot)-parseInt(curtot);
                }
                document.getElementById('chtot').value=ctot;
                var chtot=document.getElementById('bpvals').value;

                var prc=elem.getAttribute('price');
                if(elem.checked==true){
                    chtot=parseFloat(chtot)+parseFloat(prc);
                }else{ 
                    chtot=parseFloat(chtot)-parseFloat(prc);
                }

                document.getElementById('bpvals').value=chtot;

                ncftot();

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


         }

         function addalapacks(ids){

            var elem=document.getElementById(ids);
                var noop=document.getElementById('accv').value;
                if(elem.checked==true){
                    noop=parseInt(noop)+1;
                }else{
                    noop=parseInt(noop)-1;
                }
                document.getElementById('accv').value=noop;
                document.getElementById('acc').innerHTML=noop;
                var ctot=document.getElementById('chtot').value;
                var curtot=elem.getAttribute('data');
                if(elem.checked==true){
                    ctot=parseInt(ctot)+parseInt(curtot);
                }else{
                    ctot=parseInt(ctot)-parseInt(curtot);
                }
                document.getElementById('chtot').value=ctot;
                var chtot=document.getElementById('alavals').value;

                var prc=elem.getAttribute('price');
                if(elem.checked==true){
                    chtot=parseFloat(chtot)+parseFloat(prc);
                }else{ 
                    chtot=parseFloat(chtot)-parseFloat(prc);
                }

                document.getElementById('alavals').value=chtot;

                ncftot();

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


         }


         function ncftot(){

            var chtot=document.getElementById('chtot').value;
            if(parseInt(chtot)>100){

            
            var xtra=parseInt(chtot)-100;
            var ncfcount=Math.round(xtra/20);
            var ncfamt=parseInt(ncfcount)*parseInt(25);
            document.getElementById('ncfamt').value=ncfamt;
            document.getElementById('ncfcnt').value=ncfcount;
            document.getElementById('ncc').innerHTML='&#8377; '+ncfamt;
            }else{

                document.getElementById('ncfamt').value=0;
            document.getElementById('ncfcnt').value=0;

            }

            totchs();
            settotal();
            
         }


         function totchs(){

            document.getElementById('tot').innerHTML=document.getElementById('chtot').value;
         }


         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>

