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

    <!-- 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">Shipment</h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Products & Services</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Shipment</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-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Shipments Form / List 
                         <a class="btn btn-soft-primary" href="shipment.php?state=add" role="button" style="float: right;"><i class="fas fa-plus mr-2"></i> Add New Shipment</a>
                    </h4>
                    <?php echo !empty($msg)?$msg:''; ?>        
                </div>
       
                <div class="card-body">
                    <div class="row">
                        <?php if(@$_GET['state'] != "list"): ?>
                            <?php 
                                if(@$_GET['shipment_id'] != ''){
                                    $editData = $commonFunctions->getRowData('shp_'," WHERE id = '".$_GET['shipment_id']."' ");
                                }
                                if(!empty($_POST)){
                                    if(empty($_POST['reference_number'])){
                                        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 reference number !</div>');
                                    }
                                    if(!empty($_POST['reference_number'])){
                                        $added_by = $_SESSION['id'];
                                        if(empty($_POST['shipment_id'])){
                                            $commonFunctions->addData('shp_'," (reference_number,short_name_location,parent_id,description,address,zip_code,city,country_id) VALUES ('".$_POST['reference_number']."','".$_POST['short_name_location']."','".$_POST['parent_id']."','".$_POST['description']."','".$_POST['address']."','".$_POST['zip_code']."','".$_POST['city']."','".$_POST['country_id']."' ) ",'shipment.php?state=add');
                                        }else{
                                            $updated_by = $_SESSION['id'];
                                            $commonFunctions->updateData('shp_'," WHERE id = '".$_POST['shipment_id']."' ", " reference_number='".$_POST['reference_number']."', short_name_location='".$_POST['short_name_location']."' , parent_id='".$_POST['parent_id']."' , description='".$_POST['description']."' , address='".$_POST['address']."' , zip_code='".$_POST['zip_code']."' , city='".$_POST['city']."', country_id='".$_POST['country_id']."' ",'shipment.php?state=add');
                                        }
                                    }
                                }
                            ?>
                            <div class="col-md-12">
                                <!--div class="main-card mb-3 card">
                                    <div class="card-body"-->
                                        <form action="" method="post">
                                            <input type="hidden" name="shipment_id" value="<?php echo @$_GET['shipment_id'];?>">

                                            <div class="form-row">
                                                <div class="col-md-6 position-relative form-group">
                                                    <label for="reference_number" class="">Reference Number</label>
                                                    <input name="reference_number" id="reference_number" placeholder="Please enter reference number" type="text" class="form-control" value="<?php echo @$editData['reference_number'];?>">
                                                </div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <label for="short_name_location" class="">Short Name Location</label>
                                                    <textarea name="short_name_location" class="form-control" placeholder="Short Name Location"><?php echo @$editData['short_name_location'];?></textarea>
                                                </div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <label for="description" class="">Description</label>
                                                    <textarea name="description" class="form-control" placeholder="Description"><?php echo @$editData['description'];?></textarea>
                                                </div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <label for="address" class="">Address</label>
                                                    <textarea name="address" class="form-control" placeholder="Address"><?php echo @$editData['address'];?></textarea>
                                                </div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <label for="zip_code" class="">Zipcode</label>
                                                    <input name="zip_code" id="zip_code" placeholder="Please enter Zipcode" type="text" class="form-control" value="<?php echo @$editData['zip_code'];?>">
                                                </div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <label for="city" class="">City</label>
                                                    <input name="city" id="city" placeholder="Please enter city" type="text" class="form-control" value="<?php echo @$editData['city'];?>">
                                                </div>
                                                <div class="position-relative form-group col-md-4">
                                                    <label for="parent_id" class="">Add In</label>
                                                    <?php $ShipmentData = $commonFunctions->getData('shp_',' WHERE status = 0 '); ?>
                                                    <select name="parent_id" id="parent_id" class="select2 form-control" >
                                                        <option value="0">Select</option>
                                                        <?php foreach ($ShipmentData as $rw) { ?>
                                                            <option value="<?php echo $rw['id'];?>" <?php echo (@$editData['parent_id'] == $rw['id'])?"selected":"";?> ><?php echo $rw['short_name_location']; ?></option>     
                                                        <?php } ?>
                                                    </select>
                                                </div>

                                                <div class="position-relative form-group col-md-4">
                                                    <label for="country_id" class=""> Country</label>
                                                    <?php $countryData = $commonFunctions->getData('sys_erp_adm_countries',' WHERE active = 1 '); ?>
                                                    <select name="country_id" id="country_id" class="select2 form-control" >
                                                        <option value="0">Select Country</option>
                                                        <?php foreach ($countryData as $rw) { ?>
                                                            <option value="<?php echo $rw['rowid'];?>" <?php echo (@$editData['country_id'] == $rw['rowid'])?"selected":"";?> ><?php echo $rw['label']."(".$rw['code'].")"; ?></option>     
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <button type="submit" class="mt-1 btn btn-primary">Submit</button>
                                                </div>
                                            </div>
                                        </form>
                                    <!--/div>
                                </div-->
                            </div>
                        <?php else: 
                            if(!empty($_GET['shipment_id'])){
                                $commonFunctions->updateData('shp_'," WHERE id = '".$_GET['shipment_id']."' ", " status ='".$_GET['flag']."'",'shipment.php?state=list'); 
                            }
                            ?>
                            <div class="col-md-12">
                                <!--div class="main-card mb-3 card">
                                    <div class="card-body"-->
                                        <table style="width: 100% !important;" class="table datatable_t table-hover table-striped table-bordered">
                                            <thead>
                                                <tr>
                                                    <th class="text-center">Referance Number</th>
                                                    <th class="text-center">Short Name Location</th>
                                                    <th class="text-center">Description</th>
                                                    <th class="text-center">Address</th>
                                                    <th class="text-center">Zipcode</th>
                                                    <th class="text-center">City</th>
                                                    <th class="text-center">Country</th>
                                                    <th class="text-center">Status</th>
                                                    <th class="text-center">Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                $targetpage = "shipment.php?state=list";

                                                $limit = 50;
                                                $total_pages = $commonFunctions->getData('shp_','');
                                                $total_pages = count($total_pages);
                                                $stages = 3;
                                                $page = @$_GET['page'];
                                                if($page){
                                                    $start = ($page - 1) * $limit; 
                                                }else{
                                                    $start = 0; 
                                                } 
                                                
                                                $where = " WHERE id ORDER BY 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>";   
                                                }
                                                $productData = $commonFunctions->getData('shp_',$where);
                                                $i=1;
                                                foreach ($productData as $row) { 
                                                    $i++;
                                                ?>
                                                    <tr>
                                                        <th class="text-center"><?php echo $row['reference_number'];?></th>
                                                        <th class="text-center"><?php echo $row['short_name_location'];?></th>
                                                        <th class="text-center"><?php echo $row['description'];?></th>
                                                        <th class="text-center"><?php echo $row['address'];?></th>
                                                        <th class="text-center"><?php echo $row['zip_code'];?></th>
                                                        <th class="text-center"><?php echo $row['city'];?></th>
                                                        <th class="text-center"><?php echo $row['country_id'];?></th>
                                                        <th>
                                                            <?php
                                                                $flag = '';
                                                                $flag_state = 0;
                                                                if($row['status'] == 0){
                                                                    $flag = 'checked';
                                                                    $flag_state = 1;
                                                                }
                                                                $url = 'shipment.php?state=list&shipment_id='.$row['id'].'&flag='.$flag_state;
                                                                echo '<div class="custom-control custom-switch switch-primary">
                                                                    <input type="checkbox" class="custom-control-input" id="customSwitchPrimary'.$i.'" '.$flag.' onchange=change_flag("'.$url.'")>
                                                                    <label class="custom-control-label" for="customSwitchPrimary'.$i.'"></label>
                                                                </div>';
                                                            ?>
                                                        </th>
                                                        <th class="text-center">
                                                            <a class="btn btn-primary" href="shipment.php?state=edit&shipment_id=<?php echo $row['id'];?>"><i class="fa fa-pen"></i></a>
                                                        </th>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                    <!--/div>
                                </div-->
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->

<?php require "../../inc/footer.php"; ?>
<script type="text/javascript">
    function change_flag(url){
        window.location.href = url;
    }
</script>