

 <?php
if(ISSET($_POST['add_supsub'])){
		try{
		    $cpny = $_REQUEST['cpny'];
            $owner = $_REQUEST['owner'];
            $ssctg = $_REQUEST['ssctg'];
            $supp_phone = $_REQUEST['supp_phone'];
            $tino = $_REQUEST['tino'];
            $rank_name = $_REQUEST['rank_name'];
            $tpid = $_REQUEST['tpid'];

			$sql = $db->prepare("INSERT INTO `tbl_sub_supp`(`cpny_name`,`ss_rank`,`own_name`,`tel_cont`,`tin`,`su_type`,`ctg_su`,`ss_status`)
			VALUES ('$cpny','$rank_name','$owner','$supp_phone','$tino','$tpid','$ssctg',1)");
			$sql->execute();
			$msg = "Data saved successfully!";
			
		}catch(PDOException $e){
			$msg = $e->getMessage();
		}
     echo'<meta http-equiv="refresh"'.'content="3;URL=gate?b=supsub">';	
	}
?>
   <!--update unit-->
    <?php
    if(ISSET($_REQUEST['btnupdate'])){
		try{
		    
            $supsubid = $_REQUEST['supsubid'];
            $tpid = $_REQUEST['tpid'];
            $cpny = $_REQUEST['cpny'];
            $rank_name = $_REQUEST['rank_name'];
            $owner = $_REQUEST['owner'];
            $ssctg = $_REQUEST['ssctg'];
            $supp_phone = $_REQUEST['supp_phone'];
            $tino = $_REQUEST['tino'];

			$sql = $db->prepare("UPDATE `tbl_sub_supp` SET `cpny_name`='$cpny',`ss_rank`='$rank_name',`own_name`='$owner',
            `tel_cont`='$supp_phone',`tin`='$tino' WHERE `subsup_id`='$supsubid'");
		    $sql->execute();
			$msg = "Updated Successfully";
		echo'<meta http-equiv="refresh"'.'content="3;URL=gate?b=supsub">';
			
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}
    ?>   
<!-- delete unit-->
    <?php
        if(ISSET($_GET['del-subsup'])){
		$spsubID = $_GET['del-subsup'];
		$sqlsize = $conn->prepare("SELECT * FROM `tbl_invoices` WHERE `ssinv`='$spsubID' ");
		$sqlsize->execute();
		$datasize=$sqlsize->rowCount();
		if($datasize==0){
		$sql = $conn->prepare("DELETE FROM `tbl_sub_supp` WHERE `subsup_id`='$spsubID' ");
		$sql->execute();
		
		$msg = "Deleted Successfully!";
		}else{
		    
		  $message="Do not delete this data";  
		}
		echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=supsub">';
	}
	
	 // for activate user   	
    if(isset($_GET['acid']))
    {
    
    try{
    		$change_status = 1;
    		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    		$sql = "UPDATE `tbl_sub_supp` SET `ss_status` = '".$change_status."' WHERE `subsup_id` = '".$_GET['acid']."'";
    		$conn->exec($sql);
    		
    		$msg="User Activate successfully";
    	}catch(PDOException $e){
    		echo $e->getMessage();
    	}
        echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=supsub">';
    }

 // for Deactivate user
    else if(isset($_GET['did']))
      {
     try{
			$change_status = 0;
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "UPDATE `tbl_sub_supp` SET `ss_status` = '".$change_status."' WHERE `subsup_id` = '".$_GET['did']."'";
			$conn->exec($sql);
			
			$msge="User Deactivate successfully";
		}catch(PDOException $e){
			echo $e->getMessage();
		}
    echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=supsub">';	
	}
	
?>

<div class="subheader">
    <h1 class="subheader-title" style="font-weight: bold;">
        <i class='subheader-icon fa fa-wrench'></i><?php echo $title;?> 
    </h1>
    <div class="d-flex mr-4">
        <div>
          <a href="<?php echo web_root;?>beam/bm?b=box">
            <label class="fs-sm mb-0 mt-2 mt-md-0">Unread Message</label>
            <h4 class="font-weight-bold mb-0"><i class="fa fa-envelope-o" aria-hidden="true"></i> <?php echo $notifieIncom;?></h4>
          </a>
        </div>
    </div>
</div>

<div class="card mb-g border shadow-0">

    <div class="card-header bg-white p-0 header">
        <div class="p-3 d-flex flex-row">
            <div class="d-block ml-2">
                <span class="h6 font-weight-bold text-uppercase d-block m-0">
                    List | add sup/subcon 
                </span>

                <?php 
                if(empty($_REQUEST['ssid'])){
                ?>
                <div class="d-flex mr-4">
                    <li class="position-absolute pos-top pos-right d-none d-sm-block"  style="padding: 10px;">
                        <button class="btn btn-primary btn-sm" id="add-btn"><i class="fa fa-plus-circle"></i> Add New</button>
                        <button class="btn btn-default btn-sm" id="list-btn"><i class="fa fa-eye"></i> View List</button> 
                    </li>
                </div>
                <?php }?>
                <div class="clearfix"></div>  
            </div>
        </div>
    </div>

    <div class="card-body">
    
        <?php if(isset($msg)){?>
            <div class="alert alert-success alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <strong>Well Done!</strong> <?php echo htmlentities($msg); ?>
            </div>
            <?php }else if(isset($msge)){?>
            
            <div class="alert alert-danger alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <strong>Oooh Snap!</strong> <?php echo htmlentities($msge); ?>
        </div>

        <?php 
        }if(isset($_REQUEST['ssid'])){
            include 'editsupsub.php';
        }else{
        ?>

        <div id="table-result">
        <?php include 'listsupsub.php';?>
        </div>

        <div id="form-display" style="display: none;">
            <?php include 'supsubform.php';?>
        </div>
        <?php }?>
    </div>
</div>



<script>
    function getsctg(data0){
	//alert(data0);
	const ajaxreq =new XMLHttpRequest();
	ajaxreq.open('GET','../res/getVal?tpid='+data0,'TRUE');
	ajaxreq.send();
	ajaxreq.onreadystatechange= function(){

		if (ajaxreq.readyState== 4 && ajaxreq.status == 200) {

			document.getElementById('ssctg').
			innerHTML = ajaxreq.responseText;
		}
	}

	}
</script>

<script>
    $("#list-btn").hide();

    $(document).ready(function(){
    $("#add-btn").click(function(){
       $("#form-display").show();
       $("#table-result").hide();
       $("#list-btn").show();
       $("#add-btn").hide();
    });
    
    // SHOW PROJECT DETAILS
    
    $("#list-btn").click(function(){
       $("#form-display").hide();
       $("#table-result").show();
       $("#list-btn").hide();
       $("#add-btn").show();
    });
    });
</script>

