<?php
if(isset($_POST['delete']))
{
    switch ($_POST['delete'])
    {
        case 'deleteclass':
            $sql="select StudentID from studentbiodata where ClassId=:cid";
            $fields=array(':cid'=>$_POST['classid']);
            if($con->select_query($sql,$fields))
            {
                echo '<script>alert("Class is already bound to students, hence cannot delete.")</script>';
            }
            else 
            {
                $sql="select ClassID from classarm where ClassID=:cid";
                $fields=array(':cid'=>$_POST['classid']);
                if($con->select_query($sql,$fields))
                {
                    echo '<script>alert("Class is already bound to arms, hence cannot delete. Delete arms and try again.")</script>';
                }
                else
                {
                    $sql="delete from class where id=:id";
                    $fields=array(':id'=>$_POST['classid']);
                    if($con->delete_query($sql,$fields))
                    {
                        echo '<script>window.location="../admin/class_list"</script>';
                    }
                }
            }
            
            break;

        case 'deletearm':
            $sql="select StudentID from studentbiodata where ArmId=:cid";
            $fields=array(':cid'=>$_POST['armid']);
            if($con->select_query($sql,$fields))
            {
                echo '<script>alert("Arm is already bound to students, hence cannot delete.")</script>';
            }
            else 
            {
                $sql="delete from classarm where id=:id";
                $fields=array(':id'=>$_POST['armid']);
                if($con->delete_query($sql,$fields))
                {
                    echo '<script>window.location="../admin/arm_list?id='.$_SESSION['classid'].'"</script>';
                }
            }
             
            break;
            
        case 'deletesession':
            $sql="select StudentID from studentbiodata where SessionId=:id";
            $fields=array(':id'=>$_POST['sessionid']);
            if($con->select_query($sql,$fields))
            {
                echo '<script>alert("Session is already bound to students, Hence cannot delete.")</script>';
            }
            else
            {
                $sql="select IsCurrent from session where id=:id";
                $fields=array(':id'=>$_POST['sessionid']);
                $r=$con->select_query($sql, $fields);
                foreach ($r as $v)
                {
                    if($v['IsCurrent']==1)
                    {
                        echo '<script>alert("Cannot delete an active session.")</script>';
                    }
                    else 
                    {
                        $sql="delete from session where id=:id";
                        $fields=array(':id'=>$_POST['sessionid']);
                        if($con->delete_query($sql,$fields))
                        {
                            $sql="delete from session_schedule where sessionid=:id";
                            $fields=array(':id'=>$_POST['sessionid']);
                            $con->delete_query($sql,$fields);
                            echo '<script>window.location="../admin/session_list"</script>';
                        }
                    }
                }
            }
            
            break;
            
        case 'deleteyear':
            $sql="delete from year where id=:id";
            $fields=array(':id'=>$_POST['yearid']);
            if($con->delete_query($sql,$fields))
            {
                echo '<script>window.location="../admin/year_list"</script>';
            }
            break;
        
        case 'deleteterm':
            $sql="select IsCurrent from term where id=:id";
            $fields=array(':id'=>$_POST['termid']);
            $r=$con->select_query($sql,$fields);
            foreach($r as $v)
            {
                if($v['IsCurrent']==1)
                {
                    echo '<script>alert("Cannot delete an active term.")</script>';
                }
                else
                {
                    $sql="delete from term where id=:id";
                    $fields=array(':id'=>$_POST['termid']);
                    if($con->delete_query($sql,$fields))
                    {
                        echo '<script>window.location="../admin/term_list"</script>';
                    }
                }
            }
            break;
            
        case 'deletestudenttype':
            $sql="delete from studenttype where id=:id";
            $fields=array(':id'=>$_POST['typeid']);
            if($con->delete_query($sql,$fields))
            {
                echo '<script>window.location="../admin/student_type_list"</script>';
            }
            break;
            
            case 'deleteparty':
                $sql="delete from party where id=:id";
                $fields=array(':id'=>$_POST['partyid']);
                if($con->delete_query($sql,$fields))
                {
                    echo '<script>window.location="../admin/party_club_list"</script>';
                }
                break;
        
            case 'deletesubject':
                $sql = "select id from class_subject where subjectid=:id";
                $field=array(':id'=>$_POST['subid']);
                $q=$con->select_query($sql,$field);
                if(count($q) == 0)
                {
                    $sql="delete from subject where id=:id";
                    $field=array(':id'=>$_POST['subid']);
                    $q=$con->delete_query($sql,$field);
                    if($q)
                    {
                        if(isset($_POST['masterid']))
                        {
                            echo '<script>window.location="../admin/subcomponent_list?id='.$_POST['masterid'].'"</script>';
                        }
                        else
                        {
                            echo '<script>window.location="../admin/subject_list"</script>';
                        }
                    }
                }
                else 
                {
                    echo '<script>alert("Subject is already bound to class(es), hence cannot delete.")</script>';
                }
                
                break;
                
            case 'deletequestioncategory':
                $sql="delete from question_category where id=:id";
                $field=array(':id'=>$_POST['questionid']);
                $q=$con->delete_query($sql,$field);
                if($q)
                {
                    echo '<script>window.location="../admin/question_category_list"</script>';
                }
                break;
                
            case 'deletetemplate':
                $sql = "delete from template where id=:id";
                $q=$con->delete_query($sql,array(':id'=>$_POST['templateid']));
                
                break;
                
                
            case 'deletesignature':
                $sql = "delete from signature where id=:id";
                $q=$con->delete_query($sql,array(':id'=>$_POST['signatureid']));
                if($q)
                {
                    echo '<script>window.location="../admin/signature_list"</script>';
                }
                break;
            
            case 'deletenews':
                $sql = "delete from news where id=:id";
                $q=$con->delete_query($sql,array(':id'=>$_POST['newsid']));
                if($q)
                {
                    echo '<script>window.location="../reminder/news_list"</script>';
                }
                break;
                
            case 'deleteexpensebatch':
                include('../include/connection.php');
                $sql = "delete from expensebatch where id=:id";
                $q=$con->delete_query($sql,array(':id'=>$_POST['expensebatchid']));
                break;
                
            case 'deleteexpense':
                $sql = "delete from expense where id=:id";
                $q=$con->delete_query($sql,array(':id'=>$_POST['expenseid']));
                if($q)
                {
                    echo '<script>window.location="../finance/expense_list?id='.$_POST['batchid'].'"</script>';
                }
                break;
                
            case 'deletebudget':
                include('../include/connection.php');
                $sql = "delete from budget where id=:id";
                $q=$con->delete_query($sql,array(':id'=>$_POST['budgetid']));
                break;
            
         
    }
}
?>