<?php 
define('DOCUMENT_ROOT_CLASS','../../');
define('DOCUMENT_ROOT_INC_FOLDER','inc/');

$filepath = realpath(dirname(__FILE__));
require "../../inc/header.php";
$msg = Session::get('msg');
Session::set("msg", NULL);
if(!empty($_POST)){
    $error_ = 0;
    if(empty($_POST['erp_sys_org_dprtmt_ops'])){
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Please enter Operations Department !</div>');
    }else{
        $cntA = $commonFunctions->getRowData('erp_sys_org_department',' WHERE erp_sys_org_dprtmt_ops = "'.$_POST['erp_sys_org_dprtmt_ops'].'" ');
        if(@count($cntA) > 1){
           $error_ = 1;
           Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Operations Department already exists !</div>');
        }
    }

    if(!empty($_POST['erp_sys_org_dprtmt_ops']) and $error_ == 0){
        if($_GET['action'] == "add"){
            $commonFunctions->addData('erp_sys_org_department'," (erp_sys_org_dprtmt_ops,erp_sys_org_dprtmt_sales,erp_sys_org_dprtmt_mktng,erp_sys_org_dprtmt_hr,erp_sys_org_dprtmt_fin,erp_sys_org_dprtmt_acnt ) VALUES ('".$_POST['erp_sys_org_dprtmt_ops']."','".$_POST['erp_sys_org_dprtmt_sales']."','".$_POST['erp_sys_org_dprtmt_mktng']."','".$_POST['erp_sys_org_dprtmt_hr']."','".$_POST['erp_sys_org_dprtmt_fin']."','".$_POST['erp_sys_org_dprtmt_acnt']."') ",'departnemt.php?state=form');
        }
        if($_GET['action'] == "edit"){
            $commonFunctions->updateData('erp_sys_org_department'," WHERE erp_sys_org_dprtmt_id  = '".$_POST['erp_sys_org_dprtmt_id ']."' ", " erp_sys_org_dprtmt_ops = '".$_POST['erp_sys_org_dprtmt_ops']."', erp_sys_org_dprtmt_sales='".$_POST['erp_sys_org_dprtmt_sales']."', erp_sys_org_dprtmt_mktng='".$_POST['erp_sys_org_dprtmt_mktng']."' , erp_sys_org_dprtmt_hr='".$_POST['erp_sys_org_dprtmt_hr']."' , erp_sys_org_dprtmt_fin='".$_POST['erp_sys_org_dprtmt_fin']."' , erp_sys_org_dprtmt_acnt='".$_POST['erp_sys_org_dprtmt_acnt']."'",'departnemt.php?state=form');
        }
    }else{
        echo "<script>location.href='departnemt.php?state=form';</script>";
    }
}
?>    
    
<!-- Page-Title -->
<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <div class="row">
                <div class="col">
                    <h4 class="page-title">Departnemt ( Company )</h4>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Settings</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Organization Details</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Departnemt ( Company ) <?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></a></li>
                    </ol>
                </div><!--end col-->
                <!-- <div class="col-auto align-self-center">
                    <a href="#" class="btn btn-sm btn-outline-primary" id="Dash_Date">
                        <span class="day-name" id="Day_Name">Today:</span>&nbsp;
                        <span class="" id="Select_date">Jan 11</span>
                        <i data-feather="calendar" class="align-self-center icon-xs ml-1"></i>
                    </a>
                    <a href="#" class="btn btn-sm btn-outline-primary">
                        <i data-feather="download" class="align-self-center icon-xs"></i>
                    </a>
                </div> --><!--end col-->  
            </div><!--end row-->                                                              
        </div><!--end page-title-box-->
    </div><!--end col-->
</div><!--end row-->
<!-- end page title end breadcrumb -->
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">
                    <div class="form-row">
                        <div class="form-group searchForm col-md-6">
                            <a class="btn btn-sm btn-soft-primary" href="departnemt.php?state=form" role="button" style="float: left;"><i class="fas fa-plus mr-2"></i> Add New Departnemt ( Company )</a>
                        </div>    
                        <div class="form-group searchForm col-md-6">
                            <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): ?>
                            <form action="" id="searchForm" method="GET">
                                <div class="form-row">
                                    <div class="form-group searchForm col-md-8">
                                        <input type="text" name="search" id="search" placeholder="Search" class="form-control" value="<?php echo @$_GET['search']; ?>">
                                        <input type="hidden" name="state" value="<?php echo @$_GET['state']; ?>">
                                        <input type="hidden" name="page" value="<?php echo @$_GET['page']; ?>">
                                    </div>
                                    <div class="form-group searchForm col-md-4">
                                        <button type="submit" id="searchbtn" class="btn btn-primary"> <i class="fa fa-search"></i> Search</button>
                                        <button type="button" id="clearbtn" class="btn btn-danger"> <i class="fa fa-trash"></i> Clear</button>
                                    </div>
                                </div>
                            </form>
                            <?php endif; ?>
                        </div>
                    </div>
                </h4>
                <?php echo !empty($msg)?$msg:''; ?>        
                <?php echo !empty($msg1)?$msg1:''; ?>        
            </div>
    
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="main-card mb-3 ">
                            <!-- <div class="card-body"> -->
                                <?php if(!empty($_GET['state']) and $_GET['state'] == "form"): ?>
                                    <form enctype="multipart/form-data" action="departnemt.php?state=form&action=add" method="post" name="formsoc" autocomplete="off">      
                                        <div class="form-row">
                                            <input type="hidden" name="action" value="add">
                                            <input type="hidden" name="erp_sys_org_dprtmt_id" value="">
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_dprtmt_ops" class="">Operations Department</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_dprtmt_ops" id="erp_sys_org_dprtmt_ops" value="" autofocus="autofocus" class="form-control" placeholder="Operations Department">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_dprtmt_sales" class="">Sales</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_dprtmt_sales" id="erp_sys_org_dprtmt_sales" value="" autofocus="autofocus" class="form-control" placeholder="Sales">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_dprtmt_mktng" class="">Marketing</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_dprtmt_mktng" id="erp_sys_org_dprtmt_mktng" value="" autofocus="autofocus" class="form-control" placeholder="Marketing">
                                            </div>
                                            
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_dprtmt_hr" class="">HR</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_dprtmt_hr" id="erp_sys_org_dprtmt_hr" value="" autofocus="autofocus" class="form-control" placeholder="HR">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_dprtmt_fin" class="">Finance</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_dprtmt_fin" id="erp_sys_org_dprtmt_fin" value="" autofocus="autofocus" class="form-control" placeholder="Finance">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_dprtmt_acnt" class="">Accounts</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_dprtmt_acnt" id="erp_sys_org_dprtmt_acnt" value="" autofocus="autofocus" class="form-control" placeholder="Accounts">
                                            </div>
                                            
                                            <div class="position-relative form-group col-md-12">
                                                <button type="submit" class="btn btn-success">Create</button>
                                                <a href="departnemt.php?state=list"><button type="button" class="btn btn-danger">Back to Departnemt ( Company ) List</button></a>
                                            </div>
                                        </div>
                                    </form>
                                <?php endif; ?>
                                <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): 

                                    $targetpage = "departnemt.php?state=list";

                                    $limit = 50;
                                    $total_pages = $commonFunctions->getData('erp_sys_org_department','');
                                    $total_pages = count($total_pages);
                                    $stages = 3;
                                    $page = @$_GET['page'];
                                    if($page){
                                        $start = ($page - 1) * $limit; 
                                    }else{
                                        $start = 0; 
                                    } 
                                    
                                    $search_con = "";
                                    if(!empty($_GET['search'])){
                                        
                                        $search_con = " AND erp_sys_org_dprtmt_ops LIKE '%".$_GET['search']."%' OR erp_sys_org_dprtmt_sales LIKE '%".$_GET['search']."%' OR erp_sys_org_dprtmt_mktng LIKE '%".$_GET['search']."%' OR erp_sys_org_dprtmt_hr LIKE '%".$_GET['search']."%' OR erp_sys_org_dprtmt_fin LIKE '%".$_GET['search']."%' OR erp_sys_org_dprtmt_acnt LIKE '%".$_GET['search']."%' ";
                                    }
                                    $where = " WHERE erp_sys_org_dprtmt_id ".$search_con." ORDER BY erp_sys_org_dprtmt_id DESC LIMIT $start, $limit ";
                                   
                                    // Initial page num setup
                                    if ($page == 0){ $page = 1; }
                                    $prev = $page - 1;  
                                    $next = $page + 1;  
                                    $lastpage = ceil($total_pages/$limit);  
                                    $LastPagem1 = $lastpage - 1;    
                                    $paginate = '';
                                    if($lastpage > 1)
                                    {       
                                        $paginate .= "<div class='paginate'>";
                                        // Previous

                                        if ($page > 1){
                                            $paginate.= "<a href='$targetpage&page=$prev'>Previous</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Previous</span>";   }
                                        // Pages    
                                        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
                                        {   
                                            for ($counter = 1; $counter <= $lastpage; $counter++)
                                            {
                                                if ($counter == $page){
                                                    $paginate.= "<span class='current'>$counter</span>";
                                                }else{
                                                    $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                            }
                                        }
                                        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
                                        {
                                            // Beginning only hide later pages
                                            if($page < 1 + ($stages * 2))   
                                            {
                                                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // Middle hide some front and some back
                                            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // End only hide early pages
                                            else
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}
                                                }
                                            }       
                                        }       
                                                // Next
                                        if ($page < $counter - 1){ 
                                            $paginate.= "<a href='$targetpage&page=$next'>Next</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Next</span>";
                                            }
                                        $paginate.= "</div>";   
                                    }
                                    $resultData = $commonFunctions->getData('erp_sys_org_department',$where);
                                    ?>
                                    <div class="form-row1 table-rep-plugin">
                                        <div class="table-responsive1 mb-0" data-pattern="priority-columns">
                                            <table style="width: 100% !important;" class="table datatable_t table-hover table-striped table-bordered">
                                                <thead>
                                                    <tr>
                                                        <th  class="text-center">SL</th>
                                                        <th  class="text-center">Operations Department</th>
                                                        <th  class="text-center">Sales</th>
                                                        <th  class="text-center">Marketing</th>
                                                        <th  class="text-center">Hr</th>
                                                        <th  class="text-center">Finance</th>
                                                        <th  class="text-center">Accounts</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $i = 1; foreach ($resultData as $rw) { ?>
                                                        <tr>
                                                            <td><?php echo $i++; ?></td>
                                                            <td><?php echo $rw['erp_sys_org_dprtmt_ops'];?></td>
                                                            <td><?php echo $rw['erp_sys_org_dprtmt_sales'];?></td>
                                                            <td><?php echo $rw['erp_sys_org_dprtmt_mktng'];?></td>
                                                            <td><?php echo $rw['erp_sys_org_dprtmt_hr'];?></td>
                                                            <td><?php echo $rw['erp_sys_org_dprtmt_fin'];?></td>
                                                            <td><?php echo $rw['erp_sys_org_dprtmt_acnt'];?></td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    <?php echo "<center>".$paginate."</center>";?>
                                <?php endif; ?>
                            <!-- </div> -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div> <!-- end col -->
</div> <!-- end row -->

<?php require "../../inc/footer.php"; ?>
<script type="text/javascript">
$("#clearbtn").click(function(){
    $("#search").val('');
    $("#searchForm").submit();
});
</script>