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

$resultData = $commonFunctions->getData('invty_pdt_vrnt',' WHERE isActive = 0 ');
$variant_id = "";
if( !empty($_GET['variant_id']) and $_GET['flag'] == 1){
    $editData = $commonFunctions->getRowData('invty_pdt_vrnt'," WHERE id = '".$_GET['variant_id']."' ");
    $variant_id = $_GET['variant_id'];
}
if( !empty($_GET['variant_id']) and $_GET['flag'] == 2){
    $commonFunctions->deleteRowData('invty_pdt_vrnt'," WHERE id = '".$_GET['variant_id']."' ", " isActive='1' ",'product-variant.php');
    exit();
}
if(!empty($_POST)){
    if(!empty($_POST['reference_number']) and !empty($_POST['label'])){    
        if(!empty($_POST['variant_id'])){
            $commonFunctions->updateData('invty_pdt_vrnt'," WHERE id = '".$_POST['variant_id']."' ", " reference_number='".$_POST['reference_number']."', label='".$_POST['label']."' ",'product-variant.php');
        }else{
            $commonFunctions->addData('invty_pdt_vrnt'," (reference_number,label) VALUES ('".$_POST['reference_number']."','".$_POST['label']."') ",'product-variant.php');
        }
    }else{
        echo "<script>location.href='product-variant.php';</script>";
        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 all required data !</div>');
    }
}
?>


    <!-- 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">Products & Services</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);">Products</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Product Variant</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">Product Variant Form / List 
                         <a class="btn btn-sm btn-soft-primary" href="product-variant.php?state=add" role="button" style="float: right;"><i class="fas fa-plus mr-2"></i> Add New</a>
                    </h4>
                    <?php echo !empty($msg)?$msg:''; ?>        
                </div>
       
                <div class="card-body">
                    <div class="row">
                        <?php if(@$_GET['state'] != "list"): ?>
                            <div class="col-md-12">
                                <div class="main-card mb-3 card">
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <input type="hidden" name="variant_id" value="<?php echo $variant_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="label" class="">Label</label>
                                                    <input name="label" id="label" placeholder="Please enter label" type="text" class="form-control" value="<?php echo @$editData['label'];?>">
                                                </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: ?>
                            <div class="col-md-12">
                                <div class="main-card mb-3 card">
                                    <div class="card-body">
                                        <table style="width: 100%;" id="example" class="table table-hover table-striped table-bordered">
                                            <thead>
                                                <tr>
                                                    <th>SR NO.</th>
                                                    <th>Reference Number</th>
                                                    <th>Label</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                    $i=1;
                                                    foreach ($resultData as $row) {
                                                        echo "<tr>";
                                                        echo "<td>".$i++."</td>";
                                                        echo "<td>".$row['reference_number']."</td>";
                                                        echo "<td>".$row['label']."</td>";
                                                        echo "<td><a class='btn btn-primary' href='product-variant.php?flag=1&variant_id=".$row['id']."'><i class='fa fa-pen'></i></a> <a class='btn btn-danger' href='product-variant.php?flag=2&variant_id=".$row['id']."'><i class='fa fa-trash '></i></a></td>";
                                                        echo "</tr>";
                                                    }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->

<?php require "../../inc/footer.php"; ?>