<?php 
    
    require_once 'connect.inc.php';
    
    function n_table(){
        global $con;
        
        $q = "select * from notice where 1";
        $i=0;
        $r = mysqli_query($con,$q);
        while($qr = mysqli_fetch_assoc($r)){
            $i++;
            $id = $qr['id'];
            $email = $qr['email'];
            $msg = $qr['message'];
            $date = $qr['date'];
            $type = $qr['type'];
            $c = $qr['c'];
            $d = $qr['d'];
      
            if($c == 0){
                $c="READ";
            }else{
                   $c="Unread";

            }
            
                  if($d == 0){
                      $cl = "Withdrawal Deactiveted";
                $cq="Activate Withdrawal";
                 $cls = "btn btn-success";

            }else{
                   $cq="Decativate withdrawal";
                    $cls = "btn btn-primary";
                $cl = "Withdrawal active";

            }
            
            $tr = '<tr><td>'.$i.'</td><td>'.$email.'</td><td>'.$msg.'</td><td>'.$c.'</td><td>'.$date.'</td><td>'.$cl.'</td><td><form action="#" method="post"> <input hidden name="mdel" value="'.$id.'"> <button type="submit" class="btn btn-danger"> Delete </button>  </form> </td> <td><form action="#" method="post"> <input hidden name="wcl" value="'.$id.'"> <button type="submit" class="'.$cls.'"> '.$cq.' </button>  </form> </td></tr>';
            
            echo $tr;
            
        }
    }

if(isset($_POST['wcl'])){
    $v =$_POST['wcl'];
    $q = "select d from notice where id ='$v' ";
    $r = mysqli_query($con,$q);
    $ce = mysqli_R($r,0);
    if($ce == 0){
    $q= "update notice set d ='1' where id= '$v'";
    }else{
            $q= "update notice set d ='0' where id= '$v'";

    }
    if(mysqli_query($con,$q)){
                            echo '<script> alert("action success ful"); </script> ';

    }
    
}


if(isset($_POST['mdel'])){
    $v =$_POST['mdel'];
    $q= "delete From notice where id= '$v'";
    if(mysqli_query($con,$q)){
                            echo '<script> alert("message Deleted"); </script> ';

    }
    
}


    if(isset($_POST['act']) && isset($_POST['msg']) && isset($_POST['type'])){
        $act = $_POST['act'];
        $msg = trim(mysqli_real_escape_string($con,$_POST['msg']));
        $type = $_POST['type'];
        $date = date('d-m-Y',time());
        if(!empty($act) && !empty($msg) && !empty($type)){
                $q = "insert into notice VALUES(NULL,'$act','$msg','$type','$date','1','0')";
                if(mysqli_query($con,$q)){
                    echo '<script> alert("message sent"); </script> ';
                }
        }
    }



    if(isset($_POST['da'])){
        
        $q = "select msg from notice where email = '$mail' and c ='1'  ORDER BY ID DESC LIMIT 1";
        $r = mysqli_query($con,$q);
        $msg = mysqli_R($r,0);
        
        echo $msg;
    }
    
        if(isset($_POST['da'])){
        
        $q = "select msg from notice where email = '$mail' AND c ='1' AND type='da'  ORDER BY ID DESC LIMIT 1";
        $r = mysqli_query($con,$q);
        $msg = mysqli_R($r,0);
        
        echo $msg;
    }
    
           if(isset($_POST['w'])){
        
        $q = "select msg from notice where email = '$mail' AND c ='1' AND type='w'  ORDER BY ID DESC LIMIT 1";
        $r = mysqli_query($con,$q);
        $msg = mysqli_R($r,0);
        
        echo $msg;
    }

?>